#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 *, 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[]) {
  uint8_t buff[512];
  int counter = 0;
  char imgname[8];
  FILE *inputr = fopen("card.raw", "r");
  if (inputr == NULL) {
    printf("Unable to read from \"card.raw\"\n");
    return 1;
  }
  FILE *outputr = NULL;
  while (fread(&buff, 512, 1, inputr) != 0) {
    if (buff[0] == 0xff && buff[1] == 0xd8 && buff[2] == 0xff &&
        (buff[3] == 0xe0 || buff[3] == 0xe1)) {
      if (outputr != NULL) {
        fclose(outputr);
      }
      sprintf(imgname, "%03d.jpg", counter);
      counter++;
      outputr = fopen(imgname, "w");
      if (outputr == NULL) {
        printf("Unable to create outfile\n");
        return 2;
      }
      fwrite(&buff, 512, 1, outputr);
    } else {
      if (outputr != NULL) {
        fwrite(&buff, 512, 1, outputr);
      }
    }
  }
  fclose(inputr);
  fclose(outputr);
  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   $0x0,-0x814(%rbp)
lea    0xe76(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x828(%rbp)
cmpq   $0x0,-0x828(%rbp)
jne    11c3 <main+0x63>
lea    0xe59(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    133d <main+0x1dd>
movq   $0x0,-0x830(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x828(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
je     131e <main+0x1be>
cmpl   $0xff,-0x810(%rbp)
jne    12e9 <main+0x189>
cmpl   $0xd8,-0x80c(%rbp)
jne    12e9 <main+0x189>
cmpl   $0xff,-0x808(%rbp)
jne    12e9 <main+0x189>
cmpl   $0xe0,-0x804(%rbp)
je     1245 <main+0xe5>
cmpl   $0xe1,-0x804(%rbp)
jne    12e9 <main+0x189>
cmpq   $0x0,-0x830(%rbp)
je     125f <main+0xff>
mov    -0x830(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x81c(%rbp),%rdi
mov    -0x814(%rbp),%edx
lea    0xdb8(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x814(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x814(%rbp)
lea    -0x81c(%rbp),%rdi
lea    0xd72(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x830(%rbp)
cmpq   $0x0,-0x830(%rbp)
jne    12c7 <main+0x167>
lea    0xd7e(%rip),%rdi
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    133d <main+0x1dd>
lea    -0x810(%rbp),%rdi
mov    -0x830(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
jmp    1319 <main+0x1b9>
cmpq   $0x0,-0x830(%rbp)
je     1314 <main+0x1b4>
lea    -0x810(%rbp),%rdi
mov    -0x830(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
jmp    1319 <main+0x1b9>
jmp    11ce <main+0x6e>
mov    -0x828(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x830(%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    0xe88(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1288 <main+0x128>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     129b <main+0x13b>
lea    0x8(%rsp),%r12
lea    0xe48(%rip),%r13
lea    0x10(%rsp),%rbp
xor    %r15d,%r15d
jmp    120c <main+0xac>
nopl   0x0(%rax)
test   %r15,%r15
je     1280 <main+0x120>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     129e <main+0x13e>
cmpl   $0xff,0x10(%rsp)
jne    11d0 <main+0x70>
cmpl   $0xd8,0x14(%rsp)
jne    11d0 <main+0x70>
cmpl   $0xff,0x18(%rsp)
jne    11d0 <main+0x70>
mov    0x1c(%rsp),%eax
and    $0xfffffffe,%eax
cmp    $0xe0,%eax
jne    11d0 <main+0x70>
test   %r15,%r15
je     1245 <main+0xe5>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
lea    0xdbc(%rip),%rsi
mov    %r14d,%edx
call   1070 <sprintf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12c2 <main+0x162>
mov    %rax,%r15
add    $0x1,%r14d
jmp    11d9 <main+0x79>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %r15d,%r15d
jmp    11ee <main+0x8e>
lea    0xd9e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    12b0 <main+0x150>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
jmp    12b0 <main+0x150>

LLVM IR (-O0):
@.str = [9 x i8] c"card.raw\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"Unable to read from \22card.raw\22\0A\00"
@.str.3 = [9 x i8] c"%03d.jpg\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [26 x i8] c"Unable to create outfile\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 [8 x i8]
%9 = alloca i32*
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%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 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %78
16: ; preds = %2
store i32* null, i32** %10
br label %17
17: ; preds = %72, %16
%18 = bitcast [512 x i32]* %6 to i32**
%19 = load i32*, i32** %9
%20 = call i64 @fread(i32** %18, i32 512, i32 1, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %73
22: ; preds = %17
%23 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 0
%24 = load i32, i32* %23
%25 = icmp eq i32 %24, 255
br i1 %25, label %26, label %64
26: ; preds = %22
%27 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 1
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, 216
br i1 %29, label %30, label %64
30: ; preds = %26
%31 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 2
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, 255
br i1 %33, label %34, label %64
34: ; preds = %30
%35 = getelementptr inbounds [512 x i32], [512 x i32]* %6, 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]* %6, i64 0, i64 3
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, 225
br i1 %41, label %42, label %64
42: ; preds = %38, %34
%43 = load i32*, i32** %10
%44 = icmp ne i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32*, i32** %10
%47 = call i32 @fclose(i32* %46)
br label %48
48: ; preds = %45, %42
%49 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%50 = load i32, i32* %7
%51 = call i32 @sprintf(i8* %49, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
%54 = getelementptr inbounds [8 x i8], [8 x i8]* %8, 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** %10
%56 = load i32*, i32** %10
%57 = icmp eq i32* %56, null
br i1 %57, label %58, label %60
58: ; preds = %48
%59 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %3
br label %78
60: ; preds = %48
%61 = bitcast [512 x i32]* %6 to i32**
%62 = load i32*, i32** %10
%63 = call i32 @fwrite(i32** %61, i32 512, i32 1, i32* %62)
br label %72
64: ; preds = %38, %30, %26, %22
%65 = load i32*, i32** %10
%66 = icmp ne i32* %65, null
br i1 %66, label %67, label %71
67: ; preds = %64
%68 = bitcast [512 x i32]* %6 to i32**
%69 = load i32*, i32** %10
%70 = call i32 @fwrite(i32** %68, i32 512, i32 1, i32* %69)
br label %71
71: ; preds = %67, %64
br label %72
72: ; preds = %71, %60
br label %17
73: ; preds = %17
%74 = load i32*, i32** %9
%75 = call i32 @fclose(i32* %74)
%76 = load i32*, i32** %10
%77 = call i32 @fclose(i32* %76)
store i32 0, i32* %3
br label %78
78: ; preds = %73, %58, %14
%79 = load i32, i32* %3
ret i32 %79
}

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 = [25 x i8] c"Unable to create outfile\00"
@str.6 = [31 x i8] c"Unable to read from \22card.raw\22\00"
define i32 @main(i32 %0, i8** nocapture readnone %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 = 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))
%8 = icmp eq i32* %7, null
br i1 %8, label %18, label %9
9: ; preds = %2
%10 = bitcast [512 x i32]* %3 to i32**
%11 = call i64 @fread(i32** %10, i32 512, i32 1, i32* %7)
%12 = icmp eq i64 %11, 0
br i1 %12, label %58, label %13
13: ; preds = %9
%14 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 0
%15 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 1
%16 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 2
%17 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 3
br label %20
18: ; preds = %2
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.6, i64 0, i64 0))
br label %62
20: ; preds = %13, %53
%21 = phi i32* [ null, %13 ], [ %55, %53 ]
%22 = phi i32 [ 0, %13 ], [ %54, %53 ]
%23 = load i32, i32* %14
%24 = icmp eq i32 %23, 255
%25 = load i32, i32* %15
%26 = icmp eq i32 %25, 216
%27 = select i1 %24, i1 %26, i1 false
%28 = load i32, i32* %16
%29 = icmp eq i32 %28, 255
%30 = select i1 %27, i1 %29, i1 false
br i1 %30, label %31, label %47
31: ; preds = %20
%32 = load i32, i32* %17
%33 = and i32 %32, -2
%34 = icmp eq i32 %33, 224
br i1 %34, label %35, label %47
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 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%41 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %62
45: ; preds = %39
%46 = add nsw i32 %22, 1
br label %49
47: ; preds = %31, %20
%48 = icmp eq i32* %21, null
br i1 %48, label %53, label %49
49: ; preds = %47, %45
%50 = phi i32* [ %41, %45 ], [ %21, %47 ]
%51 = phi i32 [ %46, %45 ], [ %22, %47 ]
%52 = call i32 @fwrite(i32** %10, i32 512, i32 1, i32* %50)
br label %53
53: ; preds = %49, %47
%54 = phi i32 [ %22, %47 ], [ %51, %49 ]
%55 = phi i32* [ null, %47 ], [ %50, %49 ]
%56 = call i64 @fread(i32** %10, i32 512, i32 1, i32* %7)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %20
58: ; preds = %53, %9
%59 = phi i32* [ null, %9 ], [ %55, %53 ]
%60 = tail call i32 @fclose(i32* %7)
%61 = tail call i32 @fclose(i32* %59)
br label %62
62: ; preds = %43, %58, %18
%63 = phi i32 [ 1, %18 ], [ 2, %43 ], [ 0, %58 ]
ret i32 %63
}