Given the following C code and unoptimized assembly code, your task is to generate highly optimized x86-64 assembly code.
C Code:

```c
// AOJ 2299:   Tiles are Colorful
// 2017.12.10

#include <stdio.h>
#include <string.h>

int m, n;
char map[501][503];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int rpos['Z'+1], cpos['Z'+1];
int sr[501], sc[501];

int calc()
{
	int r, c, i, r2, c2, a, ans = 0;
	for (r = 0; r < m; r++) for (c = 0; c < n; c++) 
		if ((sr[c] + sc[r]) && map[r][c] == '.' ) {
			memset(rpos, -1, sizeof(rpos));

		    for (i = 0; i < 4; i++) {
				r2 = r, c2 = c;
				while (1) {
					r2 += mv[i][0], c2 += mv[i][1];
					if (r2 < 0 || r2 >= m || c2 < 0 || c2 >= n) break;
					if (map[r2][c2] == '.') continue;

	                a = map[r2][c2];
					if (rpos[a] >= 0) {
	                    map[r2][c2] = '.', sr[c2]--, sc[r2]--;
						r2 = rpos[a], c2 = cpos[a];
	                    map[r2][c2] = '.', sr[c2]--, sc[r2]--;
	                    ans += 2;
					} else rpos[a] = r2, cpos[a] = c2;
					break;
				}
            }
        }

	return ans;
}

int main()
{
	int r, c, t, ans;
	char buf[10];

    fgets(buf, 10, stdin);
	sscanf(buf, "%d%d", &m, &n);
	for (r = 0; r < m; r++) {
		fgets(map[r], 503, stdin);
		for (c = 0; c < n; c++) {
			if (map[r][c] != '.') sr[c]++, sc[r]++;
		}
	}
	ans = 0; while (t = calc()) ans += t;
	printf("%d\n", ans);
	return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	m
	.bss
	.align 4
	.type	m, @object
	.size	m, 4
m:
	.zero	4
	.globl	n
	.align 4
	.type	n, @object
	.size	n, 4
n:
	.zero	4
	.globl	map
	.align 32
	.type	map, @object
	.size	map, 252003
map:
	.zero	252003
	.globl	mv
	.data
	.align 32
	.type	mv, @object
	.size	mv, 32
mv:
	.long	-1
	.long	0
	.long	0
	.long	1
	.long	1
	.long	0
	.long	0
	.long	-1
	.globl	rpos
	.bss
	.align 32
	.type	rpos, @object
	.size	rpos, 364
rpos:
	.zero	364
	.globl	cpos
	.align 32
	.type	cpos, @object
	.size	cpos, 364
cpos:
	.zero	364
	.globl	sr
	.align 32
	.type	sr, @object
	.size	sr, 2004
sr:
	.zero	2004
	.globl	sc
	.align 32
	.type	sc, @object
	.size	sc, 2004
sc:
	.zero	2004
	.text
	.globl	calc
	.type	calc, @function
calc:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$32, %rsp
	movl	$0, -8(%rbp)
	movl	$0, -28(%rbp)
	jmp	.L2
.L14:
	movl	$0, -24(%rbp)
	jmp	.L3
.L13:
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %ecx
	movl	-28(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %eax
	addl	%ecx, %eax
	testl	%eax, %eax
	je	.L4
	movl	-24(%rbp), %eax
	movslq	%eax, %rdx
	movl	-28(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$46, %al
	jne	.L4
	movl	$364, %edx
	movl	$-1, %esi
	leaq	rpos(%rip), %rax
	movq	%rax, %rdi
	call	memset@PLT
	movl	$0, -20(%rbp)
	jmp	.L5
.L12:
	movl	-28(%rbp), %eax
	movl	%eax, -16(%rbp)
	movl	-24(%rbp), %eax
	movl	%eax, -12(%rbp)
.L11:
	movl	-20(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	mv(%rip), %rax
	movl	(%rdx,%rax), %eax
	addl	%eax, -16(%rbp)
	movl	-20(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	4+mv(%rip), %rax
	movl	(%rdx,%rax), %eax
	addl	%eax, -12(%rbp)
	cmpl	$0, -16(%rbp)
	js	.L6
	movl	m(%rip), %eax
	cmpl	%eax, -16(%rbp)
	jge	.L6
	cmpl	$0, -12(%rbp)
	js	.L6
	movl	n(%rip), %eax
	cmpl	%eax, -12(%rbp)
	jge	.L6
	movl	-12(%rbp), %eax
	movslq	%eax, %rdx
	movl	-16(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$46, %al
	je	.L17
	movl	-12(%rbp), %eax
	movslq	%eax, %rdx
	movl	-16(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	movsbl	%al, %eax
	movl	%eax, -4(%rbp)
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	rpos(%rip), %rax
	movl	(%rdx,%rax), %eax
	testl	%eax, %eax
	js	.L9
	movl	-12(%rbp), %eax
	movslq	%eax, %rdx
	movl	-16(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movb	$46, (%rax)
	movl	-12(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	-1(%rax), %ecx
	movl	-12(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	-16(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	-1(%rax), %ecx
	movl	-16(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	rpos(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -16(%rbp)
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	cpos(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -12(%rbp)
	movl	-12(%rbp), %eax
	movslq	%eax, %rdx
	movl	-16(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movb	$46, (%rax)
	movl	-12(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	-1(%rax), %ecx
	movl	-12(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	-16(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	-1(%rax), %ecx
	movl	-16(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	addl	$2, -8(%rbp)
	jmp	.L6
.L9:
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	rpos(%rip), %rdx
	movl	-16(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	cpos(%rip), %rdx
	movl	-12(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	jmp	.L6
.L17:
	nop
	jmp	.L11
.L6:
	addl	$1, -20(%rbp)
.L5:
	cmpl	$3, -20(%rbp)
	jle	.L12
.L4:
	addl	$1, -24(%rbp)
.L3:
	movl	n(%rip), %eax
	cmpl	%eax, -24(%rbp)
	jl	.L13
	addl	$1, -28(%rbp)
.L2:
	movl	m(%rip), %eax
	cmpl	%eax, -28(%rbp)
	jl	.L14
	movl	-8(%rbp), %eax
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	calc, .-calc
	.section	.rodata
.LC0:
	.string	"%d%d"
.LC1:
	.string	"%d\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$48, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movq	stdin(%rip), %rdx
	leaq	-18(%rbp), %rax
	movl	$10, %esi
	movq	%rax, %rdi
	call	fgets@PLT
	leaq	-18(%rbp), %rax
	leaq	n(%rip), %rdx
	movq	%rdx, %rcx
	leaq	m(%rip), %rdx
	leaq	.LC0(%rip), %rsi
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_sscanf@PLT
	movl	$0, -36(%rbp)
	jmp	.L19
.L23:
	movq	stdin(%rip), %rax
	movl	-36(%rbp), %edx
	movslq	%edx, %rdx
	imulq	$503, %rdx, %rdx
	leaq	map(%rip), %rcx
	addq	%rdx, %rcx
	movq	%rax, %rdx
	movl	$503, %esi
	movq	%rcx, %rdi
	call	fgets@PLT
	movl	$0, -32(%rbp)
	jmp	.L20
.L22:
	movl	-32(%rbp), %eax
	movslq	%eax, %rdx
	movl	-36(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$46, %al
	je	.L21
	movl	-32(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	1(%rax), %ecx
	movl	-32(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	-36(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	1(%rax), %ecx
	movl	-36(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	%ecx, (%rdx,%rax)
.L21:
	addl	$1, -32(%rbp)
.L20:
	movl	n(%rip), %eax
	cmpl	%eax, -32(%rbp)
	jl	.L22
	addl	$1, -36(%rbp)
.L19:
	movl	m(%rip), %eax
	cmpl	%eax, -36(%rbp)
	jl	.L23
	movl	$0, -28(%rbp)
	jmp	.L24
.L25:
	movl	-24(%rbp), %eax
	addl	%eax, -28(%rbp)
.L24:
	movl	$0, %eax
	call	calc
	movl	%eax, -24(%rbp)
	cmpl	$0, -24(%rbp)
	jne	.L25
	movl	-28(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L27
	call	__stack_chk_fail@PLT
.L27:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
```

Only output the optimized assembly code. Do not include any other text. Do not write any comments in the assembly code. Wrap the assembly code in ```assembly``` tags.
user
	.file	"temp.c"
	.text
	.p2align 4
	.globl	calc
	.type	calc, @function
calc:
.LFB35:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	m(%rip), %eax
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%eax, -32(%rsp)
	testl	%eax, %eax
	jle	.L15
	movl	n(%rip), %eax
	leaq	map(%rip), %r13
	xorl	%r15d, %r15d
	movl	$0, -16(%rsp)
	leaq	sc(%rip), %rsi
	leaq	sr(%rip), %r14
	movl	%eax, -28(%rsp)
	movq	%r13, %rax
	movl	%r15d, %r13d
	movq	%rax, %r15
	.p2align 4,,10
	.p2align 3
.L3:
	movslq	-28(%rsp), %r12
	xorl	%eax, %eax
	testl	%r12d, %r12d
	jle	.L14
	.p2align 4,,10
	.p2align 3
.L12:
	movl	(%rsi), %ecx
	addl	(%r14,%rax,4), %ecx
	movl	%eax, %r8d
	leaq	1(%rax), %rdx
	je	.L4
	cmpb	$46, (%r15,%rax)
	je	.L35
.L4:
	movq	%rdx, %rax
	cmpq	%r12, %rdx
	jne	.L12
.L14:
	addl	$1, %r13d
	addq	$503, %r15
	addq	$4, %rsi
	cmpl	-32(%rsp), %r13d
	jne	.L3
	movl	-16(%rsp), %eax
	popq	%rbx
	.cfi_remember_state
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.p2align 4,,10
	.p2align 3
.L35:
	.cfi_restore_state
	movq	$-1, %rax
	movl	$45, %ecx
	leaq	rpos(%rip), %rdi
	movq	%rsi, -24(%rsp)
	rep stosq
	movq	%r15, %rax
	leaq	mv(%rip), %rbp
	movq	%r12, %r15
	movq	%rax, %r12
	movl	$-1, (%rdi)
.L11:
	movl	4(%rbp), %edi
	movl	0(%rbp), %esi
	cmpl	$1, %edi
	jne	.L36
	leal	(%rsi,%r13), %edi
	movslq	%esi, %r9
	leaq	map(%rip), %rax
	movl	%r8d, %ecx
	movslq	%edi, %rdi
	imulq	$503, %r9, %r9
	imulq	$503, %rdi, %rdi
	leaq	1(%r9), %rbx
	negq	%r9
	addq	%rdx, %rdi
	addq	%rax, %rdi
	movl	%r13d, %eax
.L9:
	addl	%esi, %eax
	addl	$1, %ecx
	cmpl	%ecx, -28(%rsp)
	setle	%r10b
	cmpl	%eax, -32(%rsp)
	setle	%r11b
	orb	%r11b, %r10b
	jne	.L6
	testl	%eax, %eax
	js	.L6
	addq	%rbx, %rdi
	movsbq	-1(%rdi,%r9), %r11
	cmpb	$46, %r11b
	je	.L9
.L8:
	leaq	rpos(%rip), %rbx
	movslq	(%rbx,%r11,4), %rsi
	testl	%esi, %esi
	js	.L10
	cltq
	leaq	map(%rip), %rbx
	movslq	%ecx, %rcx
	addl	$2, -16(%rsp)
	imulq	$503, %rax, %rdi
	subl	$1, (%r14,%rcx,4)
	addq	%rbx, %rdi
	movb	$46, (%rdi,%rcx)
	leaq	sc(%rip), %rdi
	subl	$1, (%rdi,%rax,4)
	imulq	$503, %rsi, %rcx
	leaq	cpos(%rip), %rax
	movslq	(%rax,%r11,4), %rax
	subl	$1, (%rdi,%rsi,4)
	addq	%rbx, %rcx
	subl	$1, (%r14,%rax,4)
	movb	$46, (%rcx,%rax)
.L6:
	addq	$8, %rbp
	leaq	32+mv(%rip), %rax
	cmpq	%rax, %rbp
	jne	.L11
	movq	%r12, %rax
	movq	-24(%rsp), %rsi
	movq	%r15, %r12
	movq	%rax, %r15
	jmp	.L4
	.p2align 4,,10
	.p2align 3
.L10:
	leaq	rpos(%rip), %rbx
	movl	%eax, (%rbx,%r11,4)
	leaq	cpos(%rip), %rax
	movl	%ecx, (%rax,%r11,4)
	jmp	.L6
	.p2align 4,,10
	.p2align 3
.L36:
	movslq	%esi, %rbx
	leal	(%rsi,%r13), %r9d
	movslq	%edi, %r10
	movl	%r8d, %ecx
	imulq	$503, %rbx, %rbx
	movslq	%r9d, %r9
	imulq	$503, %r9, %r9
	leaq	(%rbx,%r10), %rax
	negq	%r10
	movq	%rax, -8(%rsp)
	leal	(%rdi,%r8), %eax
	cltq
	addq	%rax, %r9
	leaq	map(%rip), %rax
	addq	%rax, %r9
	movl	%r13d, %eax
.L7:
	addl	%esi, %eax
	addl	%edi, %ecx
	movl	%eax, %r11d
	orl	%ecx, %r11d
	shrl	$31, %r11d
	cmpl	-32(%rsp), %eax
	setge	-9(%rsp)
	orb	-9(%rsp), %r11b
	jne	.L6
	cmpl	-28(%rsp), %ecx
	jge	.L6
	addq	-8(%rsp), %r9
	movq	%r9, %r11
	subq	%rbx, %r11
	movsbq	(%r11,%r10), %r11
	cmpb	$46, %r11b
	jne	.L8
	jmp	.L7
.L15:
	movl	$0, -16(%rsp)
	movl	-16(%rsp), %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE35:
	.size	calc, .-calc
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d%d"
.LC1:
	.string	"%d\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB36:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	$10, %esi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	movq	stdin(%rip), %rdx
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	14(%rsp), %rbp
	movq	%rbp, %rdi
	call	fgets@PLT
	leaq	n(%rip), %rcx
	movq	%rbp, %rdi
	xorl	%eax, %eax
	leaq	m(%rip), %rdx
	leaq	.LC0(%rip), %rsi
	call	__isoc99_sscanf@PLT
	movl	m(%rip), %eax
	testl	%eax, %eax
	jle	.L38
	leaq	map(%rip), %rbp
	leaq	sc(%rip), %rbx
	xorl	%r13d, %r13d
	leaq	4+sr(%rip), %r12
	.p2align 4,,10
	.p2align 3
.L42:
	movq	stdin(%rip), %rdx
	movl	$503, %esi
	movq	%rbp, %rdi
	call	fgets@PLT
	movl	n(%rip), %edx
	testl	%edx, %edx
	jle	.L39
	subl	$1, %edx
	leaq	sr(%rip), %rax
	leaq	(%r12,%rdx,4), %rcx
	movq	%rbp, %rdx
	.p2align 4,,10
	.p2align 3
.L41:
	cmpb	$46, (%rdx)
	je	.L40
	addl	$1, (%rax)
	addl	$1, (%rbx)
.L40:
	addq	$4, %rax
	addq	$1, %rdx
	cmpq	%rcx, %rax
	jne	.L41
.L39:
	addl	$1, %r13d
	addq	$503, %rbp
	addq	$4, %rbx
	cmpl	%r13d, m(%rip)
	jg	.L42
.L38:
	xorl	%r12d, %r12d
	jmp	.L43
	.p2align 4,,10
	.p2align 3
.L44:
	addl	%eax, %r12d
.L43:
	xorl	%eax, %eax
	call	calc
	testl	%eax, %eax
	jne	.L44
	movl	%r12d, %edx
	leaq	.LC1(%rip), %rsi
	movl	$1, %edi
	call	__printf_chk@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L49
	addq	$40, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
.L49:
	.cfi_restore_state
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE36:
	.size	main, .-main
	.globl	sc
	.bss
	.align 32
	.type	sc, @object
	.size	sc, 2004
sc:
	.zero	2004
	.globl	sr
	.align 32
	.type	sr, @object
	.size	sr, 2004
sr:
	.zero	2004
	.globl	cpos
	.align 32
	.type	cpos, @object
	.size	cpos, 364
cpos:
	.zero	364
	.globl	rpos
	.align 32
	.type	rpos, @object
	.size	rpos, 364
rpos:
	.zero	364
	.globl	mv
	.data
	.align 32
	.type	mv, @object
	.size	mv, 32
mv:
	.long	-1
	.long	0
	.long	0
	.long	1
	.long	1
	.long	0
	.long	0
	.long	-1
	.globl	map
	.bss
	.align 32
	.type	map, @object
	.size	map, 252003
map:
	.zero	252003
	.globl	n
	.align 4
	.type	n, @object
	.size	n, 4
n:
	.zero	4
	.globl	m
	.align 4
	.type	m, @object
	.size	m, 4
m:
	.zero	4
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

rule
	.file	"temp.c"
	.text
	.p2align 4
	.globl	calc
	.type	calc, @function
calc:
.LFB35:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	m(%rip), %eax
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%eax, -32(%rsp)
	testl	%eax, %eax
	jle	.L15
	movl	n(%rip), %eax
	leaq	map(%rip), %r13
	xorl	%r15d, %r15d
	movl	$0, -16(%rsp)
	leaq	sc(%rip), %rsi
	leaq	sr(%rip), %r14
	movl	%eax, -28(%rsp)
	movq	%r13, %rax
	movl	%r15d, %r13d
	movq	%rax, %r15
	.p2align 4,,10
	.p2align 3
.L3:
	movslq	-28(%rsp), %r12
	xorl	%eax, %eax
	testl	%r12d, %r12d
	jle	.L14
	.p2align 4,,10
	.p2align 3
.L12:
	movl	(%rsi), %ecx
	addl	(%r14,%rax,4), %ecx
	movl	%eax, %r8d
	leaq	1(%rax), %rdx
	je	.L4
	cmpb	$46, (%r15,%rax)
	je	.L35
.L4:
	movq	%rdx, %rax
	cmpq	%r12, %rdx
	jne	.L12
.L14:
	addl	$1, %r13d
	addq	$503, %r15
	addq	$4, %rsi
	cmpl	-32(%rsp), %r13d
	jne	.L3
	movl	-16(%rsp), %eax
	popq	%rbx
	.cfi_remember_state
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.p2align 4,,10
	.p2align 3
.L35:
	.cfi_restore_state
	movq	$-1, %rax
	movl	$45, %ecx
	leaq	rpos(%rip), %rdi
	movq	%rsi, -24(%rsp)
	rep stosq
	movq	%r15, %rax
	leaq	mv(%rip), %rbp
	movq	%r12, %r15
	movq	%rax, %r12
	movl	$-1, (%rdi)
.L11:
	movl	4(%rbp), %edi
	movl	0(%rbp), %esi
	cmpl	$1, %edi
	jne	.L36
	leal	(%rsi,%r13), %edi
	movslq	%esi, %r9
	leaq	map(%rip), %rax
	movl	%r8d, %ecx
	movslq	%edi, %rdi
	imulq	$503, %r9, %r9
	imulq	$503, %rdi, %rdi
	leaq	1(%r9), %rbx
	negq	%r9
	addq	%rdx, %rdi
	addq	%rax, %rdi
	movl	%r13d, %eax
.L9:
	addl	%esi, %eax
	addl	$1, %ecx
	cmpl	%ecx, -28(%rsp)
	setle	%r10b
	cmpl	%eax, -32(%rsp)
	setle	%r11b
	orb	%r11b, %r10b
	jne	.L6
	testl	%eax, %eax
	js	.L6
	addq	%rbx, %rdi
	movsbq	-1(%rdi,%r9), %r11
	cmpb	$46, %r11b
	je	.L9
.L8:
	leaq	rpos(%rip), %rbx
	movslq	(%rbx,%r11,4), %rsi
	testl	%esi, %esi
	js	.L10
	cltq
	leaq	map(%rip), %rbx
	movslq	%ecx, %rcx
	addl	$2, -16(%rsp)
	imulq	$503, %rax, %rdi
	subl	$1, (%r14,%rcx,4)
	addq	%rbx, %rdi
	movb	$46, (%rdi,%rcx)
	leaq	sc(%rip), %rdi
	subl	$1, (%rdi,%rax,4)
	imulq	$503, %rsi, %rcx
	leaq	cpos(%rip), %rax
	movslq	(%rax,%r11,4), %rax
	subl	$1, (%rdi,%rsi,4)
	addq	%rbx, %rcx
	subl	$1, (%r14,%rax,4)
	movb	$46, (%rcx,%rax)
.L6:
	addq	$8, %rbp
	leaq	32+mv(%rip), %rax
	cmpq	%rax, %rbp
	jne	.L11
	movq	%r12, %rax
	movq	-24(%rsp), %rsi
	movq	%r15, %r12
	movq	%rax, %r15
	jmp	.L4
	.p2align 4,,10
	.p2align 3
.L10:
	leaq	rpos(%rip), %rbx
	movl	%eax, (%rbx,%r11,4)
	leaq	cpos(%rip), %rax
	movl	%ecx, (%rax,%r11,4)
	jmp	.L6
	.p2align 4,,10
	.p2align 3
.L36:
	movslq	%esi, %rbx
	leal	(%rsi,%r13), %r9d
	movslq	%edi, %r10
	movl	%r8d, %ecx
	imulq	$503, %rbx, %rbx
	movslq	%r9d, %r9
	imulq	$503, %r9, %r9
	leaq	(%rbx,%r10), %rax
	negq	%r10
	movq	%rax, -8(%rsp)
	leal	(%rdi,%r8), %eax
	cltq
	addq	%rax, %r9
	leaq	map(%rip), %rax
	addq	%rax, %r9
	movl	%r13d, %eax
.L7:
	addl	%esi, %eax
	addl	%edi, %ecx
	movl	%eax, %r11d
	orl	%ecx, %r11d
	shrl	$31, %r11d
	cmpl	-32(%rsp), %eax
	setge	-9(%rsp)
	orb	-9(%rsp), %r11b
	jne	.L6
	cmpl	-28(%rsp), %ecx
	jge	.L6
	addq	-8(%rsp), %r9
	movq	%r9, %r11
	subq	%rbx, %r11
	movsbq	(%r11,%r10), %r11
	cmpb	$46, %r11b
	jne	.L8
	jmp	.L7
.L15:
	movl	$0, -16(%rsp)
	movl	-16(%rsp), %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE35:
	.size	calc, .-calc
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d%d"
.LC1:
	.string	"%d\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB36:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	$10, %esi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	movq	stdin(%rip), %rdx
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	14(%rsp), %rbp
	movq	%rbp, %rdi
	call	fgets@PLT
	leaq	n(%rip), %rcx
	movq	%rbp, %rdi
	xorl	%eax, %eax
	leaq	m(%rip), %rdx
	leaq	.LC0(%rip), %rsi
	call	__isoc99_sscanf@PLT
	movl	m(%rip), %eax
	testl	%eax, %eax
	jle	.L38
	leaq	map(%rip), %rbp
	leaq	sc(%rip), %rbx
	xorl	%r13d, %r13d
	leaq	4+sr(%rip), %r12
	.p2align 4,,10
	.p2align 3
.L42:
	movq	stdin(%rip), %rdx
	movl	$503, %esi
	movq	%rbp, %rdi
	call	fgets@PLT
	movl	n(%rip), %edx
	testl	%edx, %edx
	jle	.L39
	subl	$1, %edx
	leaq	sr(%rip), %rax
	leaq	(%r12,%rdx,4), %rcx
	movq	%rbp, %rdx
	.p2align 4,,10
	.p2align 3
.L41:
	cmpb	$46, (%rdx)
	je	.L40
	addl	$1, (%rax)
	addl	$1, (%rbx)
.L40:
	addq	$4, %rax
	addq	$1, %rdx
	cmpq	%rcx, %rax
	jne	.L41
.L39:
	addl	$1, %r13d
	addq	$503, %rbp
	addq	$4, %rbx
	cmpl	%r13d, m(%rip)
	jg	.L42
.L38:
	xorl	%r12d, %r12d
	jmp	.L43
	.p2align 4,,10
	.p2align 3
.L44:
	addl	%eax, %r12d
.L43:
	xorl	%eax, %eax
	call	calc
	testl	%eax, %eax
	jne	.L44
	movl	%r12d, %edx
	leaq	.LC1(%rip), %rsi
	movl	$1, %edi
	call	__printf_chk@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L49
	addq	$40, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
.L49:
	.cfi_restore_state
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE36:
	.size	main, .-main
	.globl	sc
	.bss
	.align 32
	.type	sc, @object
	.size	sc, 2004
sc:
	.zero	2004
	.globl	sr
	.align 32
	.type	sr, @object
	.size	sr, 2004
sr:
	.zero	2004
	.globl	cpos
	.align 32
	.type	cpos, @object
	.size	cpos, 364
cpos:
	.zero	364
	.globl	rpos
	.align 32
	.type	rpos, @object
	.size	rpos, 364
rpos:
	.zero	364
	.globl	mv
	.data
	.align 32
	.type	mv, @object
	.size	mv, 32
mv:
	.long	-1
	.long	0
	.long	0
	.long	1
	.long	1
	.long	0
	.long	0
	.long	-1
	.globl	map
	.bss
	.align 32
	.type	map, @object
	.size	map, 252003
map:
	.zero	252003
	.globl	n
	.align 4
	.type	n, @object
	.size	n, 4
n:
	.zero	4
	.globl	m
	.align 4
	.type	m, @object
	.size	m, 4
m:
	.zero	4
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

// AOJ 2299:   Tiles are Colorful
// 2017.12.10

#include <stdio.h>
#include <string.h>

int m, n;
char map[501][503];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int rpos['Z'+1], cpos['Z'+1];
int sr[501], sc[501];

int calc()
{
	int r, c, i, r2, c2, a, ans = 0;
	for (r = 0; r < m; r++) for (c = 0; c < n; c++) 
		if ((sr[c] + sc[r]) && map[r][c] == '.' ) {
			memset(rpos, -1, sizeof(rpos));

		    for (i = 0; i < 4; i++) {
				r2 = r, c2 = c;
				while (1) {
					r2 += mv[i][0], c2 += mv[i][1];
					if (r2 < 0 || r2 >= m || c2 < 0 || c2 >= n) break;
					if (map[r2][c2] == '.') continue;

	                a = map[r2][c2];
					if (rpos[a] >= 0) {
	                    map[r2][c2] = '.', sr[c2]--, sc[r2]--;
						r2 = rpos[a], c2 = cpos[a];
	                    map[r2][c2] = '.', sr[c2]--, sc[r2]--;
	                    ans += 2;
					} else rpos[a] = r2, cpos[a] = c2;
					break;
				}
            }
        }

	return ans;
}

int main()
{
	int r, c, t, ans;
	char buf[10];

    fgets(buf, 10, stdin);
	sscanf(buf, "%d%d", &m, &n);
	for (r = 0; r < m; r++) {
		fgets(map[r], 503, stdin);
		for (c = 0; c < n; c++) {
			if (map[r][c] != '.') sr[c]++, sc[r]++;
		}
	}
	ans = 0; while (t = calc()) ans += t;
	printf("%d\n", ans);
	return 0;
}
5 7
NBTUUOR
QT.SZRQ
SANAGIP
LMDGZBM
KLKIODP
 5 0
NBTUUOR
QT.SZRQ
PIGANAS
LMDGZBM
PDOIKKL
 5 7
OUTUBOR
QT.SZRQ
SANAGIP
LMDGZBM
KLKIODP
 5 7
NBTUUOR
QT.SZSQ
ASNAGIP
MBYGDML
KKLIODP
 3 7
NUTUBOR
RT.SZQQ
RANCGIP
MLZGDMB
LDOIKPK
 5 7
NUTUBOR
QT.SZRQ
SANAGIP
LMDGZBM
LKKIODP
 5 7
NUTUBOR
QT.SZRQ
TANAGIP
LMDGZBM
LKKIODP
 3 7
NUTUBOR
ST.SZQQ
RANCGIP
MLZGDMB
LCOIKPK
 5 7
NBTUUOR
QT.SZRQ
SANAGIP
LMDGZBM
LKKIODP
 5 3
NBTUUOR
QT.SZRQ
SANAGIP
LMDGZBM
LKKIODP

2
 0
 4
 14
 6
 30
 26
 8
 2
 2

Given the following C code and unoptimized assembly code, your task is to generate highly optimized x86-64 assembly code.
C Code:

```c
// AOJ 2299:   Tiles are Colorful
// 2017.12.10

#include <stdio.h>
#include <string.h>

int m, n;
char map[501][503];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int rpos['Z'+1], cpos['Z'+1];
int sr[501], sc[501];

int calc()
{
	int r, c, i, r2, c2, a, ans = 0;
	for (r = 0; r < m; r++) for (c = 0; c < n; c++) 
		if ((sr[c] + sc[r]) && map[r][c] == '.' ) {
			memset(rpos, -1, sizeof(rpos));

		    for (i = 0; i < 4; i++) {
				r2 = r, c2 = c;
				while (1) {
					r2 += mv[i][0], c2 += mv[i][1];
					if (r2 < 0 || r2 >= m || c2 < 0 || c2 >= n) break;
					if (map[r2][c2] == '.') continue;

	                a = map[r2][c2];
					if (rpos[a] >= 0) {
	                    map[r2][c2] = '.', sr[c2]--, sc[r2]--;
						r2 = rpos[a], c2 = cpos[a];
	                    map[r2][c2] = '.', sr[c2]--, sc[r2]--;
	                    ans += 2;
					} else rpos[a] = r2, cpos[a] = c2;
					break;
				}
            }
        }

	return ans;
}

int main()
{
	int r, c, t, ans;
	char buf[10];

    fgets(buf, 10, stdin);
	sscanf(buf, "%d%d", &m, &n);
	for (r = 0; r < m; r++) {
		fgets(map[r], 503, stdin);
		for (c = 0; c < n; c++) {
			if (map[r][c] != '.') sr[c]++, sc[r]++;
		}
	}
	ans = 0; while (t = calc()) ans += t;
	printf("%d\n", ans);
	return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	m
	.bss
	.align 4
	.type	m, @object
	.size	m, 4
m:
	.zero	4
	.globl	n
	.align 4
	.type	n, @object
	.size	n, 4
n:
	.zero	4
	.globl	map
	.align 32
	.type	map, @object
	.size	map, 252003
map:
	.zero	252003
	.globl	mv
	.data
	.align 32
	.type	mv, @object
	.size	mv, 32
mv:
	.long	-1
	.long	0
	.long	0
	.long	1
	.long	1
	.long	0
	.long	0
	.long	-1
	.globl	rpos
	.bss
	.align 32
	.type	rpos, @object
	.size	rpos, 364
rpos:
	.zero	364
	.globl	cpos
	.align 32
	.type	cpos, @object
	.size	cpos, 364
cpos:
	.zero	364
	.globl	sr
	.align 32
	.type	sr, @object
	.size	sr, 2004
sr:
	.zero	2004
	.globl	sc
	.align 32
	.type	sc, @object
	.size	sc, 2004
sc:
	.zero	2004
	.text
	.globl	calc
	.type	calc, @function
calc:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$32, %rsp
	movl	$0, -8(%rbp)
	movl	$0, -28(%rbp)
	jmp	.L2
.L14:
	movl	$0, -24(%rbp)
	jmp	.L3
.L13:
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %ecx
	movl	-28(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %eax
	addl	%ecx, %eax
	testl	%eax, %eax
	je	.L4
	movl	-24(%rbp), %eax
	movslq	%eax, %rdx
	movl	-28(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$46, %al
	jne	.L4
	movl	$364, %edx
	movl	$-1, %esi
	leaq	rpos(%rip), %rax
	movq	%rax, %rdi
	call	memset@PLT
	movl	$0, -20(%rbp)
	jmp	.L5
.L12:
	movl	-28(%rbp), %eax
	movl	%eax, -16(%rbp)
	movl	-24(%rbp), %eax
	movl	%eax, -12(%rbp)
.L11:
	movl	-20(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	mv(%rip), %rax
	movl	(%rdx,%rax), %eax
	addl	%eax, -16(%rbp)
	movl	-20(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	4+mv(%rip), %rax
	movl	(%rdx,%rax), %eax
	addl	%eax, -12(%rbp)
	cmpl	$0, -16(%rbp)
	js	.L6
	movl	m(%rip), %eax
	cmpl	%eax, -16(%rbp)
	jge	.L6
	cmpl	$0, -12(%rbp)
	js	.L6
	movl	n(%rip), %eax
	cmpl	%eax, -12(%rbp)
	jge	.L6
	movl	-12(%rbp), %eax
	movslq	%eax, %rdx
	movl	-16(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$46, %al
	je	.L17
	movl	-12(%rbp), %eax
	movslq	%eax, %rdx
	movl	-16(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	movsbl	%al, %eax
	movl	%eax, -4(%rbp)
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	rpos(%rip), %rax
	movl	(%rdx,%rax), %eax
	testl	%eax, %eax
	js	.L9
	movl	-12(%rbp), %eax
	movslq	%eax, %rdx
	movl	-16(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movb	$46, (%rax)
	movl	-12(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	-1(%rax), %ecx
	movl	-12(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	-16(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	-1(%rax), %ecx
	movl	-16(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	rpos(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -16(%rbp)
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	cpos(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -12(%rbp)
	movl	-12(%rbp), %eax
	movslq	%eax, %rdx
	movl	-16(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movb	$46, (%rax)
	movl	-12(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	-1(%rax), %ecx
	movl	-12(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	-16(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	-1(%rax), %ecx
	movl	-16(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	addl	$2, -8(%rbp)
	jmp	.L6
.L9:
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	rpos(%rip), %rdx
	movl	-16(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	cpos(%rip), %rdx
	movl	-12(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	jmp	.L6
.L17:
	nop
	jmp	.L11
.L6:
	addl	$1, -20(%rbp)
.L5:
	cmpl	$3, -20(%rbp)
	jle	.L12
.L4:
	addl	$1, -24(%rbp)
.L3:
	movl	n(%rip), %eax
	cmpl	%eax, -24(%rbp)
	jl	.L13
	addl	$1, -28(%rbp)
.L2:
	movl	m(%rip), %eax
	cmpl	%eax, -28(%rbp)
	jl	.L14
	movl	-8(%rbp), %eax
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	calc, .-calc
	.section	.rodata
.LC0:
	.string	"%d%d"
.LC1:
	.string	"%d\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$48, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movq	stdin(%rip), %rdx
	leaq	-18(%rbp), %rax
	movl	$10, %esi
	movq	%rax, %rdi
	call	fgets@PLT
	leaq	-18(%rbp), %rax
	leaq	n(%rip), %rdx
	movq	%rdx, %rcx
	leaq	m(%rip), %rdx
	leaq	.LC0(%rip), %rsi
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_sscanf@PLT
	movl	$0, -36(%rbp)
	jmp	.L19
.L23:
	movq	stdin(%rip), %rax
	movl	-36(%rbp), %edx
	movslq	%edx, %rdx
	imulq	$503, %rdx, %rdx
	leaq	map(%rip), %rcx
	addq	%rdx, %rcx
	movq	%rax, %rdx
	movl	$503, %esi
	movq	%rcx, %rdi
	call	fgets@PLT
	movl	$0, -32(%rbp)
	jmp	.L20
.L22:
	movl	-32(%rbp), %eax
	movslq	%eax, %rdx
	movl	-36(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$46, %al
	je	.L21
	movl	-32(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	1(%rax), %ecx
	movl	-32(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	-36(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	1(%rax), %ecx
	movl	-36(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	%ecx, (%rdx,%rax)
.L21:
	addl	$1, -32(%rbp)
.L20:
	movl	n(%rip), %eax
	cmpl	%eax, -32(%rbp)
	jl	.L22
	addl	$1, -36(%rbp)
.L19:
	movl	m(%rip), %eax
	cmpl	%eax, -36(%rbp)
	jl	.L23
	movl	$0, -28(%rbp)
	jmp	.L24
.L25:
	movl	-24(%rbp), %eax
	addl	%eax, -28(%rbp)
.L24:
	movl	$0, %eax
	call	calc
	movl	%eax, -24(%rbp)
	cmpl	$0, -24(%rbp)
	jne	.L25
	movl	-28(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L27
	call	__stack_chk_fail@PLT
.L27:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
```

Only output the optimized assembly code. Do not include any other text. Do not write any comments in the assembly code. Wrap the assembly code in ```assembly``` tags.
train
	.file	"temp.c"
	.text
	.globl	m
	.bss
	.align 4
	.type	m, @object
	.size	m, 4
m:
	.zero	4
	.globl	n
	.align 4
	.type	n, @object
	.size	n, 4
n:
	.zero	4
	.globl	map
	.align 32
	.type	map, @object
	.size	map, 252003
map:
	.zero	252003
	.globl	mv
	.data
	.align 32
	.type	mv, @object
	.size	mv, 32
mv:
	.long	-1
	.long	0
	.long	0
	.long	1
	.long	1
	.long	0
	.long	0
	.long	-1
	.globl	rpos
	.bss
	.align 32
	.type	rpos, @object
	.size	rpos, 364
rpos:
	.zero	364
	.globl	cpos
	.align 32
	.type	cpos, @object
	.size	cpos, 364
cpos:
	.zero	364
	.globl	sr
	.align 32
	.type	sr, @object
	.size	sr, 2004
sr:
	.zero	2004
	.globl	sc
	.align 32
	.type	sc, @object
	.size	sc, 2004
sc:
	.zero	2004
	.text
	.globl	calc
	.type	calc, @function
calc:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$32, %rsp
	movl	$0, -8(%rbp)
	movl	$0, -28(%rbp)
	jmp	.L2
.L14:
	movl	$0, -24(%rbp)
	jmp	.L3
.L13:
	movl	-24(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %ecx
	movl	-28(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %eax
	addl	%ecx, %eax
	testl	%eax, %eax
	je	.L4
	movl	-24(%rbp), %eax
	movslq	%eax, %rdx
	movl	-28(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$46, %al
	jne	.L4
	movl	$364, %edx
	movl	$-1, %esi
	leaq	rpos(%rip), %rax
	movq	%rax, %rdi
	call	memset@PLT
	movl	$0, -20(%rbp)
	jmp	.L5
.L12:
	movl	-28(%rbp), %eax
	movl	%eax, -16(%rbp)
	movl	-24(%rbp), %eax
	movl	%eax, -12(%rbp)
.L11:
	movl	-20(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	mv(%rip), %rax
	movl	(%rdx,%rax), %eax
	addl	%eax, -16(%rbp)
	movl	-20(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	4+mv(%rip), %rax
	movl	(%rdx,%rax), %eax
	addl	%eax, -12(%rbp)
	cmpl	$0, -16(%rbp)
	js	.L6
	movl	m(%rip), %eax
	cmpl	%eax, -16(%rbp)
	jge	.L6
	cmpl	$0, -12(%rbp)
	js	.L6
	movl	n(%rip), %eax
	cmpl	%eax, -12(%rbp)
	jge	.L6
	movl	-12(%rbp), %eax
	movslq	%eax, %rdx
	movl	-16(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$46, %al
	je	.L17
	movl	-12(%rbp), %eax
	movslq	%eax, %rdx
	movl	-16(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	movsbl	%al, %eax
	movl	%eax, -4(%rbp)
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	rpos(%rip), %rax
	movl	(%rdx,%rax), %eax
	testl	%eax, %eax
	js	.L9
	movl	-12(%rbp), %eax
	movslq	%eax, %rdx
	movl	-16(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movb	$46, (%rax)
	movl	-12(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	-1(%rax), %ecx
	movl	-12(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	-16(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	-1(%rax), %ecx
	movl	-16(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	rpos(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -16(%rbp)
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	cpos(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -12(%rbp)
	movl	-12(%rbp), %eax
	movslq	%eax, %rdx
	movl	-16(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movb	$46, (%rax)
	movl	-12(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	-1(%rax), %ecx
	movl	-12(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	-16(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	-1(%rax), %ecx
	movl	-16(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	addl	$2, -8(%rbp)
	jmp	.L6
.L9:
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	rpos(%rip), %rdx
	movl	-16(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rcx
	leaq	cpos(%rip), %rdx
	movl	-12(%rbp), %eax
	movl	%eax, (%rcx,%rdx)
	jmp	.L6
.L17:
	nop
	jmp	.L11
.L6:
	addl	$1, -20(%rbp)
.L5:
	cmpl	$3, -20(%rbp)
	jle	.L12
.L4:
	addl	$1, -24(%rbp)
.L3:
	movl	n(%rip), %eax
	cmpl	%eax, -24(%rbp)
	jl	.L13
	addl	$1, -28(%rbp)
.L2:
	movl	m(%rip), %eax
	cmpl	%eax, -28(%rbp)
	jl	.L14
	movl	-8(%rbp), %eax
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	calc, .-calc
	.section	.rodata
.LC0:
	.string	"%d%d"
.LC1:
	.string	"%d\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$48, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movq	stdin(%rip), %rdx
	leaq	-18(%rbp), %rax
	movl	$10, %esi
	movq	%rax, %rdi
	call	fgets@PLT
	leaq	-18(%rbp), %rax
	leaq	n(%rip), %rdx
	movq	%rdx, %rcx
	leaq	m(%rip), %rdx
	leaq	.LC0(%rip), %rsi
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_sscanf@PLT
	movl	$0, -36(%rbp)
	jmp	.L19
.L23:
	movq	stdin(%rip), %rax
	movl	-36(%rbp), %edx
	movslq	%edx, %rdx
	imulq	$503, %rdx, %rdx
	leaq	map(%rip), %rcx
	addq	%rdx, %rcx
	movq	%rax, %rdx
	movl	$503, %esi
	movq	%rcx, %rdi
	call	fgets@PLT
	movl	$0, -32(%rbp)
	jmp	.L20
.L22:
	movl	-32(%rbp), %eax
	movslq	%eax, %rdx
	movl	-36(%rbp), %eax
	cltq
	imulq	$503, %rax, %rax
	addq	%rax, %rdx
	leaq	map(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$46, %al
	je	.L21
	movl	-32(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	1(%rax), %ecx
	movl	-32(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sr(%rip), %rax
	movl	%ecx, (%rdx,%rax)
	movl	-36(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	(%rdx,%rax), %eax
	leal	1(%rax), %ecx
	movl	-36(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	sc(%rip), %rax
	movl	%ecx, (%rdx,%rax)
.L21:
	addl	$1, -32(%rbp)
.L20:
	movl	n(%rip), %eax
	cmpl	%eax, -32(%rbp)
	jl	.L22
	addl	$1, -36(%rbp)
.L19:
	movl	m(%rip), %eax
	cmpl	%eax, -36(%rbp)
	jl	.L23
	movl	$0, -28(%rbp)
	jmp	.L24
.L25:
	movl	-24(%rbp), %eax
	addl	%eax, -28(%rbp)
.L24:
	movl	$0, %eax
	call	calc
	movl	%eax, -24(%rbp)
	cmpl	$0, -24(%rbp)
	jne	.L25
	movl	-28(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L27
	call	__stack_chk_fail@PLT
.L27:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
