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

```c
// Aizu Vol-3 0306: Symmetric Ternary Number
// 2017.8.27 bal4u@uu

#include <stdio.h>

char *balancedTernary(char *p, int plen, int n)
{
	int r;

	p += plen;
	*--p = 0;
	if (n == 0) *--p = '0';
	else {
		while (n != 0) {
			r = n % 3;
			p--;
			if (r == 0) *p = '0';
			else if (r == 1) *p = '+';
			else *p = '-', n++;
			n /= 3;
		}
	}
	return p;
}

int main(void)
{
	int w;
	char buf[50];

	scanf("%d", &w);
	puts(balancedTernary(buf, 50, w));
	return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	balancedTernary
	.type	balancedTernary, @function
balancedTernary:
.LFB0:
	.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)
	movl	%esi, -28(%rbp)
	movl	%edx, -32(%rbp)
	movl	-28(%rbp), %eax
	cltq
	addq	%rax, -24(%rbp)
	subq	$1, -24(%rbp)
	movq	-24(%rbp), %rax
	movb	$0, (%rax)
	cmpl	$0, -32(%rbp)
	jne	.L4
	subq	$1, -24(%rbp)
	movq	-24(%rbp), %rax
	movb	$48, (%rax)
	jmp	.L3
.L8:
	movl	-32(%rbp), %eax
	movslq	%eax, %rdx
	imulq	$1431655766, %rdx, %rdx
	shrq	$32, %rdx
	movl	%eax, %esi
	sarl	$31, %esi
	movl	%edx, %ecx
	subl	%esi, %ecx
	movl	%ecx, %edx
	addl	%edx, %edx
	addl	%ecx, %edx
	subl	%edx, %eax
	movl	%eax, -4(%rbp)
	subq	$1, -24(%rbp)
	cmpl	$0, -4(%rbp)
	jne	.L5
	movq	-24(%rbp), %rax
	movb	$48, (%rax)
	jmp	.L6
.L5:
	cmpl	$1, -4(%rbp)
	jne	.L7
	movq	-24(%rbp), %rax
	movb	$43, (%rax)
	jmp	.L6
.L7:
	movq	-24(%rbp), %rax
	movb	$45, (%rax)
	addl	$1, -32(%rbp)
.L6:
	movl	-32(%rbp), %eax
	movslq	%eax, %rdx
	imulq	$1431655766, %rdx, %rdx
	shrq	$32, %rdx
	sarl	$31, %eax
	movl	%eax, %ecx
	movl	%edx, %eax
	subl	%ecx, %eax
	movl	%eax, -32(%rbp)
.L4:
	cmpl	$0, -32(%rbp)
	jne	.L8
.L3:
	movq	-24(%rbp), %rax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	balancedTernary, .-balancedTernary
	.section	.rodata
.LC0:
	.string	"%d"
	.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
	subq	$80, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-68(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-68(%rbp), %edx
	leaq	-64(%rbp), %rax
	movl	$50, %esi
	movq	%rax, %rdi
	call	balancedTernary
	movq	%rax, %rdi
	call	puts@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	leave
	.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	balancedTernary
	.type	balancedTernary, @function
balancedTernary:
.LFB23:
	.cfi_startproc
	endbr64
	movslq	%esi, %rsi
	movl	%edx, %ecx
	addq	%rsi, %rdi
	movb	$0, -1(%rdi)
	testl	%edx, %edx
	jne	.L2
	leaq	-2(%rdi), %r8
	movb	$48, -2(%rdi)
	movq	%r8, %rax
	ret
	.p2align 4,,10
	.p2align 3
.L2:
	leaq	-1(%rdi), %r8
	.p2align 4,,10
	.p2align 3
.L5:
	movslq	%ecx, %rdx
	movl	%ecx, %esi
	subq	$1, %r8
	movl	$48, %edi
	imulq	$1431655766, %rdx, %rax
	sarl	$31, %esi
	shrq	$32, %rax
	subl	%esi, %eax
	leal	(%rax,%rax,2), %esi
	movl	%ecx, %eax
	subl	%esi, %eax
	je	.L4
	movl	$43, %edi
	cmpl	$1, %eax
	je	.L4
	addl	$1, %ecx
	movl	$45, %edi
	movslq	%ecx, %rdx
.L4:
	imulq	$1431655766, %rdx, %rdx
	movl	%ecx, %eax
	movb	%dil, (%r8)
	sarl	$31, %eax
	shrq	$32, %rdx
	movl	%edx, %ecx
	subl	%eax, %ecx
	jne	.L5
	movq	%r8, %rax
	ret
	.cfi_endproc
.LFE23:
	.size	balancedTernary, .-balancedTernary
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB24:
	.cfi_startproc
	endbr64
	subq	$88, %rsp
	.cfi_def_cfa_offset 96
	leaq	.LC0(%rip), %rdi
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	leaq	12(%rsp), %rsi
	call	__isoc99_scanf@PLT
	movl	12(%rsp), %ecx
	movb	$0, 65(%rsp)
	testl	%ecx, %ecx
	jne	.L17
	movb	$48, 64(%rsp)
	leaq	64(%rsp), %rdi
.L14:
	call	puts@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L24
	xorl	%eax, %eax
	addq	$88, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 8
	ret
.L17:
	.cfi_restore_state
	leaq	65(%rsp), %rdi
	.p2align 4,,10
	.p2align 3
.L13:
	movslq	%ecx, %rdx
	movl	%ecx, %esi
	subq	$1, %rdi
	movl	$48, %r8d
	imulq	$1431655766, %rdx, %rax
	sarl	$31, %esi
	shrq	$32, %rax
	subl	%esi, %eax
	leal	(%rax,%rax,2), %esi
	movl	%ecx, %eax
	subl	%esi, %eax
	je	.L15
	movl	$43, %r8d
	cmpl	$1, %eax
	je	.L15
	addl	$1, %ecx
	movl	$45, %r8d
	movslq	%ecx, %rdx
.L15:
	imulq	$1431655766, %rdx, %rdx
	movl	%ecx, %eax
	movb	%r8b, (%rdi)
	sarl	$31, %eax
	shrq	$32, %rdx
	movl	%edx, %ecx
	subl	%eax, %ecx
	jne	.L13
	jmp	.L14
.L24:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE24:
	.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	balancedTernary
	.type	balancedTernary, @function
balancedTernary:
.LFB23:
	.cfi_startproc
	endbr64
	movslq	%esi, %rsi
	movl	%edx, %ecx
	addq	%rsi, %rdi
	movb	$0, -1(%rdi)
	testl	%edx, %edx
	jne	.L2
	leaq	-2(%rdi), %r8
	movb	$48, -2(%rdi)
	movq	%r8, %rax
	ret
	.p2align 4,,10
	.p2align 3
.L2:
	leaq	-1(%rdi), %r8
	.p2align 4,,10
	.p2align 3
.L5:
	movslq	%ecx, %rdx
	movl	%ecx, %esi
	subq	$1, %r8
	movl	$48, %edi
	imulq	$1431655766, %rdx, %rax
	sarl	$31, %esi
	shrq	$32, %rax
	subl	%esi, %eax
	leal	(%rax,%rax,2), %esi
	movl	%ecx, %eax
	subl	%esi, %eax
	je	.L4
	movl	$43, %edi
	cmpl	$1, %eax
	je	.L4
	addl	$1, %ecx
	movl	$45, %edi
	movslq	%ecx, %rdx
.L4:
	imulq	$1431655766, %rdx, %rdx
	movl	%ecx, %eax
	movb	%dil, (%r8)
	sarl	$31, %eax
	shrq	$32, %rdx
	movl	%edx, %ecx
	subl	%eax, %ecx
	jne	.L5
	movq	%r8, %rax
	ret
	.cfi_endproc
.LFE23:
	.size	balancedTernary, .-balancedTernary
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB24:
	.cfi_startproc
	endbr64
	subq	$88, %rsp
	.cfi_def_cfa_offset 96
	leaq	.LC0(%rip), %rdi
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	leaq	12(%rsp), %rsi
	call	__isoc99_scanf@PLT
	movl	12(%rsp), %ecx
	movb	$0, 65(%rsp)
	testl	%ecx, %ecx
	jne	.L17
	movb	$48, 64(%rsp)
	leaq	64(%rsp), %rdi
.L14:
	call	puts@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L24
	xorl	%eax, %eax
	addq	$88, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 8
	ret
.L17:
	.cfi_restore_state
	leaq	65(%rsp), %rdi
	.p2align 4,,10
	.p2align 3
.L13:
	movslq	%ecx, %rdx
	movl	%ecx, %esi
	subq	$1, %rdi
	movl	$48, %r8d
	imulq	$1431655766, %rdx, %rax
	sarl	$31, %esi
	shrq	$32, %rax
	subl	%esi, %eax
	leal	(%rax,%rax,2), %esi
	movl	%ecx, %eax
	subl	%esi, %eax
	je	.L15
	movl	$43, %r8d
	cmpl	$1, %eax
	je	.L15
	addl	$1, %ecx
	movl	$45, %r8d
	movslq	%ecx, %rdx
.L15:
	imulq	$1431655766, %rdx, %rdx
	movl	%ecx, %eax
	movb	%r8b, (%rdi)
	sarl	$31, %eax
	shrq	$32, %rdx
	movl	%edx, %ecx
	subl	%eax, %ecx
	jne	.L13
	jmp	.L14
.L24:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE24:
	.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:

// Aizu Vol-3 0306: Symmetric Ternary Number
// 2017.8.27 bal4u@uu

#include <stdio.h>

char *balancedTernary(char *p, int plen, int n)
{
	int r;

	p += plen;
	*--p = 0;
	if (n == 0) *--p = '0';
	else {
		while (n != 0) {
			r = n % 3;
			p--;
			if (r == 0) *p = '0';
			else if (r == 1) *p = '+';
			else *p = '-', n++;
			n /= 3;
		}
	}
	return p;
}

int main(void)
{
	int w;
	char buf[50];

	scanf("%d", &w);
	puts(balancedTernary(buf, 50, w));
	return 0;
}
15
 9
 30
 24
 20
 34
 49
 14
 10
 13

+--0
 +00
 +0+0
 +0-0
 +-+-
 ++-+
 +--++
 +---
 +0+
 +++

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

```c
// Aizu Vol-3 0306: Symmetric Ternary Number
// 2017.8.27 bal4u@uu

#include <stdio.h>

char *balancedTernary(char *p, int plen, int n)
{
	int r;

	p += plen;
	*--p = 0;
	if (n == 0) *--p = '0';
	else {
		while (n != 0) {
			r = n % 3;
			p--;
			if (r == 0) *p = '0';
			else if (r == 1) *p = '+';
			else *p = '-', n++;
			n /= 3;
		}
	}
	return p;
}

int main(void)
{
	int w;
	char buf[50];

	scanf("%d", &w);
	puts(balancedTernary(buf, 50, w));
	return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	balancedTernary
	.type	balancedTernary, @function
balancedTernary:
.LFB0:
	.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)
	movl	%esi, -28(%rbp)
	movl	%edx, -32(%rbp)
	movl	-28(%rbp), %eax
	cltq
	addq	%rax, -24(%rbp)
	subq	$1, -24(%rbp)
	movq	-24(%rbp), %rax
	movb	$0, (%rax)
	cmpl	$0, -32(%rbp)
	jne	.L4
	subq	$1, -24(%rbp)
	movq	-24(%rbp), %rax
	movb	$48, (%rax)
	jmp	.L3
.L8:
	movl	-32(%rbp), %eax
	movslq	%eax, %rdx
	imulq	$1431655766, %rdx, %rdx
	shrq	$32, %rdx
	movl	%eax, %esi
	sarl	$31, %esi
	movl	%edx, %ecx
	subl	%esi, %ecx
	movl	%ecx, %edx
	addl	%edx, %edx
	addl	%ecx, %edx
	subl	%edx, %eax
	movl	%eax, -4(%rbp)
	subq	$1, -24(%rbp)
	cmpl	$0, -4(%rbp)
	jne	.L5
	movq	-24(%rbp), %rax
	movb	$48, (%rax)
	jmp	.L6
.L5:
	cmpl	$1, -4(%rbp)
	jne	.L7
	movq	-24(%rbp), %rax
	movb	$43, (%rax)
	jmp	.L6
.L7:
	movq	-24(%rbp), %rax
	movb	$45, (%rax)
	addl	$1, -32(%rbp)
.L6:
	movl	-32(%rbp), %eax
	movslq	%eax, %rdx
	imulq	$1431655766, %rdx, %rdx
	shrq	$32, %rdx
	sarl	$31, %eax
	movl	%eax, %ecx
	movl	%edx, %eax
	subl	%ecx, %eax
	movl	%eax, -32(%rbp)
.L4:
	cmpl	$0, -32(%rbp)
	jne	.L8
.L3:
	movq	-24(%rbp), %rax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	balancedTernary, .-balancedTernary
	.section	.rodata
.LC0:
	.string	"%d"
	.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
	subq	$80, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-68(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-68(%rbp), %edx
	leaq	-64(%rbp), %rax
	movl	$50, %esi
	movq	%rax, %rdi
	call	balancedTernary
	movq	%rax, %rdi
	call	puts@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	leave
	.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	balancedTernary
	.type	balancedTernary, @function
balancedTernary:
.LFB0:
	.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)
	movl	%esi, -28(%rbp)
	movl	%edx, -32(%rbp)
	movl	-28(%rbp), %eax
	cltq
	addq	%rax, -24(%rbp)
	subq	$1, -24(%rbp)
	movq	-24(%rbp), %rax
	movb	$0, (%rax)
	cmpl	$0, -32(%rbp)
	jne	.L4
	subq	$1, -24(%rbp)
	movq	-24(%rbp), %rax
	movb	$48, (%rax)
	jmp	.L3
.L8:
	movl	-32(%rbp), %eax
	movslq	%eax, %rdx
	imulq	$1431655766, %rdx, %rdx
	shrq	$32, %rdx
	movl	%eax, %esi
	sarl	$31, %esi
	movl	%edx, %ecx
	subl	%esi, %ecx
	movl	%ecx, %edx
	addl	%edx, %edx
	addl	%ecx, %edx
	subl	%edx, %eax
	movl	%eax, -4(%rbp)
	subq	$1, -24(%rbp)
	cmpl	$0, -4(%rbp)
	jne	.L5
	movq	-24(%rbp), %rax
	movb	$48, (%rax)
	jmp	.L6
.L5:
	cmpl	$1, -4(%rbp)
	jne	.L7
	movq	-24(%rbp), %rax
	movb	$43, (%rax)
	jmp	.L6
.L7:
	movq	-24(%rbp), %rax
	movb	$45, (%rax)
	addl	$1, -32(%rbp)
.L6:
	movl	-32(%rbp), %eax
	movslq	%eax, %rdx
	imulq	$1431655766, %rdx, %rdx
	shrq	$32, %rdx
	sarl	$31, %eax
	movl	%eax, %ecx
	movl	%edx, %eax
	subl	%ecx, %eax
	movl	%eax, -32(%rbp)
.L4:
	cmpl	$0, -32(%rbp)
	jne	.L8
.L3:
	movq	-24(%rbp), %rax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	balancedTernary, .-balancedTernary
	.section	.rodata
.LC0:
	.string	"%d"
	.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
	subq	$80, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-68(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-68(%rbp), %edx
	leaq	-64(%rbp), %rax
	movl	$50, %esi
	movq	%rax, %rdi
	call	balancedTernary
	movq	%rax, %rdi
	call	puts@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	leave
	.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:
