#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*/ O_RDONLY;
int STDIN_FILENO;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char **, int);
int tryBody(int, char *, int, char *, int *, int);
int tryHeader(char *, int, char *, int *, int *);
int main(int argc, char const *argv[]) {
  int headerFilePointer;
  headerFilePointer = open("SIGNATURE.CR2", O_RDONLY);
  if (headerFilePointer == -1) {
    perror("Couldn't read signature");
    return 2;
  }
  char header[12];
  int headerCursor = 0;
  if (read(headerFilePointer, &header, 12) != 12) {
    perror("Not enough header bytes");
    return 3;
  }
  close(headerFilePointer);
  char closer[] = {0xff, 0xd9};
  int closerCursor = 0;
  int readState = 0;
  char inBytes[8000000];
  int byteCount;
  long fileSize = 0;
  int targetFilePointer = 0;
  long totalBytes;
  int add;
  int sad;
  int i;
  while ((byteCount = read(STDIN_FILENO, &inBytes, 8000000)) > 0) {
    for (i = 0; i < byteCount; i++) {
      switch (readState) {
      case 2:
      case 3:
      case 4:
      case 5:
        readState = tryBody(readState, inBytes, i, closer, &closerCursor,
                            targetFilePointer);
        break;
      default:
        readState =
            tryHeader(inBytes, i, header, &headerCursor, &targetFilePointer);
        break;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x7a1260,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b3 <main+0x53>
lea    0xe6c(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12f0 <main+0x190>
movl   $0x0,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1050 <read@plt>
cmp    $0xc,%eax
je     11ec <main+0x8c>
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    12f0 <main+0x190>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0xe43(%rip),%ax
mov    %ax,-0x26(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movq   $0x0,-0x7a1240(%rbp)
movl   $0x0,-0x7a1244(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    -0x7a1230(%rbp),%rsi
mov    $0x7a1200,%edx
call   1050 <read@plt>
mov    %eax,-0x7a1234(%rbp)
cmp    $0x0,%eax
jle    12e9 <main+0x189>
movl   $0x0,-0x7a125c(%rbp)
mov    -0x7a125c(%rbp),%eax
cmp    -0x7a1234(%rbp),%eax
jge    12e4 <main+0x184>
mov    -0x30(%rbp),%eax
add    $0xfffffffe,%eax
sub    $0x3,%eax
ja     12a7 <main+0x147>
jmp    127b <main+0x11b>
mov    -0x30(%rbp),%edi
lea    -0x7a1230(%rbp),%rsi
mov    -0x7a125c(%rbp),%edx
lea    -0x26(%rbp),%rcx
mov    -0x7a1244(%rbp),%r9d
lea    -0x2c(%rbp),%r8
call   1060 <tryBody@plt>
mov    %eax,-0x30(%rbp)
jmp    12cb <main+0x16b>
lea    -0x7a1230(%rbp),%rdi
mov    -0x7a125c(%rbp),%esi
lea    -0x20(%rbp),%rdx
lea    -0x24(%rbp),%rcx
lea    -0x7a1244(%rbp),%r8
call   1030 <tryHeader@plt>
mov    %eax,-0x30(%rbp)
jmp    12d0 <main+0x170>
mov    -0x7a125c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7a125c(%rbp)
jmp    1255 <main+0xf5>
jmp    1222 <main+0xc2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x7a1260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x7a1228,%rsp
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     129e <main+0x13e>
mov    %eax,%ebx
movl   $0x0,0x10(%rsp)
lea    0x14(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1050 <read@plt>
cmp    $0xc,%eax
jne    12b1 <main+0x151>
mov    %ebx,%edi
call   1040 <close@plt>
movw   $0xd9ff,0x6(%rsp)
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0x20(%rsp),%rsi
mov    $0x7a1200,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    128a <main+0x12a>
mov    %eax,%ebp
xor    %r15d,%r15d
lea    0x20(%rsp),%rbx
lea    0x10(%rsp),%r13
lea    0x8(%rsp),%r14
jmp    122c <main+0xcc>
nopw   0x0(%rax,%rax,1)
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    $0x7a1200,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    128a <main+0x12a>
xor    %r12d,%r12d
jmp    1262 <main+0x102>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12d,%esi
lea    0x14(%rsp),%rdx
mov    %r13,%rcx
mov    %r14,%r8
call   1030 <tryHeader@plt>
mov    %eax,%r15d
add    $0x1,%r12d
cmp    %r12d,%ebp
je     1210 <main+0xb0>
lea    -0x2(%r15),%eax
cmp    $0x3,%eax
ja     1240 <main+0xe0>
mov    0x8(%rsp),%r9d
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
lea    0x6(%rsp),%rcx
lea    0xc(%rsp),%r8
call   1060 <tryBody@plt>
jmp    1256 <main+0xf6>
xor    %eax,%eax
add    $0x7a1228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd69(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%eax
jmp    128c <main+0x12c>
lea    0xd6e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%eax
jmp    128c <main+0x12c>

LLVM IR (-O0):
@.str = [14 x i8] c"SIGNATURE.CR2\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"Couldn't read signature\00"
@.str.2 = [24 x i8] c"Not enough header bytes\00"
@__const.main.closer = [2 x i8] c"\FF\D9"
@STDIN_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [12 x i8]
%8 = alloca i32
%9 = alloca [2 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca [8000000 x i8]
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %67
26: ; preds = %2
store i32 0, i32* %8
%27 = load i32, i32* %6
%28 = bitcast [12 x i8]* %7 to i8**
%29 = call i32 @read(i32 %27, i8** %28, i32 12)
%30 = icmp ne i32 %29, 12
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %3
br label %67
33: ; preds = %26
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
%36 = bitcast [2 x i8]* %9 to i8*
store i32 0, i32* %10
store i32 0, i32* %11
store i64 0, i64* %14
store i32 0, i32* %15
br label %37
37: ; preds = %65, %33
%38 = load i32, i32* @STDIN_FILENO
%39 = bitcast [8000000 x i8]* %12 to i8**
%40 = call i32 @read(i32 %38, i8** %39, i32 8000000)
store i32 %40, i32* %13
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %66
42: ; preds = %37
store i32 0, i32* %19
br label %43
43: ; preds = %62, %42
%44 = load i32, i32* %19
%45 = load i32, i32* %13
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %65
47: ; preds = %43
%48 = load i32, i32* %11
switch i32 %48, label %56 [
i32 2, label %49
i32 3, label %49
i32 4, label %49
i32 5, label %49
]
49: ; preds = %47, %47, %47, %47
%50 = load i32, i32* %11
%51 = getelementptr inbounds [8000000 x i8], [8000000 x i8]* %12, i64 0, i64 0
%52 = load i32, i32* %19
%53 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%54 = load i32, i32* %15
%55 = call i32 @tryBody(i32 %50, i8* %51, i32 %52, i8* %53, i32* %10, i32 %54)
store i32 %55, i32* %11
br label %61
56: ; preds = %47
%57 = getelementptr inbounds [8000000 x i8], [8000000 x i8]* %12, i64 0, i64 0
%58 = load i32, i32* %19
%59 = getelementptr inbounds [12 x i8], [12 x i8]* %7, i64 0, i64 0
%60 = call i32 @tryHeader(i8* %57, i32 %58, i8* %59, i32* %8, i32* %15)
store i32 %60, i32* %11
br label %61
61: ; preds = %56, %49
br label %62
62: ; preds = %61
%63 = load i32, i32* %19
%64 = add nsw i32 %63, 1
store i32 %64, i32* %19
br label %43
65: ; preds = %43
br label %37
66: ; preds = %37
store i32 0, i32* %3
br label %67
67: ; preds = %66, %31, %24
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [14 x i8] c"SIGNATURE.CR2\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"Couldn't read signature\00"
@.str.2 = [24 x i8] c"Not enough header bytes\00"
@STDIN_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [12 x i8]
%4 = alloca i32
%5 = alloca i16
%6 = alloca i32
%7 = alloca [8000000 x i8]
%8 = alloca i32
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %56
14: ; preds = %2
%15 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%16 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%17 = bitcast [12 x i8]* %3 to i8**
%18 = call i32 @read(i32 %10, i8** %17, i32 12)
%19 = icmp eq i32 %18, 12
br i1 %19, label %22, label %20
20: ; preds = %14
%21 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %54
22: ; preds = %14
%23 = tail call i32 @close(i32 %10)
%24 = bitcast i16* %5 to i8*
store i16 -9729, i16* %5
%25 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%26 = getelementptr inbounds [8000000 x i8], [8000000 x i8]* %7, i64 0, i64 0
%27 = bitcast i32* %8 to i8*
store i32 0, i32* %8
%28 = bitcast [8000000 x i8]* %7 to i8**
%29 = load i32, i32* @STDIN_FILENO
%30 = call i32 @read(i32 %29, i8** %28, i32 8000000)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %36, label %53
32: ; preds = %49
%33 = load i32, i32* @STDIN_FILENO
%34 = call i32 @read(i32 %33, i8** %28, i32 8000000)
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %53
36: ; preds = %22, %32
%37 = phi i32 [ %34, %32 ], [ %30, %22 ]
%38 = phi i32 [ %50, %32 ], [ 0, %22 ]
br label %39
39: ; preds = %36, %49
%40 = phi i32 [ %51, %49 ], [ 0, %36 ]
%41 = phi i32 [ %50, %49 ], [ %38, %36 ]
%42 = add i32 %41, -2
%43 = icmp ult i32 %42, 4
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i32, i32* %8
%46 = call i32 @tryBody(i32 %41, i8* %26, i32 %40, i8* %24, i32* %6, i32 %45)
br label %49
47: ; preds = %39
%48 = call i32 @tryHeader(i8* %26, i32 %40, i8* %15, i32* %4, i32* %8)
br label %49
49: ; preds = %44, %47
%50 = phi i32 [ %48, %47 ], [ %46, %44 ]
%51 = add nuw nsw i32 %40, 1
%52 = icmp eq i32 %51, %37
br i1 %52, label %32, label %39
53: ; preds = %32, %22
br label %54
54: ; preds = %53, %20
%55 = phi i32 [ 3, %20 ], [ 0, %53 ]
br label %56
56: ; preds = %54, %12
%57 = phi i32 [ 2, %12 ], [ %55, %54 ]
ret i32 %57
}