#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 /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, const char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: ./recover infile \n");
    return 1;
  }
  const char *inFileName = argv[1];
  FILE *inFilePointer = fopen(inFileName, "r");
  if (inFilePointer == NULL) {
    fprintf(stderr, "Could not open %s.\n", inFileName);
    return 2;
  }
  FILE *outFilePointer = NULL;
  bool iCameAfterTheHeader = false;
  int fileNumber = 0;
  while (true) {
    unsigned char buffer[512];
    int bytesRead = fread(buffer, 1, 512, inFilePointer);
    if (bytesRead < 512) {
      break;
    }
    bool iFoundTheHeader = (buffer[0] == 0xff && buffer[1] == 0xd8 &&
                            buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0);
    if (iFoundTheHeader) {
      char outFileName[8];
      sprintf(outFileName, "%00i.jpg", fileNumber);
      outFilePointer = fopen(outFileName, "w");
      if (outFilePointer == NULL) {
        fprintf(stderr, "Could not open %s.\n", outFileName);
        return 3;
      }
      fileNumber = fileNumber + 1;
    }
    bool iShouldWriteToTheOutputFile =
        (iFoundTheHeader) || (iCameAfterTheHeader);
    if (iShouldWriteToTheOutputFile) {
      fwrite(buffer, 1, 512, outFilePointer);
      iCameAfterTheHeader = true;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1196 <main+0x46>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1394 <main+0x244>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe6d(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e8 <main+0x98>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1394 <main+0x244>
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1030 <fread@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x200,-0x234(%rbp)
jge    1233 <main+0xe3>
jmp    138d <main+0x23d>
movzbl -0x230(%rbp),%ecx
xor    %eax,%eax
cmp    $0xff,%ecx
mov    %al,-0x245(%rbp)
jne    129e <main+0x14e>
movzbl -0x22f(%rbp),%ecx
xor    %eax,%eax
cmp    $0xd8,%ecx
mov    %al,-0x245(%rbp)
jne    129e <main+0x14e>
movzbl -0x22e(%rbp),%ecx
xor    %eax,%eax
cmp    $0xff,%ecx
mov    %al,-0x245(%rbp)
jne    129e <main+0x14e>
movzbl -0x22d(%rbp),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
sete   %al
mov    %al,-0x245(%rbp)
mov    -0x245(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x238(%rbp)
cmpl   $0x0,-0x238(%rbp)
je     1327 <main+0x1d7>
lea    -0x240(%rbp),%rdi
mov    -0x30(%rbp),%edx
lea    0xd63(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x240(%rbp),%rdi
lea    0xd59(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    131e <main+0x1ce>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
lea    -0x240(%rbp),%rdx
lea    0xd11(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
jmp    1394 <main+0x244>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    $0x1,%al
cmpl   $0x0,-0x238(%rbp)
mov    %al,-0x246(%rbp)
jne    1349 <main+0x1f9>
cmpl   $0x0,-0x2c(%rbp)
setne  %al
mov    %al,-0x246(%rbp)
mov    -0x246(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x244(%rbp)
cmpl   $0x0,-0x244(%rbp)
je     1388 <main+0x238>
lea    -0x230(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1070 <fwrite@plt>
movl   $0x1,-0x2c(%rbp)
jmp    11fe <main+0xae>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
cmp    $0x2,%edi
jne    11a3 <main+0x53>
mov    0x8(%rsi),%rbp
lea    0xea9(%rip),%rsi
mov    %rbp,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11c1 <main+0x71>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x200,%eax
jge    11f2 <main+0xa2>
xor    %eax,%eax
jmp    11e0 <main+0x90>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%eax
jmp    11e0 <main+0x90>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x2,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r13
xor    %ebp,%ebp
xor    %r12d,%r12d
jmp    1254 <main+0x104>
cs nopw 0x0(%rax,%rax,1)
test   %r12d,%r12d
je     12b0 <main+0x160>
mov    $0x1,%r12d
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1ff,%eax
jle    119f <main+0x4f>
cmpb   $0xff,0x10(%rsp)
jne    1210 <main+0xc0>
cmpb   $0xd8,0x11(%rsp)
jne    1210 <main+0xc0>
cmpb   $0xff,0x12(%rsp)
jne    1210 <main+0xc0>
movzbl 0x13(%rsp),%eax
and    $0xf0,%al
cmp    $0xe0,%al
jne    1210 <main+0xc0>
mov    %r15,%rdi
lea    0xdb2(%rip),%rsi
mov    %ebp,%edx
call   1060 <sprintf@plt>
mov    %r15,%rdi
lea    0xdaa(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     12b8 <main+0x168>
mov    %rax,%r14
add    $0x1,%ebp
jmp    1219 <main+0xc9>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %r12d,%r12d
jmp    1234 <main+0xe4>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
lea    0x8(%rsp),%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x3,%eax
jmp    11e0 <main+0x90>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"Usage: ./recover infile \0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"Could not open %s.\0A\00"
@.str.3 = [9 x i8] c"%00i.jpg\00"
@.str.4 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [512 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca [8 x i8]
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 2
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %99
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %7
%27 = load i32*, i32** %7
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %33
29: ; preds = %21
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %6
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %31)
store i32 2, i32* %3
br label %99
33: ; preds = %21
store i32* null, i32** %8
store i32 0, i32* %9
store i32 0, i32* %10
br label %34
34: ; preds = %33, %97
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%36 = load i32*, i32** %7
%37 = call i32 @fread(i8* %35, i32 1, i32 512, i32* %36)
store i32 %37, i32* %12
%38 = load i32, i32* %12
%39 = icmp slt i32 %38, 512
br i1 %39, label %40, label %41
40: ; preds = %34
br label %98
41: ; preds = %34
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = icmp eq i32 %44, 255
br i1 %45, label %46, label %62
46: ; preds = %41
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 1
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = icmp eq i32 %49, 216
br i1 %50, label %51, label %62
51: ; preds = %46
%52 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 2
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
%55 = icmp eq i32 %54, 255
br i1 %55, label %56, label %62
56: ; preds = %51
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 3
%58 = load i8, i8* %57
%59 = zext i8 %58 to i32
%60 = and i32 %59, 240
%61 = icmp eq i32 %60, 224
br label %62
62: ; preds = %56, %51, %46, %41
%63 = phi i1 [ false, %51 ], [ false, %46 ], [ false, %41 ], [ %61, %56 ]
%64 = zext i1 %63 to i32
store i32 %64, i32* %13
%65 = load i32, i32* %13
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %82
67: ; preds = %62
%68 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 0
%69 = load i32, i32* %10
%70 = call i32 @sprintf(i8* %68, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %69)
%71 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 0
%72 = call i32* @fopen(i8* %71, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %72, i32** %8
%73 = load i32*, i32** %8
%74 = icmp eq i32* %73, null
br i1 %74, label %75, label %79
75: ; preds = %67
%76 = load i32, i32* @stderr
%77 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 0
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %77)
store i32 3, i32* %3
br label %99
79: ; preds = %67
%80 = load i32, i32* %10
%81 = add nsw i32 %80, 1
store i32 %81, i32* %10
br label %82
82: ; preds = %79, %62
%83 = load i32, i32* %13
%84 = icmp ne i32 %83, 0
br i1 %84, label %88, label %85
85: ; preds = %82
%86 = load i32, i32* %9
%87 = icmp ne i32 %86, 0
br label %88
88: ; preds = %85, %82
%89 = phi i1 [ true, %82 ], [ %87, %85 ]
%90 = zext i1 %89 to i32
store i32 %90, i32* %15
%91 = load i32, i32* %15
%92 = icmp ne i32 %91, 0
br i1 %92, label %93, label %97
93: ; preds = %88
%94 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%95 = load i32*, i32** %8
%96 = call i32 @fwrite(i8* %94, i32 1, i32 512, i32* %95)
store i32 1, i32* %9
br label %97
97: ; preds = %93, %88
br label %34
98: ; preds = %40
store i32 0, i32* %3
br label %99
99: ; preds = %98, %75, %29, %18
%100 = load i32, i32* %3
ret i32 %100
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"Usage: ./recover infile \0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"Could not open %s.\0A\00"
@.str.3 = [9 x i8] c"%00i.jpg\00"
@.str.4 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = alloca [8 x i8]
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %64
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %23, label %14
14: ; preds = %9
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%16 = call i32 @fread(i8* %15, i32 1, i32 512, i32* %12)
%17 = icmp slt i32 %16, 512
br i1 %17, label %63, label %18
18: ; preds = %14
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 1
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 2
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 3
%22 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
br label %26
23: ; preds = %9
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %11)
br label %64
26: ; preds = %18, %57
%27 = phi i32* [ null, %18 ], [ %60, %57 ]
%28 = phi i32 [ 0, %18 ], [ %59, %57 ]
%29 = phi i32 [ 0, %18 ], [ %58, %57 ]
%30 = load i8, i8* %15
%31 = icmp eq i8 %30, -1
%32 = load i8, i8* %19
%33 = icmp eq i8 %32, -40
%34 = select i1 %31, i1 %33, i1 false
%35 = load i8, i8* %20
%36 = icmp eq i8 %35, -1
%37 = select i1 %34, i1 %36, i1 false
br i1 %37, label %38, label %51
38: ; preds = %26
%39 = load i8, i8* %21
%40 = and i8 %39, -16
%41 = icmp eq i8 %40, -32
br i1 %41, label %42, label %51
42: ; preds = %38
%43 = call i32 @sprintf(i8* %22, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%44 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %64
49: ; preds = %42
%50 = add nsw i32 %28, 1
br label %53
51: ; preds = %26, %38
%52 = icmp eq i32 %29, 0
br i1 %52, label %57, label %53
53: ; preds = %49, %51
%54 = phi i32* [ %44, %49 ], [ %27, %51 ]
%55 = phi i32 [ %50, %49 ], [ %28, %51 ]
%56 = call i32 @fwrite(i8* %15, i32 1, i32 512, i32* %54)
br label %57
57: ; preds = %53, %51
%58 = phi i32 [ 1, %53 ], [ 0, %51 ]
%59 = phi i32 [ %55, %53 ], [ %28, %51 ]
%60 = phi i32* [ %54, %53 ], [ %27, %51 ]
%61 = call i32 @fread(i8* %15, i32 1, i32 512, i32* %12)
%62 = icmp slt i32 %61, 512
br i1 %62, label %63, label %26
63: ; preds = %57, %14
br label %64
64: ; preds = %46, %23, %63, %6
%65 = phi i32 [ 1, %6 ], [ 2, %23 ], [ 0, %63 ], [ 3, %46 ]
ret i32 %65
}