#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 const *, char *);
size_t fread(int *, int, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int *, int, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ safe_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  static const char *filename = "card.raw";
  FILE *file = fopen(filename, "r");
  if (file == NULL) {
    printf("Could not open %s.\n", filename);
    return 1;
  }
  static const int blockSize = 512;
  uint8_t block[blockSize];
  FILE *outfile = NULL;
  int curFile = 0;
  for (size_t bytesRead = 0;
       (bytesRead = fread(block, sizeof(*block), blockSize, file)) ==
       blockSize;) {
    if (((block[0] == 0xff) && (block[1] == 0xd8) && (block[2] == 0xff)) &&
        ((block[3] == 0xe0) || (block[3] == 0xe1))) {
      safe_close(outfile);
      char filename[4];
      sprintf(filename, "%03i.jpg", curFile++);
      outfile = fopen(filename, "wb");
    }
    if (outfile != NULL) {
      fwrite(block, sizeof(*block), blockSize, outfile);
    }
  }
  safe_close(outfile);
  fclose(file);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2ecb(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c7 <main+0x57>
mov    0x2ea9(%rip),%rsi
lea    0xe55(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e8 <main+0x178>
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
mov    %rsp,%rax
add    $0xfffffffffffff800,%rax
mov    %rax,-0x48(%rbp)
mov    %rax,%rsp
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x4,%esi
mov    $0x200,%edx
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x200,%rax
jne    12cf <main+0x15f>
mov    -0x48(%rbp),%rax
cmpl   $0xff,(%rax)
jne    12a8 <main+0x138>
mov    -0x48(%rbp),%rax
cmpl   $0xd8,0x4(%rax)
jne    12a8 <main+0x138>
mov    -0x48(%rbp),%rax
cmpl   $0xff,0x8(%rax)
jne    12a8 <main+0x138>
mov    -0x48(%rbp),%rax
cmpl   $0xe0,0xc(%rax)
je     1270 <main+0x100>
mov    -0x48(%rbp),%rax
cmpl   $0xe1,0xc(%rax)
jne    12a8 <main+0x138>
mov    -0x28(%rbp),%rdi
call   1060 <safe_close@plt>
lea    -0x3c(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    %edx,%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
lea    0xd90(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x3c(%rbp),%rdi
lea    0xd89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12ca <main+0x15a>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x4,%esi
mov    $0x200,%edx
call   1090 <fwrite@plt>
jmp    11f5 <main+0x85>
mov    -0x28(%rbp),%rdi
call   1060 <safe_close@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe78(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1283 <main+0x113>
mov    %rax,%r15
mov    %rsp,-0x30(%rbp)
mov    %rsp,%rbx
add    $0xfffffffffffff800,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x200,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x200,%rax
jne    129d <main+0x12d>
xor    %r12d,%r12d
xor    %r14d,%r14d
jmp    1201 <main+0x91>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x200,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    $0x200,%rax
jne    12a0 <main+0x130>
cmpl   $0xff,(%rbx)
jne    1260 <main+0xf0>
cmpl   $0xd8,0x4(%rbx)
jne    1260 <main+0xf0>
cmpl   $0xff,0x8(%rbx)
jne    1260 <main+0xf0>
mov    0xc(%rbx),%eax
and    $0xfffffffe,%eax
cmp    $0xe0,%eax
jne    1260 <main+0xf0>
mov    %r14,%rdi
call   1060 <safe_close@plt>
lea    0x1(%r12),%r13d
lea    -0x34(%rbp),%r14
mov    %r14,%rdi
lea    0xddc(%rip),%rsi
mov    %r12d,%edx
call   1080 <sprintf@plt>
mov    %r14,%rdi
lea    0xdd3(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
mov    %r13d,%r12d
test   %r14,%r14
je     11e0 <main+0x70>
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x200,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
jmp    11e0 <main+0x70>
lea    0xd81(%rip),%rdi
lea    0xd6f(%rip),%rsi
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    12b6 <main+0x146>
xor    %r14d,%r14d
mov    %r14,%rdi
call   1060 <safe_close@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@main.filename = internal global i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i32 0, i32 0)
@.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"
@main.blockSize = internal constant i32 512
@.str.3 = [9 x i8] c"%03i.jpg\00"
@.str.4 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i64
%11 = alloca [4 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8*, i8** @main.filename
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i8*, i8** @main.filename
%18 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %68
19: ; preds = %2
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %7
%21 = alloca i32, i64 512
store i32* null, i32** %8
store i32 0, i32* %9
store i64 0, i64* %10
br label %22
22: ; preds = %61, %19
%23 = load i32*, i32** %6
%24 = call i64 @fread(i32* %21, i32 4, i32 512, i32* %23)
store i64 %24, i64* %10
%25 = icmp eq i64 %24, 512
br i1 %25, label %26, label %62
26: ; preds = %22
%27 = getelementptr inbounds i32, i32* %21, i64 0
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, 255
br i1 %29, label %30, label %55
30: ; preds = %26
%31 = getelementptr inbounds i32, i32* %21, i64 1
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, 216
br i1 %33, label %34, label %55
34: ; preds = %30
%35 = getelementptr inbounds i32, i32* %21, i64 2
%36 = load i32, i32* %35
%37 = icmp eq i32 %36, 255
br i1 %37, label %38, label %55
38: ; preds = %34
%39 = getelementptr inbounds i32, i32* %21, i64 3
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, 224
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = getelementptr inbounds i32, i32* %21, i64 3
%44 = load i32, i32* %43
%45 = icmp eq i32 %44, 225
br i1 %45, label %46, label %55
46: ; preds = %42, %38
%47 = load i32*, i32** %8
%48 = call i32 @safe_close(i32* %47)
%49 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
%52 = call i32 @sprintf(i8* %49, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %50)
%53 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %54, i32** %8
br label %55
55: ; preds = %46, %42, %34, %30, %26
%56 = load i32*, i32** %8
%57 = icmp ne i32* %56, null
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = load i32*, i32** %8
%60 = call i32 @fwrite(i32* %21, i32 4, i32 512, i32* %59)
br label %61
61: ; preds = %58, %55
br label %22
62: ; preds = %22
%63 = load i32*, i32** %8
%64 = call i32 @safe_close(i32* %63)
%65 = load i32*, i32** %6
%66 = call i32 @fclose(i32* %65)
%67 = load i8*, i8** %7
br label %68
68: ; preds = %62, %16
%69 = load i32, i32* %3
ret i32 %69
}

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