#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 uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: ./recover image\n");
    return 1;
  }
  FILE *inptr = fopen(argv[1], "r");
  if (inptr == NULL) {
    fprintf(stderr, "Could not open card.raw.\n");
    return 2;
  }
  uint8_t buffer[512];
  int counter = 0;
  FILE *outptr = NULL;
  while (fread(buffer, 512, 1, inptr)) {
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff &&
        (buffer[3] & 0xf0) == 0xe0) {
      if (outptr != NULL) {
        fclose(outptr);
      }
      char filename[8];
      sprintf(filename, "%03i.jpg", counter);
      outptr = fopen(filename, "w");
      counter++;
    }
    if (outptr != NULL) {
      fwrite(&buffer, 512, 1, outptr);
    }
  }
  fclose(inptr);
  fclose(outptr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a4 <main+0x44>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130f <main+0x1af>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe65(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e8 <main+0x88>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    130f <main+0x1af>
movl   $0x0,-0x824(%rbp)
movq   $0x0,-0x830(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
je     12f3 <main+0x193>
cmpl   $0xff,-0x820(%rbp)
jne    12c3 <main+0x163>
cmpl   $0xd8,-0x81c(%rbp)
jne    12c3 <main+0x163>
cmpl   $0xff,-0x818(%rbp)
jne    12c3 <main+0x163>
mov    -0x814(%rbp),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
jne    12c3 <main+0x163>
cmpq   $0x0,-0x830(%rbp)
je     1281 <main+0x121>
mov    -0x830(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x838(%rbp),%rdi
mov    -0x824(%rbp),%edx
lea    0xd9f(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x838(%rbp),%rdi
lea    0xd95(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x830(%rbp)
mov    -0x824(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x824(%rbp)
cmpq   $0x0,-0x830(%rbp)
je     12ee <main+0x18e>
lea    -0x820(%rbp),%rdi
mov    -0x830(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
jmp    11fd <main+0x9d>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x830(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
cmp    $0x2,%edi
jne    1272 <main+0x112>
mov    0x8(%rsi),%rdi
lea    0xe93(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     128e <main+0x12e>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     12aa <main+0x14a>
mov    $0xf0,%r13d
lea    0x8(%rsp),%r12
lea    0x10(%rsp),%rbp
xor    %r15d,%r15d
jmp    11ee <main+0x8e>
xchg   %ax,%ax
mov    %rbp,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     12ad <main+0x14d>
cmpl   $0xff,0x10(%rsp)
jne    124f <main+0xef>
cmpl   $0xd8,0x14(%rsp)
jne    124f <main+0xef>
cmpl   $0xff,0x18(%rsp)
jne    124f <main+0xef>
mov    0x1c(%rsp),%eax
and    %r13d,%eax
cmp    $0xe0,%eax
jne    124f <main+0xef>
test   %r15,%r15
je     1227 <main+0xc7>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
lea    0xe03(%rip),%rsi
mov    %r14d,%edx
call   1070 <sprintf@plt>
mov    %r12,%rdi
lea    0xdfa(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
add    $0x1,%r14d
test   %r15,%r15
je     11d0 <main+0x70>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
jmp    11d0 <main+0x70>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xd7e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    12bf <main+0x15f>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%eax
jmp    12bf <main+0x15f>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Usage: ./recover image\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"Could not open card.raw.\0A\00"
@.str.3 = [9 x i8] c"%03i.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 i32*
%7 = alloca [512 x i32]
%8 = alloca i32
%9 = alloca i32*
%10 = alloca [8 x i8]
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 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %76
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
%21 = load i32*, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %3
br label %76
26: ; preds = %16
store i32 0, i32* %8
store i32* null, i32** %9
br label %27
27: ; preds = %70, %26
%28 = getelementptr inbounds [512 x i32], [512 x i32]* %7, i64 0, i64 0
%29 = load i32*, i32** %6
%30 = call i64 @fread(i32* %28, i32 512, i32 1, i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %71
32: ; preds = %27
%33 = getelementptr inbounds [512 x i32], [512 x i32]* %7, i64 0, i64 0
%34 = load i32, i32* %33
%35 = icmp eq i32 %34, 255
br i1 %35, label %36, label %63
36: ; preds = %32
%37 = getelementptr inbounds [512 x i32], [512 x i32]* %7, i64 0, i64 1
%38 = load i32, i32* %37
%39 = icmp eq i32 %38, 216
br i1 %39, label %40, label %63
40: ; preds = %36
%41 = getelementptr inbounds [512 x i32], [512 x i32]* %7, i64 0, i64 2
%42 = load i32, i32* %41
%43 = icmp eq i32 %42, 255
br i1 %43, label %44, label %63
44: ; preds = %40
%45 = getelementptr inbounds [512 x i32], [512 x i32]* %7, i64 0, i64 3
%46 = load i32, i32* %45
%47 = and i32 %46, 240
%48 = icmp eq i32 %47, 224
br i1 %48, label %49, label %63
49: ; preds = %44
%50 = load i32*, i32** %9
%51 = icmp ne i32* %50, null
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i32*, i32** %9
%54 = call i32 @fclose(i32* %53)
br label %55
55: ; preds = %52, %49
%56 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%57 = load i32, i32* %8
%58 = call i32 @sprintf(i8* %56, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %57)
%59 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%60 = call i32* @fopen(i8* %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %60, i32** %9
%61 = load i32, i32* %8
%62 = add nsw i32 %61, 1
store i32 %62, i32* %8
br label %63
63: ; preds = %55, %44, %40, %36, %32
%64 = load i32*, i32** %9
%65 = icmp ne i32* %64, null
br i1 %65, label %66, label %70
66: ; preds = %63
%67 = bitcast [512 x i32]* %7 to i32**
%68 = load i32*, i32** %9
%69 = call i32 @fwrite(i32** %67, i32 512, i32 1, i32* %68)
br label %70
70: ; preds = %66, %63
br label %27
71: ; preds = %27
%72 = load i32*, i32** %6
%73 = call i32 @fclose(i32* %72)
%74 = load i32*, i32** %9
%75 = call i32 @fclose(i32* %74)
store i32 0, i32* %3
br label %76
76: ; preds = %71, %23, %13
%77 = load i32, i32* %3
ret i32 %77
}

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