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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fread(int **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  uint8_t block[512];
  int fileNo = 0;
  FILE *outfile = NULL;
  FILE *infile = fopen("card.raw", "r");
  if (infile == NULL) {
    fprintf(stderr, "Could not open input file, 'card.raw'\n");
    return 1;
  }
  fread(&block, sizeof(block), 1, infile);
  while (!feof(infile)) {
    if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff) {
      if ((0 == ((block[3] >> 4) & 0)) && (1 == ((block[3] >> 5) & 1)) &&
          (1 == ((block[3] >> 6) & 1)) && (1 == ((block[3] >> 7) & 1))) {
        if (outfile != NULL) {
          fclose(outfile);
        }
        char outname[20];
        sprintf(outname, "%03d.jpg", fileNo);
        fileNo++;
        outfile = fopen(outname, "w");
        if (outfile == NULL) {
          fclose(infile);
          fprintf(stderr, "Could not create %s.\n", outname);
          return 1;
        }
        fwrite(&block, sizeof(block), 1, outfile);
      }
    } else {
      if (outfile != NULL) {
        fwrite(&block, sizeof(block), 1, outfile);
      }
    }
    fread(&block, sizeof(block), 1, infile);
  }
  fclose(infile);
  fclose(outfile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x814(%rbp)
movq   $0x0,-0x820(%rbp)
lea    0xe5b(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x828(%rbp)
cmpq   $0x0,-0x828(%rbp)
jne    11e9 <main+0x79>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13eb <main+0x27b>
lea    -0x810(%rbp),%rdi
mov    -0x828(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x828(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1227 <main+0xb7>
jmp    13cc <main+0x25c>
cmpl   $0xff,-0x810(%rbp)
jne    137a <main+0x20a>
cmpl   $0xd8,-0x80c(%rbp)
jne    137a <main+0x20a>
cmpl   $0xff,-0x808(%rbp)
jne    137a <main+0x20a>
mov    -0x804(%rbp),%ecx
sar    $0x4,%ecx
and    $0x0,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    1375 <main+0x205>
mov    -0x804(%rbp),%ecx
sar    $0x5,%ecx
and    $0x1,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1375 <main+0x205>
mov    -0x804(%rbp),%ecx
sar    $0x6,%ecx
and    $0x1,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1375 <main+0x205>
mov    -0x804(%rbp),%ecx
sar    $0x7,%ecx
and    $0x1,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1375 <main+0x205>
cmpq   $0x0,-0x820(%rbp)
je     12d2 <main+0x162>
mov    -0x820(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x840(%rbp),%rdi
mov    -0x814(%rbp),%edx
lea    0xd4c(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x814(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x814(%rbp)
lea    -0x840(%rbp),%rdi
lea    0xcff(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x820(%rbp)
cmpq   $0x0,-0x820(%rbp)
jne    1358 <main+0x1e8>
mov    -0x828(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
lea    -0x840(%rbp),%rdx
lea    0xcf6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13eb <main+0x27b>
lea    -0x810(%rbp),%rdi
mov    -0x820(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
jmp    13aa <main+0x23a>
cmpq   $0x0,-0x820(%rbp)
je     13a5 <main+0x235>
lea    -0x810(%rbp),%rdi
mov    -0x820(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
jmp    13aa <main+0x23a>
lea    -0x810(%rbp),%rdi
mov    -0x828(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
jmp    1206 <main+0x96>
mov    -0x828(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x820(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
lea    0xe78(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d7 <main+0x67>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
xor    %r14d,%r14d
xor    %r13d,%r13d
test   %eax,%eax
je     1209 <main+0x99>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
jmp    11f4 <main+0x84>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%r12
lea    0x20(%rsp),%rbp
xor    %r15d,%r15d
jmp    1263 <main+0xf3>
cs nopw 0x0(%rax,%rax,1)
test   %r13,%r13
je     12ca <main+0x15a>
mov    %rbp,%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1090 <fwrite@plt>
mov    %rbp,%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11c5 <main+0x55>
cmpl   $0xff,0x20(%rsp)
jne    1220 <main+0xb0>
cmpl   $0xd8,0x24(%rsp)
jne    1220 <main+0xb0>
cmpl   $0xff,0x28(%rsp)
jne    1220 <main+0xb0>
mov    0x2c(%rsp),%eax
not    %eax
test   $0xe0,%al
jne    123e <main+0xce>
test   %r13,%r13
je     1298 <main+0x128>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
lea    0xd90(%rip),%rsi
mov    %r15d,%edx
call   1080 <sprintf@plt>
mov    %r12,%rdi
lea    0xd53(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12d2 <main+0x162>
mov    %rax,%r13
add    $0x1,%r15d
jmp    1229 <main+0xb9>
xor    %r13d,%r13d
jmp    123e <main+0xce>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11ee <main+0x7e>

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

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