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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(int **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(int argc, char *argv[]) {
  int filenum = -1;
  char filename[8];
  FILE *output = NULL;
  FILE *input = fopen("card.raw", "r");
  if (input == NULL) {
    printf("Could not open file!\n");
    return 1;
  }
  uint8_t buffer[512];
  while ((fread(&buffer, sizeof(buffer), 1, input)) != 0) {
    if ((buffer[0] == 0xFF) && (buffer[1] == 0xD8) && (buffer[2] == 0xFF) &&
        ((buffer[3] == 0xE0) || (buffer[3] == 0xE1))) {
      if (output != NULL) {
        fclose(output);
      }
      filenum++;
      sprintf(filename, "%03d.jpg", filenum);
      output = fopen(filename, "w");
      if (output == NULL) {
        printf("Couldn't open output file %s!\n", filename);
        break;
      }
    }
    if (output != NULL) {
      fwrite(&buffer, sizeof(buffer), 1, output);
    }
  }
  fclose(output);
  fclose(input);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe71(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c4 <main+0x64>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12eb <main+0x18b>
jmp    11c9 <main+0x69>
lea    -0x830(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
je     12d2 <main+0x172>
cmpl   $0xff,-0x830(%rbp)
jne    12a8 <main+0x148>
cmpl   $0xd8,-0x82c(%rbp)
jne    12a8 <main+0x148>
cmpl   $0xff,-0x828(%rbp)
jne    12a8 <main+0x148>
cmpl   $0xe0,-0x824(%rbp)
je     123d <main+0xdd>
cmpl   $0xe1,-0x824(%rbp)
jne    12a8 <main+0x148>
cmpq   $0x0,-0x28(%rbp)
je     1251 <main+0xf1>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
lea    -0x1c(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xdb9(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x1c(%rbp),%rdi
lea    0xd8f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12a3 <main+0x143>
lea    -0x1c(%rbp),%rsi
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12d2 <main+0x172>
jmp    12a8 <main+0x148>
cmpq   $0x0,-0x28(%rbp)
je     12cd <main+0x16d>
lea    -0x830(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
jmp    11c9 <main+0x69>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
lea    0xe78(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1297 <main+0x127>
mov    %rax,%r13
lea    0x10(%rsp),%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     12ab <main+0x13b>
mov    $0xffffffff,%ebp
xor    %ebx,%ebx
lea    0x8(%rsp),%r12
lea    0x10(%rsp),%r15
jmp    121c <main+0xac>
cs nopw 0x0(%rax,%rax,1)
test   %rbx,%rbx
je     1290 <main+0x120>
mov    %r15,%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %r15,%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     12c4 <main+0x154>
cmpl   $0xff,0x10(%rsp)
jne    11e0 <main+0x70>
cmpl   $0xd8,0x14(%rsp)
jne    11e0 <main+0x70>
cmpl   $0xff,0x18(%rsp)
jne    11e0 <main+0x70>
mov    0x1c(%rsp),%eax
and    $0xfffffffe,%eax
cmp    $0xe0,%eax
jne    11e0 <main+0x70>
test   %rbx,%rbx
je     1255 <main+0xe5>
mov    %rbx,%rdi
call   1050 <fclose@plt>
add    $0x1,%ebp
mov    %r12,%rdi
lea    0xda9(%rip),%rsi
mov    %ebp,%edx
call   1080 <sprintf@plt>
mov    %r12,%rdi
lea    0xd94(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e9 <main+0x79>
jmp    12af <main+0x13f>
cs nopw 0x0(%rax,%rax,1)
xor    %ebx,%ebx
jmp    11fe <main+0x8e>
lea    0xd95(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
jmp    12d4 <main+0x164>
xor    %ebx,%ebx
jmp    12c4 <main+0x154>
lea    0xd5e(%rip),%rdi
xor    %ebx,%ebx
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %r14d,%eax
add    $0x818,%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 = [22 x i8] c"Could not open file!\0A\00"
@.str.3 = [9 x i8] c"%03d.jpg\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [31 x i8] c"Couldn't open output file %s!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [8 x i8]
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca [512 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32* null, i32** %8
%11 = 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* %11, i32** %9
%12 = load i32*, i32** %9
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %75
16: ; preds = %2
br label %17
17: ; preds = %69, %16
%18 = bitcast [512 x i32]* %10 to i32**
%19 = load i32*, i32** %9
%20 = call i64 @fread(i32** %18, i32 2048, i32 1, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %70
22: ; preds = %17
%23 = getelementptr inbounds [512 x i32], [512 x i32]* %10, i64 0, i64 0
%24 = load i32, i32* %23
%25 = icmp eq i32 %24, 255
br i1 %25, label %26, label %62
26: ; preds = %22
%27 = getelementptr inbounds [512 x i32], [512 x i32]* %10, i64 0, i64 1
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, 216
br i1 %29, label %30, label %62
30: ; preds = %26
%31 = getelementptr inbounds [512 x i32], [512 x i32]* %10, i64 0, i64 2
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, 255
br i1 %33, label %34, label %62
34: ; preds = %30
%35 = getelementptr inbounds [512 x i32], [512 x i32]* %10, i64 0, i64 3
%36 = load i32, i32* %35
%37 = icmp eq i32 %36, 224
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = getelementptr inbounds [512 x i32], [512 x i32]* %10, i64 0, i64 3
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, 225
br i1 %41, label %42, label %62
42: ; preds = %38, %34
%43 = load i32*, i32** %8
%44 = icmp ne i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32*, i32** %8
%47 = call i32 @fclose(i32* %46)
br label %48
48: ; preds = %45, %42
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
%51 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%52 = load i32, i32* %6
%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** %8
%56 = load i32*, i32** %8
%57 = icmp eq i32* %56, null
br i1 %57, label %58, label %61
58: ; preds = %48
%59 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %59)
br label %70
61: ; preds = %48
br label %62
62: ; preds = %61, %38, %30, %26, %22
%63 = load i32*, i32** %8
%64 = icmp ne i32* %63, null
br i1 %64, label %65, label %69
65: ; preds = %62
%66 = bitcast [512 x i32]* %10 to i32**
%67 = load i32*, i32** %8
%68 = call i32 @fwrite(i32** %66, i32 2048, i32 1, i32* %67)
br label %69
69: ; preds = %65, %62
br label %17
70: ; preds = %58, %17
%71 = load i32*, i32** %8
%72 = call i32 @fclose(i32* %71)
%73 = load i32*, i32** %9
%74 = call i32 @fclose(i32* %73)
store i32 0, i32* %3
br label %75
75: ; preds = %70, %14
%76 = load i32, i32* %3
ret i32 %76
}

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