#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 checkCellar(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
char *stderr;
scalar_t__ upOrDown(int);
int main(int argc, char *argv[]) {
  int floor = 0;
  int instructionIndex = 0;
  int instruction;
  int isCellar = 0;
  FILE *inputFile;
  inputFile = fopen(argv[1], "r");
  if (inputFile == NULL) {
    printf(stderr, "File not valid for reading you christmas toad.\n");
    exit(-1);
  }
  while ((instruction = fgetc(inputFile)) != EOF && isCellar == 0) {
    instructionIndex++;
    floor += upOrDown(instruction);
    isCellar = checkCellar(floor);
  }
  fclose(inputFile);
  if (isCellar == 1)
    printf("\nThe instruction that traps him in the basement is:  %i\n",
           instructionIndex);
  else
    printf("\nAfter %i instructions Crimbo never tickled the underbelly.\n",
           instructionIndex);
}


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,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe56(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e0 <main+0x70>
mov    0x2e13(%rip),%rax
mov    (%rax),%rdi
lea    0xe33(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    11e5 <main+0x75>
mov    -0x28(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
mov    0x2dec(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x29(%rbp)
je     1211 <main+0xa1>
cmpl   $0x0,-0x20(%rbp)
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1221 <main+0xb1>
jmp    124f <main+0xdf>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <upOrDown@plt>
mov    %rax,%rcx
movslq -0x14(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <checkCellar@plt>
mov    %eax,-0x20(%rbp)
jmp    11e5 <main+0x75>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x1,-0x20(%rbp)
jne    1278 <main+0x108>
mov    -0x18(%rbp),%esi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1289 <main+0x119>
mov    -0x18(%rbp),%esi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
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
mov    0x8(%rsi),%rdi
lea    0xe7d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     121e <main+0xae>
mov    %rax,%r14
mov    %rax,%rdi
call   1070 <fgetc@plt>
mov    0x2e45(%rip),%r12
cmp    (%r12),%eax
jne    11b6 <main+0x46>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %r15d,%r15d
jmp    1202 <main+0x92>
xor    %ebx,%ebx
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
add    $0x1,%r15d
mov    %eax,%edi
call   1060 <upOrDown@plt>
movslq %ebx,%rbx
add    %rax,%rbx
mov    %ebx,%edi
call   1040 <checkCellar@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1070 <fgetc@plt>
cmp    (%r12),%eax
je     11ec <main+0x7c>
test   %ebp,%ebp
je     11c0 <main+0x50>
mov    %r14,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%ebp
jne    1202 <main+0x92>
lea    0xe32(%rip),%rdi
jmp    1209 <main+0x99>
lea    0xe62(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db3(%rip),%rax
mov    (%rax),%rdi
lea    0xdd3(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i8* null
@.str.1 = [48 x i8] c"File not valid for reading you christmas toad.\0A\00"
@EOF = global i32 0
@.str.2 = [57 x i8] c"\0AThe instruction that traps him in the basement is: %i\0A\00"
@.str.3 = [61 x i8] c"\0AAfter %i instructions Crimbo never tickled the underbelly.\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
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %9
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i8*, i8** @stderr
%19 = call i32 (i8*, ...) @printf(i8* %18, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %2
br label %22
22: ; preds = %32, %21
%23 = load i32*, i32** %10
%24 = call i32 @fgetc(i32* %23)
store i32 %24, i32* %8
%25 = load i32, i32* @EOF
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, 0
br label %30
30: ; preds = %27, %22
%31 = phi i1 [ false, %22 ], [ %29, %27 ]
br i1 %31, label %32, label %43
32: ; preds = %30
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
%35 = load i32, i32* %8
%36 = call i64 @upOrDown(i32 %35)
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = add nsw i64 %38, %36
%40 = trunc i64 %39 to i32
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = call i32 @checkCellar(i32 %41)
store i32 %42, i32* %9
br label %22
43: ; preds = %30
%44 = load i32*, i32** %10
%45 = call i32 @fclose(i32* %44)
%46 = load i32, i32* %9
%47 = icmp eq i32 %46, 1
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* %7
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i32 %49)
br label %54
51: ; preds = %43
%52 = load i32, i32* %7
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %51, %48
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i8* null
@.str.1 = [48 x i8] c"File not valid for reading you christmas toad.\0A\00"
@EOF = global i32 0
@.str.2 = [57 x i8] c"\0AThe instruction that traps him in the basement is: %i\0A\00"
@.str.3 = [61 x i8] c"\0AAfter %i instructions Crimbo never tickled the underbelly.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = tail call i32 @fgetc(i32* %5)
%9 = load i32, i32* @EOF
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %17
11: ; preds = %7
%12 = tail call i32 @fclose(i32* %5)
br label %38
13: ; preds = %2
%14 = load i8*, i8** @stderr
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %14, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %7, %17
%18 = phi i32 [ %28, %17 ], [ %8, %7 ]
%19 = phi i64 [ %25, %17 ], [ 0, %7 ]
%20 = phi i32 [ %21, %17 ], [ 0, %7 ]
%21 = add nuw nsw i32 %20, 1
%22 = tail call i64 @upOrDown(i32 %18)
%23 = shl i64 %19, 32
%24 = ashr exact i64 %23, 32
%25 = add nsw i64 %22, %24
%26 = trunc i64 %25 to i32
%27 = tail call i32 @checkCellar(i32 %26)
%28 = tail call i32 @fgetc(i32* %5)
%29 = load i32, i32* @EOF
%30 = icmp ne i32 %28, %29
%31 = icmp eq i32 %27, 0
%32 = select i1 %30, i1 %31, i1 false
br i1 %32, label %17, label %33
33: ; preds = %17
%34 = icmp eq i32 %27, 1
%35 = tail call i32 @fclose(i32* %5)
br i1 %34, label %36, label %38
36: ; preds = %33
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %41
38: ; preds = %11, %33
%39 = phi i32 [ 0, %11 ], [ %21, %33 ]
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %38, %36
ret i32 0
}