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>

#define NEW(p,n){p=malloc((n)*sizeof(p[0]));if(p==NULL){printf("not enough memory\n");exit(1);};}
//pの型の変数n個の要素分のメモリを確保し、そのアドレスをpに代入するマクロ

//actionの定義
typedef struct{
    int max_card; //B
    long value;   //C
}action;

//昇順(第一引数が小さければ負になる)
int comp(const void* a, const void* b){
    return *(int*)a-*(int*)b;
}

//昇順(第一引数が小さければ負になる)
int comp_act(const void* a, const void* b){
    return ((action*)a)->value-((action*)b)->value;
}

int main(void){
    int N, M;
    scanf("%d %d",&N, &M);

    //A[0]~A[N-1]に格納する(i番目のカードに書いてある整数)
    long* A;
    NEW(A,N);
    for(int i=0;i<N;i++){
        scanf("%ld",&A[i]);
    }
    //Aを昇順ソート
    qsort(A,N,sizeof(long),comp);


    //B,Cを格納
    action* D;
    NEW(D,M);
    for(int j=0;j<M;j++){
        scanf("%d %ld",&D[j].max_card, &D[j].value);
    }
    //Cの値を元に昇順ソート
    qsort(D,M,sizeof(action),comp_act);

    //for(int j=0;j<M;j++) printf("%d %ld \n",D[j].max_card, D[j].value);

    int a=0;
    for(int j=M-1;j>=0;j--){
        for(int i=0;i<D[j].max_card && a<N;i++){
            if(A[a]<D[j].value){
                A[a]=D[j].value;
                a++;
            }
            else break;
        }
    }

    long ans=0;
    for(int i=0;i<N;i++) ans+=A[i];
    printf("%ld\n",ans);

    return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	comp
	.type	comp, @function
comp:
.LFB6:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movq	%rdi, -8(%rbp)
	movq	%rsi, -16(%rbp)
	movq	-8(%rbp), %rax
	movl	(%rax), %edx
	movq	-16(%rbp), %rax
	movl	(%rax), %ecx
	movl	%edx, %eax
	subl	%ecx, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE6:
	.size	comp, .-comp
	.globl	comp_act
	.type	comp_act, @function
comp_act:
.LFB7:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movq	%rdi, -8(%rbp)
	movq	%rsi, -16(%rbp)
	movq	-8(%rbp), %rax
	movq	8(%rax), %rax
	movl	%eax, %edx
	movq	-16(%rbp), %rax
	movq	8(%rax), %rax
	movl	%eax, %ecx
	movl	%edx, %eax
	subl	%ecx, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE7:
	.size	comp_act, .-comp_act
	.section	.rodata
.LC0:
	.string	"%d %d"
.LC1:
	.string	"not enough memory"
.LC2:
	.string	"%ld"
.LC3:
	.string	"%d %ld"
.LC4:
	.string	"%ld\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB8:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$64, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-60(%rbp), %rdx
	leaq	-64(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-64(%rbp), %eax
	cltq
	salq	$3, %rax
	movq	%rax, %rdi
	call	malloc@PLT
	movq	%rax, -24(%rbp)
	cmpq	$0, -24(%rbp)
	jne	.L6
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	call	puts@PLT
	movl	$1, %edi
	call	exit@PLT
.L6:
	movl	$0, -56(%rbp)
	jmp	.L7
.L8:
	movl	-56(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	movq	-24(%rbp), %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -56(%rbp)
.L7:
	movl	-64(%rbp), %eax
	cmpl	%eax, -56(%rbp)
	jl	.L8
	movl	-64(%rbp), %eax
	movslq	%eax, %rsi
	movq	-24(%rbp), %rax
	leaq	comp(%rip), %rdx
	movq	%rdx, %rcx
	movl	$8, %edx
	movq	%rax, %rdi
	call	qsort@PLT
	movl	-60(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rdi
	call	malloc@PLT
	movq	%rax, -16(%rbp)
	cmpq	$0, -16(%rbp)
	jne	.L9
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	call	puts@PLT
	movl	$1, %edi
	call	exit@PLT
.L9:
	movl	$0, -52(%rbp)
	jmp	.L10
.L11:
	movl	-52(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rdx
	movq	-16(%rbp), %rax
	addq	%rdx, %rax
	addq	$8, %rax
	movl	-52(%rbp), %edx
	movslq	%edx, %rdx
	movq	%rdx, %rcx
	salq	$4, %rcx
	movq	-16(%rbp), %rdx
	addq	%rcx, %rdx
	movq	%rdx, %rcx
	movq	%rax, %rdx
	movq	%rcx, %rsi
	leaq	.LC3(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -52(%rbp)
.L10:
	movl	-60(%rbp), %eax
	cmpl	%eax, -52(%rbp)
	jl	.L11
	movl	-60(%rbp), %eax
	movslq	%eax, %rsi
	movq	-16(%rbp), %rax
	leaq	comp_act(%rip), %rdx
	movq	%rdx, %rcx
	movl	$16, %edx
	movq	%rax, %rdi
	call	qsort@PLT
	movl	$0, -48(%rbp)
	movl	-60(%rbp), %eax
	subl	$1, %eax
	movl	%eax, -44(%rbp)
	jmp	.L12
.L18:
	movl	$0, -40(%rbp)
	jmp	.L13
.L17:
	movl	-48(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	movq	-24(%rbp), %rax
	addq	%rdx, %rax
	movq	(%rax), %rdx
	movl	-44(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rcx
	movq	-16(%rbp), %rax
	addq	%rcx, %rax
	movq	8(%rax), %rax
	cmpq	%rax, %rdx
	jge	.L23
	movl	-44(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rdx
	movq	-16(%rbp), %rax
	addq	%rdx, %rax
	movl	-48(%rbp), %edx
	movslq	%edx, %rdx
	leaq	0(,%rdx,8), %rcx
	movq	-24(%rbp), %rdx
	addq	%rcx, %rdx
	movq	8(%rax), %rax
	movq	%rax, (%rdx)
	addl	$1, -48(%rbp)
	addl	$1, -40(%rbp)
.L13:
	movl	-44(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rdx
	movq	-16(%rbp), %rax
	addq	%rdx, %rax
	movl	(%rax), %eax
	cmpl	%eax, -40(%rbp)
	jge	.L16
	movl	-64(%rbp), %eax
	cmpl	%eax, -48(%rbp)
	jl	.L17
	jmp	.L16
.L23:
	nop
.L16:
	subl	$1, -44(%rbp)
.L12:
	cmpl	$0, -44(%rbp)
	jns	.L18
	movq	$0, -32(%rbp)
	movl	$0, -36(%rbp)
	jmp	.L19
.L20:
	movl	-36(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	movq	-24(%rbp), %rax
	addq	%rdx, %rax
	movq	(%rax), %rax
	addq	%rax, -32(%rbp)
	addl	$1, -36(%rbp)
.L19:
	movl	-64(%rbp), %eax
	cmpl	%eax, -36(%rbp)
	jl	.L20
	movq	-32(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC4(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L22
	call	__stack_chk_fail@PLT
.L22:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE8:
	.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	comp
	.type	comp, @function
comp:
.LFB39:
	.cfi_startproc
	endbr64
	movl	(%rdi), %eax
	subl	(%rsi), %eax
	ret
	.cfi_endproc
.LFE39:
	.size	comp, .-comp
	.p2align 4
	.globl	comp_act
	.type	comp_act, @function
comp_act:
.LFB40:
	.cfi_startproc
	endbr64
	movq	8(%rdi), %rax
	subl	8(%rsi), %eax
	ret
	.cfi_endproc
.LFE40:
	.size	comp_act, .-comp_act
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d %d"
.LC1:
	.string	"not enough memory"
.LC2:
	.string	"%ld"
.LC3:
	.string	"%d %ld"
.LC4:
	.string	"%ld\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB41:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC0(%rip), %rdi
	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	$24, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	leaq	4(%rsp), %rdx
	movq	%rsp, %rsi
	call	__isoc99_scanf@PLT
	movslq	(%rsp), %r15
	leaq	0(,%r15,8), %rdi
	call	malloc@PLT
	testq	%rax, %rax
	je	.L5
	movq	%rax, %rbx
	movq	%rax, %r12
	leaq	.LC2(%rip), %r14
	xorl	%ebp, %ebp
	testl	%r15d, %r15d
	jle	.L7
	.p2align 4,,10
	.p2align 3
.L8:
	movq	%r12, %rsi
	movq	%r14, %rdi
	xorl	%eax, %eax
	addl	$1, %ebp
	call	__isoc99_scanf@PLT
	movl	(%rsp), %r13d
	addq	$8, %r12
	cmpl	%ebp, %r13d
	jg	.L8
	movslq	%r13d, %r15
.L7:
	leaq	comp(%rip), %rcx
	movl	$8, %edx
	movq	%r15, %rsi
	movq	%rbx, %rdi
	call	qsort@PLT
	movslq	4(%rsp), %rbp
	movq	%rbp, %rdi
	salq	$4, %rdi
	call	malloc@PLT
	movq	%rax, %r13
	testq	%rax, %rax
	je	.L5
	testl	%ebp, %ebp
	jle	.L9
	movq	%rax, %rbp
	xorl	%r12d, %r12d
	leaq	.LC3(%rip), %r15
	.p2align 4,,10
	.p2align 3
.L10:
	leaq	8(%rbp), %rdx
	movq	%rbp, %rsi
	movq	%r15, %rdi
	xorl	%eax, %eax
	call	__isoc99_scanf@PLT
	movl	4(%rsp), %r14d
	addl	$1, %r12d
	addq	$16, %rbp
	cmpl	%r12d, %r14d
	jg	.L10
	movslq	%r14d, %rbp
.L9:
	movl	$16, %edx
	leaq	comp_act(%rip), %rcx
	movq	%rbp, %rsi
	movq	%r13, %rdi
	call	qsort@PLT
	movl	4(%rsp), %edx
	movl	(%rsp), %r8d
	testl	%edx, %edx
	jle	.L15
	movslq	%edx, %rax
	subl	$1, %edx
	salq	$4, %rax
	salq	$4, %rdx
	leaq	0(%r13,%rax), %rsi
	subq	$16, %rax
	subq	%rdx, %rax
	addq	%rax, %r13
	xorl	%eax, %eax
	.p2align 4,,10
	.p2align 3
.L12:
	movl	-16(%rsi), %edi
	testl	%edi, %edi
	jle	.L13
	movslq	%eax, %rdx
	addl	%eax, %edi
	leaq	(%rbx,%rdx,8), %rdx
	jmp	.L16
	.p2align 4,,10
	.p2align 3
.L14:
	movq	-8(%rsi), %rcx
	cmpq	%rcx, (%rdx)
	jge	.L13
	addl	$1, %eax
	movq	%rcx, (%rdx)
	addq	$8, %rdx
	cmpl	%edi, %eax
	je	.L13
.L16:
	cmpl	%eax, %r8d
	jg	.L14
.L13:
	subq	$16, %rsi
	cmpq	%r13, %rsi
	jne	.L12
.L15:
	testl	%r8d, %r8d
	jle	.L24
	cmpl	$1, %r8d
	je	.L25
	movl	%r8d, %edx
	movq	%rbx, %rax
	pxor	%xmm0, %xmm0
	shrl	%edx
	subl	$1, %edx
	salq	$4, %rdx
	leaq	16(%rbx,%rdx), %rdx
	.p2align 4,,10
	.p2align 3
.L19:
	movdqu	(%rax), %xmm2
	addq	$16, %rax
	paddq	%xmm2, %xmm0
	cmpq	%rax, %rdx
	jne	.L19
	movdqa	%xmm0, %xmm1
	movl	%r8d, %eax
	psrldq	$8, %xmm1
	andl	$-2, %eax
	andl	$1, %r8d
	paddq	%xmm1, %xmm0
	movq	%xmm0, %rdx
	je	.L17
.L18:
	cltq
	addq	(%rbx,%rax,8), %rdx
.L17:
	xorl	%eax, %eax
	leaq	.LC4(%rip), %rsi
	movl	$1, %edi
	call	__printf_chk@PLT
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L37
	addq	$24, %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
.L24:
	.cfi_restore_state
	xorl	%edx, %edx
	jmp	.L17
.L25:
	xorl	%eax, %eax
	xorl	%edx, %edx
	jmp	.L18
.L37:
	call	__stack_chk_fail@PLT
.L5:
	leaq	.LC1(%rip), %rdi
	call	puts@PLT
	movl	$1, %edi
	call	exit@PLT
	.cfi_endproc
.LFE41:
	.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
	.p2align 4
	.globl	comp
	.type	comp, @function
comp:
.LFB39:
	.cfi_startproc
	endbr64
	movl	(%rdi), %eax
	subl	(%rsi), %eax
	ret
	.cfi_endproc
.LFE39:
	.size	comp, .-comp
	.p2align 4
	.globl	comp_act
	.type	comp_act, @function
comp_act:
.LFB40:
	.cfi_startproc
	endbr64
	movq	8(%rdi), %rax
	subl	8(%rsi), %eax
	ret
	.cfi_endproc
.LFE40:
	.size	comp_act, .-comp_act
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d %d"
.LC1:
	.string	"not enough memory"
.LC2:
	.string	"%ld"
.LC3:
	.string	"%d %ld"
.LC4:
	.string	"%ld\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB41:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC0(%rip), %rdi
	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	$24, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	leaq	4(%rsp), %rdx
	movq	%rsp, %rsi
	call	__isoc99_scanf@PLT
	movslq	(%rsp), %r15
	leaq	0(,%r15,8), %rdi
	call	malloc@PLT
	testq	%rax, %rax
	je	.L5
	movq	%rax, %rbx
	movq	%rax, %r12
	leaq	.LC2(%rip), %r14
	xorl	%ebp, %ebp
	testl	%r15d, %r15d
	jle	.L7
	.p2align 4,,10
	.p2align 3
.L8:
	movq	%r12, %rsi
	movq	%r14, %rdi
	xorl	%eax, %eax
	addl	$1, %ebp
	call	__isoc99_scanf@PLT
	movl	(%rsp), %r13d
	addq	$8, %r12
	cmpl	%ebp, %r13d
	jg	.L8
	movslq	%r13d, %r15
.L7:
	leaq	comp(%rip), %rcx
	movl	$8, %edx
	movq	%r15, %rsi
	movq	%rbx, %rdi
	call	qsort@PLT
	movslq	4(%rsp), %rbp
	movq	%rbp, %rdi
	salq	$4, %rdi
	call	malloc@PLT
	movq	%rax, %r13
	testq	%rax, %rax
	je	.L5
	testl	%ebp, %ebp
	jle	.L9
	movq	%rax, %rbp
	xorl	%r12d, %r12d
	leaq	.LC3(%rip), %r15
	.p2align 4,,10
	.p2align 3
.L10:
	leaq	8(%rbp), %rdx
	movq	%rbp, %rsi
	movq	%r15, %rdi
	xorl	%eax, %eax
	call	__isoc99_scanf@PLT
	movl	4(%rsp), %r14d
	addl	$1, %r12d
	addq	$16, %rbp
	cmpl	%r12d, %r14d
	jg	.L10
	movslq	%r14d, %rbp
.L9:
	movl	$16, %edx
	leaq	comp_act(%rip), %rcx
	movq	%rbp, %rsi
	movq	%r13, %rdi
	call	qsort@PLT
	movl	4(%rsp), %edx
	movl	(%rsp), %r8d
	testl	%edx, %edx
	jle	.L15
	movslq	%edx, %rax
	subl	$1, %edx
	salq	$4, %rax
	salq	$4, %rdx
	leaq	0(%r13,%rax), %rsi
	subq	$16, %rax
	subq	%rdx, %rax
	addq	%rax, %r13
	xorl	%eax, %eax
	.p2align 4,,10
	.p2align 3
.L12:
	movl	-16(%rsi), %edi
	testl	%edi, %edi
	jle	.L13
	movslq	%eax, %rdx
	addl	%eax, %edi
	leaq	(%rbx,%rdx,8), %rdx
	jmp	.L16
	.p2align 4,,10
	.p2align 3
.L14:
	movq	-8(%rsi), %rcx
	cmpq	%rcx, (%rdx)
	jge	.L13
	addl	$1, %eax
	movq	%rcx, (%rdx)
	addq	$8, %rdx
	cmpl	%edi, %eax
	je	.L13
.L16:
	cmpl	%eax, %r8d
	jg	.L14
.L13:
	subq	$16, %rsi
	cmpq	%r13, %rsi
	jne	.L12
.L15:
	testl	%r8d, %r8d
	jle	.L24
	cmpl	$1, %r8d
	je	.L25
	movl	%r8d, %edx
	movq	%rbx, %rax
	pxor	%xmm0, %xmm0
	shrl	%edx
	subl	$1, %edx
	salq	$4, %rdx
	leaq	16(%rbx,%rdx), %rdx
	.p2align 4,,10
	.p2align 3
.L19:
	movdqu	(%rax), %xmm2
	addq	$16, %rax
	paddq	%xmm2, %xmm0
	cmpq	%rax, %rdx
	jne	.L19
	movdqa	%xmm0, %xmm1
	movl	%r8d, %eax
	psrldq	$8, %xmm1
	andl	$-2, %eax
	andl	$1, %r8d
	paddq	%xmm1, %xmm0
	movq	%xmm0, %rdx
	je	.L17
.L18:
	cltq
	addq	(%rbx,%rax,8), %rdx
.L17:
	xorl	%eax, %eax
	leaq	.LC4(%rip), %rsi
	movl	$1, %edi
	call	__printf_chk@PLT
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L37
	addq	$24, %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
.L24:
	.cfi_restore_state
	xorl	%edx, %edx
	jmp	.L17
.L25:
	xorl	%eax, %eax
	xorl	%edx, %edx
	jmp	.L18
.L37:
	call	__stack_chk_fail@PLT
.L5:
	leaq	.LC1(%rip), %rdi
	call	puts@PLT
	movl	$1, %edi
	call	exit@PLT
	.cfi_endproc
.LFE41:
	.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<stdlib.h>

#define NEW(p,n){p=malloc((n)*sizeof(p[0]));if(p==NULL){printf("not enough memory\n");exit(1);};}
//pの型の変数n個の要素分のメモリを確保し、そのアドレスをpに代入するマクロ

//actionの定義
typedef struct{
    int max_card; //B
    long value;   //C
}action;

//昇順(第一引数が小さければ負になる)
int comp(const void* a, const void* b){
    return *(int*)a-*(int*)b;
}

//昇順(第一引数が小さければ負になる)
int comp_act(const void* a, const void* b){
    return ((action*)a)->value-((action*)b)->value;
}

int main(void){
    int N, M;
    scanf("%d %d",&N, &M);

    //A[0]~A[N-1]に格納する(i番目のカードに書いてある整数)
    long* A;
    NEW(A,N);
    for(int i=0;i<N;i++){
        scanf("%ld",&A[i]);
    }
    //Aを昇順ソート
    qsort(A,N,sizeof(long),comp);


    //B,Cを格納
    action* D;
    NEW(D,M);
    for(int j=0;j<M;j++){
        scanf("%d %ld",&D[j].max_card, &D[j].value);
    }
    //Cの値を元に昇順ソート
    qsort(D,M,sizeof(action),comp_act);

    //for(int j=0;j<M;j++) printf("%d %ld \n",D[j].max_card, D[j].value);

    int a=0;
    for(int j=M-1;j>=0;j--){
        for(int i=0;i<D[j].max_card && a<N;i++){
            if(A[a]<D[j].value){
                A[a]=D[j].value;
                a++;
            }
            else break;
        }
    }

    long ans=0;
    for(int i=0;i<N;i++) ans+=A[i];
    printf("%ld\n",ans);

    return 0;
}
10 3
1 8 5 7 100 4 7 33 13 5
3 10
4 30
1 4
 3 2
5 1 1
2 3
1 5
 3 2
100 100 100
0 99
3 99
 11 3
1 1 1 1 1 1 1 1 1 1 1
3 1000000000
4 1000000000
3 1000000001
 10 2
0 8 5 7 100 4 7 33 24 5
3 10
4 30
1 4
 3 2
5 1 1
2 3
1 7
 10 2
0 8 5 7 100 4 7 33 24 5
3 15
4 30
1 4
 3 2
5 1 1
2 3
1 6
 3 2
5 1 1
2 3
2 6
 10 2
0 4 5 7 100 4 7 33 33 5
3 15
4 30
1 4

296
 13
 300
 10000000004
 307
 15
 322
 14
 17
 331

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>

#define NEW(p,n){p=malloc((n)*sizeof(p[0]));if(p==NULL){printf("not enough memory\n");exit(1);};}
//pの型の変数n個の要素分のメモリを確保し、そのアドレスをpに代入するマクロ

//actionの定義
typedef struct{
    int max_card; //B
    long value;   //C
}action;

//昇順(第一引数が小さければ負になる)
int comp(const void* a, const void* b){
    return *(int*)a-*(int*)b;
}

//昇順(第一引数が小さければ負になる)
int comp_act(const void* a, const void* b){
    return ((action*)a)->value-((action*)b)->value;
}

int main(void){
    int N, M;
    scanf("%d %d",&N, &M);

    //A[0]~A[N-1]に格納する(i番目のカードに書いてある整数)
    long* A;
    NEW(A,N);
    for(int i=0;i<N;i++){
        scanf("%ld",&A[i]);
    }
    //Aを昇順ソート
    qsort(A,N,sizeof(long),comp);


    //B,Cを格納
    action* D;
    NEW(D,M);
    for(int j=0;j<M;j++){
        scanf("%d %ld",&D[j].max_card, &D[j].value);
    }
    //Cの値を元に昇順ソート
    qsort(D,M,sizeof(action),comp_act);

    //for(int j=0;j<M;j++) printf("%d %ld \n",D[j].max_card, D[j].value);

    int a=0;
    for(int j=M-1;j>=0;j--){
        for(int i=0;i<D[j].max_card && a<N;i++){
            if(A[a]<D[j].value){
                A[a]=D[j].value;
                a++;
            }
            else break;
        }
    }

    long ans=0;
    for(int i=0;i<N;i++) ans+=A[i];
    printf("%ld\n",ans);

    return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	comp
	.type	comp, @function
comp:
.LFB6:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movq	%rdi, -8(%rbp)
	movq	%rsi, -16(%rbp)
	movq	-8(%rbp), %rax
	movl	(%rax), %edx
	movq	-16(%rbp), %rax
	movl	(%rax), %ecx
	movl	%edx, %eax
	subl	%ecx, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE6:
	.size	comp, .-comp
	.globl	comp_act
	.type	comp_act, @function
comp_act:
.LFB7:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movq	%rdi, -8(%rbp)
	movq	%rsi, -16(%rbp)
	movq	-8(%rbp), %rax
	movq	8(%rax), %rax
	movl	%eax, %edx
	movq	-16(%rbp), %rax
	movq	8(%rax), %rax
	movl	%eax, %ecx
	movl	%edx, %eax
	subl	%ecx, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE7:
	.size	comp_act, .-comp_act
	.section	.rodata
.LC0:
	.string	"%d %d"
.LC1:
	.string	"not enough memory"
.LC2:
	.string	"%ld"
.LC3:
	.string	"%d %ld"
.LC4:
	.string	"%ld\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB8:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$64, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-60(%rbp), %rdx
	leaq	-64(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-64(%rbp), %eax
	cltq
	salq	$3, %rax
	movq	%rax, %rdi
	call	malloc@PLT
	movq	%rax, -24(%rbp)
	cmpq	$0, -24(%rbp)
	jne	.L6
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	call	puts@PLT
	movl	$1, %edi
	call	exit@PLT
.L6:
	movl	$0, -56(%rbp)
	jmp	.L7
.L8:
	movl	-56(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	movq	-24(%rbp), %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -56(%rbp)
.L7:
	movl	-64(%rbp), %eax
	cmpl	%eax, -56(%rbp)
	jl	.L8
	movl	-64(%rbp), %eax
	movslq	%eax, %rsi
	movq	-24(%rbp), %rax
	leaq	comp(%rip), %rdx
	movq	%rdx, %rcx
	movl	$8, %edx
	movq	%rax, %rdi
	call	qsort@PLT
	movl	-60(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rdi
	call	malloc@PLT
	movq	%rax, -16(%rbp)
	cmpq	$0, -16(%rbp)
	jne	.L9
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	call	puts@PLT
	movl	$1, %edi
	call	exit@PLT
.L9:
	movl	$0, -52(%rbp)
	jmp	.L10
.L11:
	movl	-52(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rdx
	movq	-16(%rbp), %rax
	addq	%rdx, %rax
	addq	$8, %rax
	movl	-52(%rbp), %edx
	movslq	%edx, %rdx
	movq	%rdx, %rcx
	salq	$4, %rcx
	movq	-16(%rbp), %rdx
	addq	%rcx, %rdx
	movq	%rdx, %rcx
	movq	%rax, %rdx
	movq	%rcx, %rsi
	leaq	.LC3(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -52(%rbp)
.L10:
	movl	-60(%rbp), %eax
	cmpl	%eax, -52(%rbp)
	jl	.L11
	movl	-60(%rbp), %eax
	movslq	%eax, %rsi
	movq	-16(%rbp), %rax
	leaq	comp_act(%rip), %rdx
	movq	%rdx, %rcx
	movl	$16, %edx
	movq	%rax, %rdi
	call	qsort@PLT
	movl	$0, -48(%rbp)
	movl	-60(%rbp), %eax
	subl	$1, %eax
	movl	%eax, -44(%rbp)
	jmp	.L12
.L18:
	movl	$0, -40(%rbp)
	jmp	.L13
.L17:
	movl	-48(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	movq	-24(%rbp), %rax
	addq	%rdx, %rax
	movq	(%rax), %rdx
	movl	-44(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rcx
	movq	-16(%rbp), %rax
	addq	%rcx, %rax
	movq	8(%rax), %rax
	cmpq	%rax, %rdx
	jge	.L23
	movl	-44(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rdx
	movq	-16(%rbp), %rax
	addq	%rdx, %rax
	movl	-48(%rbp), %edx
	movslq	%edx, %rdx
	leaq	0(,%rdx,8), %rcx
	movq	-24(%rbp), %rdx
	addq	%rcx, %rdx
	movq	8(%rax), %rax
	movq	%rax, (%rdx)
	addl	$1, -48(%rbp)
	addl	$1, -40(%rbp)
.L13:
	movl	-44(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rdx
	movq	-16(%rbp), %rax
	addq	%rdx, %rax
	movl	(%rax), %eax
	cmpl	%eax, -40(%rbp)
	jge	.L16
	movl	-64(%rbp), %eax
	cmpl	%eax, -48(%rbp)
	jl	.L17
	jmp	.L16
.L23:
	nop
.L16:
	subl	$1, -44(%rbp)
.L12:
	cmpl	$0, -44(%rbp)
	jns	.L18
	movq	$0, -32(%rbp)
	movl	$0, -36(%rbp)
	jmp	.L19
.L20:
	movl	-36(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	movq	-24(%rbp), %rax
	addq	%rdx, %rax
	movq	(%rax), %rax
	addq	%rax, -32(%rbp)
	addl	$1, -36(%rbp)
.L19:
	movl	-64(%rbp), %eax
	cmpl	%eax, -36(%rbp)
	jl	.L20
	movq	-32(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC4(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L22
	call	__stack_chk_fail@PLT
.L22:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE8:
	.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	comp
	.type	comp, @function
comp:
.LFB6:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movq	%rdi, -8(%rbp)
	movq	%rsi, -16(%rbp)
	movq	-8(%rbp), %rax
	movl	(%rax), %edx
	movq	-16(%rbp), %rax
	movl	(%rax), %ecx
	movl	%edx, %eax
	subl	%ecx, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE6:
	.size	comp, .-comp
	.globl	comp_act
	.type	comp_act, @function
comp_act:
.LFB7:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movq	%rdi, -8(%rbp)
	movq	%rsi, -16(%rbp)
	movq	-8(%rbp), %rax
	movq	8(%rax), %rax
	movl	%eax, %edx
	movq	-16(%rbp), %rax
	movq	8(%rax), %rax
	movl	%eax, %ecx
	movl	%edx, %eax
	subl	%ecx, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE7:
	.size	comp_act, .-comp_act
	.section	.rodata
.LC0:
	.string	"%d %d"
.LC1:
	.string	"not enough memory"
.LC2:
	.string	"%ld"
.LC3:
	.string	"%d %ld"
.LC4:
	.string	"%ld\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB8:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$64, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-60(%rbp), %rdx
	leaq	-64(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-64(%rbp), %eax
	cltq
	salq	$3, %rax
	movq	%rax, %rdi
	call	malloc@PLT
	movq	%rax, -24(%rbp)
	cmpq	$0, -24(%rbp)
	jne	.L6
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	call	puts@PLT
	movl	$1, %edi
	call	exit@PLT
.L6:
	movl	$0, -56(%rbp)
	jmp	.L7
.L8:
	movl	-56(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	movq	-24(%rbp), %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -56(%rbp)
.L7:
	movl	-64(%rbp), %eax
	cmpl	%eax, -56(%rbp)
	jl	.L8
	movl	-64(%rbp), %eax
	movslq	%eax, %rsi
	movq	-24(%rbp), %rax
	leaq	comp(%rip), %rdx
	movq	%rdx, %rcx
	movl	$8, %edx
	movq	%rax, %rdi
	call	qsort@PLT
	movl	-60(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rdi
	call	malloc@PLT
	movq	%rax, -16(%rbp)
	cmpq	$0, -16(%rbp)
	jne	.L9
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	call	puts@PLT
	movl	$1, %edi
	call	exit@PLT
.L9:
	movl	$0, -52(%rbp)
	jmp	.L10
.L11:
	movl	-52(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rdx
	movq	-16(%rbp), %rax
	addq	%rdx, %rax
	addq	$8, %rax
	movl	-52(%rbp), %edx
	movslq	%edx, %rdx
	movq	%rdx, %rcx
	salq	$4, %rcx
	movq	-16(%rbp), %rdx
	addq	%rcx, %rdx
	movq	%rdx, %rcx
	movq	%rax, %rdx
	movq	%rcx, %rsi
	leaq	.LC3(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -52(%rbp)
.L10:
	movl	-60(%rbp), %eax
	cmpl	%eax, -52(%rbp)
	jl	.L11
	movl	-60(%rbp), %eax
	movslq	%eax, %rsi
	movq	-16(%rbp), %rax
	leaq	comp_act(%rip), %rdx
	movq	%rdx, %rcx
	movl	$16, %edx
	movq	%rax, %rdi
	call	qsort@PLT
	movl	$0, -48(%rbp)
	movl	-60(%rbp), %eax
	subl	$1, %eax
	movl	%eax, -44(%rbp)
	jmp	.L12
.L18:
	movl	$0, -40(%rbp)
	jmp	.L13
.L17:
	movl	-48(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	movq	-24(%rbp), %rax
	addq	%rdx, %rax
	movq	(%rax), %rdx
	movl	-44(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rcx
	movq	-16(%rbp), %rax
	addq	%rcx, %rax
	movq	8(%rax), %rax
	cmpq	%rax, %rdx
	jge	.L23
	movl	-44(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rdx
	movq	-16(%rbp), %rax
	addq	%rdx, %rax
	movl	-48(%rbp), %edx
	movslq	%edx, %rdx
	leaq	0(,%rdx,8), %rcx
	movq	-24(%rbp), %rdx
	addq	%rcx, %rdx
	movq	8(%rax), %rax
	movq	%rax, (%rdx)
	addl	$1, -48(%rbp)
	addl	$1, -40(%rbp)
.L13:
	movl	-44(%rbp), %eax
	cltq
	salq	$4, %rax
	movq	%rax, %rdx
	movq	-16(%rbp), %rax
	addq	%rdx, %rax
	movl	(%rax), %eax
	cmpl	%eax, -40(%rbp)
	jge	.L16
	movl	-64(%rbp), %eax
	cmpl	%eax, -48(%rbp)
	jl	.L17
	jmp	.L16
.L23:
	nop
.L16:
	subl	$1, -44(%rbp)
.L12:
	cmpl	$0, -44(%rbp)
	jns	.L18
	movq	$0, -32(%rbp)
	movl	$0, -36(%rbp)
	jmp	.L19
.L20:
	movl	-36(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	movq	-24(%rbp), %rax
	addq	%rdx, %rax
	movq	(%rax), %rax
	addq	%rax, -32(%rbp)
	addl	$1, -36(%rbp)
.L19:
	movl	-64(%rbp), %eax
	cmpl	%eax, -36(%rbp)
	jl	.L20
	movq	-32(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC4(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L22
	call	__stack_chk_fail@PLT
.L22:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE8:
	.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:
