#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*/ *fopen(char *, char *);
int fread(int **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(int argc, char *argv[]) {
  uint8_t block[512];
  char *filename;
  FILE *rawfile;
  FILE *picfile;
  int picnum = 0;
  char picname[8];
  picname[7] = '\0';
  if (argc > 1)
    filename = argv[1];
  else
    filename = "card.raw";
  rawfile = fopen(filename, "r");
  while (picnum == 0 && fread(&block, sizeof(block), 1, rawfile) == 1) {
    if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff &&
        (block[3] == 0xe0 || block[3] == 0xe1)) {
      picfile = fopen("000.jpg", "w");
      picnum++;
      fwrite(&block, sizeof(block), 1, picfile);
    }
  }
  while (fread(&block, sizeof(block), 1, rawfile) == 1) {
    if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff &&
        (block[3] == 0xe0 || block[3] == 0xe1)) {
      fclose(picfile);
      sprintf(picname, "%.3d.jpg", picnum);
      picfile = fopen(picname, "w");
      picnum++;
    }
    fwrite(&block, sizeof(block), 1, picfile);
  }
  fclose(rawfile);
  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,-0x82c(%rbp)
movb   $0x0,-0x82d(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1198 <main+0x48>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x818(%rbp)
jmp    11a6 <main+0x56>
lea    0xe61(%rip),%rax
mov    %rax,-0x818(%rbp)
mov    -0x818(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x820(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x82c(%rbp)
mov    %al,-0x835(%rbp)
jne    11fe <main+0xae>
lea    -0x810(%rbp),%rdi
mov    -0x820(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
sete   %al
mov    %al,-0x835(%rbp)
mov    -0x835(%rbp),%al
test   $0x1,%al
jne    1211 <main+0xc1>
jmp    12ac <main+0x15c>
cmpl   $0xff,-0x810(%rbp)
jne    12a7 <main+0x157>
cmpl   $0xd8,-0x80c(%rbp)
jne    12a7 <main+0x157>
cmpl   $0xff,-0x808(%rbp)
jne    12a7 <main+0x157>
cmpl   $0xe0,-0x804(%rbp)
je     1261 <main+0x111>
cmpl   $0xe1,-0x804(%rbp)
jne    12a7 <main+0x157>
lea    0xda3(%rip),%rdi
lea    0xd98(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x828(%rbp)
mov    -0x82c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x82c(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x828(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
jmp    11c0 <main+0x70>
jmp    12b1 <main+0x161>
lea    -0x810(%rbp),%rdi
mov    -0x820(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1397 <main+0x247>
cmpl   $0xff,-0x810(%rbp)
jne    1375 <main+0x225>
cmpl   $0xd8,-0x80c(%rbp)
jne    1375 <main+0x225>
cmpl   $0xff,-0x808(%rbp)
jne    1375 <main+0x225>
cmpl   $0xe0,-0x804(%rbp)
je     1327 <main+0x1d7>
cmpl   $0xe1,-0x804(%rbp)
jne    1375 <main+0x225>
mov    -0x828(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x834(%rbp),%rdi
mov    -0x82c(%rbp),%edx
lea    0xccc(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x834(%rbp),%rdi
lea    0xcad(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x828(%rbp)
mov    -0x82c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x82c(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x828(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
jmp    12b1 <main+0x161>
mov    -0x820(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%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    $0x818,%rsp
movb   $0x0,0xf(%rsp)
cmp    $0x2,%edi
jl     1171 <main+0x21>
mov    0x8(%rsi),%rdi
jmp    1178 <main+0x28>
lea    0xe88(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
xor    %r14d,%r14d
cmp    $0x1,%eax
jne    123b <main+0xeb>
lea    0x10(%rsp),%rbp
jmp    11da <main+0x8a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    123b <main+0xeb>
cmpl   $0xff,0x10(%rsp)
jne    11c0 <main+0x70>
cmpl   $0xd8,0x14(%rsp)
jne    11c0 <main+0x70>
cmpl   $0xff,0x18(%rsp)
jne    11c0 <main+0x70>
mov    0x1c(%rsp),%eax
and    $0xfffffffe,%eax
cmp    $0xe0,%eax
jne    11c0 <main+0x70>
lea    0xdfe(%rip),%rdi
lea    0xdf3(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbp
lea    0x10(%rsp),%rdi
mov    $0x1,%r14d
mov    $0x800,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
jmp    123b <main+0xeb>
lea    0x10(%rsp),%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    12fc <main+0x1ac>
lea    0x8(%rsp),%r12
lea    0xda0(%rip),%r13
lea    0x10(%rsp),%r15
jmp    129f <main+0x14f>
xchg   %ax,%ax
mov    %r15,%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1070 <fwrite@plt>
mov    %r15,%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    12fc <main+0x1ac>
cmpl   $0xff,0x10(%rsp)
jne    1270 <main+0x120>
cmpl   $0xd8,0x14(%rsp)
jne    1270 <main+0x120>
cmpl   $0xff,0x18(%rsp)
jne    1270 <main+0x120>
mov    0x1c(%rsp),%eax
and    $0xfffffffe,%eax
cmp    $0xe0,%eax
jne    1270 <main+0x120>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
lea    0xd36(%rip),%rsi
mov    %r14d,%edx
call   1060 <sprintf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1050 <fopen@plt>
mov    %rax,%rbp
add    $0x1,%r14d
jmp    1270 <main+0x120>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%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 = [8 x i8] c"000.jpg\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [9 x i8] c"%.3d.jpg\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [512 x i32]
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca [8 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
%12 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 7
store i8 0, i8* %12
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %7
br label %20
19: ; preds = %2
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %7
br label %20
20: ; preds = %19, %15
%21 = load i8*, i8** %7
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %8
br label %23
23: ; preds = %60, %20
%24 = load i32, i32* %10
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = bitcast [512 x i32]* %6 to i32**
%28 = load i32*, i32** %8
%29 = call i32 @fread(i32** %27, i32 2048, i32 1, i32* %28)
%30 = icmp eq i32 %29, 1
br label %31
31: ; preds = %26, %23
%32 = phi i1 [ false, %23 ], [ %30, %26 ]
br i1 %32, label %33, label %61
33: ; preds = %31
%34 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 0
%35 = load i32, i32* %34
%36 = icmp eq i32 %35, 255
br i1 %36, label %37, label %60
37: ; preds = %33
%38 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 1
%39 = load i32, i32* %38
%40 = icmp eq i32 %39, 216
br i1 %40, label %41, label %60
41: ; preds = %37
%42 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 2
%43 = load i32, i32* %42
%44 = icmp eq i32 %43, 255
br i1 %44, label %45, label %60
45: ; preds = %41
%46 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 3
%47 = load i32, i32* %46
%48 = icmp eq i32 %47, 224
br i1 %48, label %53, label %49
49: ; preds = %45
%50 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 3
%51 = load i32, i32* %50
%52 = icmp eq i32 %51, 225
br i1 %52, label %53, label %60
53: ; preds = %49, %45
%54 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %54, i32** %9
%55 = load i32, i32* %10
%56 = add nsw i32 %55, 1
store i32 %56, i32* %10
%57 = bitcast [512 x i32]* %6 to i32**
%58 = load i32*, i32** %9
%59 = call i32 @fwrite(i32** %57, i32 2048, i32 1, i32* %58)
br label %60
60: ; preds = %53, %49, %41, %37, %33
br label %23
61: ; preds = %31
br label %62
62: ; preds = %97, %61
%63 = bitcast [512 x i32]* %6 to i32**
%64 = load i32*, i32** %8
%65 = call i32 @fread(i32** %63, i32 2048, i32 1, i32* %64)
%66 = icmp eq i32 %65, 1
br i1 %66, label %67, label %101
67: ; preds = %62
%68 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 0
%69 = load i32, i32* %68
%70 = icmp eq i32 %69, 255
br i1 %70, label %71, label %97
71: ; preds = %67
%72 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 1
%73 = load i32, i32* %72
%74 = icmp eq i32 %73, 216
br i1 %74, label %75, label %97
75: ; preds = %71
%76 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 2
%77 = load i32, i32* %76
%78 = icmp eq i32 %77, 255
br i1 %78, label %79, label %97
79: ; preds = %75
%80 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 3
%81 = load i32, i32* %80
%82 = icmp eq i32 %81, 224
br i1 %82, label %87, label %83
83: ; preds = %79
%84 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 3
%85 = load i32, i32* %84
%86 = icmp eq i32 %85, 225
br i1 %86, label %87, label %97
87: ; preds = %83, %79
%88 = load i32*, i32** %9
%89 = call i32 @fclose(i32* %88)
%90 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%91 = load i32, i32* %10
%92 = call i32 @sprintf(i8* %90, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %91)
%93 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%94 = call i32* @fopen(i8* %93, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %94, i32** %9
%95 = load i32, i32* %10
%96 = add nsw i32 %95, 1
store i32 %96, i32* %10
br label %97
97: ; preds = %87, %83, %75, %71, %67
%98 = bitcast [512 x i32]* %6 to i32**
%99 = load i32*, i32** %9
%100 = call i32 @fwrite(i32** %98, i32 2048, i32 1, i32* %99)
br label %62
101: ; preds = %62
%102 = load i32*, i32** %8
%103 = call i32 @fclose(i32* %102)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"card.raw\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"000.jpg\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [9 x i8] c"%.3d.jpg\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i32]
%4 = alloca [8 x i8]
%5 = bitcast [512 x i32]* %3 to i8*
%6 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 7
store i8 0, i8* %7
%8 = icmp sgt i32 %0, 1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
br label %12
12: ; preds = %2, %9
%13 = phi i8* [ %11, %9 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %2 ]
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = bitcast [512 x i32]* %3 to i32**
%16 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 0
%17 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 1
%18 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 2
%19 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 3
%20 = call i32 @fread(i32** %15, i32 2048, i32 1, i32* %14)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %41
22: ; preds = %12, %38
%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 %38
31: ; preds = %22
%32 = load i32, i32* %19
%33 = and i32 %32, -2
%34 = icmp eq i32 %33, 224
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @fwrite(i32** %15, i32 2048, i32 1, i32* %36)
br label %41
38: ; preds = %31, %22
%39 = call i32 @fread(i32** %15, i32 2048, i32 1, i32* %14)
%40 = icmp eq i32 %39, 1
br i1 %40, label %22, label %41
41: ; preds = %38, %12, %35
%42 = phi i32* [ %36, %35 ], [ undef, %12 ], [ undef, %38 ]
%43 = phi i32 [ 1, %35 ], [ 0, %12 ], [ 0, %38 ]
%44 = call i32 @fread(i32** %15, i32 2048, i32 1, i32* %14)
%45 = icmp eq i32 %44, 1
br i1 %45, label %46, label %72
46: ; preds = %41, %66
%47 = phi i32 [ %68, %66 ], [ %43, %41 ]
%48 = phi i32* [ %67, %66 ], [ %42, %41 ]
%49 = load i32, i32* %16
%50 = icmp eq i32 %49, 255
%51 = load i32, i32* %17
%52 = icmp eq i32 %51, 216
%53 = select i1 %50, i1 %52, i1 false
%54 = load i32, i32* %18
%55 = icmp eq i32 %54, 255
%56 = select i1 %53, i1 %55, i1 false
br i1 %56, label %57, label %66
57: ; preds = %46
%58 = load i32, i32* %19
%59 = and i32 %58, -2
%60 = icmp eq i32 %59, 224
br i1 %60, label %61, label %66
61: ; preds = %57
%62 = tail call i32 @fclose(i32* %48)
%63 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %47)
%64 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%65 = add nsw i32 %47, 1
br label %66
66: ; preds = %57, %61, %46
%67 = phi i32* [ %64, %61 ], [ %48, %57 ], [ %48, %46 ]
%68 = phi i32 [ %65, %61 ], [ %47, %57 ], [ %47, %46 ]
%69 = call i32 @fwrite(i32** %15, i32 2048, i32 1, i32* %67)
%70 = call i32 @fread(i32** %15, i32 2048, i32 1, i32* %14)
%71 = icmp eq i32 %70, 1
br i1 %71, label %46, label %72
72: ; preds = %66, %41
%73 = tail call i32 @fclose(i32* %14)
ret i32 0
}