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>          // printf(), scanf()
#include <stdlib.h>         // qsort()
#include <stdbool.h>

#define MAX_N 1000
#define MAX_V 10000
#define min(a, b) ((a) < (b) ? a : b)

int a[MAX_N];
int n;

int
cmp_func(const void *l_, const void *r_)
{
	const int* l = (const int*) l_;
	const int* r = (const int*) r_;

	if (*l < *r)
		return -1;

	if (*l > *r)
		return 1;

	return 0;
}

int
solve()
{
	int b[MAX_N];
	int t[MAX_V + 1];
	bool v[MAX_N];
	int i;

	int ans = 0;
	for (i = 0; i < n; ++i)
	{
		b[i] = a[i];
		v[i] = false;
	}

	qsort(b, n, sizeof(int), cmp_func);
	for (i = 0; i < n; ++i)
		t[b[i]] = i;

	for (i = 0; i < n; ++i)
	{
		if (v[i])
			continue;

		int cur = i;
		int s = 0;
		int m = MAX_V;
		int an = 0;
		while (true)
		{
			v[cur] = true;
			an++;
			int u = a[cur];
			if (u < m)
				m = u;

			s += u;
			cur = t[u];
			if (v[cur])
				break;
		}

		ans += min(s + (an - 2) * m, s + m + (an + 1) * b[0]);
	}

	return ans;
}

int
main(int argc, char** argv)
{
	int i;

	scanf("%d", &n);
	for (i = 0; i < n; ++i)
		scanf("%d", &a[i]);

	printf("%d\n", solve());
	return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	a
	.bss
	.align 32
	.type	a, @object
	.size	a, 4000
a:
	.zero	4000
	.globl	n
	.align 4
	.type	n, @object
	.size	n, 4
n:
	.zero	4
	.text
	.globl	cmp_func
	.type	cmp_func, @function
cmp_func:
.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, -24(%rbp)
	movq	%rsi, -32(%rbp)
	movq	-24(%rbp), %rax
	movq	%rax, -16(%rbp)
	movq	-32(%rbp), %rax
	movq	%rax, -8(%rbp)
	movq	-16(%rbp), %rax
	movl	(%rax), %edx
	movq	-8(%rbp), %rax
	movl	(%rax), %eax
	cmpl	%eax, %edx
	jge	.L2
	movl	$-1, %eax
	jmp	.L3
.L2:
	movq	-16(%rbp), %rax
	movl	(%rax), %edx
	movq	-8(%rbp), %rax
	movl	(%rax), %eax
	cmpl	%eax, %edx
	jle	.L4
	movl	$1, %eax
	jmp	.L3
.L4:
	movl	$0, %eax
.L3:
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE6:
	.size	cmp_func, .-cmp_func
	.globl	solve
	.type	solve, @function
solve:
.LFB7:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	leaq	-45056(%rsp), %r11
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movl	$0, -45048(%rbp)
	movl	$0, -45052(%rbp)
	jmp	.L6
.L7:
	movl	-45052(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	a(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	-45052(%rbp), %eax
	cltq
	movl	%edx, -44016(%rbp,%rax,4)
	movl	-45052(%rbp), %eax
	cltq
	movb	$0, -45024(%rbp,%rax)
	addl	$1, -45052(%rbp)
.L6:
	movl	n(%rip), %eax
	cmpl	%eax, -45052(%rbp)
	jl	.L7
	movl	n(%rip), %eax
	movslq	%eax, %rsi
	leaq	-44016(%rbp), %rax
	leaq	cmp_func(%rip), %rdx
	movq	%rdx, %rcx
	movl	$4, %edx
	movq	%rax, %rdi
	call	qsort@PLT
	movl	$0, -45052(%rbp)
	jmp	.L8
.L9:
	movl	-45052(%rbp), %eax
	cltq
	movl	-44016(%rbp,%rax,4), %eax
	cltq
	movl	-45052(%rbp), %edx
	movl	%edx, -40016(%rbp,%rax,4)
	addl	$1, -45052(%rbp)
.L8:
	movl	n(%rip), %eax
	cmpl	%eax, -45052(%rbp)
	jl	.L9
	movl	$0, -45052(%rbp)
	jmp	.L10
.L17:
	movl	-45052(%rbp), %eax
	cltq
	movzbl	-45024(%rbp,%rax), %eax
	testb	%al, %al
	jne	.L21
	movl	-45052(%rbp), %eax
	movl	%eax, -45044(%rbp)
	movl	$0, -45040(%rbp)
	movl	$10000, -45036(%rbp)
	movl	$0, -45032(%rbp)
.L16:
	movl	-45044(%rbp), %eax
	cltq
	movb	$1, -45024(%rbp,%rax)
	addl	$1, -45032(%rbp)
	movl	-45044(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	a(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -45028(%rbp)
	movl	-45028(%rbp), %eax
	cmpl	-45036(%rbp), %eax
	jge	.L13
	movl	-45028(%rbp), %eax
	movl	%eax, -45036(%rbp)
.L13:
	movl	-45028(%rbp), %eax
	addl	%eax, -45040(%rbp)
	movl	-45028(%rbp), %eax
	cltq
	movl	-40016(%rbp,%rax,4), %eax
	movl	%eax, -45044(%rbp)
	movl	-45044(%rbp), %eax
	cltq
	movzbl	-45024(%rbp,%rax), %eax
	testb	%al, %al
	jne	.L22
	jmp	.L16
.L22:
	nop
	movl	-45040(%rbp), %edx
	movl	-45036(%rbp), %eax
	leal	(%rdx,%rax), %ecx
	movl	-45032(%rbp), %eax
	leal	1(%rax), %edx
	movl	-44016(%rbp), %eax
	imull	%edx, %eax
	leal	(%rcx,%rax), %edx
	movl	-45032(%rbp), %eax
	subl	$2, %eax
	imull	-45036(%rbp), %eax
	movl	%eax, %ecx
	movl	-45040(%rbp), %eax
	addl	%ecx, %eax
	cmpl	%eax, %edx
	cmovle	%edx, %eax
	addl	%eax, -45048(%rbp)
	jmp	.L12
.L21:
	nop
.L12:
	addl	$1, -45052(%rbp)
.L10:
	movl	n(%rip), %eax
	cmpl	%eax, -45052(%rbp)
	jl	.L17
	movl	-45048(%rbp), %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
.LFE7:
	.size	solve, .-solve
	.section	.rodata
.LC0:
	.string	"%d"
.LC1:
	.string	"%d\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	$32, %rsp
	movl	%edi, -20(%rbp)
	movq	%rsi, -32(%rbp)
	leaq	n(%rip), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$0, -4(%rbp)
	jmp	.L24
.L25:
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	a(%rip), %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -4(%rbp)
.L24:
	movl	n(%rip), %eax
	cmpl	%eax, -4(%rbp)
	jl	.L25
	movl	$0, %eax
	call	solve
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	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	cmp_func
	.type	cmp_func, @function
cmp_func:
.LFB39:
	.cfi_startproc
	endbr64
	movl	(%rsi), %eax
	cmpl	%eax, (%rdi)
	movl	$-1, %edx
	setg	%al
	movzbl	%al, %eax
	cmovl	%edx, %eax
	ret
	.cfi_endproc
.LFE39:
	.size	cmp_func, .-cmp_func
	.p2align 4
	.globl	solve
	.type	solve, @function
solve:
.LFB40:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	leaq	-40960(%rsp), %r11
	.cfi_def_cfa 11, 40984
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	.cfi_def_cfa_register 7
	subq	$4072, %rsp
	.cfi_def_cfa_offset 45056
	movslq	n(%rip), %r12
	movq	%fs:40, %rax
	movq	%rax, 45016(%rsp)
	xorl	%eax, %eax
	leaq	1008(%rsp), %rbx
	testl	%r12d, %r12d
	jle	.L6
	leaq	0(,%r12,4), %rdx
	leaq	a(%rip), %rsi
	movq	%rbx, %rdi
	call	memcpy@PLT
	movq	%rsp, %r11
	movq	%r12, %rdx
	xorl	%esi, %esi
	movq	%r11, %rdi
	call	memset@PLT
.L6:
	leaq	cmp_func(%rip), %rcx
	movl	$4, %edx
	movq	%r12, %rsi
	movq	%rbx, %rdi
	call	qsort@PLT
	movslq	n(%rip), %r10
	testl	%r10d, %r10d
	jle	.L13
	xorl	%eax, %eax
	.p2align 4,,10
	.p2align 3
.L8:
	movslq	(%rbx,%rax,4), %rdx
	movl	%eax, 5008(%rsp,%rdx,4)
	addq	$1, %rax
	cmpq	%rax, %r10
	jne	.L8
	movl	1008(%rsp), %ebx
	xorl	%r8d, %r8d
	xorl	%r12d, %r12d
	movq	%rsp, %r11
	leaq	a(%rip), %r9
	jmp	.L11
	.p2align 4,,10
	.p2align 3
.L9:
	addq	$1, %r8
	cmpq	%r8, %r10
	je	.L5
.L11:
	cmpb	$0, (%r11,%r8)
	movslq	%r8d, %rax
	jne	.L9
	xorl	%ecx, %ecx
	movl	$10000, %edx
	xorl	%esi, %esi
	.p2align 4,,10
	.p2align 3
.L10:
	movb	$1, (%rsp,%rax)
	movslq	(%r9,%rax,4), %rax
	movl	%ecx, %edi
	addl	$1, %ecx
	cmpl	%eax, %edx
	cmovg	%eax, %edx
	addl	%eax, %esi
	movslq	5008(%rsp,%rax,4), %rax
	cmpb	$0, (%rsp,%rax)
	je	.L10
	leal	2(%rdi), %eax
	subl	$1, %edi
	leal	(%rdx,%rsi), %ecx
	imull	%ebx, %eax
	imull	%edx, %edi
	addl	%ecx, %eax
	addl	%esi, %edi
	cmpl	%edi, %eax
	cmovg	%edi, %eax
	addq	$1, %r8
	addl	%eax, %r12d
	cmpq	%r8, %r10
	jne	.L11
.L5:
	movq	45016(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L20
	addq	$45032, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 24
	movl	%r12d, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
.L13:
	.cfi_restore_state
	xorl	%r12d, %r12d
	jmp	.L5
.L20:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE40:
	.size	solve, .-solve
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d"
.LC1:
	.string	"%d\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB41:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	.LC0(%rip), %r12
	leaq	n(%rip), %rsi
	xorl	%eax, %eax
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%r12, %rdi
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	call	__isoc99_scanf@PLT
	movl	n(%rip), %eax
	testl	%eax, %eax
	jle	.L22
	leaq	a(%rip), %rbp
	xorl	%ebx, %ebx
	.p2align 4,,10
	.p2align 3
.L23:
	movq	%rbp, %rsi
	xorl	%eax, %eax
	movq	%r12, %rdi
	addl	$1, %ebx
	call	__isoc99_scanf@PLT
	addq	$4, %rbp
	cmpl	%ebx, n(%rip)
	jg	.L23
.L22:
	xorl	%eax, %eax
	call	solve
	leaq	.LC1(%rip), %rsi
	movl	$1, %edi
	movl	%eax, %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	popq	%rbx
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE41:
	.size	main, .-main
	.globl	n
	.bss
	.align 4
	.type	n, @object
	.size	n, 4
n:
	.zero	4
	.globl	a
	.align 32
	.type	a, @object
	.size	a, 4000
a:
	.zero	4000
	.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	cmp_func
	.type	cmp_func, @function
cmp_func:
.LFB39:
	.cfi_startproc
	endbr64
	movl	(%rsi), %eax
	cmpl	%eax, (%rdi)
	movl	$-1, %edx
	setg	%al
	movzbl	%al, %eax
	cmovl	%edx, %eax
	ret
	.cfi_endproc
.LFE39:
	.size	cmp_func, .-cmp_func
	.p2align 4
	.globl	solve
	.type	solve, @function
solve:
.LFB40:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	leaq	-40960(%rsp), %r11
	.cfi_def_cfa 11, 40984
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	.cfi_def_cfa_register 7
	subq	$4072, %rsp
	.cfi_def_cfa_offset 45056
	movslq	n(%rip), %r12
	movq	%fs:40, %rax
	movq	%rax, 45016(%rsp)
	xorl	%eax, %eax
	leaq	1008(%rsp), %rbx
	testl	%r12d, %r12d
	jle	.L6
	leaq	0(,%r12,4), %rdx
	leaq	a(%rip), %rsi
	movq	%rbx, %rdi
	call	memcpy@PLT
	movq	%rsp, %r11
	movq	%r12, %rdx
	xorl	%esi, %esi
	movq	%r11, %rdi
	call	memset@PLT
.L6:
	leaq	cmp_func(%rip), %rcx
	movl	$4, %edx
	movq	%r12, %rsi
	movq	%rbx, %rdi
	call	qsort@PLT
	movslq	n(%rip), %r10
	testl	%r10d, %r10d
	jle	.L13
	xorl	%eax, %eax
	.p2align 4,,10
	.p2align 3
.L8:
	movslq	(%rbx,%rax,4), %rdx
	movl	%eax, 5008(%rsp,%rdx,4)
	addq	$1, %rax
	cmpq	%rax, %r10
	jne	.L8
	movl	1008(%rsp), %ebx
	xorl	%r8d, %r8d
	xorl	%r12d, %r12d
	movq	%rsp, %r11
	leaq	a(%rip), %r9
	jmp	.L11
	.p2align 4,,10
	.p2align 3
.L9:
	addq	$1, %r8
	cmpq	%r8, %r10
	je	.L5
.L11:
	cmpb	$0, (%r11,%r8)
	movslq	%r8d, %rax
	jne	.L9
	xorl	%ecx, %ecx
	movl	$10000, %edx
	xorl	%esi, %esi
	.p2align 4,,10
	.p2align 3
.L10:
	movb	$1, (%rsp,%rax)
	movslq	(%r9,%rax,4), %rax
	movl	%ecx, %edi
	addl	$1, %ecx
	cmpl	%eax, %edx
	cmovg	%eax, %edx
	addl	%eax, %esi
	movslq	5008(%rsp,%rax,4), %rax
	cmpb	$0, (%rsp,%rax)
	je	.L10
	leal	2(%rdi), %eax
	subl	$1, %edi
	leal	(%rdx,%rsi), %ecx
	imull	%ebx, %eax
	imull	%edx, %edi
	addl	%ecx, %eax
	addl	%esi, %edi
	cmpl	%edi, %eax
	cmovg	%edi, %eax
	addq	$1, %r8
	addl	%eax, %r12d
	cmpq	%r8, %r10
	jne	.L11
.L5:
	movq	45016(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L20
	addq	$45032, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 24
	movl	%r12d, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
.L13:
	.cfi_restore_state
	xorl	%r12d, %r12d
	jmp	.L5
.L20:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE40:
	.size	solve, .-solve
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d"
.LC1:
	.string	"%d\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB41:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	.LC0(%rip), %r12
	leaq	n(%rip), %rsi
	xorl	%eax, %eax
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%r12, %rdi
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	call	__isoc99_scanf@PLT
	movl	n(%rip), %eax
	testl	%eax, %eax
	jle	.L22
	leaq	a(%rip), %rbp
	xorl	%ebx, %ebx
	.p2align 4,,10
	.p2align 3
.L23:
	movq	%rbp, %rsi
	xorl	%eax, %eax
	movq	%r12, %rdi
	addl	$1, %ebx
	call	__isoc99_scanf@PLT
	addq	$4, %rbp
	cmpl	%ebx, n(%rip)
	jg	.L23
.L22:
	xorl	%eax, %eax
	call	solve
	leaq	.LC1(%rip), %rsi
	movl	$1, %edi
	movl	%eax, %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	popq	%rbx
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE41:
	.size	main, .-main
	.globl	n
	.bss
	.align 4
	.type	n, @object
	.size	n, 4
n:
	.zero	4
	.globl	a
	.align 32
	.type	a, @object
	.size	a, 4000
a:
	.zero	4000
	.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>          // printf(), scanf()
#include <stdlib.h>         // qsort()
#include <stdbool.h>

#define MAX_N 1000
#define MAX_V 10000
#define min(a, b) ((a) < (b) ? a : b)

int a[MAX_N];
int n;

int
cmp_func(const void *l_, const void *r_)
{
	const int* l = (const int*) l_;
	const int* r = (const int*) r_;

	if (*l < *r)
		return -1;

	if (*l > *r)
		return 1;

	return 0;
}

int
solve()
{
	int b[MAX_N];
	int t[MAX_V + 1];
	bool v[MAX_N];
	int i;

	int ans = 0;
	for (i = 0; i < n; ++i)
	{
		b[i] = a[i];
		v[i] = false;
	}

	qsort(b, n, sizeof(int), cmp_func);
	for (i = 0; i < n; ++i)
		t[b[i]] = i;

	for (i = 0; i < n; ++i)
	{
		if (v[i])
			continue;

		int cur = i;
		int s = 0;
		int m = MAX_V;
		int an = 0;
		while (true)
		{
			v[cur] = true;
			an++;
			int u = a[cur];
			if (u < m)
				m = u;

			s += u;
			cur = t[u];
			if (v[cur])
				break;
		}

		ans += min(s + (an - 2) * m, s + m + (an + 1) * b[0]);
	}

	return ans;
}

int
main(int argc, char** argv)
{
	int i;

	scanf("%d", &n);
	for (i = 0; i < n; ++i)
		scanf("%d", &a[i]);

	printf("%d\n", solve());
	return 0;
}
4
8 3 2 1
 5
0 5 3 4 2
 5
0 5 3 1 2
 5
1 8 3 4 2
 5
0 9 3 1 2
 5
1 8 3 4 0
 5
0 15 3 1 2
 5
1 5 3 8 2
 5
0 5 6 4 2
 5
0 5 3 1 4

14
 7
 12
 10
 16
 9
 22
 17
 19
 11

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>          // printf(), scanf()
#include <stdlib.h>         // qsort()
#include <stdbool.h>

#define MAX_N 1000
#define MAX_V 10000
#define min(a, b) ((a) < (b) ? a : b)

int a[MAX_N];
int n;

int
cmp_func(const void *l_, const void *r_)
{
	const int* l = (const int*) l_;
	const int* r = (const int*) r_;

	if (*l < *r)
		return -1;

	if (*l > *r)
		return 1;

	return 0;
}

int
solve()
{
	int b[MAX_N];
	int t[MAX_V + 1];
	bool v[MAX_N];
	int i;

	int ans = 0;
	for (i = 0; i < n; ++i)
	{
		b[i] = a[i];
		v[i] = false;
	}

	qsort(b, n, sizeof(int), cmp_func);
	for (i = 0; i < n; ++i)
		t[b[i]] = i;

	for (i = 0; i < n; ++i)
	{
		if (v[i])
			continue;

		int cur = i;
		int s = 0;
		int m = MAX_V;
		int an = 0;
		while (true)
		{
			v[cur] = true;
			an++;
			int u = a[cur];
			if (u < m)
				m = u;

			s += u;
			cur = t[u];
			if (v[cur])
				break;
		}

		ans += min(s + (an - 2) * m, s + m + (an + 1) * b[0]);
	}

	return ans;
}

int
main(int argc, char** argv)
{
	int i;

	scanf("%d", &n);
	for (i = 0; i < n; ++i)
		scanf("%d", &a[i]);

	printf("%d\n", solve());
	return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	a
	.bss
	.align 32
	.type	a, @object
	.size	a, 4000
a:
	.zero	4000
	.globl	n
	.align 4
	.type	n, @object
	.size	n, 4
n:
	.zero	4
	.text
	.globl	cmp_func
	.type	cmp_func, @function
cmp_func:
.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, -24(%rbp)
	movq	%rsi, -32(%rbp)
	movq	-24(%rbp), %rax
	movq	%rax, -16(%rbp)
	movq	-32(%rbp), %rax
	movq	%rax, -8(%rbp)
	movq	-16(%rbp), %rax
	movl	(%rax), %edx
	movq	-8(%rbp), %rax
	movl	(%rax), %eax
	cmpl	%eax, %edx
	jge	.L2
	movl	$-1, %eax
	jmp	.L3
.L2:
	movq	-16(%rbp), %rax
	movl	(%rax), %edx
	movq	-8(%rbp), %rax
	movl	(%rax), %eax
	cmpl	%eax, %edx
	jle	.L4
	movl	$1, %eax
	jmp	.L3
.L4:
	movl	$0, %eax
.L3:
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE6:
	.size	cmp_func, .-cmp_func
	.globl	solve
	.type	solve, @function
solve:
.LFB7:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	leaq	-45056(%rsp), %r11
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movl	$0, -45048(%rbp)
	movl	$0, -45052(%rbp)
	jmp	.L6
.L7:
	movl	-45052(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	a(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	-45052(%rbp), %eax
	cltq
	movl	%edx, -44016(%rbp,%rax,4)
	movl	-45052(%rbp), %eax
	cltq
	movb	$0, -45024(%rbp,%rax)
	addl	$1, -45052(%rbp)
.L6:
	movl	n(%rip), %eax
	cmpl	%eax, -45052(%rbp)
	jl	.L7
	movl	n(%rip), %eax
	movslq	%eax, %rsi
	leaq	-44016(%rbp), %rax
	leaq	cmp_func(%rip), %rdx
	movq	%rdx, %rcx
	movl	$4, %edx
	movq	%rax, %rdi
	call	qsort@PLT
	movl	$0, -45052(%rbp)
	jmp	.L8
.L9:
	movl	-45052(%rbp), %eax
	cltq
	movl	-44016(%rbp,%rax,4), %eax
	cltq
	movl	-45052(%rbp), %edx
	movl	%edx, -40016(%rbp,%rax,4)
	addl	$1, -45052(%rbp)
.L8:
	movl	n(%rip), %eax
	cmpl	%eax, -45052(%rbp)
	jl	.L9
	movl	$0, -45052(%rbp)
	jmp	.L10
.L17:
	movl	-45052(%rbp), %eax
	cltq
	movzbl	-45024(%rbp,%rax), %eax
	testb	%al, %al
	jne	.L21
	movl	-45052(%rbp), %eax
	movl	%eax, -45044(%rbp)
	movl	$0, -45040(%rbp)
	movl	$10000, -45036(%rbp)
	movl	$0, -45032(%rbp)
.L16:
	movl	-45044(%rbp), %eax
	cltq
	movb	$1, -45024(%rbp,%rax)
	addl	$1, -45032(%rbp)
	movl	-45044(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	a(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -45028(%rbp)
	movl	-45028(%rbp), %eax
	cmpl	-45036(%rbp), %eax
	jge	.L13
	movl	-45028(%rbp), %eax
	movl	%eax, -45036(%rbp)
.L13:
	movl	-45028(%rbp), %eax
	addl	%eax, -45040(%rbp)
	movl	-45028(%rbp), %eax
	cltq
	movl	-40016(%rbp,%rax,4), %eax
	movl	%eax, -45044(%rbp)
	movl	-45044(%rbp), %eax
	cltq
	movzbl	-45024(%rbp,%rax), %eax
	testb	%al, %al
	jne	.L22
	jmp	.L16
.L22:
	nop
	movl	-45040(%rbp), %edx
	movl	-45036(%rbp), %eax
	leal	(%rdx,%rax), %ecx
	movl	-45032(%rbp), %eax
	leal	1(%rax), %edx
	movl	-44016(%rbp), %eax
	imull	%edx, %eax
	leal	(%rcx,%rax), %edx
	movl	-45032(%rbp), %eax
	subl	$2, %eax
	imull	-45036(%rbp), %eax
	movl	%eax, %ecx
	movl	-45040(%rbp), %eax
	addl	%ecx, %eax
	cmpl	%eax, %edx
	cmovle	%edx, %eax
	addl	%eax, -45048(%rbp)
	jmp	.L12
.L21:
	nop
.L12:
	addl	$1, -45052(%rbp)
.L10:
	movl	n(%rip), %eax
	cmpl	%eax, -45052(%rbp)
	jl	.L17
	movl	-45048(%rbp), %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
.LFE7:
	.size	solve, .-solve
	.section	.rodata
.LC0:
	.string	"%d"
.LC1:
	.string	"%d\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	$32, %rsp
	movl	%edi, -20(%rbp)
	movq	%rsi, -32(%rbp)
	leaq	n(%rip), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$0, -4(%rbp)
	jmp	.L24
.L25:
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	a(%rip), %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -4(%rbp)
.L24:
	movl	n(%rip), %eax
	cmpl	%eax, -4(%rbp)
	jl	.L25
	movl	$0, %eax
	call	solve
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	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	a
	.bss
	.align 32
	.type	a, @object
	.size	a, 4000
a:
	.zero	4000
	.globl	n
	.align 4
	.type	n, @object
	.size	n, 4
n:
	.zero	4
	.text
	.globl	cmp_func
	.type	cmp_func, @function
cmp_func:
.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, -24(%rbp)
	movq	%rsi, -32(%rbp)
	movq	-24(%rbp), %rax
	movq	%rax, -16(%rbp)
	movq	-32(%rbp), %rax
	movq	%rax, -8(%rbp)
	movq	-16(%rbp), %rax
	movl	(%rax), %edx
	movq	-8(%rbp), %rax
	movl	(%rax), %eax
	cmpl	%eax, %edx
	jge	.L2
	movl	$-1, %eax
	jmp	.L3
.L2:
	movq	-16(%rbp), %rax
	movl	(%rax), %edx
	movq	-8(%rbp), %rax
	movl	(%rax), %eax
	cmpl	%eax, %edx
	jle	.L4
	movl	$1, %eax
	jmp	.L3
.L4:
	movl	$0, %eax
.L3:
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE6:
	.size	cmp_func, .-cmp_func
	.globl	solve
	.type	solve, @function
solve:
.LFB7:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	leaq	-45056(%rsp), %r11
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movl	$0, -45048(%rbp)
	movl	$0, -45052(%rbp)
	jmp	.L6
.L7:
	movl	-45052(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	a(%rip), %rax
	movl	(%rdx,%rax), %edx
	movl	-45052(%rbp), %eax
	cltq
	movl	%edx, -44016(%rbp,%rax,4)
	movl	-45052(%rbp), %eax
	cltq
	movb	$0, -45024(%rbp,%rax)
	addl	$1, -45052(%rbp)
.L6:
	movl	n(%rip), %eax
	cmpl	%eax, -45052(%rbp)
	jl	.L7
	movl	n(%rip), %eax
	movslq	%eax, %rsi
	leaq	-44016(%rbp), %rax
	leaq	cmp_func(%rip), %rdx
	movq	%rdx, %rcx
	movl	$4, %edx
	movq	%rax, %rdi
	call	qsort@PLT
	movl	$0, -45052(%rbp)
	jmp	.L8
.L9:
	movl	-45052(%rbp), %eax
	cltq
	movl	-44016(%rbp,%rax,4), %eax
	cltq
	movl	-45052(%rbp), %edx
	movl	%edx, -40016(%rbp,%rax,4)
	addl	$1, -45052(%rbp)
.L8:
	movl	n(%rip), %eax
	cmpl	%eax, -45052(%rbp)
	jl	.L9
	movl	$0, -45052(%rbp)
	jmp	.L10
.L17:
	movl	-45052(%rbp), %eax
	cltq
	movzbl	-45024(%rbp,%rax), %eax
	testb	%al, %al
	jne	.L21
	movl	-45052(%rbp), %eax
	movl	%eax, -45044(%rbp)
	movl	$0, -45040(%rbp)
	movl	$10000, -45036(%rbp)
	movl	$0, -45032(%rbp)
.L16:
	movl	-45044(%rbp), %eax
	cltq
	movb	$1, -45024(%rbp,%rax)
	addl	$1, -45032(%rbp)
	movl	-45044(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	a(%rip), %rax
	movl	(%rdx,%rax), %eax
	movl	%eax, -45028(%rbp)
	movl	-45028(%rbp), %eax
	cmpl	-45036(%rbp), %eax
	jge	.L13
	movl	-45028(%rbp), %eax
	movl	%eax, -45036(%rbp)
.L13:
	movl	-45028(%rbp), %eax
	addl	%eax, -45040(%rbp)
	movl	-45028(%rbp), %eax
	cltq
	movl	-40016(%rbp,%rax,4), %eax
	movl	%eax, -45044(%rbp)
	movl	-45044(%rbp), %eax
	cltq
	movzbl	-45024(%rbp,%rax), %eax
	testb	%al, %al
	jne	.L22
	jmp	.L16
.L22:
	nop
	movl	-45040(%rbp), %edx
	movl	-45036(%rbp), %eax
	leal	(%rdx,%rax), %ecx
	movl	-45032(%rbp), %eax
	leal	1(%rax), %edx
	movl	-44016(%rbp), %eax
	imull	%edx, %eax
	leal	(%rcx,%rax), %edx
	movl	-45032(%rbp), %eax
	subl	$2, %eax
	imull	-45036(%rbp), %eax
	movl	%eax, %ecx
	movl	-45040(%rbp), %eax
	addl	%ecx, %eax
	cmpl	%eax, %edx
	cmovle	%edx, %eax
	addl	%eax, -45048(%rbp)
	jmp	.L12
.L21:
	nop
.L12:
	addl	$1, -45052(%rbp)
.L10:
	movl	n(%rip), %eax
	cmpl	%eax, -45052(%rbp)
	jl	.L17
	movl	-45048(%rbp), %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
.LFE7:
	.size	solve, .-solve
	.section	.rodata
.LC0:
	.string	"%d"
.LC1:
	.string	"%d\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	$32, %rsp
	movl	%edi, -20(%rbp)
	movq	%rsi, -32(%rbp)
	leaq	n(%rip), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$0, -4(%rbp)
	jmp	.L24
.L25:
	movl	-4(%rbp), %eax
	cltq
	leaq	0(,%rax,4), %rdx
	leaq	a(%rip), %rax
	addq	%rdx, %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	addl	$1, -4(%rbp)
.L24:
	movl	n(%rip), %eax
	cmpl	%eax, -4(%rbp)
	jl	.L25
	movl	$0, %eax
	call	solve
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	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:
