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-1 0174: Badminton
// 2017.8.9

#include <stdio.h>
#include <ctype.h>

char *gets(char *);
char buf[150], *p;

int main()
{
	int a, b, k;

	while (1) {
		for (k = 3; k-- > 0; k--) {
			gets(p = buf); while (isspace(*p)) p++;
			if (*p == '0') return 0;
			a = b = 0;
			while (*++p) if (*p == 'A') a++; else b++;
			if (a > b) a++; else b++;
			printf("%d %d\n", a, b);
		}
	}
	return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	buf
	.bss
	.align 32
	.type	buf, @object
	.size	buf, 150
buf:
	.zero	150
	.globl	p
	.align 8
	.type	p, @object
	.size	p, 8
p:
	.zero	8
	.section	.rodata
.LC0:
	.string	"%d %d\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$16, %rsp
.L13:
	movl	$3, -4(%rbp)
	jmp	.L2
.L12:
	leaq	buf(%rip), %rax
	movq	%rax, p(%rip)
	movq	p(%rip), %rax
	movq	%rax, %rdi
	call	gets@PLT
	jmp	.L3
.L4:
	movq	p(%rip), %rax
	addq	$1, %rax
	movq	%rax, p(%rip)
.L3:
	call	__ctype_b_loc@PLT
	movq	(%rax), %rdx
	movq	p(%rip), %rax
	movzbl	(%rax), %eax
	movsbq	%al, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	movzwl	(%rax), %eax
	movzwl	%ax, %eax
	andl	$8192, %eax
	testl	%eax, %eax
	jne	.L4
	movq	p(%rip), %rax
	movzbl	(%rax), %eax
	cmpb	$48, %al
	jne	.L5
	movl	$0, %eax
	jmp	.L14
.L5:
	movl	$0, -8(%rbp)
	movl	-8(%rbp), %eax
	movl	%eax, -12(%rbp)
	jmp	.L7
.L9:
	movq	p(%rip), %rax
	movzbl	(%rax), %eax
	cmpb	$65, %al
	jne	.L8
	addl	$1, -12(%rbp)
	jmp	.L7
.L8:
	addl	$1, -8(%rbp)
.L7:
	movq	p(%rip), %rax
	addq	$1, %rax
	movq	%rax, p(%rip)
	movq	p(%rip), %rax
	movzbl	(%rax), %eax
	testb	%al, %al
	jne	.L9
	movl	-12(%rbp), %eax
	cmpl	-8(%rbp), %eax
	jle	.L10
	addl	$1, -12(%rbp)
	jmp	.L11
.L10:
	addl	$1, -8(%rbp)
.L11:
	movl	-8(%rbp), %edx
	movl	-12(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	subl	$1, -4(%rbp)
.L2:
	movl	-4(%rbp), %eax
	leal	-1(%rax), %edx
	movl	%edx, -4(%rbp)
	testl	%eax, %eax
	jg	.L12
	jmp	.L13
.L14:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.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
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d %d\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB25:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC0(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	leaq	buf(%rip), %rbx
	subq	$8, %rsp
	.cfi_def_cfa_offset 48
	call	__ctype_b_loc@PLT
	movq	%rax, %r12
	.p2align 4,,10
	.p2align 3
.L12:
	movl	$2, %ebp
.L11:
	movq	%rbx, %rdi
	movq	%rbx, p(%rip)
	call	gets@PLT
	movq	p(%rip), %rsi
	movq	(%r12), %rdi
	movsbq	(%rsi), %rax
	movq	%rax, %rdx
	testb	$32, 1(%rdi,%rax,2)
	je	.L2
	leaq	1(%rsi), %rax
	.p2align 4,,10
	.p2align 3
.L3:
	movq	%rax, p(%rip)
	movsbq	(%rax), %rcx
	movq	%rax, %rsi
	addq	$1, %rax
	movq	%rcx, %rdx
	testb	$32, 1(%rdi,%rcx,2)
	jne	.L3
.L2:
	cmpb	$48, %dl
	je	.L16
	leaq	1(%rsi), %rax
	xorl	%ecx, %ecx
	xorl	%r8d, %r8d
	movq	%rax, p(%rip)
	movzbl	1(%rsi), %edx
	testb	%dl, %dl
	jne	.L5
	jmp	.L24
	.p2align 4,,10
	.p2align 3
.L7:
	addl	$1, %ecx
.L8:
	addq	$1, %rax
	movq	%rax, p(%rip)
	movzbl	(%rax), %edx
	testb	%dl, %dl
	je	.L25
.L5:
	cmpb	$65, %dl
	jne	.L7
	addl	$1, %r8d
	jmp	.L8
	.p2align 4,,10
	.p2align 3
.L25:
	cmpl	%ecx, %r8d
	jle	.L26
	addl	$1, %r8d
.L10:
	xorl	%eax, %eax
	movl	%r8d, %edx
	movq	%r13, %rsi
	movl	$1, %edi
	call	__printf_chk@PLT
	cmpl	$1, %ebp
	je	.L12
	movl	$1, %ebp
	jmp	.L11
	.p2align 4,,10
	.p2align 3
.L26:
	addl	$1, %ecx
	jmp	.L10
	.p2align 4,,10
	.p2align 3
.L24:
	movl	$1, %ecx
	jmp	.L10
.L16:
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE25:
	.size	main, .-main
	.globl	p
	.bss
	.align 8
	.type	p, @object
	.size	p, 8
p:
	.zero	8
	.globl	buf
	.align 32
	.type	buf, @object
	.size	buf, 150
buf:
	.zero	150
	.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
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d %d\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB25:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC0(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	leaq	buf(%rip), %rbx
	subq	$8, %rsp
	.cfi_def_cfa_offset 48
	call	__ctype_b_loc@PLT
	movq	%rax, %r12
	.p2align 4,,10
	.p2align 3
.L12:
	movl	$2, %ebp
.L11:
	movq	%rbx, %rdi
	movq	%rbx, p(%rip)
	call	gets@PLT
	movq	p(%rip), %rsi
	movq	(%r12), %rdi
	movsbq	(%rsi), %rax
	movq	%rax, %rdx
	testb	$32, 1(%rdi,%rax,2)
	je	.L2
	leaq	1(%rsi), %rax
	.p2align 4,,10
	.p2align 3
.L3:
	movq	%rax, p(%rip)
	movsbq	(%rax), %rcx
	movq	%rax, %rsi
	addq	$1, %rax
	movq	%rcx, %rdx
	testb	$32, 1(%rdi,%rcx,2)
	jne	.L3
.L2:
	cmpb	$48, %dl
	je	.L16
	leaq	1(%rsi), %rax
	xorl	%ecx, %ecx
	xorl	%r8d, %r8d
	movq	%rax, p(%rip)
	movzbl	1(%rsi), %edx
	testb	%dl, %dl
	jne	.L5
	jmp	.L24
	.p2align 4,,10
	.p2align 3
.L7:
	addl	$1, %ecx
.L8:
	addq	$1, %rax
	movq	%rax, p(%rip)
	movzbl	(%rax), %edx
	testb	%dl, %dl
	je	.L25
.L5:
	cmpb	$65, %dl
	jne	.L7
	addl	$1, %r8d
	jmp	.L8
	.p2align 4,,10
	.p2align 3
.L25:
	cmpl	%ecx, %r8d
	jle	.L26
	addl	$1, %r8d
.L10:
	xorl	%eax, %eax
	movl	%r8d, %edx
	movq	%r13, %rsi
	movl	$1, %edi
	call	__printf_chk@PLT
	cmpl	$1, %ebp
	je	.L12
	movl	$1, %ebp
	jmp	.L11
	.p2align 4,,10
	.p2align 3
.L26:
	addl	$1, %ecx
	jmp	.L10
	.p2align 4,,10
	.p2align 3
.L24:
	movl	$1, %ecx
	jmp	.L10
.L16:
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE25:
	.size	main, .-main
	.globl	p
	.bss
	.align 8
	.type	p, @object
	.size	p, 8
p:
	.zero	8
	.globl	buf
	.align 32
	.type	buf, @object
	.size	buf, 150
buf:
	.zero	150
	.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-1 0174: Badminton
// 2017.8.9

#include <stdio.h>
#include <ctype.h>

char *gets(char *);
char buf[150], *p;

int main()
{
	int a, b, k;

	while (1) {
		for (k = 3; k-- > 0; k--) {
			gets(p = buf); while (isspace(*p)) p++;
			if (*p == '0') return 0;
			a = b = 0;
			while (*++p) if (*p == 'A') a++; else b++;
			if (a > b) a++; else b++;
			printf("%d %d\n", a, b);
		}
	}
	return 0;
}
AABBAAABABAABBBAABA
AABBBABBABBAAABABABAAB
BABAABAABABABBAAAB
AABABAAABBAABBBABAA
AAAAAAAAAAA
ABBBBBBBBBB
0
 ABAABBBAABABAAABBAA
AABBBABBABBAAABABABAAB
BABAABAABABABBAAAB
AABABAAABBAABBBABAA
AAAAAAAAAAA
ABBBBBABBBB
0
 ABAABBBAABABAAABBAA
AABBBABBABBAAABABABAAB
BABAABAABABABBAAAB
AABAABBAABBAAABABAA
AAAAAAAAAAA
ABBBBBABBBB
0
 ABAABABAABABAAABBAA
AABBBABBABBAAABABABAAB
BABAABAABABABBAAAB
AABABAAABBAABBBABAA
AAAAAAAAAAA
ABBBBBABBBB
0
 AABBAAABABAABBBAABA
AABBBABBABBAAABABABAAB
BABAABAABABABBAAAB
AABAABBAABBAAABABAA
AAAAAAAAAAA
ABBBABABBBB
0
 ABBABBAAABABAAABBAA
AABBBABBABBAAABABABAAB
BABAABAABABABBBAAB
AABBBBBAABAAAABABAA
AAAAAAAAAAA
ABBBBBABBBB
0
 AABBAAABABAABBBAABA
AABBBABBABBAAABABABAAB
BABAABAABAABBBAAAB
AABAABBAABBAAABABAB
AAAAAAAAAAA
ABBBABABBBB
0
 ABAABBBAABABAAABBAA
AABBBABBABBAAABABABAAB
BABAABAABABABBAAAA
AABAABBAABBAAABABAA
AAAAAAAAAAA
ABBBBBABBBB
0
 ABBABBAAABABAAABBAA
AABBBABBABBAAABABABAAB
BABAABAABABABBBAAB
AABBBBBAABAAAABABAA
AAAAAAAAAAA
ABBBBBABBBA
0
 AABBAAABABAABBBAABA
AABBBABBABBAAABABABAAB
BABAABAABAABBBAAAB
AABAABBAABBAAABABAB
AAAAAAAAAAA
ABBAABABBBB
0

11 8
10 12
11 7
11 8
11 0
0 11
 11 8
10 12
11 7
11 8
11 0
1 10
 11 8
10 12
11 7
12 7
11 0
1 10
 12 7
10 12
11 7
11 8
11 0
1 10
 11 8
10 12
11 7
12 7
11 0
2 9
 11 8
10 12
10 8
11 8
11 0
1 10
 11 8
10 12
11 7
11 8
11 0
2 9
 11 8
10 12
12 6
12 7
11 0
1 10
 11 8
10 12
10 8
11 8
11 0
2 9
 11 8
10 12
11 7
11 8
11 0
3 8

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-1 0174: Badminton
// 2017.8.9

#include <stdio.h>
#include <ctype.h>

char *gets(char *);
char buf[150], *p;

int main()
{
	int a, b, k;

	while (1) {
		for (k = 3; k-- > 0; k--) {
			gets(p = buf); while (isspace(*p)) p++;
			if (*p == '0') return 0;
			a = b = 0;
			while (*++p) if (*p == 'A') a++; else b++;
			if (a > b) a++; else b++;
			printf("%d %d\n", a, b);
		}
	}
	return 0;
}```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.globl	buf
	.bss
	.align 32
	.type	buf, @object
	.size	buf, 150
buf:
	.zero	150
	.globl	p
	.align 8
	.type	p, @object
	.size	p, 8
p:
	.zero	8
	.section	.rodata
.LC0:
	.string	"%d %d\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$16, %rsp
.L13:
	movl	$3, -4(%rbp)
	jmp	.L2
.L12:
	leaq	buf(%rip), %rax
	movq	%rax, p(%rip)
	movq	p(%rip), %rax
	movq	%rax, %rdi
	call	gets@PLT
	jmp	.L3
.L4:
	movq	p(%rip), %rax
	addq	$1, %rax
	movq	%rax, p(%rip)
.L3:
	call	__ctype_b_loc@PLT
	movq	(%rax), %rdx
	movq	p(%rip), %rax
	movzbl	(%rax), %eax
	movsbq	%al, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	movzwl	(%rax), %eax
	movzwl	%ax, %eax
	andl	$8192, %eax
	testl	%eax, %eax
	jne	.L4
	movq	p(%rip), %rax
	movzbl	(%rax), %eax
	cmpb	$48, %al
	jne	.L5
	movl	$0, %eax
	jmp	.L14
.L5:
	movl	$0, -8(%rbp)
	movl	-8(%rbp), %eax
	movl	%eax, -12(%rbp)
	jmp	.L7
.L9:
	movq	p(%rip), %rax
	movzbl	(%rax), %eax
	cmpb	$65, %al
	jne	.L8
	addl	$1, -12(%rbp)
	jmp	.L7
.L8:
	addl	$1, -8(%rbp)
.L7:
	movq	p(%rip), %rax
	addq	$1, %rax
	movq	%rax, p(%rip)
	movq	p(%rip), %rax
	movzbl	(%rax), %eax
	testb	%al, %al
	jne	.L9
	movl	-12(%rbp), %eax
	cmpl	-8(%rbp), %eax
	jle	.L10
	addl	$1, -12(%rbp)
	jmp	.L11
.L10:
	addl	$1, -8(%rbp)
.L11:
	movl	-8(%rbp), %edx
	movl	-12(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	subl	$1, -4(%rbp)
.L2:
	movl	-4(%rbp), %eax
	leal	-1(%rax), %edx
	movl	%edx, -4(%rbp)
	testl	%eax, %eax
	jg	.L12
	jmp	.L13
.L14:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.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	buf
	.bss
	.align 32
	.type	buf, @object
	.size	buf, 150
buf:
	.zero	150
	.globl	p
	.align 8
	.type	p, @object
	.size	p, 8
p:
	.zero	8
	.section	.rodata
.LC0:
	.string	"%d %d\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$16, %rsp
.L13:
	movl	$3, -4(%rbp)
	jmp	.L2
.L12:
	leaq	buf(%rip), %rax
	movq	%rax, p(%rip)
	movq	p(%rip), %rax
	movq	%rax, %rdi
	call	gets@PLT
	jmp	.L3
.L4:
	movq	p(%rip), %rax
	addq	$1, %rax
	movq	%rax, p(%rip)
.L3:
	call	__ctype_b_loc@PLT
	movq	(%rax), %rdx
	movq	p(%rip), %rax
	movzbl	(%rax), %eax
	movsbq	%al, %rax
	addq	%rax, %rax
	addq	%rdx, %rax
	movzwl	(%rax), %eax
	movzwl	%ax, %eax
	andl	$8192, %eax
	testl	%eax, %eax
	jne	.L4
	movq	p(%rip), %rax
	movzbl	(%rax), %eax
	cmpb	$48, %al
	jne	.L5
	movl	$0, %eax
	jmp	.L14
.L5:
	movl	$0, -8(%rbp)
	movl	-8(%rbp), %eax
	movl	%eax, -12(%rbp)
	jmp	.L7
.L9:
	movq	p(%rip), %rax
	movzbl	(%rax), %eax
	cmpb	$65, %al
	jne	.L8
	addl	$1, -12(%rbp)
	jmp	.L7
.L8:
	addl	$1, -8(%rbp)
.L7:
	movq	p(%rip), %rax
	addq	$1, %rax
	movq	%rax, p(%rip)
	movq	p(%rip), %rax
	movzbl	(%rax), %eax
	testb	%al, %al
	jne	.L9
	movl	-12(%rbp), %eax
	cmpl	-8(%rbp), %eax
	jle	.L10
	addl	$1, -12(%rbp)
	jmp	.L11
.L10:
	addl	$1, -8(%rbp)
.L11:
	movl	-8(%rbp), %edx
	movl	-12(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	subl	$1, -4(%rbp)
.L2:
	movl	-4(%rbp), %eax
	leal	-1(%rax), %edx
	movl	%edx, -4(%rbp)
	testl	%eax, %eax
	jg	.L12
	jmp	.L13
.L14:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.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:
