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>
#include <stdlib.h>

int main(void){
    int n,i,flg,c;
    char *str;
    scanf("%d",&n);
    str = (char*)malloc(sizeof(char)*(n+1));
    scanf("%s",str);
    flg = -1;c=0;
    for(i=0;i<n;i++){
        if(str[i]=='O'){
            if(flg==1){
                c++;
                flg = -1;
            }else{
                flg = 0;
            }
        }else{
            if(flg==0){
                c++;
                flg = -1;
            }else{
                flg = 1;
            }
        }
    }
    printf("%d\n",c);
    free(str);
    return 0;
}
```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.section	.rodata
.LC0:
	.string	"%d"
.LC1:
	.string	"%s"
.LC2:
	.string	"%d\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB6:
	.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
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-32(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-32(%rbp), %eax
	addl	$1, %eax
	cltq
	movq	%rax, %rdi
	call	malloc@PLT
	movq	%rax, -16(%rbp)
	movq	-16(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$-1, -24(%rbp)
	movl	$0, -20(%rbp)
	movl	$0, -28(%rbp)
	jmp	.L2
.L7:
	movl	-28(%rbp), %eax
	movslq	%eax, %rdx
	movq	-16(%rbp), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$79, %al
	jne	.L3
	cmpl	$1, -24(%rbp)
	jne	.L4
	addl	$1, -20(%rbp)
	movl	$-1, -24(%rbp)
	jmp	.L5
.L4:
	movl	$0, -24(%rbp)
	jmp	.L5
.L3:
	cmpl	$0, -24(%rbp)
	jne	.L6
	addl	$1, -20(%rbp)
	movl	$-1, -24(%rbp)
	jmp	.L5
.L6:
	movl	$1, -24(%rbp)
.L5:
	addl	$1, -28(%rbp)
.L2:
	movl	-32(%rbp), %eax
	cmpl	%eax, -28(%rbp)
	jl	.L7
	movl	-20(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movq	-16(%rbp), %rax
	movq	%rax, %rdi
	call	free@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L9
	call	__stack_chk_fail@PLT
.L9:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE6:
	.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"
.LC1:
	.string	"%s"
.LC2:
	.string	"%d\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB39:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC0(%rip), %rdi
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	leaq	4(%rsp), %rsi
	call	__isoc99_scanf@PLT
	movl	4(%rsp), %eax
	leal	1(%rax), %edi
	movslq	%edi, %rdi
	call	malloc@PLT
	leaq	.LC1(%rip), %rdi
	movq	%rax, %rsi
	movq	%rax, %rbp
	xorl	%eax, %eax
	call	__isoc99_scanf@PLT
	movslq	4(%rsp), %rcx
	testl	%ecx, %ecx
	jle	.L7
	movq	%rbp, %rax
	addq	%rbp, %rcx
	xorl	%r8d, %r8d
	movl	$-1, %edx
	jmp	.L5
	.p2align 4,,10
	.p2align 3
.L3:
	testl	%edx, %edx
	je	.L12
	movl	$1, %edx
.L4:
	addq	$1, %rax
	cmpq	%rax, %rcx
	je	.L2
.L5:
	cmpb	$79, (%rax)
	jne	.L3
	cmpl	$1, %edx
	je	.L12
	addq	$1, %rax
	xorl	%edx, %edx
	cmpq	%rax, %rcx
	jne	.L5
	.p2align 4,,10
	.p2align 3
.L2:
	movl	$1, %edi
	xorl	%eax, %eax
	movl	%r8d, %edx
	leaq	.LC2(%rip), %rsi
	call	__printf_chk@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L14
	addq	$16, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 16
	xorl	%eax, %eax
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.p2align 4,,10
	.p2align 3
.L12:
	.cfi_restore_state
	addl	$1, %r8d
	movl	$-1, %edx
	jmp	.L4
.L7:
	xorl	%r8d, %r8d
	jmp	.L2
.L14:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE39:
	.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
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d"
.LC1:
	.string	"%s"
.LC2:
	.string	"%d\n"
	.section	.text.startup,"ax",@progbits
	.p2align 4
	.globl	main
	.type	main, @function
main:
.LFB39:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC0(%rip), %rdi
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	leaq	4(%rsp), %rsi
	call	__isoc99_scanf@PLT
	movl	4(%rsp), %eax
	leal	1(%rax), %edi
	movslq	%edi, %rdi
	call	malloc@PLT
	leaq	.LC1(%rip), %rdi
	movq	%rax, %rsi
	movq	%rax, %rbp
	xorl	%eax, %eax
	call	__isoc99_scanf@PLT
	movslq	4(%rsp), %rcx
	testl	%ecx, %ecx
	jle	.L7
	movq	%rbp, %rax
	addq	%rbp, %rcx
	xorl	%r8d, %r8d
	movl	$-1, %edx
	jmp	.L5
	.p2align 4,,10
	.p2align 3
.L3:
	testl	%edx, %edx
	je	.L12
	movl	$1, %edx
.L4:
	addq	$1, %rax
	cmpq	%rax, %rcx
	je	.L2
.L5:
	cmpb	$79, (%rax)
	jne	.L3
	cmpl	$1, %edx
	je	.L12
	addq	$1, %rax
	xorl	%edx, %edx
	cmpq	%rax, %rcx
	jne	.L5
	.p2align 4,,10
	.p2align 3
.L2:
	movl	$1, %edi
	xorl	%eax, %eax
	movl	%r8d, %edx
	leaq	.LC2(%rip), %rsi
	call	__printf_chk@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L14
	addq	$16, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 16
	xorl	%eax, %eax
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.p2align 4,,10
	.p2align 3
.L12:
	.cfi_restore_state
	addl	$1, %r8d
	movl	$-1, %edx
	jmp	.L4
.L7:
	xorl	%r8d, %r8d
	jmp	.L2
.L14:
	call	__stack_chk_fail@PLT
	.cfi_endproc
.LFE39:
	.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:

#include <stdio.h>
#include <stdlib.h>

int main(void){
    int n,i,flg,c;
    char *str;
    scanf("%d",&n);
    str = (char*)malloc(sizeof(char)*(n+1));
    scanf("%s",str);
    flg = -1;c=0;
    for(i=0;i<n;i++){
        if(str[i]=='O'){
            if(flg==1){
                c++;
                flg = -1;
            }else{
                flg = 0;
            }
        }else{
            if(flg==0){
                c++;
                flg = -1;
            }else{
                flg = 1;
            }
        }
    }
    printf("%d\n",c);
    free(str);
    return 0;
}

5
OXXOX

2

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>
#include <stdlib.h>

int main(void){
    int n,i,flg,c;
    char *str;
    scanf("%d",&n);
    str = (char*)malloc(sizeof(char)*(n+1));
    scanf("%s",str);
    flg = -1;c=0;
    for(i=0;i<n;i++){
        if(str[i]=='O'){
            if(flg==1){
                c++;
                flg = -1;
            }else{
                flg = 0;
            }
        }else{
            if(flg==0){
                c++;
                flg = -1;
            }else{
                flg = 1;
            }
        }
    }
    printf("%d\n",c);
    free(str);
    return 0;
}
```

Unoptimized Assembly Code:

```assembly
	.file	"temp.c"
	.text
	.section	.rodata
.LC0:
	.string	"%d"
.LC1:
	.string	"%s"
.LC2:
	.string	"%d\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB6:
	.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
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-32(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-32(%rbp), %eax
	addl	$1, %eax
	cltq
	movq	%rax, %rdi
	call	malloc@PLT
	movq	%rax, -16(%rbp)
	movq	-16(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$-1, -24(%rbp)
	movl	$0, -20(%rbp)
	movl	$0, -28(%rbp)
	jmp	.L2
.L7:
	movl	-28(%rbp), %eax
	movslq	%eax, %rdx
	movq	-16(%rbp), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$79, %al
	jne	.L3
	cmpl	$1, -24(%rbp)
	jne	.L4
	addl	$1, -20(%rbp)
	movl	$-1, -24(%rbp)
	jmp	.L5
.L4:
	movl	$0, -24(%rbp)
	jmp	.L5
.L3:
	cmpl	$0, -24(%rbp)
	jne	.L6
	addl	$1, -20(%rbp)
	movl	$-1, -24(%rbp)
	jmp	.L5
.L6:
	movl	$1, -24(%rbp)
.L5:
	addl	$1, -28(%rbp)
.L2:
	movl	-32(%rbp), %eax
	cmpl	%eax, -28(%rbp)
	jl	.L7
	movl	-20(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movq	-16(%rbp), %rax
	movq	%rax, %rdi
	call	free@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L9
	call	__stack_chk_fail@PLT
.L9:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE6:
	.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
	.section	.rodata
.LC0:
	.string	"%d"
.LC1:
	.string	"%s"
.LC2:
	.string	"%d\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB6:
	.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
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	leaq	-32(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	-32(%rbp), %eax
	addl	$1, %eax
	cltq
	movq	%rax, %rdi
	call	malloc@PLT
	movq	%rax, -16(%rbp)
	movq	-16(%rbp), %rax
	movq	%rax, %rsi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	__isoc99_scanf@PLT
	movl	$-1, -24(%rbp)
	movl	$0, -20(%rbp)
	movl	$0, -28(%rbp)
	jmp	.L2
.L7:
	movl	-28(%rbp), %eax
	movslq	%eax, %rdx
	movq	-16(%rbp), %rax
	addq	%rdx, %rax
	movzbl	(%rax), %eax
	cmpb	$79, %al
	jne	.L3
	cmpl	$1, -24(%rbp)
	jne	.L4
	addl	$1, -20(%rbp)
	movl	$-1, -24(%rbp)
	jmp	.L5
.L4:
	movl	$0, -24(%rbp)
	jmp	.L5
.L3:
	cmpl	$0, -24(%rbp)
	jne	.L6
	addl	$1, -20(%rbp)
	movl	$-1, -24(%rbp)
	jmp	.L5
.L6:
	movl	$1, -24(%rbp)
.L5:
	addl	$1, -28(%rbp)
.L2:
	movl	-32(%rbp), %eax
	cmpl	%eax, -28(%rbp)
	jl	.L7
	movl	-20(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC2(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movq	-16(%rbp), %rax
	movq	%rax, %rdi
	call	free@PLT
	movl	$0, %eax
	movq	-8(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L9
	call	__stack_chk_fail@PLT
.L9:
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE6:
	.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:
