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 <string.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <time.h>
#define ll long long
#define mod 1000000007
int main(){
    ll n,x,m,i,j,amari,repeat=0,
    ans=0,a[100010]={},flag[100010]={},num[100010]={};
    scanf("%lld %lld %lld",&n,&x,&m);
    a[1]=x;
    flag[x]++;
    num[x]=1;
    ans+=a[1];
    for(i=1; i<100009; i++){
        a[i+1]=(a[i]*a[i])%m;
        flag[a[i+1]]++;
        if(flag[a[i+1]]==2)break;
        num[a[i+1]]=i+1;
        if(a[i+1]==0){
            printf("%lld",ans);
            return 0;
        }
        ans+=a[i+1];
    }
    ans=0;
    amari=(n+1-num[a[i+1]])%(i+1-num[a[i+1]]);
    for(j=i+1; j<=i+amari; j++){
        a[j]=(a[j-1]*a[j-1])%m;
        ans+=a[j];
    }
    for(j=1; j<num[a[i+1]]; j++){
        ans+=a[j];
    }
    for(j=num[a[i+1]]; j<i+1; j++){
        repeat+=a[j];
    }
    repeat*=(n+1-num[a[i+1]])/(i+1-num[a[i+1]]);
    ans+=repeat;
    printf("%lld",ans);
    return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.section	.rodata
.LC0:
	.string	"%lld %lld %lld"
.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
	leaq	-2400256(%rsp), %r11
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	subq	$64, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movq	$0, -2400280(%rbp)
	movq	$0, -2400272(%rbp)
	leaq	-2400256(%rbp), %rax
	movl	$800080, %edx
	movl	$0, %esi
	movq	%rax, %rdi
	call	memset@PLT
	leaq	-1600176(%rbp), %rax
	movl	$800080, %edx
	movl	$0, %esi
	movq	%rax, %rdi
	call	memset@PLT
	leaq	-800096(%rbp), %rax
	movl	$800080, %edx
	movl	$0, %esi
	movq	%rax, %rdi
	call	memset@PLT
	leaq	-2400304(%rbp), %rcx
	leaq	-2400312(%rbp), %rdx
	leaq	-2400320(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movq	-2400312(%rbp), %rax
	movq	%rax, -2400248(%rbp)
	movq	-2400312(%rbp), %rax
	movq	-1600176(%rbp,%rax,8), %rdx
	addq	$1, %rdx
	movq	%rdx, -1600176(%rbp,%rax,8)
	movq	-2400312(%rbp), %rax
	movq	$1, -800096(%rbp,%rax,8)
	movq	-2400248(%rbp), %rax
	addq	%rax, -2400272(%rbp)
	movq	$1, -2400296(%rbp)
	jmp	.L2
.L7:
	movq	-2400296(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rdx
	movq	-2400296(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rax
	imulq	%rdx, %rax
	movq	-2400304(%rbp), %rsi
	movq	-2400296(%rbp), %rdx
	leaq	1(%rdx), %rcx
	cqto
	idivq	%rsi
	movq	%rdx, %rax
	movq	%rax, -2400256(%rbp,%rcx,8)
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-1600176(%rbp,%rax,8), %rdx
	addq	$1, %rdx
	movq	%rdx, -1600176(%rbp,%rax,8)
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-1600176(%rbp,%rax,8), %rax
	cmpq	$2, %rax
	je	.L16
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-2400296(%rbp), %rdx
	addq	$1, %rdx
	movq	%rdx, -800096(%rbp,%rax,8)
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	testq	%rax, %rax
	jne	.L5
	movq	-2400272(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	jmp	.L14
.L5:
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	addq	%rax, -2400272(%rbp)
	addq	$1, -2400296(%rbp)
.L2:
	cmpq	$100008, -2400296(%rbp)
	jle	.L7
	jmp	.L4
.L16:
	nop
.L4:
	movq	$0, -2400272(%rbp)
	movq	-2400320(%rbp), %rax
	leaq	1(%rax), %rdx
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	subq	%rax, %rdx
	movq	-2400296(%rbp), %rax
	leaq	1(%rax), %rcx
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	subq	%rax, %rcx
	movq	%rdx, %rax
	cqto
	idivq	%rcx
	movq	%rdx, -2400264(%rbp)
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	%rax, -2400288(%rbp)
	jmp	.L8
.L9:
	movq	-2400288(%rbp), %rax
	subq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rdx
	movq	-2400288(%rbp), %rax
	subq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	imulq	%rdx, %rax
	movq	-2400304(%rbp), %rcx
	cqto
	idivq	%rcx
	movq	-2400288(%rbp), %rax
	movq	%rdx, -2400256(%rbp,%rax,8)
	movq	-2400288(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rax
	addq	%rax, -2400272(%rbp)
	addq	$1, -2400288(%rbp)
.L8:
	movq	-2400296(%rbp), %rdx
	movq	-2400264(%rbp), %rax
	addq	%rdx, %rax
	cmpq	%rax, -2400288(%rbp)
	jle	.L9
	movq	$1, -2400288(%rbp)
	jmp	.L10
.L11:
	movq	-2400288(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rax
	addq	%rax, -2400272(%rbp)
	addq	$1, -2400288(%rbp)
.L10:
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	cmpq	%rax, -2400288(%rbp)
	jl	.L11
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	movq	%rax, -2400288(%rbp)
	jmp	.L12
.L13:
	movq	-2400288(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rax
	addq	%rax, -2400280(%rbp)
	addq	$1, -2400288(%rbp)
.L12:
	movq	-2400296(%rbp), %rax
	cmpq	-2400288(%rbp), %rax
	jge	.L13
	movq	-2400320(%rbp), %rax
	leaq	1(%rax), %rdx
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	subq	%rax, %rdx
	movq	-2400296(%rbp), %rax
	leaq	1(%rax), %rcx
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	subq	%rax, %rcx
	movq	%rdx, %rax
	cqto
	idivq	%rcx
	movq	%rax, %rdx
	movq	-2400280(%rbp), %rax
	imulq	%rdx, %rax
	movq	%rax, -2400280(%rbp)
	movq	-2400280(%rbp), %rax
	addq	%rax, -2400272(%rbp)
	movq	-2400272(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
.L14:
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	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
.LC0:
	.string	"%lld %lld %lld"
.LC1:
	.string	"%lld"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB51:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	-2400256(%rsp), %r11
	.cfi_def_cfa 11, 2400272
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	.cfi_def_cfa_register 7
	subq	$32, %rsp
	.cfi_def_cfa_offset 2400304
	movl	$800080, %edx
	xorl	%esi, %esi
	movq	%fs:40, %rax
	movq	%rax, 2400280(%rsp)
	xorl	%eax, %eax
	leaq	32(%rsp), %rbx
	movq	%rbx, %rdi
	call	memset@PLT
	leaq	800112(%rsp), %rdi
	movl	$800080, %edx
	xorl	%esi, %esi
	call	memset@PLT
	leaq	1600192(%rsp), %rdi
	movl	$800080, %edx
	xorl	%esi, %esi
	call	memset@PLT
	leaq	24(%rsp), %rcx
	leaq	16(%rsp), %rdx
	xorl	%eax, %eax
	leaq	.LC0(%rip), %rdi
	leaq	8(%rsp), %rsi
	call	__isoc99_scanf@PLT
	movq	16(%rsp), %rax
	movq	24(%rsp), %rdi
	movl	$1, %ecx
	movq	%rax, 40(%rsp)
	movq	%rax, %r8
	movq	$1, 800112(%rsp,%rax,8)
	movq	$1, 1600192(%rsp,%rax,8)
	jmp	.L5
	.p2align 4,,10
	.p2align 3
.L34:
	movq	%rcx, 1600192(%rsp,%rdx,8)
	testq	%rdx, %rdx
	je	.L32
	addq	%rdx, %r8
	cmpq	$100009, %rcx
	je	.L33
.L5:
	imulq	%rax, %rax
	movq	%rcx, %r9
	addq	$1, %rcx
	cqto
	idivq	%rdi
	movq	800112(%rsp,%rdx,8), %rsi
	movq	%rdx, (%rbx,%rcx,8)
	movq	%rdx, %rax
	addq	$1, %rsi
	movq	%rsi, 800112(%rsp,%rdx,8)
	cmpq	$2, %rsi
	jne	.L34
.L2:
	movq	8(%rsp), %rax
	leaq	1(%r9), %r10
	movq	32(%rsp,%r10,8), %rcx
	movq	%r10, %rsi
	leaq	1(%rax), %r8
	movq	%r8, %rax
	subq	1600192(%rsp,%rcx,8), %rax
	subq	1600192(%rsp,%rcx,8), %rsi
	cqto
	idivq	%rsi
	leaq	(%r9,%rdx), %r11
	testq	%rdx, %rdx
	jle	.L19
	movq	32(%rsp,%r9,8), %rdx
	movq	%r10, %rcx
	xorl	%esi, %esi
	.p2align 4,,10
	.p2align 3
.L7:
	imulq	%rdx, %rdx
	movq	%rdx, %rax
	cqto
	idivq	%rdi
	movq	%rdx, (%rbx,%rcx,8)
	addq	$1, %rcx
	addq	%rdx, %rsi
	cmpq	%r11, %rcx
	jle	.L7
	movq	32(%rsp,%r10,8), %rcx
.L6:
	movq	1600192(%rsp,%rcx,8), %rdi
	cmpq	$1, %rdi
	jle	.L8
	leaq	-1(%rdi), %rcx
	cmpq	$2, %rdi
	je	.L20
	movq	%rcx, %rax
	leaq	40(%rsp), %rdx
	pxor	%xmm0, %xmm0
	shrq	%rax
	salq	$4, %rax
	addq	%rdx, %rax
	.p2align 4,,10
	.p2align 3
.L10:
	movdqu	(%rdx), %xmm2
	addq	$16, %rdx
	paddq	%xmm2, %xmm0
	cmpq	%rax, %rdx
	jne	.L10
	movdqa	%xmm0, %xmm1
	movq	%rcx, %rax
	psrldq	$8, %xmm1
	orq	$1, %rax
	paddq	%xmm1, %xmm0
	movq	%xmm0, %rdx
	addq	%rdx, %rsi
	andb	$1, %cl
	je	.L13
.L9:
	addq	32(%rsp,%rax,8), %rsi
.L13:
	xorl	%ecx, %ecx
	cmpq	%rdi, %r9
	jl	.L12
.L8:
	movq	%r10, %r11
	subq	%rdi, %r11
	cmpq	%rdi, %r9
	je	.L21
	movq	%r11, %rax
	leaq	(%rbx,%rdi,8), %rdx
	pxor	%xmm0, %xmm0
	shrq	%rax
	salq	$4, %rax
	addq	%rdx, %rax
	.p2align 4,,10
	.p2align 3
.L15:
	movdqu	(%rdx), %xmm3
	addq	$16, %rdx
	paddq	%xmm3, %xmm0
	cmpq	%rax, %rdx
	jne	.L15
	movdqa	%xmm0, %xmm1
	movq	%r11, %rdx
	psrldq	$8, %xmm1
	andq	$-2, %rdx
	paddq	%xmm1, %xmm0
	leaq	(%rdx,%rdi), %rax
	movq	%xmm0, %rcx
	cmpq	%r11, %rdx
	je	.L12
.L14:
	addq	32(%rsp,%rax,8), %rcx
.L12:
	movq	%r8, %rax
	subq	%rdi, %r10
	subq	%rdi, %rax
	movl	$1, %edi
	cqto
	idivq	%r10
	imulq	%rcx, %rax
	leaq	(%rax,%rsi), %rdx
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L4:
	movq	2400280(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L35
	addq	$2400288, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 16
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
.L32:
	.cfi_restore_state
	movq	%r8, %rdx
	leaq	.LC1(%rip), %rsi
	movl	$1, %edi
	call	__printf_chk@PLT
	jmp	.L4
.L20:
	movl	$1, %eax
	jmp	.L9
.L21:
	movq	%rdi, %rax
	xorl	%ecx, %ecx
	jmp	.L14
.L33:
	movl	$100009, %r9d
	jmp	.L2
.L19:
	xorl	%esi, %esi
	jmp	.L6
.L35:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE51:
	.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:

rule
	.file	"temp.c"
	.text
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%lld %lld %lld"
.LC1:
	.string	"%lld"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB51:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	-2400256(%rsp), %r11
	.cfi_def_cfa 11, 2400272
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	.cfi_def_cfa_register 7
	subq	$32, %rsp
	.cfi_def_cfa_offset 2400304
	movl	$800080, %edx
	xorl	%esi, %esi
	movq	%fs:40, %rax
	movq	%rax, 2400280(%rsp)
	xorl	%eax, %eax
	leaq	32(%rsp), %rbx
	movq	%rbx, %rdi
	call	memset@PLT
	leaq	800112(%rsp), %rdi
	movl	$800080, %edx
	xorl	%esi, %esi
	call	memset@PLT
	leaq	1600192(%rsp), %rdi
	movl	$800080, %edx
	xorl	%esi, %esi
	call	memset@PLT
	leaq	24(%rsp), %rcx
	leaq	16(%rsp), %rdx
	xorl	%eax, %eax
	leaq	.LC0(%rip), %rdi
	leaq	8(%rsp), %rsi
	call	__isoc99_scanf@PLT
	movq	16(%rsp), %rax
	movq	24(%rsp), %rdi
	movl	$1, %ecx
	movq	%rax, 40(%rsp)
	movq	%rax, %r8
	movq	$1, 800112(%rsp,%rax,8)
	movq	$1, 1600192(%rsp,%rax,8)
	jmp	.L5
	.p2align 4,,10
	.p2align 3
.L34:
	movq	%rcx, 1600192(%rsp,%rdx,8)
	testq	%rdx, %rdx
	je	.L32
	addq	%rdx, %r8
	cmpq	$100009, %rcx
	je	.L33
.L5:
	imulq	%rax, %rax
	movq	%rcx, %r9
	addq	$1, %rcx
	cqto
	idivq	%rdi
	movq	800112(%rsp,%rdx,8), %rsi
	movq	%rdx, (%rbx,%rcx,8)
	movq	%rdx, %rax
	addq	$1, %rsi
	movq	%rsi, 800112(%rsp,%rdx,8)
	cmpq	$2, %rsi
	jne	.L34
.L2:
	movq	8(%rsp), %rax
	leaq	1(%r9), %r10
	movq	32(%rsp,%r10,8), %rcx
	movq	%r10, %rsi
	leaq	1(%rax), %r8
	movq	%r8, %rax
	subq	1600192(%rsp,%rcx,8), %rax
	subq	1600192(%rsp,%rcx,8), %rsi
	cqto
	idivq	%rsi
	leaq	(%r9,%rdx), %r11
	testq	%rdx, %rdx
	jle	.L19
	movq	32(%rsp,%r9,8), %rdx
	movq	%r10, %rcx
	xorl	%esi, %esi
	.p2align 4,,10
	.p2align 3
.L7:
	imulq	%rdx, %rdx
	movq	%rdx, %rax
	cqto
	idivq	%rdi
	movq	%rdx, (%rbx,%rcx,8)
	addq	$1, %rcx
	addq	%rdx, %rsi
	cmpq	%r11, %rcx
	jle	.L7
	movq	32(%rsp,%r10,8), %rcx
.L6:
	movq	1600192(%rsp,%rcx,8), %rdi
	cmpq	$1, %rdi
	jle	.L8
	leaq	-1(%rdi), %rcx
	cmpq	$2, %rdi
	je	.L20
	movq	%rcx, %rax
	leaq	40(%rsp), %rdx
	pxor	%xmm0, %xmm0
	shrq	%rax
	salq	$4, %rax
	addq	%rdx, %rax
	.p2align 4,,10
	.p2align 3
.L10:
	movdqu	(%rdx), %xmm2
	addq	$16, %rdx
	paddq	%xmm2, %xmm0
	cmpq	%rax, %rdx
	jne	.L10
	movdqa	%xmm0, %xmm1
	movq	%rcx, %rax
	psrldq	$8, %xmm1
	orq	$1, %rax
	paddq	%xmm1, %xmm0
	movq	%xmm0, %rdx
	addq	%rdx, %rsi
	andb	$1, %cl
	je	.L13
.L9:
	addq	32(%rsp,%rax,8), %rsi
.L13:
	xorl	%ecx, %ecx
	cmpq	%rdi, %r9
	jl	.L12
.L8:
	movq	%r10, %r11
	subq	%rdi, %r11
	cmpq	%rdi, %r9
	je	.L21
	movq	%r11, %rax
	leaq	(%rbx,%rdi,8), %rdx
	pxor	%xmm0, %xmm0
	shrq	%rax
	salq	$4, %rax
	addq	%rdx, %rax
	.p2align 4,,10
	.p2align 3
.L15:
	movdqu	(%rdx), %xmm3
	addq	$16, %rdx
	paddq	%xmm3, %xmm0
	cmpq	%rax, %rdx
	jne	.L15
	movdqa	%xmm0, %xmm1
	movq	%r11, %rdx
	psrldq	$8, %xmm1
	andq	$-2, %rdx
	paddq	%xmm1, %xmm0
	leaq	(%rdx,%rdi), %rax
	movq	%xmm0, %rcx
	cmpq	%r11, %rdx
	je	.L12
.L14:
	addq	32(%rsp,%rax,8), %rcx
.L12:
	movq	%r8, %rax
	subq	%rdi, %r10
	subq	%rdi, %rax
	movl	$1, %edi
	cqto
	idivq	%r10
	imulq	%rcx, %rax
	leaq	(%rax,%rsi), %rdx
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L4:
	movq	2400280(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L35
	addq	$2400288, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 16
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
.L32:
	.cfi_restore_state
	movq	%r8, %rdx
	leaq	.LC1(%rip), %rsi
	movl	$1, %edi
	call	__printf_chk@PLT
	jmp	.L4
.L20:
	movl	$1, %eax
	jmp	.L9
.L21:
	movq	%rdi, %rax
	xorl	%ecx, %ecx
	jmp	.L14
.L33:
	movl	$100009, %r9d
	jmp	.L2
.L19:
	xorl	%esi, %esi
	jmp	.L6
.L35:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE51:
	.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:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <time.h>
#define ll long long
#define mod 1000000007
int main(){
    ll n,x,m,i,j,amari,repeat=0,
    ans=0,a[100010]={},flag[100010]={},num[100010]={};
    scanf("%lld %lld %lld",&n,&x,&m);
    a[1]=x;
    flag[x]++;
    num[x]=1;
    ans+=a[1];
    for(i=1; i<100009; i++){
        a[i+1]=(a[i]*a[i])%m;
        flag[a[i+1]]++;
        if(flag[a[i+1]]==2)break;
        num[a[i+1]]=i+1;
        if(a[i+1]==0){
            printf("%lld",ans);
            return 0;
        }
        ans+=a[i+1];
    }
    ans=0;
    amari=(n+1-num[a[i+1]])%(i+1-num[a[i+1]]);
    for(j=i+1; j<=i+amari; j++){
        a[j]=(a[j-1]*a[j-1])%m;
        ans+=a[j];
    }
    for(j=1; j<num[a[i+1]]; j++){
        ans+=a[j];
    }
    for(j=num[a[i+1]]; j<i+1; j++){
        repeat+=a[j];
    }
    repeat*=(n+1-num[a[i+1]])/(i+1-num[a[i+1]]);
    ans+=repeat;
    printf("%lld",ans);
    return 0;
}
10000000000 2 99959
 1000 0 16
 10010000000 2 99959
 10010000100 2 99959
 10000000000 15 99959
 6 2 1101
 1100 2 16
 10010001000 2 99959
 11010000100 2 99959
 1000 1 20

500550770156172 0 501051320374664 501051325615660 500550769812856 1282 6 501051370879075 551106402301729 1000
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 <string.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <time.h>
#define ll long long
#define mod 1000000007
int main(){
    ll n,x,m,i,j,amari,repeat=0,
    ans=0,a[100010]={},flag[100010]={},num[100010]={};
    scanf("%lld %lld %lld",&n,&x,&m);
    a[1]=x;
    flag[x]++;
    num[x]=1;
    ans+=a[1];
    for(i=1; i<100009; i++){
        a[i+1]=(a[i]*a[i])%m;
        flag[a[i+1]]++;
        if(flag[a[i+1]]==2)break;
        num[a[i+1]]=i+1;
        if(a[i+1]==0){
            printf("%lld",ans);
            return 0;
        }
        ans+=a[i+1];
    }
    ans=0;
    amari=(n+1-num[a[i+1]])%(i+1-num[a[i+1]]);
    for(j=i+1; j<=i+amari; j++){
        a[j]=(a[j-1]*a[j-1])%m;
        ans+=a[j];
    }
    for(j=1; j<num[a[i+1]]; j++){
        ans+=a[j];
    }
    for(j=num[a[i+1]]; j<i+1; j++){
        repeat+=a[j];
    }
    repeat*=(n+1-num[a[i+1]])/(i+1-num[a[i+1]]);
    ans+=repeat;
    printf("%lld",ans);
    return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.section	.rodata
.LC0:
	.string	"%lld %lld %lld"
.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
	leaq	-2400256(%rsp), %r11
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	subq	$64, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movq	$0, -2400280(%rbp)
	movq	$0, -2400272(%rbp)
	leaq	-2400256(%rbp), %rax
	movl	$800080, %edx
	movl	$0, %esi
	movq	%rax, %rdi
	call	memset@PLT
	leaq	-1600176(%rbp), %rax
	movl	$800080, %edx
	movl	$0, %esi
	movq	%rax, %rdi
	call	memset@PLT
	leaq	-800096(%rbp), %rax
	movl	$800080, %edx
	movl	$0, %esi
	movq	%rax, %rdi
	call	memset@PLT
	leaq	-2400304(%rbp), %rcx
	leaq	-2400312(%rbp), %rdx
	leaq	-2400320(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movq	-2400312(%rbp), %rax
	movq	%rax, -2400248(%rbp)
	movq	-2400312(%rbp), %rax
	movq	-1600176(%rbp,%rax,8), %rdx
	addq	$1, %rdx
	movq	%rdx, -1600176(%rbp,%rax,8)
	movq	-2400312(%rbp), %rax
	movq	$1, -800096(%rbp,%rax,8)
	movq	-2400248(%rbp), %rax
	addq	%rax, -2400272(%rbp)
	movq	$1, -2400296(%rbp)
	jmp	.L2
.L7:
	movq	-2400296(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rdx
	movq	-2400296(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rax
	imulq	%rdx, %rax
	movq	-2400304(%rbp), %rsi
	movq	-2400296(%rbp), %rdx
	leaq	1(%rdx), %rcx
	cqto
	idivq	%rsi
	movq	%rdx, %rax
	movq	%rax, -2400256(%rbp,%rcx,8)
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-1600176(%rbp,%rax,8), %rdx
	addq	$1, %rdx
	movq	%rdx, -1600176(%rbp,%rax,8)
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-1600176(%rbp,%rax,8), %rax
	cmpq	$2, %rax
	je	.L16
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-2400296(%rbp), %rdx
	addq	$1, %rdx
	movq	%rdx, -800096(%rbp,%rax,8)
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	testq	%rax, %rax
	jne	.L5
	movq	-2400272(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	jmp	.L14
.L5:
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	addq	%rax, -2400272(%rbp)
	addq	$1, -2400296(%rbp)
.L2:
	cmpq	$100008, -2400296(%rbp)
	jle	.L7
	jmp	.L4
.L16:
	nop
.L4:
	movq	$0, -2400272(%rbp)
	movq	-2400320(%rbp), %rax
	leaq	1(%rax), %rdx
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	subq	%rax, %rdx
	movq	-2400296(%rbp), %rax
	leaq	1(%rax), %rcx
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	subq	%rax, %rcx
	movq	%rdx, %rax
	cqto
	idivq	%rcx
	movq	%rdx, -2400264(%rbp)
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	%rax, -2400288(%rbp)
	jmp	.L8
.L9:
	movq	-2400288(%rbp), %rax
	subq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rdx
	movq	-2400288(%rbp), %rax
	subq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	imulq	%rdx, %rax
	movq	-2400304(%rbp), %rcx
	cqto
	idivq	%rcx
	movq	-2400288(%rbp), %rax
	movq	%rdx, -2400256(%rbp,%rax,8)
	movq	-2400288(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rax
	addq	%rax, -2400272(%rbp)
	addq	$1, -2400288(%rbp)
.L8:
	movq	-2400296(%rbp), %rdx
	movq	-2400264(%rbp), %rax
	addq	%rdx, %rax
	cmpq	%rax, -2400288(%rbp)
	jle	.L9
	movq	$1, -2400288(%rbp)
	jmp	.L10
.L11:
	movq	-2400288(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rax
	addq	%rax, -2400272(%rbp)
	addq	$1, -2400288(%rbp)
.L10:
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	cmpq	%rax, -2400288(%rbp)
	jl	.L11
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	movq	%rax, -2400288(%rbp)
	jmp	.L12
.L13:
	movq	-2400288(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rax
	addq	%rax, -2400280(%rbp)
	addq	$1, -2400288(%rbp)
.L12:
	movq	-2400296(%rbp), %rax
	cmpq	-2400288(%rbp), %rax
	jge	.L13
	movq	-2400320(%rbp), %rax
	leaq	1(%rax), %rdx
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	subq	%rax, %rdx
	movq	-2400296(%rbp), %rax
	leaq	1(%rax), %rcx
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	subq	%rax, %rcx
	movq	%rdx, %rax
	cqto
	idivq	%rcx
	movq	%rax, %rdx
	movq	-2400280(%rbp), %rax
	imulq	%rdx, %rax
	movq	%rax, -2400280(%rbp)
	movq	-2400280(%rbp), %rax
	addq	%rax, -2400272(%rbp)
	movq	-2400272(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
.L14:
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	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	"%lld %lld %lld"
.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
	leaq	-2400256(%rsp), %r11
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	subq	$64, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movq	$0, -2400280(%rbp)
	movq	$0, -2400272(%rbp)
	leaq	-2400256(%rbp), %rax
	movl	$800080, %edx
	movl	$0, %esi
	movq	%rax, %rdi
	call	memset@PLT
	leaq	-1600176(%rbp), %rax
	movl	$800080, %edx
	movl	$0, %esi
	movq	%rax, %rdi
	call	memset@PLT
	leaq	-800096(%rbp), %rax
	movl	$800080, %edx
	movl	$0, %esi
	movq	%rax, %rdi
	call	memset@PLT
	leaq	-2400304(%rbp), %rcx
	leaq	-2400312(%rbp), %rdx
	leaq	-2400320(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movq	-2400312(%rbp), %rax
	movq	%rax, -2400248(%rbp)
	movq	-2400312(%rbp), %rax
	movq	-1600176(%rbp,%rax,8), %rdx
	addq	$1, %rdx
	movq	%rdx, -1600176(%rbp,%rax,8)
	movq	-2400312(%rbp), %rax
	movq	$1, -800096(%rbp,%rax,8)
	movq	-2400248(%rbp), %rax
	addq	%rax, -2400272(%rbp)
	movq	$1, -2400296(%rbp)
	jmp	.L2
.L7:
	movq	-2400296(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rdx
	movq	-2400296(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rax
	imulq	%rdx, %rax
	movq	-2400304(%rbp), %rsi
	movq	-2400296(%rbp), %rdx
	leaq	1(%rdx), %rcx
	cqto
	idivq	%rsi
	movq	%rdx, %rax
	movq	%rax, -2400256(%rbp,%rcx,8)
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-1600176(%rbp,%rax,8), %rdx
	addq	$1, %rdx
	movq	%rdx, -1600176(%rbp,%rax,8)
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-1600176(%rbp,%rax,8), %rax
	cmpq	$2, %rax
	je	.L16
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-2400296(%rbp), %rdx
	addq	$1, %rdx
	movq	%rdx, -800096(%rbp,%rax,8)
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	testq	%rax, %rax
	jne	.L5
	movq	-2400272(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	jmp	.L14
.L5:
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	addq	%rax, -2400272(%rbp)
	addq	$1, -2400296(%rbp)
.L2:
	cmpq	$100008, -2400296(%rbp)
	jle	.L7
	jmp	.L4
.L16:
	nop
.L4:
	movq	$0, -2400272(%rbp)
	movq	-2400320(%rbp), %rax
	leaq	1(%rax), %rdx
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	subq	%rax, %rdx
	movq	-2400296(%rbp), %rax
	leaq	1(%rax), %rcx
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	subq	%rax, %rcx
	movq	%rdx, %rax
	cqto
	idivq	%rcx
	movq	%rdx, -2400264(%rbp)
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	%rax, -2400288(%rbp)
	jmp	.L8
.L9:
	movq	-2400288(%rbp), %rax
	subq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rdx
	movq	-2400288(%rbp), %rax
	subq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	imulq	%rdx, %rax
	movq	-2400304(%rbp), %rcx
	cqto
	idivq	%rcx
	movq	-2400288(%rbp), %rax
	movq	%rdx, -2400256(%rbp,%rax,8)
	movq	-2400288(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rax
	addq	%rax, -2400272(%rbp)
	addq	$1, -2400288(%rbp)
.L8:
	movq	-2400296(%rbp), %rdx
	movq	-2400264(%rbp), %rax
	addq	%rdx, %rax
	cmpq	%rax, -2400288(%rbp)
	jle	.L9
	movq	$1, -2400288(%rbp)
	jmp	.L10
.L11:
	movq	-2400288(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rax
	addq	%rax, -2400272(%rbp)
	addq	$1, -2400288(%rbp)
.L10:
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	cmpq	%rax, -2400288(%rbp)
	jl	.L11
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	movq	%rax, -2400288(%rbp)
	jmp	.L12
.L13:
	movq	-2400288(%rbp), %rax
	movq	-2400256(%rbp,%rax,8), %rax
	addq	%rax, -2400280(%rbp)
	addq	$1, -2400288(%rbp)
.L12:
	movq	-2400296(%rbp), %rax
	cmpq	-2400288(%rbp), %rax
	jge	.L13
	movq	-2400320(%rbp), %rax
	leaq	1(%rax), %rdx
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	subq	%rax, %rdx
	movq	-2400296(%rbp), %rax
	leaq	1(%rax), %rcx
	movq	-2400296(%rbp), %rax
	addq	$1, %rax
	movq	-2400256(%rbp,%rax,8), %rax
	movq	-800096(%rbp,%rax,8), %rax
	subq	%rax, %rcx
	movq	%rdx, %rax
	cqto
	idivq	%rcx
	movq	%rax, %rdx
	movq	-2400280(%rbp), %rax
	imulq	%rdx, %rax
	movq	%rax, -2400280(%rbp)
	movq	-2400280(%rbp), %rax
	addq	%rax, -2400272(%rbp)
	movq	-2400272(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
.L14:
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	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:
