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>
#include<stdlib.h>

int func(int num);

int main() {
    int n = 0;
    int i,start=1000;
    int a[100]={0};
    //int max[100]={0};//極大
    //int min[100]={0};//極小
    int trend[100]={0};//1か-1

    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        scanf("%d",&(a[i]));
    }
    //初期トレンドを探る
    int point=1;
    int nt=0;
    while(point<=n) {

        if (a[0] < a[point]) {
            trend[0] = -1;
            nt=1;
            break;
        } else if (a[0] > a[point]) {
            trend[0] = 1;
            nt=-1;
            break;
        } else point++;
    }
    if(nt==0)//ずっと株価が同じだｄった
    {
        printf("%d",start);
        return 0;
    }

    for(i=1;i<n;i++)
    {
        if(a[i-1]<a[i])//上昇トレンド
        {
            if(nt==-1)
            {
                trend[i-1]=-1;
                nt=1;
            }
        }
        else if(a[i-1]>a[i])//下降トレンド
        {
            if(nt==1)
            {
                trend[i-1]=1;
                nt=-1;
            }
        }

        if(i==n-1&&nt==1)trend[n-1]=1;
    }

    int lasttrend=0;

    for(i=0;i<n;i++)
    {
        if(trend[i]==1)lasttrend=i;
    }

    long long int kabu=0;
    long long int money=(long long int)start;
    for(i=0;i<=lasttrend;i++)
    {
        if(trend[i]==-1)
        {
            kabu+=(money/(long long int)a[i]);
            money=(money%(long long int)a[i]);
        }
        else if(trend[i]==1)
        {
            money+=(kabu*(long long int)a[i]);
            kabu=0;
        }
        //printf("%d ",money);
    }
    /*
    printf("\n");
    for(i=0;i<n;i++)
    {
        printf("%d ",trend[i]);
    }
     */
    printf("%lld",money);
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.section	.rodata
.LC0:
	.string	"%d"
.LC1:
	.string	"%lld"
	.text
	.globl	main
	.type	main, @function
main:
.LFB6:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$864, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movl	$0, -856(%rbp)
	movl	$1000, -836(%rbp)
	leaq	-816(%rbp), %rdx
	movl	$0, %eax
	movl	$50, %ecx
	movq	%rdx, %rdi
	rep stosq
	leaq	-416(%rbp), %rdx
	movl	$0, %eax
	movl	$50, %ecx
	movq	%rdx, %rdi
	rep stosq
	leaq	-856(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$0, -852(%rbp)
	jmp	.L2
.L3:
	leaq	-816(%rbp), %rdx
	movl	-852(%rbp), %eax
	cltq
	salq	$2, %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -852(%rbp)
.L2:
	movl	-856(%rbp), %eax
	cmpl	%eax, -852(%rbp)
	jl	.L3
	movl	$1, -848(%rbp)
	movl	$0, -844(%rbp)
	jmp	.L4
.L8:
	movl	-816(%rbp), %edx
	movl	-848(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cmpl	%eax, %edx
	jge	.L5
	movl	$-1, -416(%rbp)
	movl	$1, -844(%rbp)
	jmp	.L6
.L5:
	movl	-816(%rbp), %edx
	movl	-848(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cmpl	%eax, %edx
	jle	.L7
	movl	$1, -416(%rbp)
	movl	$-1, -844(%rbp)
	jmp	.L6
.L7:
	addl	$1, -848(%rbp)
.L4:
	movl	-856(%rbp), %eax
	cmpl	%eax, -848(%rbp)
	jle	.L8
.L6:
	cmpl	$0, -844(%rbp)
	jne	.L9
	movl	-836(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	jmp	.L10
.L9:
	movl	$1, -852(%rbp)
	jmp	.L11
.L15:
	movl	-852(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	-816(%rbp,%rax,4), %edx
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cmpl	%eax, %edx
	jge	.L12
	cmpl	$-1, -844(%rbp)
	jne	.L13
	movl	-852(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	$-1, -416(%rbp,%rax,4)
	movl	$1, -844(%rbp)
	jmp	.L13
.L12:
	movl	-852(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	-816(%rbp,%rax,4), %edx
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cmpl	%eax, %edx
	jle	.L13
	cmpl	$1, -844(%rbp)
	jne	.L13
	movl	-852(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	$1, -416(%rbp,%rax,4)
	movl	$-1, -844(%rbp)
.L13:
	movl	-856(%rbp), %eax
	subl	$1, %eax
	cmpl	%eax, -852(%rbp)
	jne	.L14
	cmpl	$1, -844(%rbp)
	jne	.L14
	movl	-856(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	$1, -416(%rbp,%rax,4)
.L14:
	addl	$1, -852(%rbp)
.L11:
	movl	-856(%rbp), %eax
	cmpl	%eax, -852(%rbp)
	jl	.L15
	movl	$0, -840(%rbp)
	movl	$0, -852(%rbp)
	jmp	.L16
.L18:
	movl	-852(%rbp), %eax
	cltq
	movl	-416(%rbp,%rax,4), %eax
	cmpl	$1, %eax
	jne	.L17
	movl	-852(%rbp), %eax
	movl	%eax, -840(%rbp)
.L17:
	addl	$1, -852(%rbp)
.L16:
	movl	-856(%rbp), %eax
	cmpl	%eax, -852(%rbp)
	jl	.L18
	movq	$0, -832(%rbp)
	movl	-836(%rbp), %eax
	cltq
	movq	%rax, -824(%rbp)
	movl	$0, -852(%rbp)
	jmp	.L19
.L22:
	movl	-852(%rbp), %eax
	cltq
	movl	-416(%rbp,%rax,4), %eax
	cmpl	$-1, %eax
	jne	.L20
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	movslq	%eax, %rsi
	movq	-824(%rbp), %rax
	cqto
	idivq	%rsi
	addq	%rax, -832(%rbp)
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	movslq	%eax, %rcx
	movq	-824(%rbp), %rax
	cqto
	idivq	%rcx
	movq	%rdx, -824(%rbp)
	jmp	.L21
.L20:
	movl	-852(%rbp), %eax
	cltq
	movl	-416(%rbp,%rax,4), %eax
	cmpl	$1, %eax
	jne	.L21
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cltq
	imulq	-832(%rbp), %rax
	addq	%rax, -824(%rbp)
	movq	$0, -832(%rbp)
.L21:
	addl	$1, -852(%rbp)
.L19:
	movl	-852(%rbp), %eax
	cmpl	-840(%rbp), %eax
	jle	.L22
	movq	-824(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
.L10:
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L23
	call	__stack_chk_fail@PLT
.L23:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE6:
	.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
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"%d"
.LC4:
	.string	"%lld"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB39:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	$50, %ecx
	leaq	.LC1(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	xorl	%ebx, %ebx
	subq	$832, %rsp
	.cfi_def_cfa_offset 880
	movq	%fs:40, %rax
	movq	%rax, 824(%rsp)
	xorl	%eax, %eax
	leaq	16(%rsp), %r13
	leaq	416(%rsp), %rbp
	movl	$0, 12(%rsp)
	movq	%r13, %rdi
	leaq	12(%rsp), %rsi
	movq	%r13, %r12
	rep stosq
	movl	$50, %ecx
	movq	%rbp, %rdi
	rep stosq
	movq	%r14, %rdi
	call	__isoc99_scanf@PLT
	movl	12(%rsp), %eax
	testl	%eax, %eax
	jle	.L5
	.p2align 4,,10
	.p2align 3
.L4:
	movq	%r12, %rsi
	movq	%r14, %rdi
	xorl	%eax, %eax
	addl	$1, %ebx
	call	__isoc99_scanf@PLT
	movl	12(%rsp), %edx
	addq	$4, %r12
	cmpl	%ebx, %edx
	jg	.L4
	testl	%edx, %edx
	jle	.L5
	movl	16(%rsp), %ecx
	movl	$1, %eax
	jmp	.L11
	.p2align 4,,10
	.p2align 3
.L6:
	jg	.L29
	addq	$1, %rax
	cmpl	%eax, %edx
	jl	.L5
.L11:
	cmpl	0(%r13,%rax,4), %ecx
	jge	.L6
	orl	$-1, %eax
	movl	$1, %r9d
.L10:
	movl	%eax, 416(%rsp)
	cmpl	$1, %edx
	je	.L28
	movslq	%edx, %r10
	movl	$1, %eax
	leal	-1(%rdx), %r8d
	jmp	.L16
	.p2align 4,,10
	.p2align 3
.L51:
	cmpl	$-1, %r9d
	je	.L49
.L13:
	movslq	%r8d, %rdi
	cmpl	%eax, %r8d
	jne	.L15
	cmpl	$1, %r9d
	je	.L14
.L15:
	addq	$1, %rax
	cmpq	%r10, %rax
	je	.L50
.L16:
	movl	%ecx, %esi
	movl	0(%r13,%rax,4), %ecx
	movslq	%eax, %rdi
	cmpl	%esi, %ecx
	jg	.L51
	jge	.L13
	cmpl	$1, %r9d
	jne	.L15
	movl	$1, -4(%rbp,%rax,4)
	orl	$-1, %r9d
	jmp	.L15
	.p2align 4,,10
	.p2align 3
.L49:
	movl	$-1, -4(%rbp,%rax,4)
	cmpl	%eax, %r8d
	je	.L14
.L47:
	addq	$1, %rax
	movl	$1, %r9d
	cmpq	%r10, %rax
	jne	.L16
.L50:
	cmpl	$2, %r8d
	jbe	.L28
	movl	%edx, %ecx
	movq	%rbp, %rax
	pcmpeqd	%xmm3, %xmm3
	movdqa	.LC0(%rip), %xmm2
	shrl	$2, %ecx
	movdqa	.LC2(%rip), %xmm5
	movdqa	.LC3(%rip), %xmm4
	subl	$1, %ecx
	salq	$4, %rcx
	leaq	432(%rsp,%rcx), %rcx
	.p2align 4,,10
	.p2align 3
.L22:
	movdqa	%xmm4, %xmm0
	movdqa	%xmm2, %xmm1
	paddd	%xmm5, %xmm2
	addq	$16, %rax
	pcmpeqd	-16(%rax), %xmm0
	pand	%xmm0, %xmm1
	pandn	%xmm3, %xmm0
	movdqa	%xmm0, %xmm3
	por	%xmm1, %xmm3
	cmpq	%rcx, %rax
	jne	.L22
	movdqa	%xmm3, %xmm0
	xorl	%eax, %eax
	psrldq	$8, %xmm0
	movdqa	%xmm0, %xmm1
	pcmpgtd	%xmm3, %xmm1
	pand	%xmm1, %xmm0
	pandn	%xmm3, %xmm1
	por	%xmm0, %xmm1
	movdqa	%xmm1, %xmm2
	psrldq	$4, %xmm2
	movdqa	%xmm2, %xmm0
	pcmpgtd	%xmm1, %xmm0
	pand	%xmm0, %xmm2
	pandn	%xmm1, %xmm0
	por	%xmm2, %xmm0
	movd	%xmm0, %esi
	cmpl	$-1, %esi
	cmove	%eax, %esi
	movl	%edx, %eax
	andl	$-4, %eax
	testb	$3, %dl
	je	.L18
.L8:
	movslq	%eax, %rcx
	cmpl	$1, 416(%rsp,%rcx,4)
	leal	1(%rax), %ecx
	cmove	%eax, %esi
	cmpl	%ecx, %edx
	jle	.L18
	movslq	%ecx, %rdi
	cmpl	$1, 416(%rsp,%rdi,4)
	cmove	%ecx, %esi
	addl	$2, %eax
	cmpl	%eax, %edx
	jle	.L18
	movslq	%eax, %rdx
	cmpl	$1, 416(%rsp,%rdx,4)
	cmove	%eax, %esi
.L18:
	xorl	%ecx, %ecx
	movl	$1000, %eax
	xorl	%r8d, %r8d
	jmp	.L26
	.p2align 4,,10
	.p2align 3
.L24:
	cmpl	$1, %edi
	jne	.L25
	movslq	0(%r13,%rcx,4), %rdx
	imulq	%r8, %rdx
	xorl	%r8d, %r8d
	addq	%rdx, %rax
.L25:
	addq	$1, %rcx
	cmpl	%ecx, %esi
	jl	.L52
.L26:
	movl	0(%rbp,%rcx,4), %edi
	cmpl	$-1, %edi
	jne	.L24
	movslq	0(%r13,%rcx,4), %rdi
	cqto
	addq	$1, %rcx
	idivq	%rdi
	addq	%rax, %r8
	movq	%rdx, %rax
	cmpl	%ecx, %esi
	jge	.L26
.L52:
	movq	%rax, %rdx
	leaq	.LC4(%rip), %rsi
	movl	$1, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L3:
	movq	824(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L53
	addq	$832, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 48
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
.L5:
	.cfi_restore_state
	movl	$1000, %edx
	movq	%r14, %rsi
	movl	$1, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L3
.L14:
	movl	$1, 416(%rsp,%rdi,4)
	jmp	.L47
.L29:
	movl	$1, %eax
	orl	$-1, %r9d
	jmp	.L10
.L28:
	xorl	%esi, %esi
	xorl	%eax, %eax
	jmp	.L8
.L53:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE39:
	.size	main, .-main
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC0:
	.long	0
	.long	1
	.long	2
	.long	3
	.align 16
.LC2:
	.long	4
	.long	4
	.long	4
	.long	4
	.align 16
.LC3:
	.long	1
	.long	1
	.long	1
	.long	1
	.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
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"%d"
.LC4:
	.string	"%lld"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB39:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	$50, %ecx
	leaq	.LC1(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	xorl	%ebx, %ebx
	subq	$832, %rsp
	.cfi_def_cfa_offset 880
	movq	%fs:40, %rax
	movq	%rax, 824(%rsp)
	xorl	%eax, %eax
	leaq	16(%rsp), %r13
	leaq	416(%rsp), %rbp
	movl	$0, 12(%rsp)
	movq	%r13, %rdi
	leaq	12(%rsp), %rsi
	movq	%r13, %r12
	rep stosq
	movl	$50, %ecx
	movq	%rbp, %rdi
	rep stosq
	movq	%r14, %rdi
	call	__isoc99_scanf@PLT
	movl	12(%rsp), %eax
	testl	%eax, %eax
	jle	.L5
	.p2align 4,,10
	.p2align 3
.L4:
	movq	%r12, %rsi
	movq	%r14, %rdi
	xorl	%eax, %eax
	addl	$1, %ebx
	call	__isoc99_scanf@PLT
	movl	12(%rsp), %edx
	addq	$4, %r12
	cmpl	%ebx, %edx
	jg	.L4
	testl	%edx, %edx
	jle	.L5
	movl	16(%rsp), %ecx
	movl	$1, %eax
	jmp	.L11
	.p2align 4,,10
	.p2align 3
.L6:
	jg	.L29
	addq	$1, %rax
	cmpl	%eax, %edx
	jl	.L5
.L11:
	cmpl	0(%r13,%rax,4), %ecx
	jge	.L6
	orl	$-1, %eax
	movl	$1, %r9d
.L10:
	movl	%eax, 416(%rsp)
	cmpl	$1, %edx
	je	.L28
	movslq	%edx, %r10
	movl	$1, %eax
	leal	-1(%rdx), %r8d
	jmp	.L16
	.p2align 4,,10
	.p2align 3
.L51:
	cmpl	$-1, %r9d
	je	.L49
.L13:
	movslq	%r8d, %rdi
	cmpl	%eax, %r8d
	jne	.L15
	cmpl	$1, %r9d
	je	.L14
.L15:
	addq	$1, %rax
	cmpq	%r10, %rax
	je	.L50
.L16:
	movl	%ecx, %esi
	movl	0(%r13,%rax,4), %ecx
	movslq	%eax, %rdi
	cmpl	%esi, %ecx
	jg	.L51
	jge	.L13
	cmpl	$1, %r9d
	jne	.L15
	movl	$1, -4(%rbp,%rax,4)
	orl	$-1, %r9d
	jmp	.L15
	.p2align 4,,10
	.p2align 3
.L49:
	movl	$-1, -4(%rbp,%rax,4)
	cmpl	%eax, %r8d
	je	.L14
.L47:
	addq	$1, %rax
	movl	$1, %r9d
	cmpq	%r10, %rax
	jne	.L16
.L50:
	cmpl	$2, %r8d
	jbe	.L28
	movl	%edx, %ecx
	movq	%rbp, %rax
	pcmpeqd	%xmm3, %xmm3
	movdqa	.LC0(%rip), %xmm2
	shrl	$2, %ecx
	movdqa	.LC2(%rip), %xmm5
	movdqa	.LC3(%rip), %xmm4
	subl	$1, %ecx
	salq	$4, %rcx
	leaq	432(%rsp,%rcx), %rcx
	.p2align 4,,10
	.p2align 3
.L22:
	movdqa	%xmm4, %xmm0
	movdqa	%xmm2, %xmm1
	paddd	%xmm5, %xmm2
	addq	$16, %rax
	pcmpeqd	-16(%rax), %xmm0
	pand	%xmm0, %xmm1
	pandn	%xmm3, %xmm0
	movdqa	%xmm0, %xmm3
	por	%xmm1, %xmm3
	cmpq	%rcx, %rax
	jne	.L22
	movdqa	%xmm3, %xmm0
	xorl	%eax, %eax
	psrldq	$8, %xmm0
	movdqa	%xmm0, %xmm1
	pcmpgtd	%xmm3, %xmm1
	pand	%xmm1, %xmm0
	pandn	%xmm3, %xmm1
	por	%xmm0, %xmm1
	movdqa	%xmm1, %xmm2
	psrldq	$4, %xmm2
	movdqa	%xmm2, %xmm0
	pcmpgtd	%xmm1, %xmm0
	pand	%xmm0, %xmm2
	pandn	%xmm1, %xmm0
	por	%xmm2, %xmm0
	movd	%xmm0, %esi
	cmpl	$-1, %esi
	cmove	%eax, %esi
	movl	%edx, %eax
	andl	$-4, %eax
	testb	$3, %dl
	je	.L18
.L8:
	movslq	%eax, %rcx
	cmpl	$1, 416(%rsp,%rcx,4)
	leal	1(%rax), %ecx
	cmove	%eax, %esi
	cmpl	%ecx, %edx
	jle	.L18
	movslq	%ecx, %rdi
	cmpl	$1, 416(%rsp,%rdi,4)
	cmove	%ecx, %esi
	addl	$2, %eax
	cmpl	%eax, %edx
	jle	.L18
	movslq	%eax, %rdx
	cmpl	$1, 416(%rsp,%rdx,4)
	cmove	%eax, %esi
.L18:
	xorl	%ecx, %ecx
	movl	$1000, %eax
	xorl	%r8d, %r8d
	jmp	.L26
	.p2align 4,,10
	.p2align 3
.L24:
	cmpl	$1, %edi
	jne	.L25
	movslq	0(%r13,%rcx,4), %rdx
	imulq	%r8, %rdx
	xorl	%r8d, %r8d
	addq	%rdx, %rax
.L25:
	addq	$1, %rcx
	cmpl	%ecx, %esi
	jl	.L52
.L26:
	movl	0(%rbp,%rcx,4), %edi
	cmpl	$-1, %edi
	jne	.L24
	movslq	0(%r13,%rcx,4), %rdi
	cqto
	addq	$1, %rcx
	idivq	%rdi
	addq	%rax, %r8
	movq	%rdx, %rax
	cmpl	%ecx, %esi
	jge	.L26
.L52:
	movq	%rax, %rdx
	leaq	.LC4(%rip), %rsi
	movl	$1, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L3:
	movq	824(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L53
	addq	$832, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 48
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
.L5:
	.cfi_restore_state
	movl	$1000, %edx
	movq	%r14, %rsi
	movl	$1, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L3
.L14:
	movl	$1, 416(%rsp,%rdi,4)
	jmp	.L47
.L29:
	movl	$1, %eax
	orl	$-1, %r9d
	jmp	.L10
.L28:
	xorl	%esi, %esi
	xorl	%eax, %eax
	jmp	.L8
.L53:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE39:
	.size	main, .-main
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC0:
	.long	0
	.long	1
	.long	2
	.long	3
	.align 16
.LC2:
	.long	4
	.long	4
	.long	4
	.long	4
	.align 16
.LC3:
	.long	1
	.long	1
	.long	1
	.long	1
	.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>
#include<stdlib.h>

int func(int num);

int main() {
    int n = 0;
    int i,start=1000;
    int a[100]={0};
    //int max[100]={0};//極大
    //int min[100]={0};//極小
    int trend[100]={0};//1か-1

    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        scanf("%d",&(a[i]));
    }
    //初期トレンドを探る
    int point=1;
    int nt=0;
    while(point<=n) {

        if (a[0] < a[point]) {
            trend[0] = -1;
            nt=1;
            break;
        } else if (a[0] > a[point]) {
            trend[0] = 1;
            nt=-1;
            break;
        } else point++;
    }
    if(nt==0)//ずっと株価が同じだｄった
    {
        printf("%d",start);
        return 0;
    }

    for(i=1;i<n;i++)
    {
        if(a[i-1]<a[i])//上昇トレンド
        {
            if(nt==-1)
            {
                trend[i-1]=-1;
                nt=1;
            }
        }
        else if(a[i-1]>a[i])//下降トレンド
        {
            if(nt==1)
            {
                trend[i-1]=1;
                nt=-1;
            }
        }

        if(i==n-1&&nt==1)trend[n-1]=1;
    }

    int lasttrend=0;

    for(i=0;i<n;i++)
    {
        if(trend[i]==1)lasttrend=i;
    }

    long long int kabu=0;
    long long int money=(long long int)start;
    for(i=0;i<=lasttrend;i++)
    {
        if(trend[i]==-1)
        {
            kabu+=(money/(long long int)a[i]);
            money=(money%(long long int)a[i]);
        }
        else if(trend[i]==1)
        {
            money+=(kabu*(long long int)a[i]);
            kabu=0;
        }
        //printf("%d ",money);
    }
    /*
    printf("\n");
    for(i=0;i<n;i++)
    {
        printf("%d ",trend[i]);
    }
     */
    printf("%lld",money);
}
2
157 145
 7
100 130 243 130 115 115 150
 6
200 237 160 140 120 100
 2
157 218
 7
100 130 243 130 115 115 271
 6
200 237 160 233 120 100
 2
103 218
 7
110 130 243 130 115 115 271
 7
110 130 136 130 115 115 271
 6
200 237 119 233 121 100

1000 3165 1185 1366 5706 1696 2035 5161 2794 2211
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>
#include<stdlib.h>

int func(int num);

int main() {
    int n = 0;
    int i,start=1000;
    int a[100]={0};
    //int max[100]={0};//極大
    //int min[100]={0};//極小
    int trend[100]={0};//1か-1

    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        scanf("%d",&(a[i]));
    }
    //初期トレンドを探る
    int point=1;
    int nt=0;
    while(point<=n) {

        if (a[0] < a[point]) {
            trend[0] = -1;
            nt=1;
            break;
        } else if (a[0] > a[point]) {
            trend[0] = 1;
            nt=-1;
            break;
        } else point++;
    }
    if(nt==0)//ずっと株価が同じだｄった
    {
        printf("%d",start);
        return 0;
    }

    for(i=1;i<n;i++)
    {
        if(a[i-1]<a[i])//上昇トレンド
        {
            if(nt==-1)
            {
                trend[i-1]=-1;
                nt=1;
            }
        }
        else if(a[i-1]>a[i])//下降トレンド
        {
            if(nt==1)
            {
                trend[i-1]=1;
                nt=-1;
            }
        }

        if(i==n-1&&nt==1)trend[n-1]=1;
    }

    int lasttrend=0;

    for(i=0;i<n;i++)
    {
        if(trend[i]==1)lasttrend=i;
    }

    long long int kabu=0;
    long long int money=(long long int)start;
    for(i=0;i<=lasttrend;i++)
    {
        if(trend[i]==-1)
        {
            kabu+=(money/(long long int)a[i]);
            money=(money%(long long int)a[i]);
        }
        else if(trend[i]==1)
        {
            money+=(kabu*(long long int)a[i]);
            kabu=0;
        }
        //printf("%d ",money);
    }
    /*
    printf("\n");
    for(i=0;i<n;i++)
    {
        printf("%d ",trend[i]);
    }
     */
    printf("%lld",money);
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.section	.rodata
.LC0:
	.string	"%d"
.LC1:
	.string	"%lld"
	.text
	.globl	main
	.type	main, @function
main:
.LFB6:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$864, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movl	$0, -856(%rbp)
	movl	$1000, -836(%rbp)
	leaq	-816(%rbp), %rdx
	movl	$0, %eax
	movl	$50, %ecx
	movq	%rdx, %rdi
	rep stosq
	leaq	-416(%rbp), %rdx
	movl	$0, %eax
	movl	$50, %ecx
	movq	%rdx, %rdi
	rep stosq
	leaq	-856(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$0, -852(%rbp)
	jmp	.L2
.L3:
	leaq	-816(%rbp), %rdx
	movl	-852(%rbp), %eax
	cltq
	salq	$2, %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -852(%rbp)
.L2:
	movl	-856(%rbp), %eax
	cmpl	%eax, -852(%rbp)
	jl	.L3
	movl	$1, -848(%rbp)
	movl	$0, -844(%rbp)
	jmp	.L4
.L8:
	movl	-816(%rbp), %edx
	movl	-848(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cmpl	%eax, %edx
	jge	.L5
	movl	$-1, -416(%rbp)
	movl	$1, -844(%rbp)
	jmp	.L6
.L5:
	movl	-816(%rbp), %edx
	movl	-848(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cmpl	%eax, %edx
	jle	.L7
	movl	$1, -416(%rbp)
	movl	$-1, -844(%rbp)
	jmp	.L6
.L7:
	addl	$1, -848(%rbp)
.L4:
	movl	-856(%rbp), %eax
	cmpl	%eax, -848(%rbp)
	jle	.L8
.L6:
	cmpl	$0, -844(%rbp)
	jne	.L9
	movl	-836(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	jmp	.L10
.L9:
	movl	$1, -852(%rbp)
	jmp	.L11
.L15:
	movl	-852(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	-816(%rbp,%rax,4), %edx
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cmpl	%eax, %edx
	jge	.L12
	cmpl	$-1, -844(%rbp)
	jne	.L13
	movl	-852(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	$-1, -416(%rbp,%rax,4)
	movl	$1, -844(%rbp)
	jmp	.L13
.L12:
	movl	-852(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	-816(%rbp,%rax,4), %edx
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cmpl	%eax, %edx
	jle	.L13
	cmpl	$1, -844(%rbp)
	jne	.L13
	movl	-852(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	$1, -416(%rbp,%rax,4)
	movl	$-1, -844(%rbp)
.L13:
	movl	-856(%rbp), %eax
	subl	$1, %eax
	cmpl	%eax, -852(%rbp)
	jne	.L14
	cmpl	$1, -844(%rbp)
	jne	.L14
	movl	-856(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	$1, -416(%rbp,%rax,4)
.L14:
	addl	$1, -852(%rbp)
.L11:
	movl	-856(%rbp), %eax
	cmpl	%eax, -852(%rbp)
	jl	.L15
	movl	$0, -840(%rbp)
	movl	$0, -852(%rbp)
	jmp	.L16
.L18:
	movl	-852(%rbp), %eax
	cltq
	movl	-416(%rbp,%rax,4), %eax
	cmpl	$1, %eax
	jne	.L17
	movl	-852(%rbp), %eax
	movl	%eax, -840(%rbp)
.L17:
	addl	$1, -852(%rbp)
.L16:
	movl	-856(%rbp), %eax
	cmpl	%eax, -852(%rbp)
	jl	.L18
	movq	$0, -832(%rbp)
	movl	-836(%rbp), %eax
	cltq
	movq	%rax, -824(%rbp)
	movl	$0, -852(%rbp)
	jmp	.L19
.L22:
	movl	-852(%rbp), %eax
	cltq
	movl	-416(%rbp,%rax,4), %eax
	cmpl	$-1, %eax
	jne	.L20
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	movslq	%eax, %rsi
	movq	-824(%rbp), %rax
	cqto
	idivq	%rsi
	addq	%rax, -832(%rbp)
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	movslq	%eax, %rcx
	movq	-824(%rbp), %rax
	cqto
	idivq	%rcx
	movq	%rdx, -824(%rbp)
	jmp	.L21
.L20:
	movl	-852(%rbp), %eax
	cltq
	movl	-416(%rbp,%rax,4), %eax
	cmpl	$1, %eax
	jne	.L21
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cltq
	imulq	-832(%rbp), %rax
	addq	%rax, -824(%rbp)
	movq	$0, -832(%rbp)
.L21:
	addl	$1, -852(%rbp)
.L19:
	movl	-852(%rbp), %eax
	cmpl	-840(%rbp), %eax
	jle	.L22
	movq	-824(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
.L10:
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L23
	call	__stack_chk_fail@PLT
.L23:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE6:
	.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
	.section	.rodata
.LC0:
	.string	"%d"
.LC1:
	.string	"%lld"
	.text
	.globl	main
	.type	main, @function
main:
.LFB6:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$864, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movl	$0, -856(%rbp)
	movl	$1000, -836(%rbp)
	leaq	-816(%rbp), %rdx
	movl	$0, %eax
	movl	$50, %ecx
	movq	%rdx, %rdi
	rep stosq
	leaq	-416(%rbp), %rdx
	movl	$0, %eax
	movl	$50, %ecx
	movq	%rdx, %rdi
	rep stosq
	leaq	-856(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$0, -852(%rbp)
	jmp	.L2
.L3:
	leaq	-816(%rbp), %rdx
	movl	-852(%rbp), %eax
	cltq
	salq	$2, %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -852(%rbp)
.L2:
	movl	-856(%rbp), %eax
	cmpl	%eax, -852(%rbp)
	jl	.L3
	movl	$1, -848(%rbp)
	movl	$0, -844(%rbp)
	jmp	.L4
.L8:
	movl	-816(%rbp), %edx
	movl	-848(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cmpl	%eax, %edx
	jge	.L5
	movl	$-1, -416(%rbp)
	movl	$1, -844(%rbp)
	jmp	.L6
.L5:
	movl	-816(%rbp), %edx
	movl	-848(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cmpl	%eax, %edx
	jle	.L7
	movl	$1, -416(%rbp)
	movl	$-1, -844(%rbp)
	jmp	.L6
.L7:
	addl	$1, -848(%rbp)
.L4:
	movl	-856(%rbp), %eax
	cmpl	%eax, -848(%rbp)
	jle	.L8
.L6:
	cmpl	$0, -844(%rbp)
	jne	.L9
	movl	-836(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	jmp	.L10
.L9:
	movl	$1, -852(%rbp)
	jmp	.L11
.L15:
	movl	-852(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	-816(%rbp,%rax,4), %edx
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cmpl	%eax, %edx
	jge	.L12
	cmpl	$-1, -844(%rbp)
	jne	.L13
	movl	-852(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	$-1, -416(%rbp,%rax,4)
	movl	$1, -844(%rbp)
	jmp	.L13
.L12:
	movl	-852(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	-816(%rbp,%rax,4), %edx
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cmpl	%eax, %edx
	jle	.L13
	cmpl	$1, -844(%rbp)
	jne	.L13
	movl	-852(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	$1, -416(%rbp,%rax,4)
	movl	$-1, -844(%rbp)
.L13:
	movl	-856(%rbp), %eax
	subl	$1, %eax
	cmpl	%eax, -852(%rbp)
	jne	.L14
	cmpl	$1, -844(%rbp)
	jne	.L14
	movl	-856(%rbp), %eax
	subl	$1, %eax
	cltq
	movl	$1, -416(%rbp,%rax,4)
.L14:
	addl	$1, -852(%rbp)
.L11:
	movl	-856(%rbp), %eax
	cmpl	%eax, -852(%rbp)
	jl	.L15
	movl	$0, -840(%rbp)
	movl	$0, -852(%rbp)
	jmp	.L16
.L18:
	movl	-852(%rbp), %eax
	cltq
	movl	-416(%rbp,%rax,4), %eax
	cmpl	$1, %eax
	jne	.L17
	movl	-852(%rbp), %eax
	movl	%eax, -840(%rbp)
.L17:
	addl	$1, -852(%rbp)
.L16:
	movl	-856(%rbp), %eax
	cmpl	%eax, -852(%rbp)
	jl	.L18
	movq	$0, -832(%rbp)
	movl	-836(%rbp), %eax
	cltq
	movq	%rax, -824(%rbp)
	movl	$0, -852(%rbp)
	jmp	.L19
.L22:
	movl	-852(%rbp), %eax
	cltq
	movl	-416(%rbp,%rax,4), %eax
	cmpl	$-1, %eax
	jne	.L20
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	movslq	%eax, %rsi
	movq	-824(%rbp), %rax
	cqto
	idivq	%rsi
	addq	%rax, -832(%rbp)
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	movslq	%eax, %rcx
	movq	-824(%rbp), %rax
	cqto
	idivq	%rcx
	movq	%rdx, -824(%rbp)
	jmp	.L21
.L20:
	movl	-852(%rbp), %eax
	cltq
	movl	-416(%rbp,%rax,4), %eax
	cmpl	$1, %eax
	jne	.L21
	movl	-852(%rbp), %eax
	cltq
	movl	-816(%rbp,%rax,4), %eax
	cltq
	imulq	-832(%rbp), %rax
	addq	%rax, -824(%rbp)
	movq	$0, -832(%rbp)
.L21:
	addl	$1, -852(%rbp)
.L19:
	movl	-852(%rbp), %eax
	cmpl	-840(%rbp), %eax
	jle	.L22
	movq	-824(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
.L10:
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L23
	call	__stack_chk_fail@PLT
.L23:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE6:
	.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:
