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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(int argc, char *argv[]) {
  char sig[] = {0xFF, 0xD8, 0xFF, 0xE0, 0xE1};
  FILE *rawfile = fopen("card.raw", "r");
  if (rawfile == NULL)
    return 1;
  int counter = 0;
  char jpgfile[8];
  char buffer[0x200];
  FILE *jpeg = NULL;
  while (fread(&buffer, sizeof(buffer), 1, rawfile)) {
    if (buffer[0] == sig[0] && buffer[1] == sig[1] && buffer[2] == sig[2] &&
        (buffer[3] == sig[3] || buffer[3] == sig[4])) {
      if (jpeg != NULL)
        fclose(jpeg);
      sprintf(jpgfile, "%03i.jpg", counter);
      jpeg = fopen(jpgfile, "w");
      counter++;
    }
    if (jpeg != NULL)
      fwrite(&buffer, sizeof(buffer), 1, jpeg);
  }
  if (jpeg != NULL)
    fclose(jpeg);
  fclose(rawfile);
}


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)
mov    0xe91(%rip),%eax
mov    %eax,-0x15(%rbp)
mov    0xe8c(%rip),%al
mov    %al,-0x11(%rbp)
lea    0xe83(%rip),%rdi
lea    0xe85(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a9 <main+0x59>
movl   $0x1,-0x4(%rbp)
jmp    12de <main+0x18e>
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x238(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
je     12bb <main+0x16b>
movsbl -0x230(%rbp),%eax
movsbl -0x15(%rbp),%ecx
cmp    %ecx,%eax
jne    128b <main+0x13b>
movsbl -0x22f(%rbp),%eax
movsbl -0x14(%rbp),%ecx
cmp    %ecx,%eax
jne    128b <main+0x13b>
movsbl -0x22e(%rbp),%eax
movsbl -0x13(%rbp),%ecx
cmp    %ecx,%eax
jne    128b <main+0x13b>
movsbl -0x22d(%rbp),%eax
movsbl -0x12(%rbp),%ecx
cmp    %ecx,%eax
je     123e <main+0xee>
movsbl -0x22d(%rbp),%eax
movsbl -0x11(%rbp),%ecx
cmp    %ecx,%eax
jne    128b <main+0x13b>
cmpq   $0x0,-0x238(%rbp)
je     1258 <main+0x108>
mov    -0x238(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x2c(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xdaa(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x2c(%rbp),%rdi
lea    0xd96(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x238(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x238(%rbp)
je     12b6 <main+0x166>
lea    -0x230(%rbp),%rdi
mov    -0x238(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
jmp    11bb <main+0x6b>
cmpq   $0x0,-0x238(%rbp)
je     12d5 <main+0x185>
mov    -0x238(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
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
lea    0xe98(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     124a <main+0xfa>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     125e <main+0x10e>
xor    %ebp,%ebp
lea    0x8(%rsp),%r15
lea    0xe59(%rip),%r12
lea    0x10(%rsp),%r13
xor    %r14d,%r14d
jmp    11da <main+0x8a>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1251 <main+0x101>
cmpb   $0xff,0x10(%rsp)
jne    122b <main+0xdb>
cmpb   $0xd8,0x11(%rsp)
jne    122b <main+0xdb>
cmpb   $0xff,0x12(%rsp)
jne    122b <main+0xdb>
movzbl 0x13(%rsp),%eax
and    $0xfe,%al
cmp    $0xe0,%al
jne    122b <main+0xdb>
test   %rbp,%rbp
je     1207 <main+0xb7>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
lea    0xdfa(%rip),%rsi
mov    %r14d,%edx
call   1060 <sprintf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <fopen@plt>
mov    %rax,%rbp
add    $0x1,%r14d
test   %rbp,%rbp
je     11c0 <main+0x70>
mov    %r13,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1070 <fwrite@plt>
jmp    11c0 <main+0x70>
mov    $0x1,%eax
jmp    1268 <main+0x118>
test   %rbp,%rbp
je     125e <main+0x10e>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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