#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 */
char EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: Day01 <input file>\n");
    return 1;
  }
  FILE *fp = fopen(argv[1], "r");
  char c;
  int f = 0, enteredBasement = 0;
  for (int i = 1; (c = fgetc(fp)) != EOF; ++i) {
    switch (c) {
    case '(': {
      ++f;
    } break;
    case ')': {
      --f;
    } break;
    }
    if (!enteredBasement && f == -1) {
      fprintf(stdout,
              "%i is the position of the first character that forces Santa to "
              "enter the basement.\n",
              i);
      enteredBasement = 1;
    }
  }
  fclose(fp);
  fprintf(stdout, "Santa has to go to floor %i.\n", f);
  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)
je     1183 <main+0x43>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1280 <main+0x140>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe89(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x19(%rbp)
movsbl %al,%eax
mov    0x2e22(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1256 <main+0x116>
movsbl -0x19(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x28,%eax
je     11f7 <main+0xb7>
jmp    11e6 <main+0xa6>
mov    -0x2c(%rbp),%eax
sub    $0x29,%eax
je     1205 <main+0xc5>
jmp    120e <main+0xce>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    120e <main+0xce>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1243 <main+0x103>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1243 <main+0x103>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x24(%rbp)
jmp    1248 <main+0x108>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11b0 <main+0x70>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@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   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11a2 <main+0x62>
mov    0x8(%rsi),%rdi
lea    0xec0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1040 <fgetc@plt>
mov    0x2e76(%rip),%r13
mov    $0x0,%ebp
cmp    0x0(%r13),%al
jne    11cd <main+0x8d>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xedc(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
jmp    11be <main+0x7e>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%ebx
lea    0xe44(%rip),%r12
xor    %r14d,%r14d
xor    %ebp,%ebp
jmp    11f1 <main+0xb1>
add    $0x1,%ebx
mov    %r15,%rdi
call   1040 <fgetc@plt>
cmp    0x0(%r13),%al
je     117d <main+0x3d>
movsbl %al,%eax
cmp    $0x29,%eax
je     1210 <main+0xd0>
cmp    $0x28,%eax
jne    1201 <main+0xc1>
add    $0x1,%ebp
test   %r14d,%r14d
je     1218 <main+0xd8>
jmp    11e0 <main+0xa0>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%ebp
test   %r14d,%r14d
jne    11e0 <main+0xa0>
cmp    $0xffffffff,%ebp
jne    11e0 <main+0xa0>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%r14d
jmp    11e0 <main+0xa0>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: Day01 <input file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i8 0
@stdout = global i32 0
@.str.2 = [84 x i8] c"%i is the position of the first character that forces Santa to enter the basement.\0A\00"
@.str.3 = [30 x i8] c"Santa has to go to floor %i.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %57
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %6
store i32 0, i32* %8
store i32 0, i32* %9
store i32 1, i32* %10
br label %21
21: ; preds = %48, %16
%22 = load i32*, i32** %6
%23 = call signext i8 @fgetc(i32* %22)
store i8 %23, i8* %7
%24 = sext i8 %23 to i32
%25 = load i8, i8* @EOF
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %24, %26
br i1 %27, label %28, label %51
28: ; preds = %21
%29 = load i8, i8* %7
%30 = sext i8 %29 to i32
switch i32 %30, label %37 [
i32 40, label %31
i32 41, label %34
]
31: ; preds = %28
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
store i32 %33, i32* %8
br label %37
34: ; preds = %28
%35 = load i32, i32* %8
%36 = add nsw i32 %35, -1
store i32 %36, i32* %8
br label %37
37: ; preds = %28, %34, %31
%38 = load i32, i32* %9
%39 = icmp ne i32 %38, 0
br i1 %39, label %47, label %40
40: ; preds = %37
%41 = load i32, i32* %8
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i32, i32* @stdout
%45 = load i32, i32* %10
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.2, i64 0, i64 0), i32 %45)
store i32 1, i32* %9
br label %47
47: ; preds = %43, %40, %37
br label %48
48: ; preds = %47
%49 = load i32, i32* %10
%50 = add nsw i32 %49, 1
store i32 %50, i32* %10
br label %21
51: ; preds = %21
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
%54 = load i32, i32* @stdout
%55 = load i32, i32* %8
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %55)
store i32 0, i32* %3
br label %57
57: ; preds = %51, %13
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: Day01 <input file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i8 0
@stdout = global i32 0
@.str.2 = [84 x i8] c"%i is the position of the first character that forces Santa to enter the basement.\0A\00"
@.str.3 = [30 x i8] c"Santa has to go to floor %i.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %43
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 signext i8 @fgetc(i32* %10)
%12 = load i8, i8* @EOF
%13 = icmp eq i8 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %37, %7
%15 = phi i32 [ 0, %7 ], [ %30, %37 ]
%16 = tail call i32 @fclose(i32* %10)
%17 = load i32, i32* @stdout
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %15)
br label %43
19: ; preds = %7, %37
%20 = phi i8 [ %40, %37 ], [ %11, %7 ]
%21 = phi i32 [ %39, %37 ], [ 1, %7 ]
%22 = phi i32 [ %38, %37 ], [ 0, %7 ]
%23 = phi i32 [ %30, %37 ], [ 0, %7 ]
%24 = sext i8 %20 to i32
switch i32 %24, label %29 [
i32 40, label %25
i32 41, label %27
]
25: ; preds = %19
%26 = add nsw i32 %23, 1
br label %29
27: ; preds = %19
%28 = add nsw i32 %23, -1
br label %29
29: ; preds = %19, %27, %25
%30 = phi i32 [ %23, %19 ], [ %28, %27 ], [ %26, %25 ]
%31 = icmp eq i32 %22, 0
%32 = icmp eq i32 %30, -1
%33 = select i1 %31, i1 %32, i1 false
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i32, i32* @stdout
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %37
37: ; preds = %29, %34
%38 = phi i32 [ 1, %34 ], [ %22, %29 ]
%39 = add nuw nsw i32 %21, 1
%40 = tail call signext i8 @fgetc(i32* %10)
%41 = load i8, i8* @EOF
%42 = icmp eq i8 %40, %41
br i1 %42, label %14, label %19
43: ; preds = %14, %4
%44 = phi i32 [ 1, %4 ], [ 0, %14 ]
ret i32 %44
}