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

```c
long n,k;long fun(long n, long x){if(n==0)return(x<=0?0:1);long p=(1LL<<n)-1;if(x<=2*p)return fun(n-1,x-1);return p+1+fun(n-1,x-1-2*p);}
main(){scanf("%ld%ld",&n,&k);printf("%ld\n",fun(n,k));}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	n
	.bss
	.align 8
	.type	n, @object
	.size	n, 8
n:
	.zero	8
	.globl	k
	.align 8
	.type	k, @object
	.size	k, 8
k:
	.zero	8
	.text
	.globl	fun
	.type	fun, @function
fun:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%rbx
	subq	$40, %rsp
	.cfi_offset 3, -24
	movq	%rdi, -40(%rbp)
	movq	%rsi, -48(%rbp)
	cmpq	$0, -40(%rbp)
	jne	.L2
	cmpq	$0, -48(%rbp)
	setg	%al
	movzbl	%al, %eax
	jmp	.L3
.L2:
	movq	-40(%rbp), %rax
	movl	$1, %edx
	movl	%eax, %ecx
	salq	%cl, %rdx
	movq	%rdx, %rax
	subq	$1, %rax
	movq	%rax, -24(%rbp)
	movq	-24(%rbp), %rax
	addq	%rax, %rax
	cmpq	%rax, -48(%rbp)
	jg	.L4
	movq	-48(%rbp), %rax
	leaq	-1(%rax), %rdx
	movq	-40(%rbp), %rax
	subq	$1, %rax
	movq	%rdx, %rsi
	movq	%rax, %rdi
	call	fun
	jmp	.L3
.L4:
	movq	-24(%rbp), %rax
	leaq	1(%rax), %rbx
	movq	-48(%rbp), %rax
	leaq	-1(%rax), %rdx
	movq	-24(%rbp), %rax
	addq	%rax, %rax
	subq	%rax, %rdx
	movq	-40(%rbp), %rax
	subq	$1, %rax
	movq	%rdx, %rsi
	movq	%rax, %rdi
	call	fun
	addq	%rbx, %rax
.L3:
	movq	-8(%rbp), %rbx
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	fun, .-fun
	.section	.rodata
.LC0:
	.string	"%ld%ld"
.LC1:
	.string	"%ld\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	leaq	k(%rip), %rax
	movq	%rax, %rdx
	leaq	n(%rip), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	scanf@PLT
	movq	k(%rip), %rdx
	movq	n(%rip), %rax
	movq	%rdx, %rsi
	movq	%rax, %rdi
	call	fun
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
```

Only output the optimized assembly code. Do not include any other text. Do not write any comments in the assembly code. Wrap the assembly code in ```assembly``` tags.
user
	.file	"temp.c"
	.text
	.p2align 4
	.globl	fun
	.type	fun, @function
fun:
.LFB0:
	.cfi_startproc
	endbr64
	movq	%rdi, %rcx
	xorl	%r8d, %r8d
	testq	%rdi, %rdi
	je	.L2
	movl	$1, %r9d
	jmp	.L4
	.p2align 4,,10
	.p2align 3
.L9:
	subq	%rdx, %rdi
	addq	%rax, %r8
	movq	%rdi, %rsi
	testq	%rcx, %rcx
	je	.L2
.L4:
	movq	%r9, %rax
	leaq	-1(%rsi), %rdi
	salq	%cl, %rax
	subq	$1, %rcx
	leaq	-2(%rax,%rax), %rdx
	cmpq	%rsi, %rdx
	jl	.L9
	movq	%rdi, %rsi
	testq	%rcx, %rcx
	jne	.L4
.L2:
	xorl	%eax, %eax
	testq	%rsi, %rsi
	setg	%al
	addq	%r8, %rax
	ret
	.cfi_endproc
.LFE0:
	.size	fun, .-fun
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%ld%ld"
.LC1:
	.string	"%ld\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	endbr64
	subq	$8, %rsp
	.cfi_def_cfa_offset 16
	leaq	k(%rip), %rdx
	leaq	n(%rip), %rsi
	xorl	%eax, %eax
	leaq	.LC0(%rip), %rdi
	call	scanf@PLT
	movq	n(%rip), %rcx
	movq	k(%rip), %rax
	xorl	%r8d, %r8d
	testq	%rcx, %rcx
	je	.L11
	movl	$1, %r9d
	.p2align 4,,10
	.p2align 3
.L13:
	movq	%r9, %rdx
	leaq	-1(%rax), %rdi
	salq	%cl, %rdx
	subq	$1, %rcx
	leaq	-2(%rdx,%rdx), %rsi
	cmpq	%rax, %rsi
	movq	%rdi, %rax
	jge	.L12
	subq	%rsi, %rax
	addq	%rdx, %r8
.L12:
	testq	%rcx, %rcx
	jne	.L13
.L11:
	xorl	%esi, %esi
	testq	%rax, %rax
	leaq	.LC1(%rip), %rdi
	setg	%sil
	xorl	%eax, %eax
	addq	%r8, %rsi
	call	printf@PLT
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.globl	k
	.bss
	.align 8
	.type	k, @object
	.size	k, 8
k:
	.zero	8
	.globl	n
	.align 8
	.type	n, @object
	.size	n, 8
n:
	.zero	8
	.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	fun
	.type	fun, @function
fun:
.LFB0:
	.cfi_startproc
	endbr64
	movq	%rdi, %rcx
	xorl	%r8d, %r8d
	testq	%rdi, %rdi
	je	.L2
	movl	$1, %r9d
	jmp	.L4
	.p2align 4,,10
	.p2align 3
.L9:
	subq	%rdx, %rdi
	addq	%rax, %r8
	movq	%rdi, %rsi
	testq	%rcx, %rcx
	je	.L2
.L4:
	movq	%r9, %rax
	leaq	-1(%rsi), %rdi
	salq	%cl, %rax
	subq	$1, %rcx
	leaq	-2(%rax,%rax), %rdx
	cmpq	%rsi, %rdx
	jl	.L9
	movq	%rdi, %rsi
	testq	%rcx, %rcx
	jne	.L4
.L2:
	xorl	%eax, %eax
	testq	%rsi, %rsi
	setg	%al
	addq	%r8, %rax
	ret
	.cfi_endproc
.LFE0:
	.size	fun, .-fun
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%ld%ld"
.LC1:
	.string	"%ld\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	endbr64
	subq	$8, %rsp
	.cfi_def_cfa_offset 16
	leaq	k(%rip), %rdx
	leaq	n(%rip), %rsi
	xorl	%eax, %eax
	leaq	.LC0(%rip), %rdi
	call	scanf@PLT
	movq	n(%rip), %rcx
	movq	k(%rip), %rax
	xorl	%r8d, %r8d
	testq	%rcx, %rcx
	je	.L11
	movl	$1, %r9d
	.p2align 4,,10
	.p2align 3
.L13:
	movq	%r9, %rdx
	leaq	-1(%rax), %rdi
	salq	%cl, %rdx
	subq	$1, %rcx
	leaq	-2(%rdx,%rdx), %rsi
	cmpq	%rax, %rsi
	movq	%rdi, %rax
	jge	.L12
	subq	%rsi, %rax
	addq	%rdx, %r8
.L12:
	testq	%rcx, %rcx
	jne	.L13
.L11:
	xorl	%esi, %esi
	testq	%rax, %rax
	leaq	.LC1(%rip), %rdi
	setg	%sil
	xorl	%eax, %eax
	addq	%r8, %rsi
	call	printf@PLT
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.globl	k
	.bss
	.align 8
	.type	k, @object
	.size	k, 8
k:
	.zero	8
	.globl	n
	.align 8
	.type	n, @object
	.size	n, 8
n:
	.zero	8
	.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:

long n,k;long fun(long n, long x){if(n==0)return(x<=0?0:1);long p=(1LL<<n)-1;if(x<=2*p)return fun(n-1,x-1);return p+1+fun(n-1,x-1-2*p);}
main(){scanf("%ld%ld",&n,&k);printf("%ld\n",fun(n,k));}
50 3396494817772502
 2 1
 4 7
 0 1
 8 19
 6 19
 6 11
 6 14
 61 4321098765432109
 6 32

1698247408886243
 0
 3
 1
 7
 8
 4
 6
 2160549382716050
 15

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

```c
long n,k;long fun(long n, long x){if(n==0)return(x<=0?0:1);long p=(1LL<<n)-1;if(x<=2*p)return fun(n-1,x-1);return p+1+fun(n-1,x-1-2*p);}
main(){scanf("%ld%ld",&n,&k);printf("%ld\n",fun(n,k));}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	n
	.bss
	.align 8
	.type	n, @object
	.size	n, 8
n:
	.zero	8
	.globl	k
	.align 8
	.type	k, @object
	.size	k, 8
k:
	.zero	8
	.text
	.globl	fun
	.type	fun, @function
fun:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%rbx
	subq	$40, %rsp
	.cfi_offset 3, -24
	movq	%rdi, -40(%rbp)
	movq	%rsi, -48(%rbp)
	cmpq	$0, -40(%rbp)
	jne	.L2
	cmpq	$0, -48(%rbp)
	setg	%al
	movzbl	%al, %eax
	jmp	.L3
.L2:
	movq	-40(%rbp), %rax
	movl	$1, %edx
	movl	%eax, %ecx
	salq	%cl, %rdx
	movq	%rdx, %rax
	subq	$1, %rax
	movq	%rax, -24(%rbp)
	movq	-24(%rbp), %rax
	addq	%rax, %rax
	cmpq	%rax, -48(%rbp)
	jg	.L4
	movq	-48(%rbp), %rax
	leaq	-1(%rax), %rdx
	movq	-40(%rbp), %rax
	subq	$1, %rax
	movq	%rdx, %rsi
	movq	%rax, %rdi
	call	fun
	jmp	.L3
.L4:
	movq	-24(%rbp), %rax
	leaq	1(%rax), %rbx
	movq	-48(%rbp), %rax
	leaq	-1(%rax), %rdx
	movq	-24(%rbp), %rax
	addq	%rax, %rax
	subq	%rax, %rdx
	movq	-40(%rbp), %rax
	subq	$1, %rax
	movq	%rdx, %rsi
	movq	%rax, %rdi
	call	fun
	addq	%rbx, %rax
.L3:
	movq	-8(%rbp), %rbx
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	fun, .-fun
	.section	.rodata
.LC0:
	.string	"%ld%ld"
.LC1:
	.string	"%ld\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	leaq	k(%rip), %rax
	movq	%rax, %rdx
	leaq	n(%rip), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	scanf@PLT
	movq	k(%rip), %rdx
	movq	n(%rip), %rax
	movq	%rdx, %rsi
	movq	%rax, %rdi
	call	fun
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
```

Only output the optimized assembly code. Do not include any other text. Do not write any comments in the assembly code. Wrap the assembly code in ```assembly``` tags.
train
	.file	"temp.c"
	.text
	.globl	n
	.bss
	.align 8
	.type	n, @object
	.size	n, 8
n:
	.zero	8
	.globl	k
	.align 8
	.type	k, @object
	.size	k, 8
k:
	.zero	8
	.text
	.globl	fun
	.type	fun, @function
fun:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%rbx
	subq	$40, %rsp
	.cfi_offset 3, -24
	movq	%rdi, -40(%rbp)
	movq	%rsi, -48(%rbp)
	cmpq	$0, -40(%rbp)
	jne	.L2
	cmpq	$0, -48(%rbp)
	setg	%al
	movzbl	%al, %eax
	jmp	.L3
.L2:
	movq	-40(%rbp), %rax
	movl	$1, %edx
	movl	%eax, %ecx
	salq	%cl, %rdx
	movq	%rdx, %rax
	subq	$1, %rax
	movq	%rax, -24(%rbp)
	movq	-24(%rbp), %rax
	addq	%rax, %rax
	cmpq	%rax, -48(%rbp)
	jg	.L4
	movq	-48(%rbp), %rax
	leaq	-1(%rax), %rdx
	movq	-40(%rbp), %rax
	subq	$1, %rax
	movq	%rdx, %rsi
	movq	%rax, %rdi
	call	fun
	jmp	.L3
.L4:
	movq	-24(%rbp), %rax
	leaq	1(%rax), %rbx
	movq	-48(%rbp), %rax
	leaq	-1(%rax), %rdx
	movq	-24(%rbp), %rax
	addq	%rax, %rax
	subq	%rax, %rdx
	movq	-40(%rbp), %rax
	subq	$1, %rax
	movq	%rdx, %rsi
	movq	%rax, %rdi
	call	fun
	addq	%rbx, %rax
.L3:
	movq	-8(%rbp), %rbx
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	fun, .-fun
	.section	.rodata
.LC0:
	.string	"%ld%ld"
.LC1:
	.string	"%ld\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	leaq	k(%rip), %rax
	movq	%rax, %rdx
	leaq	n(%rip), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	scanf@PLT
	movq	k(%rip), %rdx
	movq	n(%rip), %rax
	movq	%rdx, %rsi
	movq	%rax, %rdi
	call	fun
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
