#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc != 1) {
    printf("Usage: %s\n", argv[0]);
    return 1;
  }
  char *inFileName = "card.raw";
  int imgsFound = 0;
  char magic[3] = {0xff, 0xd8, 0xff};
  char fourthByteLower = 0xe0;
  char fourthByteUpper = 0xef;
  FILE *inptr = fopen(inFileName, "r");
  if (inptr == NULL) {
    printf("Could not open %s.\n", inFileName);
    return 2;
  }
  FILE *outptr;
  char outFileName[7];
  char storage_buffer[512];
  while (fread(storage_buffer, 512, 1, inptr) == 1) {
    if (storage_buffer[0] == magic[0] && storage_buffer[1] == magic[1] &&
        storage_buffer[2] == magic[2] && storage_buffer[3] >= fourthByteLower &&
        storage_buffer[3] <= fourthByteUpper) {
      if (imgsFound != 0) {
        fclose(outptr);
      }
      sprintf(outFileName, "%03d.jpg", imgsFound);
      outptr = fopen(outFileName, "w");
      if (outptr == NULL) {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outFileName);
        return 3;
      }
      fwrite(storage_buffer, 512, 1, outptr);
      imgsFound++;
    } else if (imgsFound != 0) {
      fwrite(storage_buffer, 512, 1, outptr);
    }
  }
  fclose(inptr);
  fclose(outptr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
je     11b2 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5f(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1384 <main+0x214>
lea    0xe52(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0xe7e(%rip),%ax
mov    %ax,-0x1f(%rbp)
mov    0xe76(%rip),%al
mov    %al,-0x1d(%rbp)
movb   $0xe0,-0x20(%rbp)
movb   $0xef,-0x21(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    121b <main+0xab>
mov    -0x18(%rbp),%rsi
lea    0xe0c(%rip),%rdi
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1384 <main+0x214>
jmp    1220 <main+0xb0>
lea    -0x240(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    136b <main+0x1fb>
movsbl -0x240(%rbp),%eax
movsbl -0x1f(%rbp),%ecx
cmp    %ecx,%eax
jne    133d <main+0x1cd>
movsbl -0x23f(%rbp),%eax
movsbl -0x1e(%rbp),%ecx
cmp    %ecx,%eax
jne    133d <main+0x1cd>
movsbl -0x23e(%rbp),%eax
movsbl -0x1d(%rbp),%ecx
cmp    %ecx,%eax
jne    133d <main+0x1cd>
movsbl -0x23d(%rbp),%eax
movsbl -0x20(%rbp),%ecx
cmp    %ecx,%eax
jl     133d <main+0x1cd>
movsbl -0x23d(%rbp),%eax
movsbl -0x21(%rbp),%ecx
cmp    %ecx,%eax
jg     133d <main+0x1cd>
cmpl   $0x0,-0x1c(%rbp)
je     12b5 <main+0x145>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x3f(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xd67(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x3f(%rbp),%rdi
lea    0xd3f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1315 <main+0x1a5>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    -0x3f(%rbp),%rdx
lea    0xd2f(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
jmp    1384 <main+0x214>
lea    -0x240(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1366 <main+0x1f6>
cmpl   $0x0,-0x1c(%rbp)
je     1361 <main+0x1f1>
lea    -0x240(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
jmp    1366 <main+0x1f6>
jmp    1220 <main+0xb0>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%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    $0x1,%edi
jne    1297 <main+0x127>
lea    0xe7a(%rip),%rdi
lea    0xe7c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12c1 <main+0x151>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    12ad <main+0x13d>
xor    %ebp,%ebp
lea    0x9(%rsp),%r15
lea    0xe3b(%rip),%r12
lea    0x10(%rsp),%r13
jmp    121b <main+0xab>
xchg   %ax,%ax
test   %ebp,%ebp
je     1290 <main+0x120>
mov    %r13,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
mov    %r13,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    12ad <main+0x13d>
cmpb   $0xff,0x10(%rsp)
jne    11e0 <main+0x70>
cmpb   $0xd8,0x11(%rsp)
jne    11e0 <main+0x70>
cmpb   $0xff,0x12(%rsp)
jne    11e0 <main+0x70>
movzbl 0x13(%rsp),%eax
and    $0xf0,%al
cmp    $0xe0,%al
jne    11e0 <main+0x70>
test   %ebp,%ebp
je     1247 <main+0xd7>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
lea    0xdd9(%rip),%rsi
mov    %ebp,%edx
call   1080 <sprintf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12eb <main+0x17b>
mov    %rax,%r14
mov    %r13,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
add    $0x1,%ebp
jmp    11fd <main+0x8d>
nopl   0x0(%rax)
xor    %ebp,%ebp
jmp    11fd <main+0x8d>
mov    (%rsi),%rsi
lea    0xd5f(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    12d9 <main+0x169>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    12d9 <main+0x169>
lea    0xd4e(%rip),%rdi
lea    0xd3c(%rip),%rsi
call   1050 <printf@plt>
mov    $0x2,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
lea    0xd30(%rip),%rsi
lea    0x9(%rsp),%rdx
call   1060 <fprintf@plt>
mov    $0x3,%eax
jmp    12d9 <main+0x169>

LLVM IR (-O0):
@.str = [11 x i8] c"Usage: %s\0A\00"
@.str.1 = [9 x i8] c"card.raw\00"
@__const.main.magic = [3 x i8] c"\FF\D8\FF"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [20 x i8] c"Could not open %s.\0A\00"
@.str.4 = [9 x i8] c"%03d.jpg\00"
@.str.5 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.6 = [22 x i8] c"Could not create %s.\0A\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 [3 x i8]
%9 = alloca i8
%10 = alloca i8
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca [7 x i8]
%14 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 1
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %115
22: ; preds = %2
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %6
store i32 0, i32* %7
%23 = bitcast [3 x i8]* %8 to i8*
store i8 -32, i8* %9
store i8 -17, i8* %10
%24 = load i8*, i8** %6
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %11
%26 = load i32*, i32** %11
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i8*, i8** %6
%30 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %29)
store i32 2, i32* %3
br label %115
31: ; preds = %22
br label %32
32: ; preds = %109, %31
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 0
%34 = load i32*, i32** %11
%35 = call i32 @fread(i8* %33, i32 512, i32 1, i32* %34)
%36 = icmp eq i32 %35, 1
br i1 %36, label %37, label %110
37: ; preds = %32
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 0
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %40, %43
br i1 %44, label %45, label %101
45: ; preds = %37
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 1
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %48, %51
br i1 %52, label %53, label %101
53: ; preds = %45
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 2
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 2
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %56, %59
br i1 %60, label %61, label %101
61: ; preds = %53
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 3
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = load i8, i8* %9
%66 = sext i8 %65 to i32
%67 = icmp sge i32 %64, %66
br i1 %67, label %68, label %101
68: ; preds = %61
%69 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 3
%70 = load i8, i8* %69
%71 = sext i8 %70 to i32
%72 = load i8, i8* %10
%73 = sext i8 %72 to i32
%74 = icmp sle i32 %71, %73
br i1 %74, label %75, label %101
75: ; preds = %68
%76 = load i32, i32* %7
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i32*, i32** %12
%80 = call i32 @fclose(i32* %79)
br label %81
81: ; preds = %78, %75
%82 = getelementptr inbounds [7 x i8], [7 x i8]* %13, i64 0, i64 0
%83 = load i32, i32* %7
%84 = call i32 @sprintf(i8* %82, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %83)
%85 = getelementptr inbounds [7 x i8], [7 x i8]* %13, i64 0, i64 0
%86 = call i32* @fopen(i8* %85, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %86, i32** %12
%87 = load i32*, i32** %12
%88 = icmp eq i32* %87, null
br i1 %88, label %89, label %95
89: ; preds = %81
%90 = load i32*, i32** %11
%91 = call i32 @fclose(i32* %90)
%92 = load i32, i32* @stderr
%93 = getelementptr inbounds [7 x i8], [7 x i8]* %13, i64 0, i64 0
%94 = call i32 @fprintf(i32 %92, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %93)
store i32 3, i32* %3
br label %115
95: ; preds = %81
%96 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 0
%97 = load i32*, i32** %12
%98 = call i32 @fwrite(i8* %96, i32 512, i32 1, i32* %97)
%99 = load i32, i32* %7
%100 = add nsw i32 %99, 1
store i32 %100, i32* %7
br label %109
101: ; preds = %68, %61, %53, %45, %37
%102 = load i32, i32* %7
%103 = icmp ne i32 %102, 0
br i1 %103, label %104, label %108
104: ; preds = %101
%105 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 0
%106 = load i32*, i32** %12
%107 = call i32 @fwrite(i8* %105, i32 512, i32 1, i32* %106)
br label %108
108: ; preds = %104, %101
br label %109
109: ; preds = %108, %95
br label %32
110: ; preds = %32
%111 = load i32*, i32** %11
%112 = call i32 @fclose(i32* %111)
%113 = load i32*, i32** %12
%114 = call i32 @fclose(i32* %113)
store i32 0, i32* %3
br label %115
115: ; preds = %110, %89, %28, %17
%116 = load i32, i32* %3
ret i32 %116
}

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