#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  FILE *file;
  int c, floor, counter;
  if (argc < 2) {
    printf("Usage: %s <file>\n", argv[0]);
    return 0;
  }
  floor = 0;
  counter = 0;
  file = fopen(argv[1], "r");
  while ((c = getc(file)) != EOF) {
    counter++;
    if (((unsigned char)c) == '(')
      floor++;
    else if (((unsigned char)c) == ')')
      floor--;
    if (floor == -1) {
      printf("Santa reaches the basement at position: %d\n", counter);
    }
  }
  printf("He ends up on floor: %d\n", floor);
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1181 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1246 <main+0x106>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe74(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <getc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%eax
je     1225 <main+0xe5>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
movzbl %al,%eax
cmp    $0x28,%eax
jne    11e8 <main+0xa8>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1205 <main+0xc5>
mov    -0x1c(%rbp),%eax
movzbl %al,%eax
cmp    $0x29,%eax
jne    1200 <main+0xc0>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    1205 <main+0xc5>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1220 <main+0xe0>
mov    -0x24(%rbp),%esi
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a7 <main+0x67>
mov    -0x20(%rbp),%esi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x1,%edi
jg     1160 <main+0x20>
mov    (%rsi),%rsi
lea    0xea9(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11a2 <main+0x62>
mov    0x8(%rsi),%rdi
lea    0xea7(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <getc@plt>
mov    0x2e66(%rip),%r12
xor    %ebp,%ebp
cmp    (%r12),%eax
jne    11ad <main+0x6d>
lea    0xeaf(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%ebx
lea    0xe5b(%rip),%r15
xor    %ebp,%ebp
jmp    11d1 <main+0x91>
nopl   (%rax)
mov    %r14,%rdi
call   1060 <getc@plt>
add    $0x1,%ebx
cmp    (%r12),%eax
je     118a <main+0x4a>
cmp    $0x29,%al
je     11f0 <main+0xb0>
cmp    $0x28,%al
jne    11dc <main+0x9c>
add    $0x1,%ebp
cmp    $0xffffffff,%ebp
jne    11c0 <main+0x80>
jmp    11f8 <main+0xb8>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0xffffffff,%ebp
cmp    $0xffffffff,%ebp
jne    11c0 <main+0x80>
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c0 <main+0x80>

LLVM IR (-O0):
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.2 = [44 x i8] c"Santa reaches the basement at position: %d\0A\00"
@.str.3 = [25 x i8] c"He ends up on floor: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 0, i32* %3
br label %58
17: ; preds = %2
store i32 0, i32* %8
store i32 0, i32* %9
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %6
br label %22
22: ; preds = %52, %17
%23 = load i32*, i32** %6
%24 = call i32 @getc(i32* %23)
store i32 %24, i32* %7
%25 = load i32, i32* @EOF
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %53
27: ; preds = %22
%28 = load i32, i32* %9
%29 = add nsw i32 %28, 1
store i32 %29, i32* %9
%30 = load i32, i32* %7
%31 = trunc i32 %30 to i8
%32 = zext i8 %31 to i32
%33 = icmp eq i32 %32, 40
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
br label %46
37: ; preds = %27
%38 = load i32, i32* %7
%39 = trunc i32 %38 to i8
%40 = zext i8 %39 to i32
%41 = icmp eq i32 %40, 41
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i32, i32* %8
%44 = add nsw i32 %43, -1
store i32 %44, i32* %8
br label %45
45: ; preds = %42, %37
br label %46
46: ; preds = %45, %34
%47 = load i32, i32* %8
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* %9
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %49, %46
br label %22
53: ; preds = %22
%54 = load i32, i32* %8
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %54)
%56 = load i32*, i32** %6
%57 = call i32 @fclose(i32* %56)
store i32 0, i32* %3
br label %58
58: ; preds = %53, %12
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.2 = [44 x i8] c"Santa reaches the basement at position: %d\0A\00"
@.str.3 = [25 x i8] c"He ends up on floor: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %37
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @getc(i32* %10)
%12 = load i32, i32* @EOF
%13 = icmp eq i32 %11, %12
br i1 %13, label %33, label %14
14: ; preds = %7, %29
%15 = phi i32 [ %30, %29 ], [ %11, %7 ]
%16 = phi i32 [ %18, %29 ], [ 0, %7 ]
%17 = phi i32 [ %25, %29 ], [ 0, %7 ]
%18 = add nuw nsw i32 %16, 1
%19 = trunc i32 %15 to i8
switch i8 %19, label %24 [
i8 40, label %20
i8 41, label %22
]
20: ; preds = %14
%21 = add nsw i32 %17, 1
br label %24
22: ; preds = %14
%23 = add nsw i32 %17, -1
br label %24
24: ; preds = %14, %22, %20
%25 = phi i32 [ %21, %20 ], [ %23, %22 ], [ %17, %14 ]
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %29
29: ; preds = %27, %24
%30 = tail call i32 @getc(i32* %10)
%31 = load i32, i32* @EOF
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %14
33: ; preds = %29, %7
%34 = phi i32 [ 0, %7 ], [ %25, %29 ]
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = tail call i32 @fclose(i32* %10)
br label %37
37: ; preds = %33, %4
ret i32 0
}