#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 */
int /*<<< orphan*/ mprintf(char *, int);
scalar_t__ mread(char *, int);
int main() {
  char buffer = 0;
  int floor = 0;
  int count = 0;
  while (mread(&buffer, 1) > 0) {
    if (buffer == '(') {
      floor += 1;
    }
    if (buffer == ')') {
      floor -= 1;
    }
    count += 1;
    if (floor < 0) {
      break;
    }
  }
  mprintf("Santa enters the basement at position: %d\n", count);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x0,-0x5(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x5(%rbp),%rdi
mov    $0x1,%esi
call   1040 <mread@plt>
cmp    $0x0,%rax
jle    11a2 <main+0x82>
movsbl -0x5(%rbp),%eax
cmp    $0x28,%eax
jne    116f <main+0x4f>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movsbl -0x5(%rbp),%eax
cmp    $0x29,%eax
jne    1185 <main+0x65>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    119d <main+0x7d>
jmp    11a2 <main+0x82>
jmp    1141 <main+0x21>
mov    -0x10(%rbp),%esi
lea    0xe54(%rip),%rdi
call   1030 <mprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movb   $0x0,0xf(%rsp)
xor    %ebp,%ebp
lea    0xf(%rsp),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x1,%esi
call   1040 <mread@plt>
test   %rax,%rax
jle    116e <main+0x4e>
movzbl 0xf(%rsp),%eax
xor    %ecx,%ecx
cmp    $0x28,%al
sete   %cl
add    %ecx,%ebp
xor    %ecx,%ecx
cmp    $0x29,%al
sete   %cl
add    $0x1,%ebx
sub    %ecx,%ebp
jns    1140 <main+0x20>
lea    0xe8b(%rip),%rdi
mov    %ebx,%esi
call   1030 <mprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [43 x i8] c"Santa enters the basement at position: %d\0A\00"
define i32 @main() {
%1 = alloca i8
store i8 0, i8* %1
br label %2
2: ; preds = %7, %0
%3 = phi i32 [ 0, %0 ], [ %14, %7 ]
%4 = phi i32 [ 0, %0 ], [ %15, %7 ]
%5 = call i64 @mread(i8* %1, i32 1)
%6 = icmp sgt i64 %5, 0
br i1 %6, label %7, label %17
7: ; preds = %2
%8 = load i8, i8* %1
%9 = icmp eq i8 %8, 40
%10 = zext i1 %9 to i32
%11 = add nuw nsw i32 %3, %10
%12 = icmp eq i8 %8, 41
%13 = sext i1 %12 to i32
%14 = add nsw i32 %11, %13
%15 = add nuw nsw i32 %4, 1
%16 = icmp slt i32 %14, 0
br i1 %16, label %17, label %2
17: ; preds = %7, %2
%18 = phi i32 [ %15, %7 ], [ %4, %2 ]
%19 = call i32 @mprintf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %18)
ret i32 0
}