#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *, char *);
int left_count(char *, int, int);
int /*<<< orphan*/ printf(char *, int, int);
int right_count(char *, int, int);
int main() {
  FILE *fin = fopen("beads.in", "r");
  FILE *fout = fopen("beads.out", "w");
  char necklace[400];
  int len;
  int i, cur_max, max = 2;
  fscanf(fin, "%d %s", &len, necklace);
  for (i = 0; i < len; i++) {
    cur_max = left_count(necklace, i, len) + right_count(necklace, i, len);
    if (cur_max > max) {
      max = cur_max;
    }
    printf("%d %d\n", i, cur_max);
  }
  if (max > len)
    max = len;
  fprintf(fout, "%d\n", max);
  fclose(fin);
  fclose(fout);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1d0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe6b(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x2,-0x1c0(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1b0(%rbp),%rcx
lea    0xe4b(%rip),%rsi
lea    -0x1b4(%rbp),%rdx
call   1060 <fscanf@plt>
movl   $0x0,-0x1b8(%rbp)
mov    -0x1b8(%rbp),%eax
cmp    -0x1b4(%rbp),%eax
jge    1284 <main+0x114>
lea    -0x1b0(%rbp),%rdi
mov    -0x1b8(%rbp),%esi
mov    -0x1b4(%rbp),%edx
call   1030 <left_count@plt>
mov    %eax,-0x1c4(%rbp)
lea    -0x1b0(%rbp),%rdi
mov    -0x1b8(%rbp),%esi
mov    -0x1b4(%rbp),%edx
call   1090 <right_count@plt>
mov    %eax,%ecx
mov    -0x1c4(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x1bc(%rbp)
mov    -0x1bc(%rbp),%eax
cmp    -0x1c0(%rbp),%eax
jle    1258 <main+0xe8>
mov    -0x1bc(%rbp),%eax
mov    %eax,-0x1c0(%rbp)
mov    -0x1b8(%rbp),%esi
mov    -0x1bc(%rbp),%edx
lea    0xdb2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1b8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1b8(%rbp)
jmp    11e2 <main+0x72>
mov    -0x1c0(%rbp),%eax
cmp    -0x1b4(%rbp),%eax
jle    12a2 <main+0x132>
mov    -0x1b4(%rbp),%eax
mov    %eax,-0x1c0(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c0(%rbp),%edx
lea    0xd6d(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x1d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1a8,%rsp
lea    0xe78(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
lea    0xe6d(%rip),%rdi
lea    0xe70(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r15
lea    0xe63(%rip),%rsi
lea    0x4(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %rbx,0x8(%rsp)
mov    %rbx,%rdi
call   1060 <fscanf@plt>
mov    0x4(%rsp),%edx
test   %edx,%edx
jle    122f <main+0xbf>
xor    %ebx,%ebx
mov    $0x2,%ebp
lea    0x10(%rsp),%r12
lea    0xe37(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %ebx,%esi
call   1030 <left_count@plt>
mov    %eax,%r14d
mov    0x4(%rsp),%edx
mov    %r12,%rdi
mov    %ebx,%esi
call   1090 <right_count@plt>
add    %eax,%r14d
cmp    %ebp,%r14d
cmovg  %r14d,%ebp
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r14d,%edx
call   1050 <printf@plt>
add    $0x1,%ebx
mov    0x4(%rsp),%edx
cmp    %edx,%ebx
jl     11f0 <main+0x80>
jmp    1234 <main+0xc4>
mov    $0x2,%ebp
cmp    %edx,%ebp
cmovg  %edx,%ebp
lea    0xde0(%rip),%rsi
mov    %r15,%rdi
mov    %ebp,%edx
call   1070 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x1a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"beads.in\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"beads.out\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [6 x i8] c"%d %s\00"
@.str.5 = [7 x i8] c"%d %d\0A\00"
@.str.6 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca [400 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %2
%10 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %10, i32** %3
store i32 2, i32* %8
%11 = load i32*, i32** %2
%12 = getelementptr inbounds [400 x i8], [400 x i8]* %4, i64 0, i64 0
%13 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %5, i8* %12)
store i32 0, i32* %6
br label %14
14: ; preds = %37, %0
%15 = load i32, i32* %6
%16 = load i32, i32* %5
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %40
18: ; preds = %14
%19 = getelementptr inbounds [400 x i8], [400 x i8]* %4, i64 0, i64 0
%20 = load i32, i32* %6
%21 = load i32, i32* %5
%22 = call i32 @left_count(i8* %19, i32 %20, i32 %21)
%23 = getelementptr inbounds [400 x i8], [400 x i8]* %4, i64 0, i64 0
%24 = load i32, i32* %6
%25 = load i32, i32* %5
%26 = call i32 @right_count(i8* %23, i32 %24, i32 %25)
%27 = add nsw i32 %22, %26
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = load i32, i32* %8
%30 = icmp sgt i32 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %18
%32 = load i32, i32* %7
store i32 %32, i32* %8
br label %33
33: ; preds = %31, %18
%34 = load i32, i32* %6
%35 = load i32, i32* %7
%36 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %34, i32 %35)
br label %37
37: ; preds = %33
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
store i32 %39, i32* %6
br label %14
40: ; preds = %14
%41 = load i32, i32* %8
%42 = load i32, i32* %5
%43 = icmp sgt i32 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = load i32, i32* %5
store i32 %45, i32* %8
br label %46
46: ; preds = %44, %40
%47 = load i32*, i32** %3
%48 = load i32, i32* %8
%49 = call i32 @fprintf(i32* %47, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %48)
%50 = load i32*, i32** %2
%51 = call i32 @fclose(i32* %50)
%52 = load i32*, i32** %3
%53 = call i32 @fclose(i32* %52)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"beads.in\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"beads.out\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [6 x i8] c"%d %s\00"
@.str.5 = [7 x i8] c"%d %d\0A\00"
@.str.6 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca [400 x i8]
%2 = alloca i32
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%5 = getelementptr inbounds [400 x i8], [400 x i8]* %1, i64 0, i64 0
%6 = bitcast i32* %2 to i8*
%7 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %2, i8* %5)
%8 = load i32, i32* %2
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %0, %10
%11 = phi i32 [ %22, %10 ], [ %8, %0 ]
%12 = phi i32 [ %19, %10 ], [ 2, %0 ]
%13 = phi i32 [ %21, %10 ], [ 0, %0 ]
%14 = call i32 @left_count(i8* %5, i32 %13, i32 %11)
%15 = load i32, i32* %2
%16 = call i32 @right_count(i8* %5, i32 %13, i32 %15)
%17 = add nsw i32 %16, %14
%18 = icmp sgt i32 %17, %12
%19 = select i1 %18, i32 %17, i32 %12
%20 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %13, i32 %17)
%21 = add nuw nsw i32 %13, 1
%22 = load i32, i32* %2
%23 = icmp slt i32 %21, %22
br i1 %23, label %10, label %24
24: ; preds = %10, %0
%25 = phi i32 [ 2, %0 ], [ %19, %10 ]
%26 = phi i32 [ %8, %0 ], [ %22, %10 ]
%27 = icmp sgt i32 %25, %26
%28 = select i1 %27, i32 %26, i32 %25
%29 = call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %28)
%30 = call i32 @fclose(i32* %3)
%31 = call i32 @fclose(i32* %4)
ret i32 0
}