#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 */

/* Variables and functions */
char EOF;
char getchar();
int /*<<< orphan*/ printf(char *, int);
int main() {
  int curFloor = 0;
  char curChar;
  int curInstruction = 1;
  while ((curChar = getchar()) != EOF) {
    if (curChar == '(') {
      ++curFloor;
    } else {
      --curFloor;
    }
    if (curFloor < 0) {
      break;
    }
    ++curInstruction;
  }
  printf("%d\n", curInstruction);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x9(%rbp)
movsbl %al,%eax
mov    0x2e97(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     119d <main+0x7d>
movsbl -0x9(%rbp),%eax
cmp    $0x28,%eax
jne    1177 <main+0x57>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1180 <main+0x60>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118f <main+0x6f>
jmp    119d <main+0x7d>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    113d <main+0x1d>
mov    -0x10(%rbp),%esi
lea    0xe59(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1040 <getchar@plt>
mov    0x2eb6(%rip),%r14
mov    $0x1,%ebp
cmp    (%r14),%al
je     116e <main+0x4e>
mov    $0x1,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %ecx,%ecx
cmp    $0x28,%al
sete   %cl
lea    (%rbx,%rcx,2),%ebx
add    $0xffffffff,%ebx
js     116e <main+0x4e>
add    $0x1,%ebp
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    (%r14),%al
jne    1150 <main+0x30>
lea    0xe8b(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i8 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 1, i32* %4
br label %5
5: ; preds = %25, %0
%6 = call signext i8 (...) @getchar()
store i8 %6, i8* %3
%7 = sext i8 %6 to i32
%8 = load i8, i8* @EOF
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %7, %9
br i1 %10, label %11, label %28
11: ; preds = %5
%12 = load i8, i8* %3
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 40
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
br label %21
18: ; preds = %11
%19 = load i32, i32* %2
%20 = add nsw i32 %19, -1
store i32 %20, i32* %2
br label %21
21: ; preds = %18, %15
%22 = load i32, i32* %2
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
br label %28
25: ; preds = %21
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %5
28: ; preds = %24, %5
%29 = load i32, i32* %4
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %29)
ret i32 0
}

LLVM IR (-O2):
@EOF = global i8 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = tail call signext i8 (...) @getchar()
%2 = load i8, i8* @EOF
%3 = icmp eq i8 %1, %2
br i1 %3, label %17, label %4
4: ; preds = %0, %12
%5 = phi i8 [ %14, %12 ], [ %1, %0 ]
%6 = phi i32 [ %13, %12 ], [ 1, %0 ]
%7 = phi i32 [ %10, %12 ], [ 0, %0 ]
%8 = icmp eq i8 %5, 40
%9 = select i1 %8, i32 1, i32 -1
%10 = add nsw i32 %9, %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %4
%13 = add nuw nsw i32 %6, 1
%14 = tail call signext i8 (...) @getchar()
%15 = load i8, i8* @EOF
%16 = icmp eq i8 %14, %15
br i1 %16, label %17, label %4
17: ; preds = %12, %4, %0
%18 = phi i32 [ 1, %0 ], [ %6, %4 ], [ %13, %12 ]
%19 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %18)
ret i32 0
}