#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*/ printf(char *, long);
int main(int args, char **argv) {
  FILE *fp;
  int floors = 0;
  int i = 0;
  long basement_position = 0;
  char input = 'a';
  fp = fopen("./input.txt", "r");
  for (i = 1; input != EOF; i++) {
    input = fgetc(fp);
    if (input == '(') {
      floors += 1;
    } else if (input == ')') {
      floors -= 1;
    } else {
    }
    if (!basement_position) {
      if (floors == -1) {
        basement_position = i;
      }
    }
  }
  fclose(fp);
  printf("Santa is at floor %d.", floors);
  printf("\nSanta reaches the basement at character %ld.", basement_position);
  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)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movb   $0x61,-0x29(%rbp)
lea    0xe89(%rip),%rdi
lea    0xe8e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
movsbl -0x29(%rbp),%eax
mov    0x2e4f(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1225 <main+0xe5>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0x28,%eax
jne    11cb <main+0x8b>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f0 <main+0xb0>
movsbl -0x29(%rbp),%eax
cmp    $0x29,%eax
jne    11e6 <main+0xa6>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11eb <main+0xab>
jmp    11eb <main+0xab>
jmp    11f0 <main+0xb0>
cmpq   $0x0,-0x28(%rbp)
jne    1212 <main+0xd2>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    120d <main+0xcd>
movslq -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1212 <main+0xd2>
jmp    1217 <main+0xd7>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    118e <main+0x4e>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movslq -0x1c(%rbp),%rsi
lea    0xdd5(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xddb(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
lea    0xeb0(%rip),%rdi
lea    0xeb5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    0x2e82(%rip),%r12
cmpb   $0x61,(%r12)
jne    11a5 <main+0x65>
xor    %r15d,%r15d
xor    %r13d,%r13d
mov    %r14,%rdi
call   1030 <fclose@plt>
movslq %r15d,%rsi
lea    0xe89(%rip),%rdi
call   1040 <printf@plt>
lea    0xe93(%rip),%rdi
mov    %r13,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
xor    %r13d,%r13d
mov    $0x1,%ebx
xor    %r15d,%r15d
jmp    11e0 <main+0xa0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0xffffffff,%r15d
cmp    $0xffffffff,%r15d
mov    %r13,%rcx
cmove  %rbx,%rcx
test   %r13,%r13
cmove  %rcx,%r13
add    $0x1,%rbx
cmp    (%r12),%al
je     1173 <main+0x33>
mov    %r14,%rdi
call   1050 <fgetc@plt>
cmp    $0x29,%al
je     11c0 <main+0x80>
cmp    $0x28,%al
jne    11c4 <main+0x84>
add    $0x1,%r15d
jmp    11c4 <main+0x84>

LLVM IR (-O0):
@.str = [12 x i8] c"./input.txt\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.2 = [22 x i8] c"Santa is at floor %d.\00"
@.str.3 = [46 x i8] c"\0ASanta reaches the basement at character %ld.\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 i64
%10 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i32 0, i32* %8
store i64 0, i64* %9
store i8 97, i8* %10
%11 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %6
store i32 1, i32* %8
br label %12
12: ; preds = %47, %2
%13 = load i8, i8* %10
%14 = sext i8 %13 to i32
%15 = load i8, i8* @EOF
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %14, %16
br i1 %17, label %18, label %50
18: ; preds = %12
%19 = load i32*, i32** %6
%20 = call signext i8 @fgetc(i32* %19)
store i8 %20, i8* %10
%21 = load i8, i8* %10
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 40
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
br label %36
27: ; preds = %18
%28 = load i8, i8* %10
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 41
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* %7
%33 = sub nsw i32 %32, 1
store i32 %33, i32* %7
br label %35
34: ; preds = %27
br label %35
35: ; preds = %34, %31
br label %36
36: ; preds = %35, %24
%37 = load i64, i64* %9
%38 = icmp ne i64 %37, 0
br i1 %38, label %46, label %39
39: ; preds = %36
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
store i64 %44, i64* %9
br label %45
45: ; preds = %42, %39
br label %46
46: ; preds = %45, %36
br label %47
47: ; preds = %46
%48 = load i32, i32* %8
%49 = add nsw i32 %48, 1
store i32 %49, i32* %8
br label %12
50: ; preds = %12
%51 = load i32*, i32** %6
%52 = call i32 @fclose(i32* %51)
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %54)
%56 = load i64, i64* %9
%57 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i64 %56)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"./input.txt\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.2 = [22 x i8] c"Santa is at floor %d.\00"
@.str.3 = [46 x i8] c"\0ASanta reaches the basement at character %ld.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = load i8, i8* @EOF
%5 = icmp eq i8 %4, 97
br i1 %5, label %24, label %6
6: ; preds = %2, %15
%7 = phi i64 [ %21, %15 ], [ 1, %2 ]
%8 = phi i64 [ %20, %15 ], [ 0, %2 ]
%9 = phi i32 [ %16, %15 ], [ 0, %2 ]
%10 = tail call signext i8 @fgetc(i32* %3)
switch i8 %10, label %15 [
i8 40, label %11
i8 41, label %13
]
11: ; preds = %6
%12 = add nsw i32 %9, 1
br label %15
13: ; preds = %6
%14 = add nsw i32 %9, -1
br label %15
15: ; preds = %6, %13, %11
%16 = phi i32 [ %12, %11 ], [ %14, %13 ], [ %9, %6 ]
%17 = icmp eq i64 %8, 0
%18 = icmp eq i32 %16, -1
%19 = select i1 %17, i1 %18, i1 false
%20 = select i1 %19, i64 %7, i64 %8
%21 = add nuw i64 %7, 1
%22 = load i8, i8* @EOF
%23 = icmp eq i8 %10, %22
br i1 %23, label %24, label %6
24: ; preds = %15, %2
%25 = phi i32 [ 0, %2 ], [ %16, %15 ]
%26 = phi i64 [ 0, %2 ], [ %20, %15 ]
%27 = tail call i32 @fclose(i32* %3)
%28 = sext i32 %25 to i64
%29 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %28)
%30 = tail call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i64 %26)
ret i32 0
}