Given the following C code and 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;
}```

Assembly Code:

```assembly
	.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:
```

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.
Optimized Assembly Code:

user
```assembly
	.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
```assembly
	.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:
```
```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;
}```
Given the following C 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;
}```

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.
Optimized Assembly Code:

15
 9
 30
 24
 20
 34
 49
 14
 10
 13

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

Given the following C code and 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;
}```

Assembly Code:

```assembly
	.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:
```

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.
Optimized Assembly Code:

train
```assembly
	.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:
```