#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int main(void) {
  char *infile = "card.raw";
  FILE *inptr = fopen(infile, "r");
  if (inptr == NULL) {
    fprintf(stderr, "Could not open %s.\n", infile);
    return 1;
  }
  unsigned char block[512];
  int imagesRecovered = 0;
  bool jpegStarted = false;
  FILE *outptr;
  while (fread(block, 512, 1, inptr) != 0) {
    if ((int)block[0] == 255 && (int)block[1] == 216 && (int)block[2] == 255 &&
        ((int)block[3] == 224 || (int)block[3] == 225)) {
      if (jpegStarted) {
        fclose(outptr);
      } else {
        jpegStarted = true;
      }
      char fileName[4];
      if (imagesRecovered < 10) {
        sprintf(fileName, "00%i.jpg", imagesRecovered);
      } else {
        sprintf(fileName, "0%i.jpg", imagesRecovered);
      }
      outptr = fopen(fileName, "w");
      if (outptr == NULL) {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", fileName);
        return 3;
      }
      imagesRecovered++;
    }
    if (jpegStarted) {
      fwrite(&block, 512, 1, outptr);
    }
  }
  fclose(outptr);
  fclose(inptr);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c1 <main+0x61>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136b <main+0x20b>
movl   $0x0,-0x224(%rbp)
movl   $0x0,-0x228(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
je     1356 <main+0x1f6>
movzbl -0x220(%rbp),%eax
cmp    $0xff,%eax
jne    1327 <main+0x1c7>
movzbl -0x21f(%rbp),%eax
cmp    $0xd8,%eax
jne    1327 <main+0x1c7>
movzbl -0x21e(%rbp),%eax
cmp    $0xff,%eax
jne    1327 <main+0x1c7>
movzbl -0x21d(%rbp),%eax
cmp    $0xe0,%eax
je     1253 <main+0xf3>
movzbl -0x21d(%rbp),%eax
cmp    $0xe1,%eax
jne    1327 <main+0x1c7>
cmpl   $0x0,-0x228(%rbp)
je     1271 <main+0x111>
mov    -0x230(%rbp),%rdi
call   1040 <fclose@plt>
jmp    127b <main+0x11b>
movl   $0x1,-0x228(%rbp)
cmpl   $0xa,-0x224(%rbp)
jge    12a6 <main+0x146>
lea    -0x234(%rbp),%rdi
mov    -0x224(%rbp),%edx
lea    0xd83(%rip),%rsi
call   1070 <sprintf@plt>
jmp    12bf <main+0x15f>
lea    -0x234(%rbp),%rdi
mov    -0x224(%rbp),%edx
lea    0xd66(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x234(%rbp),%rdi
lea    0xd3a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x230(%rbp)
cmpq   $0x0,-0x230(%rbp)
jne    1318 <main+0x1b8>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    -0x234(%rbp),%rdx
lea    0xd21(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
jmp    136b <main+0x20b>
mov    -0x224(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x224(%rbp)
cmpl   $0x0,-0x228(%rbp)
je     1351 <main+0x1f1>
lea    -0x220(%rbp),%rdi
mov    -0x230(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
jmp    11d5 <main+0x75>
mov    -0x230(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%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    0xe88(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     128c <main+0x12c>
mov    %rax,%r13
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
xor    %r12d,%r12d
lea    0xc(%rsp),%r15
xor    %ebp,%ebp
jmp    11fc <main+0x9c>
cs nopw 0x0(%rax,%rax,1)
test   %r12d,%r12d
je     1270 <main+0x110>
mov    %r14,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %r14,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1278 <main+0x118>
cmpb   $0xff,0x10(%rsp)
jne    11c0 <main+0x60>
cmpb   $0xd8,0x11(%rsp)
jne    11c0 <main+0x60>
cmpb   $0xff,0x12(%rsp)
jne    11c0 <main+0x60>
movzbl 0x13(%rsp),%eax
and    $0xfe,%al
cmp    $0xe0,%al
jne    11c0 <main+0x60>
test   %r12d,%r12d
je     1229 <main+0xc9>
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    $0xa,%ebp
lea    0xded(%rip),%rsi
lea    0xde5(%rip),%rax
cmovl  %rax,%rsi
mov    %r15,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
mov    %r15,%rdi
lea    0xdb5(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12af <main+0x14f>
mov    %rax,%rbx
add    $0x1,%ebp
mov    $0x1,%r12d
jmp    11c9 <main+0x69>
nopl   (%rax)
xor    %r12d,%r12d
jmp    11de <main+0x7e>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    12d6 <main+0x176>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xd6f(%rip),%rsi
lea    0xd5d(%rip),%rdx
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    12d6 <main+0x176>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
lea    0xd61(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1050 <fprintf@plt>
mov    $0x3,%eax
add    $0x218,%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"
@stderr = global i32 0
@.str.2 = [20 x i8] c"Could not open %s.\0A\00"
@.str.3 = [9 x i8] c"00%i.jpg\00"
@.str.4 = [8 x i8] c"0%i.jpg\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [22 x i8] c"Could not create %s.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [512 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [4 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %2
%9 = load i8*, i8** %2
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %2
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %15)
store i32 1, i32* %1
br label %93
17: ; preds = %0
store i32 0, i32* %5
store i32 0, i32* %6
br label %18
18: ; preds = %87, %17
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%20 = load i32*, i32** %3
%21 = call i64 @fread(i8* %19, i32 512, i32 1, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %88
23: ; preds = %18
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = icmp eq i32 %26, 255
br i1 %27, label %28, label %80
28: ; preds = %23
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 1
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = icmp eq i32 %31, 216
br i1 %32, label %33, label %80
33: ; preds = %28
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 2
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = icmp eq i32 %36, 255
br i1 %37, label %38, label %80
38: ; preds = %33
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 3
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = icmp eq i32 %41, 224
br i1 %42, label %48, label %43
43: ; preds = %38
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 3
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = icmp eq i32 %46, 225
br i1 %47, label %48, label %80
48: ; preds = %43, %38
%49 = load i32, i32* %6
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32*, i32** %7
%53 = call i32 @fclose(i32* %52)
br label %55
54: ; preds = %48
store i32 1, i32* %6
br label %55
55: ; preds = %54, %51
%56 = load i32, i32* %5
%57 = icmp slt i32 %56, 10
br i1 %57, label %58, label %62
58: ; preds = %55
%59 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%60 = load i32, i32* %5
%61 = call i32 @sprintf(i8* %59, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %60)
br label %66
62: ; preds = %55
%63 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%64 = load i32, i32* %5
%65 = call i32 @sprintf(i8* %63, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %64)
br label %66
66: ; preds = %62, %58
%67 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%68 = call i32* @fopen(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %68, i32** %7
%69 = load i32*, i32** %7
%70 = icmp eq i32* %69, null
br i1 %70, label %71, label %77
71: ; preds = %66
%72 = load i32*, i32** %3
%73 = call i32 @fclose(i32* %72)
%74 = load i32, i32* @stderr
%75 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%76 = call i32 @fprintf(i32 %74, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %75)
store i32 3, i32* %1
br label %93
77: ; preds = %66
%78 = load i32, i32* %5
%79 = add nsw i32 %78, 1
store i32 %79, i32* %5
br label %80
80: ; preds = %77, %43, %33, %28, %23
%81 = load i32, i32* %6
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %87
83: ; preds = %80
%84 = bitcast [512 x i8]* %4 to i8**
%85 = load i32*, i32** %7
%86 = call i32 @fwrite(i8** %84, i32 512, i32 1, i32* %85)
br label %87
87: ; preds = %83, %80
br label %18
88: ; preds = %18
%89 = load i32*, i32** %7
%90 = call i32 @fclose(i32* %89)
%91 = load i32*, i32** %3
%92 = call i32 @fclose(i32* %91)
br label %93
93: ; preds = %88, %71, %13
%94 = load i32, i32* %1
ret i32 %94
}

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