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

```c
#include <stdio.h>

char grid[255][256];
char dir[4][2] = {
	{1, 0}, {0, 1}, {1, 1}, {1, -1}
};

int length(int x, int y, int n, int l, int k)
{
	int max;
	int t;
	
	if (x == n || y == n || x < 0 || y < 0 || grid[x][y] == '0'){
		return (l);
	}
	
	return (length(x + dir[k][0], y + dir[k][1], n, l + 1, k));
}	

int main(void)
{
	int i, j, k, n;
	int max, len;
	
	while (scanf("%d", &n), n != 0){
		max = 0;
		
		for (i = 0; i < n; i++){
			scanf("%s", grid[i]);
		}
		
		for (i = 0; i < n; i++){
			for (j = 0; j < n; j++){
				if (grid[i][j] == '1'){
					for (k = 0; k < 4; k++){
						len = length(i, j, n, 0, k);
						if (max < len){
							max = len;
						}
					}
				}
			}
		}
		
		printf("%d\n", max);
	}
	
	return (0);
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	grid
	.bss
	.align 32
	.type	grid, @object
	.size	grid, 65280
grid:
	.zero	65280
	.globl	dir
	.data
	.align 8
	.type	dir, @object
	.size	dir, 8
dir:
	.string	"\001"
	.string	""
	.ascii	"\001"
	.ascii	"\001\001"
	.ascii	"\001\377"
	.text
	.globl	length
	.type	length, @function
length:
.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	%edi, -4(%rbp)
	movl	%esi, -8(%rbp)
	movl	%edx, -12(%rbp)
	movl	%ecx, -16(%rbp)
	movl	%r8d, -20(%rbp)
	movl	-4(%rbp), %eax
	cmpl	-12(%rbp), %eax
	je	.L2
	movl	-8(%rbp), %eax
	cmpl	-12(%rbp), %eax
	je	.L2
	cmpl	$0, -4(%rbp)
	js	.L2
	cmpl	$0, -8(%rbp)
	js	.L2
	movl	-8(%rbp), %eax
	movslq	%eax, %rdx
	movl	-4(%rbp), %eax
	cltq
	salq	$8, %rax
	addq	%rax, %rdx
	leaq	grid(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$48, %al
	jne	.L3
.L2:
	movl	-16(%rbp), %eax
	jmp	.L4
.L3:
	movl	-16(%rbp), %eax
	leal	1(%rax), %edx
	movl	-20(%rbp), %eax
	cltq
	leaq	(%rax,%rax), %rcx
	leaq	1+dir(%rip), %rax
	movzbl	(%rcx,%rax), %eax
	movsbl	%al, %ecx
	movl	-8(%rbp), %eax
	leal	(%rcx,%rax), %esi
	movl	-20(%rbp), %eax
	cltq
	leaq	(%rax,%rax), %rcx
	leaq	dir(%rip), %rax
	movzbl	(%rcx,%rax), %eax
	movsbl	%al, %ecx
	movl	-4(%rbp), %eax
	leal	(%rcx,%rax), %edi
	movl	-20(%rbp), %ecx
	movl	-12(%rbp), %eax
	movl	%ecx, %r8d
	movl	%edx, %ecx
	movl	%eax, %edx
	call	length
.L4:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	length, .-length
	.section	.rodata
.LC0:
	.string	"%s"
.LC1:
	.string	"%d\n"
.LC2:
	.string	"%d"
	.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	$32, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	jmp	.L6
.L17:
	movl	$0, -16(%rbp)
	movl	$0, -28(%rbp)
	jmp	.L7
.L8:
	movl	-28(%rbp), %eax
	cltq
	salq	$8, %rax
	movq	%rax, %rdx
	leaq	grid(%rip), %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -28(%rbp)
.L7:
	movl	-32(%rbp), %eax
	cmpl	%eax, -28(%rbp)
	jl	.L8
	movl	$0, -28(%rbp)
	jmp	.L9
.L16:
	movl	$0, -24(%rbp)
	jmp	.L10
.L15:
	movl	-24(%rbp), %eax
	movslq	%eax, %rdx
	movl	-28(%rbp), %eax
	cltq
	salq	$8, %rax
	addq	%rax, %rdx
	leaq	grid(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$49, %al
	jne	.L11
	movl	$0, -20(%rbp)
	jmp	.L12
.L14:
	movl	-32(%rbp), %edx
	movl	-20(%rbp), %ecx
	movl	-24(%rbp), %esi
	movl	-28(%rbp), %eax
	movl	%ecx, %r8d
	movl	$0, %ecx
	movl	%eax, %edi
	call	length
	movl	%eax, -12(%rbp)
	movl	-16(%rbp), %eax
	cmpl	-12(%rbp), %eax
	jge	.L13
	movl	-12(%rbp), %eax
	movl	%eax, -16(%rbp)
.L13:
	addl	$1, -20(%rbp)
.L12:
	cmpl	$3, -20(%rbp)
	jle	.L14
.L11:
	addl	$1, -24(%rbp)
.L10:
	movl	-32(%rbp), %eax
	cmpl	%eax, -24(%rbp)
	jl	.L15
	addl	$1, -28(%rbp)
.L9:
	movl	-32(%rbp), %eax
	cmpl	%eax, -28(%rbp)
	jl	.L16
	movl	-16(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
.L6:
	leaq	-32(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-32(%rbp), %eax
	testl	%eax, %eax
	jne	.L17
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L19
	call	__stack_chk_fail@PLT
.L19:
	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	length
	.type	length, @function
length:
.LFB23:
	.cfi_startproc
	endbr64
	movl	%edx, %r9d
	movl	%ecx, %r10d
	cmpl	%esi, %edx
	je	.L6
	cmpl	%edi, %edx
	je	.L6
	movl	%edi, %eax
	orl	%esi, %eax
	js	.L6
	leaq	grid(%rip), %r11
	leaq	dir(%rip), %rcx
	movslq	%r8d, %r8
	jmp	.L3
	.p2align 4,,10
	.p2align 3
.L9:
	movsbl	1(%rcx,%r8,2), %eax
	addl	$1, %r10d
	addl	%eax, %esi
	movsbl	(%rcx,%r8,2), %eax
	addl	%eax, %edi
	cmpl	%r9d, %edi
	je	.L6
	cmpl	%r9d, %esi
	je	.L6
	movl	%esi, %eax
	orl	%edi, %eax
	js	.L6
.L3:
	movslq	%edi, %rax
	movslq	%esi, %rdx
	salq	$8, %rax
	addq	%r11, %rax
	cmpb	$48, (%rax,%rdx)
	jne	.L9
.L6:
	movl	%r10d, %eax
	ret
	.cfi_endproc
.LFE23:
	.size	length, .-length
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%s"
.LC1:
	.string	"%d\n"
.LC2:
	.string	"%d"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB24:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	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
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	leaq	52(%rsp), %rax
	movq	%rax, 40(%rsp)
.L11:
	movq	40(%rsp), %rsi
	leaq	.LC2(%rip), %rdi
	xorl	%eax, %eax
	call	__isoc99_scanf@PLT
	movl	52(%rsp), %eax
	testl	%eax, %eax
	je	.L30
	jle	.L13
	leaq	grid(%rip), %rax
	xorl	%ebx, %ebx
	movq	%rax, (%rsp)
	movq	%rax, %rbp
	.p2align 4,,10
	.p2align 3
.L12:
	movq	%rbp, %rsi
	leaq	.LC0(%rip), %rdi
	xorl	%eax, %eax
	addl	$1, %ebx
	call	__isoc99_scanf@PLT
	movl	52(%rsp), %r13d
	addq	$256, %rbp
	cmpl	%ebx, %r13d
	jg	.L12
	testl	%r13d, %r13d
	jle	.L13
	movslq	%r13d, %rax
	xorl	%r14d, %r14d
	xorl	%r15d, %r15d
	movq	%rax, 8(%rsp)
	movl	%r14d, %r12d
	.p2align 4,,10
	.p2align 3
.L28:
	xorl	%r14d, %r14d
	jmp	.L27
	.p2align 4,,10
	.p2align 3
.L15:
	addq	$1, %r14
	cmpq	8(%rsp), %r14
	je	.L46
.L27:
	movq	(%rsp), %rax
	movl	%r14d, %ebx
	cmpb	$49, (%rax,%r14)
	jne	.L15
	cmpl	%r14d, %r13d
	movq	%r14, 32(%rsp)
	leaq	dir(%rip), %r10
	sete	%dl
	cmpl	%r12d, %r13d
	sete	%al
	orl	%eax, %edx
	leal	1(%r14), %eax
	cltq
	movb	%dl, 23(%rsp)
	movq	%rax, 24(%rsp)
.L22:
	cmpb	$0, 23(%rsp)
	jne	.L47
	movsbq	(%r10), %rdx
	movsbq	1(%r10), %rbp
	movsbl	%dl, %ecx
	movq	%rbp, %r9
	salq	$8, %rdx
	cmpb	$1, %bpl
	jne	.L48
	leal	(%rcx,%r12), %r8d
	leaq	grid(%rip), %rax
	movl	%r12d, %edi
	movl	%ebx, %esi
	movslq	%r8d, %r8
	leaq	1(%rdx), %r11
	salq	$8, %r8
	addq	24(%rsp), %r8
	addq	%rax, %r8
	xorl	%eax, %eax
	.p2align 4,,10
	.p2align 3
.L21:
	addl	$1, %esi
	addl	$1, %eax
	addl	%ecx, %edi
	cmpl	%esi, %r13d
	je	.L18
	cmpl	%edi, %r13d
	je	.L18
	movl	%edi, %r14d
	orl	%esi, %r14d
	js	.L18
	addq	%r11, %r8
	movq	%r8, %r9
	subq	%rdx, %r9
	cmpb	$48, -1(%r9)
	jne	.L21
.L18:
	cmpl	%eax, %r15d
	cmovl	%eax, %r15d
	addq	$2, %r10
	leaq	8+dir(%rip), %rax
	cmpq	%rax, %r10
	jne	.L22
	movq	32(%rsp), %r14
	addq	$1, %r14
	cmpq	8(%rsp), %r14
	jne	.L27
	.p2align 4,,10
	.p2align 3
.L46:
	addl	$1, %r12d
	addq	$256, (%rsp)
	cmpl	%r12d, %r13d
	jne	.L28
.L29:
	movl	%r15d, %edx
	leaq	.LC1(%rip), %rsi
	movl	$1, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L11
	.p2align 4,,10
	.p2align 3
.L48:
	leal	(%rcx,%r12), %r8d
	leal	0(%rbp,%rbx), %eax
	movl	%r12d, %esi
	movl	%ebx, %edi
	movslq	%r8d, %r8
	cltq
	leaq	0(%rbp,%rdx), %r14
	negq	%r9
	salq	$8, %r8
	addq	%rax, %r8
	leaq	grid(%rip), %rax
	addq	%rax, %r8
	xorl	%eax, %eax
	.p2align 4,,10
	.p2align 3
.L17:
	addl	%ecx, %esi
	addl	$1, %eax
	addl	%ebp, %edi
	cmpl	%esi, %r13d
	je	.L18
	cmpl	%edi, %r13d
	je	.L18
	movl	%edi, %r11d
	orl	%esi, %r11d
	js	.L18
	addq	%r14, %r8
	movq	%r8, %r11
	subq	%rdx, %r11
	cmpb	$48, (%r11,%r9)
	jne	.L17
	jmp	.L18
.L47:
	xorl	%eax, %eax
	jmp	.L18
.L13:
	xorl	%r15d, %r15d
	jmp	.L29
.L30:
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L49
	addq	$72, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	xorl	%eax, %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
.L49:
	.cfi_restore_state
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE24:
	.size	main, .-main
	.globl	dir
	.data
	.align 8
	.type	dir, @object
	.size	dir, 8
dir:
	.string	"\001"
	.string	""
	.ascii	"\001"
	.ascii	"\001\001"
	.ascii	"\001\377"
	.globl	grid
	.bss
	.align 32
	.type	grid, @object
	.size	grid, 65280
grid:
	.zero	65280
	.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	length
	.type	length, @function
length:
.LFB23:
	.cfi_startproc
	endbr64
	movl	%edx, %r9d
	movl	%ecx, %r10d
	cmpl	%esi, %edx
	je	.L6
	cmpl	%edi, %edx
	je	.L6
	movl	%edi, %eax
	orl	%esi, %eax
	js	.L6
	leaq	grid(%rip), %r11
	leaq	dir(%rip), %rcx
	movslq	%r8d, %r8
	jmp	.L3
	.p2align 4,,10
	.p2align 3
.L9:
	movsbl	1(%rcx,%r8,2), %eax
	addl	$1, %r10d
	addl	%eax, %esi
	movsbl	(%rcx,%r8,2), %eax
	addl	%eax, %edi
	cmpl	%r9d, %edi
	je	.L6
	cmpl	%r9d, %esi
	je	.L6
	movl	%esi, %eax
	orl	%edi, %eax
	js	.L6
.L3:
	movslq	%edi, %rax
	movslq	%esi, %rdx
	salq	$8, %rax
	addq	%r11, %rax
	cmpb	$48, (%rax,%rdx)
	jne	.L9
.L6:
	movl	%r10d, %eax
	ret
	.cfi_endproc
.LFE23:
	.size	length, .-length
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%s"
.LC1:
	.string	"%d\n"
.LC2:
	.string	"%d"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB24:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	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
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	leaq	52(%rsp), %rax
	movq	%rax, 40(%rsp)
.L11:
	movq	40(%rsp), %rsi
	leaq	.LC2(%rip), %rdi
	xorl	%eax, %eax
	call	__isoc99_scanf@PLT
	movl	52(%rsp), %eax
	testl	%eax, %eax
	je	.L30
	jle	.L13
	leaq	grid(%rip), %rax
	xorl	%ebx, %ebx
	movq	%rax, (%rsp)
	movq	%rax, %rbp
	.p2align 4,,10
	.p2align 3
.L12:
	movq	%rbp, %rsi
	leaq	.LC0(%rip), %rdi
	xorl	%eax, %eax
	addl	$1, %ebx
	call	__isoc99_scanf@PLT
	movl	52(%rsp), %r13d
	addq	$256, %rbp
	cmpl	%ebx, %r13d
	jg	.L12
	testl	%r13d, %r13d
	jle	.L13
	movslq	%r13d, %rax
	xorl	%r14d, %r14d
	xorl	%r15d, %r15d
	movq	%rax, 8(%rsp)
	movl	%r14d, %r12d
	.p2align 4,,10
	.p2align 3
.L28:
	xorl	%r14d, %r14d
	jmp	.L27
	.p2align 4,,10
	.p2align 3
.L15:
	addq	$1, %r14
	cmpq	8(%rsp), %r14
	je	.L46
.L27:
	movq	(%rsp), %rax
	movl	%r14d, %ebx
	cmpb	$49, (%rax,%r14)
	jne	.L15
	cmpl	%r14d, %r13d
	movq	%r14, 32(%rsp)
	leaq	dir(%rip), %r10
	sete	%dl
	cmpl	%r12d, %r13d
	sete	%al
	orl	%eax, %edx
	leal	1(%r14), %eax
	cltq
	movb	%dl, 23(%rsp)
	movq	%rax, 24(%rsp)
.L22:
	cmpb	$0, 23(%rsp)
	jne	.L47
	movsbq	(%r10), %rdx
	movsbq	1(%r10), %rbp
	movsbl	%dl, %ecx
	movq	%rbp, %r9
	salq	$8, %rdx
	cmpb	$1, %bpl
	jne	.L48
	leal	(%rcx,%r12), %r8d
	leaq	grid(%rip), %rax
	movl	%r12d, %edi
	movl	%ebx, %esi
	movslq	%r8d, %r8
	leaq	1(%rdx), %r11
	salq	$8, %r8
	addq	24(%rsp), %r8
	addq	%rax, %r8
	xorl	%eax, %eax
	.p2align 4,,10
	.p2align 3
.L21:
	addl	$1, %esi
	addl	$1, %eax
	addl	%ecx, %edi
	cmpl	%esi, %r13d
	je	.L18
	cmpl	%edi, %r13d
	je	.L18
	movl	%edi, %r14d
	orl	%esi, %r14d
	js	.L18
	addq	%r11, %r8
	movq	%r8, %r9
	subq	%rdx, %r9
	cmpb	$48, -1(%r9)
	jne	.L21
.L18:
	cmpl	%eax, %r15d
	cmovl	%eax, %r15d
	addq	$2, %r10
	leaq	8+dir(%rip), %rax
	cmpq	%rax, %r10
	jne	.L22
	movq	32(%rsp), %r14
	addq	$1, %r14
	cmpq	8(%rsp), %r14
	jne	.L27
	.p2align 4,,10
	.p2align 3
.L46:
	addl	$1, %r12d
	addq	$256, (%rsp)
	cmpl	%r12d, %r13d
	jne	.L28
.L29:
	movl	%r15d, %edx
	leaq	.LC1(%rip), %rsi
	movl	$1, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L11
	.p2align 4,,10
	.p2align 3
.L48:
	leal	(%rcx,%r12), %r8d
	leal	0(%rbp,%rbx), %eax
	movl	%r12d, %esi
	movl	%ebx, %edi
	movslq	%r8d, %r8
	cltq
	leaq	0(%rbp,%rdx), %r14
	negq	%r9
	salq	$8, %r8
	addq	%rax, %r8
	leaq	grid(%rip), %rax
	addq	%rax, %r8
	xorl	%eax, %eax
	.p2align 4,,10
	.p2align 3
.L17:
	addl	%ecx, %esi
	addl	$1, %eax
	addl	%ebp, %edi
	cmpl	%esi, %r13d
	je	.L18
	cmpl	%edi, %r13d
	je	.L18
	movl	%edi, %r11d
	orl	%esi, %r11d
	js	.L18
	addq	%r14, %r8
	movq	%r8, %r11
	subq	%rdx, %r11
	cmpb	$48, (%r11,%r9)
	jne	.L17
	jmp	.L18
.L47:
	xorl	%eax, %eax
	jmp	.L18
.L13:
	xorl	%r15d, %r15d
	jmp	.L29
.L30:
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L49
	addq	$72, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	xorl	%eax, %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
.L49:
	.cfi_restore_state
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE24:
	.size	main, .-main
	.globl	dir
	.data
	.align 8
	.type	dir, @object
	.size	dir, 8
dir:
	.string	"\001"
	.string	""
	.ascii	"\001"
	.ascii	"\001\001"
	.ascii	"\001\377"
	.globl	grid
	.bss
	.align 32
	.type	grid, @object
	.size	grid, 65280
grid:
	.zero	65280
	.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:

#include <stdio.h>

char grid[255][256];
char dir[4][2] = {
	{1, 0}, {0, 1}, {1, 1}, {1, -1}
};

int length(int x, int y, int n, int l, int k)
{
	int max;
	int t;
	
	if (x == n || y == n || x < 0 || y < 0 || grid[x][y] == '0'){
		return (l);
	}
	
	return (length(x + dir[k][0], y + dir[k][1], n, l + 1, k));
}	

int main(void)
{
	int i, j, k, n;
	int max, len;
	
	while (scanf("%d", &n), n != 0){
		max = 0;
		
		for (i = 0; i < n; i++){
			scanf("%s", grid[i]);
		}
		
		for (i = 0; i < n; i++){
			for (j = 0; j < n; j++){
				if (grid[i][j] == '1'){
					for (k = 0; k < 4; k++){
						len = length(i, j, n, 0, k);
						if (max < len){
							max = len;
						}
					}
				}
			}
		}
		
		printf("%d\n", max);
	}
	
	return (0);
}
5
00011
00101
01000
10101
00010
8
11000001
10110111
01100111
01111010
11111111
01001010
10100010
10000001
2
01
00
3
000
000
000
0
 5
01011
00101
01000
10101
00010
8
11000001
10110111
01100111
01111010
11111111
01001010
10100000
10000001
0
01
00
3
000
000
000
0
 5
01011
00101
01100
10101
00010
8
11000001
10110111
01100111
10111010
10111111
01001010
10100000
10000000
0
01
00
3
001
100
000
0
 5
01011
00000
01000
10101
01010
8
11010001
10110111
01100011
01111010
11111111
01101000
10100010
10000101
0
0
1
6
000
100
000
0
 5
01011
10000
01000
10001
01010
8
11010001
10110111
01100011
01111010
11111111
01101000
10100010
10000101
0
0
1
6
000
100
000
0
 5
00011
00101
01000
10101
00010
8
11000001
10110111
01100111
01111010
11111111
01001010
10100010
10000001
2
01
00
3
000
010
000
0
 5
01011
00100
01000
10101
01010
8
11010001
10110111
01100111
01111010
11011111
01001010
10100000
10000001
0
0
1
3
000
000
000
0
 5
01011
00101
01000
10101
00010
8
11000001
10110111
01100111
01111010
11111111
01001010
10100010
10000001
2
01
00
3
000
011
000
0
 5
01011
00101
01000
10101
00010
8
11100001
10110111
01100111
01111010
11111111
01000010
10100000
10000001
2
01
00
0
000
000
000
0
 5
01010
00101
01000
10101
00010
8
10000001
10110111
01100011
01111010
11111110
01001010
10110000
10000001
0
0
00
1
000
010
100
0

4
8
1
0
 4
8
 4
6
 3
8
 2
8
 4
8
1
1
 4
5
 4
8
1
2
 4
8
1
 4
7

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

```c
#include <stdio.h>

char grid[255][256];
char dir[4][2] = {
	{1, 0}, {0, 1}, {1, 1}, {1, -1}
};

int length(int x, int y, int n, int l, int k)
{
	int max;
	int t;
	
	if (x == n || y == n || x < 0 || y < 0 || grid[x][y] == '0'){
		return (l);
	}
	
	return (length(x + dir[k][0], y + dir[k][1], n, l + 1, k));
}	

int main(void)
{
	int i, j, k, n;
	int max, len;
	
	while (scanf("%d", &n), n != 0){
		max = 0;
		
		for (i = 0; i < n; i++){
			scanf("%s", grid[i]);
		}
		
		for (i = 0; i < n; i++){
			for (j = 0; j < n; j++){
				if (grid[i][j] == '1'){
					for (k = 0; k < 4; k++){
						len = length(i, j, n, 0, k);
						if (max < len){
							max = len;
						}
					}
				}
			}
		}
		
		printf("%d\n", max);
	}
	
	return (0);
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	grid
	.bss
	.align 32
	.type	grid, @object
	.size	grid, 65280
grid:
	.zero	65280
	.globl	dir
	.data
	.align 8
	.type	dir, @object
	.size	dir, 8
dir:
	.string	"\001"
	.string	""
	.ascii	"\001"
	.ascii	"\001\001"
	.ascii	"\001\377"
	.text
	.globl	length
	.type	length, @function
length:
.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	%edi, -4(%rbp)
	movl	%esi, -8(%rbp)
	movl	%edx, -12(%rbp)
	movl	%ecx, -16(%rbp)
	movl	%r8d, -20(%rbp)
	movl	-4(%rbp), %eax
	cmpl	-12(%rbp), %eax
	je	.L2
	movl	-8(%rbp), %eax
	cmpl	-12(%rbp), %eax
	je	.L2
	cmpl	$0, -4(%rbp)
	js	.L2
	cmpl	$0, -8(%rbp)
	js	.L2
	movl	-8(%rbp), %eax
	movslq	%eax, %rdx
	movl	-4(%rbp), %eax
	cltq
	salq	$8, %rax
	addq	%rax, %rdx
	leaq	grid(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$48, %al
	jne	.L3
.L2:
	movl	-16(%rbp), %eax
	jmp	.L4
.L3:
	movl	-16(%rbp), %eax
	leal	1(%rax), %edx
	movl	-20(%rbp), %eax
	cltq
	leaq	(%rax,%rax), %rcx
	leaq	1+dir(%rip), %rax
	movzbl	(%rcx,%rax), %eax
	movsbl	%al, %ecx
	movl	-8(%rbp), %eax
	leal	(%rcx,%rax), %esi
	movl	-20(%rbp), %eax
	cltq
	leaq	(%rax,%rax), %rcx
	leaq	dir(%rip), %rax
	movzbl	(%rcx,%rax), %eax
	movsbl	%al, %ecx
	movl	-4(%rbp), %eax
	leal	(%rcx,%rax), %edi
	movl	-20(%rbp), %ecx
	movl	-12(%rbp), %eax
	movl	%ecx, %r8d
	movl	%edx, %ecx
	movl	%eax, %edx
	call	length
.L4:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	length, .-length
	.section	.rodata
.LC0:
	.string	"%s"
.LC1:
	.string	"%d\n"
.LC2:
	.string	"%d"
	.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	$32, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	jmp	.L6
.L17:
	movl	$0, -16(%rbp)
	movl	$0, -28(%rbp)
	jmp	.L7
.L8:
	movl	-28(%rbp), %eax
	cltq
	salq	$8, %rax
	movq	%rax, %rdx
	leaq	grid(%rip), %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -28(%rbp)
.L7:
	movl	-32(%rbp), %eax
	cmpl	%eax, -28(%rbp)
	jl	.L8
	movl	$0, -28(%rbp)
	jmp	.L9
.L16:
	movl	$0, -24(%rbp)
	jmp	.L10
.L15:
	movl	-24(%rbp), %eax
	movslq	%eax, %rdx
	movl	-28(%rbp), %eax
	cltq
	salq	$8, %rax
	addq	%rax, %rdx
	leaq	grid(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$49, %al
	jne	.L11
	movl	$0, -20(%rbp)
	jmp	.L12
.L14:
	movl	-32(%rbp), %edx
	movl	-20(%rbp), %ecx
	movl	-24(%rbp), %esi
	movl	-28(%rbp), %eax
	movl	%ecx, %r8d
	movl	$0, %ecx
	movl	%eax, %edi
	call	length
	movl	%eax, -12(%rbp)
	movl	-16(%rbp), %eax
	cmpl	-12(%rbp), %eax
	jge	.L13
	movl	-12(%rbp), %eax
	movl	%eax, -16(%rbp)
.L13:
	addl	$1, -20(%rbp)
.L12:
	cmpl	$3, -20(%rbp)
	jle	.L14
.L11:
	addl	$1, -24(%rbp)
.L10:
	movl	-32(%rbp), %eax
	cmpl	%eax, -24(%rbp)
	jl	.L15
	addl	$1, -28(%rbp)
.L9:
	movl	-32(%rbp), %eax
	cmpl	%eax, -28(%rbp)
	jl	.L16
	movl	-16(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
.L6:
	leaq	-32(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-32(%rbp), %eax
	testl	%eax, %eax
	jne	.L17
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L19
	call	__stack_chk_fail@PLT
.L19:
	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	grid
	.bss
	.align 32
	.type	grid, @object
	.size	grid, 65280
grid:
	.zero	65280
	.globl	dir
	.data
	.align 8
	.type	dir, @object
	.size	dir, 8
dir:
	.string	"\001"
	.string	""
	.ascii	"\001"
	.ascii	"\001\001"
	.ascii	"\001\377"
	.text
	.globl	length
	.type	length, @function
length:
.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	%edi, -4(%rbp)
	movl	%esi, -8(%rbp)
	movl	%edx, -12(%rbp)
	movl	%ecx, -16(%rbp)
	movl	%r8d, -20(%rbp)
	movl	-4(%rbp), %eax
	cmpl	-12(%rbp), %eax
	je	.L2
	movl	-8(%rbp), %eax
	cmpl	-12(%rbp), %eax
	je	.L2
	cmpl	$0, -4(%rbp)
	js	.L2
	cmpl	$0, -8(%rbp)
	js	.L2
	movl	-8(%rbp), %eax
	movslq	%eax, %rdx
	movl	-4(%rbp), %eax
	cltq
	salq	$8, %rax
	addq	%rax, %rdx
	leaq	grid(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$48, %al
	jne	.L3
.L2:
	movl	-16(%rbp), %eax
	jmp	.L4
.L3:
	movl	-16(%rbp), %eax
	leal	1(%rax), %edx
	movl	-20(%rbp), %eax
	cltq
	leaq	(%rax,%rax), %rcx
	leaq	1+dir(%rip), %rax
	movzbl	(%rcx,%rax), %eax
	movsbl	%al, %ecx
	movl	-8(%rbp), %eax
	leal	(%rcx,%rax), %esi
	movl	-20(%rbp), %eax
	cltq
	leaq	(%rax,%rax), %rcx
	leaq	dir(%rip), %rax
	movzbl	(%rcx,%rax), %eax
	movsbl	%al, %ecx
	movl	-4(%rbp), %eax
	leal	(%rcx,%rax), %edi
	movl	-20(%rbp), %ecx
	movl	-12(%rbp), %eax
	movl	%ecx, %r8d
	movl	%edx, %ecx
	movl	%eax, %edx
	call	length
.L4:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	length, .-length
	.section	.rodata
.LC0:
	.string	"%s"
.LC1:
	.string	"%d\n"
.LC2:
	.string	"%d"
	.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	$32, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	jmp	.L6
.L17:
	movl	$0, -16(%rbp)
	movl	$0, -28(%rbp)
	jmp	.L7
.L8:
	movl	-28(%rbp), %eax
	cltq
	salq	$8, %rax
	movq	%rax, %rdx
	leaq	grid(%rip), %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -28(%rbp)
.L7:
	movl	-32(%rbp), %eax
	cmpl	%eax, -28(%rbp)
	jl	.L8
	movl	$0, -28(%rbp)
	jmp	.L9
.L16:
	movl	$0, -24(%rbp)
	jmp	.L10
.L15:
	movl	-24(%rbp), %eax
	movslq	%eax, %rdx
	movl	-28(%rbp), %eax
	cltq
	salq	$8, %rax
	addq	%rax, %rdx
	leaq	grid(%rip), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$49, %al
	jne	.L11
	movl	$0, -20(%rbp)
	jmp	.L12
.L14:
	movl	-32(%rbp), %edx
	movl	-20(%rbp), %ecx
	movl	-24(%rbp), %esi
	movl	-28(%rbp), %eax
	movl	%ecx, %r8d
	movl	$0, %ecx
	movl	%eax, %edi
	call	length
	movl	%eax, -12(%rbp)
	movl	-16(%rbp), %eax
	cmpl	-12(%rbp), %eax
	jge	.L13
	movl	-12(%rbp), %eax
	movl	%eax, -16(%rbp)
.L13:
	addl	$1, -20(%rbp)
.L12:
	cmpl	$3, -20(%rbp)
	jle	.L14
.L11:
	addl	$1, -24(%rbp)
.L10:
	movl	-32(%rbp), %eax
	cmpl	%eax, -24(%rbp)
	jl	.L15
	addl	$1, -28(%rbp)
.L9:
	movl	-32(%rbp), %eax
	cmpl	%eax, -28(%rbp)
	jl	.L16
	movl	-16(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
.L6:
	leaq	-32(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-32(%rbp), %eax
	testl	%eax, %eax
	jne	.L17
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L19
	call	__stack_chk_fail@PLT
.L19:
	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:
