#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 fread(unsigned char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(void) {
  FILE *dataFile = fopen("card.raw", "r");
  if (dataFile == NULL) {
    char *invalidFile = "card.raw";
    printf("Could not open %s.\n", invalidFile);
    fclose(dataFile);
    return 2;
  }
  int numFiles = 0;
  unsigned char buffer[512];
  FILE *img = NULL;
  char title[8];
  while (fread(&buffer, 512, 1, dataFile) == 1) {
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff &&
        ((buffer[3] == 0xe0) || (buffer[3] == 0xe1))) {
      if (img == NULL) {
        sprintf(title, "00%d.jpg", numFiles);
        img = fopen(title, "a");
        numFiles++;
        fwrite(buffer, 1, 512, img);
      } else {
        fclose(img);
        if (numFiles < 10) {
          sprintf(title, "00%d.jpg", numFiles);
        } else {
          sprintf(title, "0%d.jpg", numFiles);
        }
        img = fopen(title, "a");
        numFiles++;
        fwrite(buffer, 1, 512, img);
      }
    } else {
      if (numFiles > 0) {
        fwrite(buffer, 1, 512, img);
      }
    }
  }
  fclose(img);
  fclose(dataFile);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
lea    0xe89(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c4 <main+0x64>
lea    0xe65(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe61(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x2,-0x4(%rbp)
jmp    138e <main+0x22e>
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x228(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1379 <main+0x219>
movzbl -0x220(%rbp),%eax
cmp    $0xff,%eax
jne    1348 <main+0x1e8>
movzbl -0x21f(%rbp),%eax
cmp    $0xd8,%eax
jne    1348 <main+0x1e8>
movzbl -0x21e(%rbp),%eax
cmp    $0xff,%eax
jne    1348 <main+0x1e8>
movzbl -0x21d(%rbp),%eax
cmp    $0xe0,%eax
je     1253 <main+0xf3>
movzbl -0x21d(%rbp),%eax
cmp    $0xe1,%eax
jne    1348 <main+0x1e8>
cmpq   $0x0,-0x228(%rbp)
jne    12bc <main+0x15c>
lea    -0x230(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xdad(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x230(%rbp),%rdi
lea    0xda3(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x228(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x228(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1080 <fwrite@plt>
jmp    1343 <main+0x1e3>
mov    -0x228(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0xa,-0x1c(%rbp)
jge    12ed <main+0x18d>
lea    -0x230(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xd3c(%rip),%rsi
call   1070 <sprintf@plt>
jmp    1303 <main+0x1a3>
lea    -0x230(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xd22(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x230(%rbp),%rdi
lea    0xd17(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x228(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x228(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1080 <fwrite@plt>
jmp    1374 <main+0x214>
cmpl   $0x0,-0x1c(%rbp)
jle    136f <main+0x20f>
lea    -0x220(%rbp),%rdi
mov    -0x228(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1080 <fwrite@plt>
jmp    1374 <main+0x214>
jmp    11d6 <main+0x76>
mov    -0x228(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
lea    0xe88(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1279 <main+0x119>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
xor    %r12d,%r12d
cmp    $0x1,%eax
jne    1265 <main+0x105>
lea    0xe65(%rip),%r14
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r13
xor    %ebp,%ebp
jmp    1203 <main+0xa3>
nopl   0x0(%rax,%rax,1)
test   %ebp,%ebp
jle    11e9 <main+0x89>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r12,%rcx
call   1080 <fwrite@plt>
mov    %r13,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1265 <main+0x105>
cmpb   $0xff,0x10(%rsp)
jne    11d0 <main+0x70>
cmpb   $0xd8,0x11(%rsp)
jne    11d0 <main+0x70>
cmpb   $0xff,0x12(%rsp)
jne    11d0 <main+0x70>
movzbl 0x13(%rsp),%eax
and    $0xfe,%al
cmp    $0xe0,%al
jne    11d0 <main+0x70>
mov    %r14,%rsi
test   %r12,%r12
je     1241 <main+0xe1>
mov    %r12,%rdi
call   1040 <fclose@plt>
cmp    $0xa,%ebp
lea    0xde3(%rip),%rsi
cmovl  %r14,%rsi
mov    %r15,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
mov    %r15,%rdi
lea    0xdd3(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r12
add    $0x1,%ebp
jmp    11d4 <main+0x74>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1298 <main+0x138>
lea    0xd8b(%rip),%rdi
lea    0xd79(%rip),%rsi
call   1050 <printf@plt>
xor    %edi,%edi
call   1040 <fclose@plt>
mov    $0x2,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [9 x i8] c"card.raw\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"00%d.jpg\00"
@.str.4 = [2 x i8] c"a\00"
@.str.5 = [8 x i8] c"0%d.jpg\00"
define i32 @main() {
%1 = alloca [512 x i8]
%2 = alloca [8 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @fclose(i32* null)
br label %59
8: ; preds = %0
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%11 = bitcast [512 x i8]* %1 to i8**
%12 = call i32 @fread(i8** %11, i32 512, i32 1, i32* %3)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %55
14: ; preds = %8
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 1
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 2
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 3
br label %18
18: ; preds = %14, %50
%19 = phi i32* [ null, %14 ], [ %52, %50 ]
%20 = phi i32 [ 0, %14 ], [ %51, %50 ]
%21 = load i8, i8* %9
%22 = icmp eq i8 %21, -1
%23 = load i8, i8* %15
%24 = icmp eq i8 %23, -40
%25 = select i1 %22, i1 %24, i1 false
%26 = load i8, i8* %16
%27 = icmp eq i8 %26, -1
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %29, label %39
29: ; preds = %18
%30 = load i8, i8* %17
%31 = and i8 %30, -2
%32 = icmp eq i8 %31, -32
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = icmp eq i32* %19, null
br i1 %34, label %41, label %35
35: ; preds = %33
%36 = tail call i32 @fclose(i32* %19)
%37 = icmp slt i32 %20, 10
%38 = select i1 %37, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0)
br label %41
39: ; preds = %29, %18
%40 = icmp sgt i32 %20, 0
br i1 %40, label %46, label %50
41: ; preds = %33, %35
%42 = phi i8* [ %38, %35 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), %33 ]
%43 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) %42, i32 %20)
%44 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%45 = add nsw i32 %20, 1
br label %46
46: ; preds = %41, %39
%47 = phi i32* [ %19, %39 ], [ %44, %41 ]
%48 = phi i32 [ %20, %39 ], [ %45, %41 ]
%49 = call i32 @fwrite(i8* %9, i32 1, i32 512, i32* %47)
br label %50
50: ; preds = %46, %39
%51 = phi i32 [ %20, %39 ], [ %48, %46 ]
%52 = phi i32* [ %19, %39 ], [ %47, %46 ]
%53 = call i32 @fread(i8** %11, i32 512, i32 1, i32* %3)
%54 = icmp eq i32 %53, 1
br i1 %54, label %18, label %55
55: ; preds = %50, %8
%56 = phi i32* [ null, %8 ], [ %52, %50 ]
%57 = tail call i32 @fclose(i32* %56)
%58 = tail call i32 @fclose(i32* %3)
br label %59
59: ; preds = %55, %5
%60 = phi i32 [ 2, %5 ], [ 0, %55 ]
ret i32 %60
}