#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;
  while (mread(&buffer, 1) > 0) {
    if (buffer == '(') {
      floor += 1;
    }
    if (buffer == ')') {
      floor -= 1;
    }
  }
  mprintf("Santa is on floor %d\n", floor);
  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)
lea    -0x5(%rbp),%rdi
mov    $0x1,%esi
call   1040 <mread@plt>
cmp    $0x0,%rax
jle    1183 <main+0x63>
movsbl -0x5(%rbp),%eax
cmp    $0x28,%eax
jne    1168 <main+0x48>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movsbl -0x5(%rbp),%eax
cmp    $0x29,%eax
jne    117e <main+0x5e>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113a <main+0x1a>
mov    -0xc(%rbp),%esi
lea    0xe73(%rip),%rdi
call   1030 <mprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
movb   $0x0,0x7(%rsp)
lea    0x7(%rsp),%rdi
mov    $0x1,%esi
call   1040 <mread@plt>
test   %rax,%rax
jle    117d <main+0x5d>
xor    %ecx,%ecx
lea    0x7(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl 0x7(%rsp),%eax
xor    %ebx,%ebx
cmp    $0x28,%al
sete   %bl
add    %ecx,%ebx
xor    %ecx,%ecx
cmp    $0x29,%al
sete   %cl
sub    %ecx,%ebx
mov    %r14,%rdi
mov    $0x1,%esi
call   1040 <mread@plt>
mov    %ebx,%ecx
test   %rax,%rax
jg     1150 <main+0x30>
jmp    117f <main+0x5f>
xor    %ebx,%ebx
lea    0xe7a(%rip),%rdi
mov    %ebx,%esi
call   1030 <mprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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