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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
scalar_t__ memcmp(int *, int *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int main(void) {
  FILE *fp = fopen("card.raw", "r");
  if (fp == NULL) {
    fclose(fp);
    fprintf(stderr, "Wasn't able to open the cardfile.\n");
    return 1;
  }
  uint8_t checkjpg1[4] = {0xff, 0xd8, 0xff, 0xe0};
  uint8_t checkjpg2[4] = {0xff, 0xd8, 0xff, 0xe1};
  int jpgcount = 0;
  int open = 0;
  FILE *outp;
  uint8_t buffer[512];
  uint8_t check[4];
  fread(buffer, 512, 1, fp);
  while (fread(buffer, 512, 1, fp) > 0) {
    for (int i = 0; i < 4; i++) {
      check[i] = buffer[i];
    }
    if ((memcmp(checkjpg1, check, 4) == 0) ||
        (memcmp(checkjpg2, check, sizeof(check)) == 0)) {
      char filename[8];
      sprintf(filename, "%03d.jpg", jpgcount);
      if (open == 0) {
        outp = fopen(filename, "w");
        fwrite(buffer, sizeof(buffer), 1, outp);
        open = 1;
      }
      if (open == 1) {
        fclose(outp);
        outp = fopen(filename, "w");
        fwrite(buffer, sizeof(buffer), 1, outp);
        jpgcount++;
      }
    } else {
      if (open == 1) {
        fwrite(buffer, sizeof(buffer), 1, outp);
      }
    }
  }
  if (outp) {
    fclose(outp);
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x860,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ce <main+0x5e>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c7 <main+0x257>
mov    0xe6b(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe68(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe65(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe62(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
lea    -0x840(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
lea    -0x840(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
jle    13a3 <main+0x233>
movl   $0x0,-0x854(%rbp)
cmpl   $0x4,-0x854(%rbp)
jge    128d <main+0x11d>
movslq -0x854(%rbp),%rax
mov    -0x840(%rbp,%rax,4),%ecx
movslq -0x854(%rbp),%rax
mov    %ecx,-0x850(%rbp,%rax,4)
mov    -0x854(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x854(%rbp)
jmp    1250 <main+0xe0>
lea    -0x20(%rbp),%rdi
lea    -0x850(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
je     12cb <main+0x15b>
lea    -0x30(%rbp),%rdi
lea    -0x850(%rbp),%rsi
mov    $0x10,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    1375 <main+0x205>
lea    -0x85c(%rbp),%rdi
mov    -0x34(%rbp),%edx
lea    0xd52(%rip),%rsi
call   1080 <sprintf@plt>
cmpl   $0x0,-0x38(%rbp)
jne    1323 <main+0x1b3>
lea    -0x85c(%rbp),%rdi
lea    0xd0e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
lea    -0x840(%rbp),%rdi
mov    -0x40(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
movl   $0x1,-0x38(%rbp)
cmpl   $0x1,-0x38(%rbp)
jne    1370 <main+0x200>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x85c(%rbp),%rdi
lea    0xcc3(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
lea    -0x840(%rbp),%rdi
mov    -0x40(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    139e <main+0x22e>
cmpl   $0x1,-0x38(%rbp)
jne    1399 <main+0x229>
lea    -0x840(%rbp),%rdi
mov    -0x40(%rbp),%rcx
mov    $0x800,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
jmp    139e <main+0x22e>
jmp    1222 <main+0xb2>
cmpq   $0x0,-0x40(%rbp)
je     13b7 <main+0x247>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x860,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x848,%rsp
lea    0xe78(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1342 <main+0x1d2>
mov    %rax,%rbx
movaps 0xe99(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe9d(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
lea    0x40(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rbx,%r14
mov    %rbx,%rcx
call   1030 <fread@plt>
xor    %ebp,%ebp
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r12
xor    %r13d,%r13d
jmp    1283 <main+0x113>
xchg   %ax,%ax
mov    %r12,%rdi
lea    0xe24(%rip),%rsi
mov    %r13d,%edx
call   1080 <sprintf@plt>
test   %ebp,%ebp
lea    0x40(%rsp),%rbx
je     12f0 <main+0x180>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
lea    0xdd6(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r15
mov    $0x1,%r12d
mov    %rbx,%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
add    $0x1,%r13d
mov    %r14,%rcx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %r12d,%ebp
test   %rax,%rax
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r12
jle    1329 <main+0x1b9>
movaps 0x40(%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x30(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     1200 <main+0x90>
lea    0x20(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     1200 <main+0x90>
xor    %r12d,%r12d
cmp    $0x1,%ebp
jne    131c <main+0x1ac>
mov    $0x1,%r12d
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1090 <fwrite@plt>
jmp    1258 <main+0xe8>
mov    %r12,%rdi
lea    0xd0d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    $0x800,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
jmp    121f <main+0xaf>
mov    %r14,%rcx
lea    0x40(%rsp),%rbx
jmp    125b <main+0xeb>
test   %r15,%r15
je     1336 <main+0x1c6>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1363 <main+0x1f3>
xor    %edi,%edi
call   1040 <fclose@plt>
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
lea    0xcb2(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x848,%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 = [35 x i8] c"Wasn't able to open the cardfile.\0A\00"
@__const.main.checkjpg1 = [4 x i32] [i32 255, i32 216, i32 255, i32 224]
@__const.main.checkjpg2 = [4 x i32] [i32 255, i32 216, i32 255, i32 225]
@.str.3 = [9 x i8] c"%03d.jpg\00"
@.str.4 = [2 x i8] c"w\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [4 x i32]
%4 = alloca [4 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [512 x i32]
%9 = alloca [4 x i32]
%10 = alloca i32
%11 = alloca [8 x i8]
store i32 0, i32* %1
%12 = 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* %12, i32** %2
%13 = load i32*, i32** %2
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %0
%16 = load i32*, i32** %2
%17 = call i32 @fclose(i32* %16)
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %100
20: ; preds = %0
%21 = bitcast [4 x i32]* %3 to i8*
%22 = bitcast [4 x i32]* %4 to i8*
store i32 0, i32* %5
store i32 0, i32* %6
%23 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%24 = load i32*, i32** %2
%25 = call i64 @fread(i32* %23, i32 512, i32 1, i32* %24)
br label %26
26: ; preds = %90, %20
%27 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%28 = load i32*, i32** %2
%29 = call i64 @fread(i32* %27, i32 512, i32 1, i32* %28)
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %91
31: ; preds = %26
store i32 0, i32* %10
br label %32
32: ; preds = %43, %31
%33 = load i32, i32* %10
%34 = icmp slt i32 %33, 4
br i1 %34, label %35, label %46
35: ; preds = %32
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 %37
%39 = load i32, i32* %38
%40 = load i32, i32* %10
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 %41
store i32 %39, i32* %42
br label %43
43: ; preds = %35
%44 = load i32, i32* %10
%45 = add nsw i32 %44, 1
store i32 %45, i32* %10
br label %32
46: ; preds = %32
%47 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%48 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 0
%49 = call i64 @memcmp(i32* %47, i32* %48, i32 4)
%50 = icmp eq i64 %49, 0
br i1 %50, label %56, label %51
51: ; preds = %46
%52 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%53 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 0
%54 = call i64 @memcmp(i32* %52, i32* %53, i32 16)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %82
56: ; preds = %51, %46
%57 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%58 = load i32, i32* %5
%59 = call i32 @sprintf(i8* %57, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %58)
%60 = load i32, i32* %6
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %56
%63 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%64 = call i32* @fopen(i8* %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %64, i32** %7
%65 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%66 = load i32*, i32** %7
%67 = call i32 @fwrite(i32* %65, i32 2048, i32 1, i32* %66)
store i32 1, i32* %6
br label %68
68: ; preds = %62, %56
%69 = load i32, i32* %6
%70 = icmp eq i32 %69, 1
br i1 %70, label %71, label %81
71: ; preds = %68
%72 = load i32*, i32** %7
%73 = call i32 @fclose(i32* %72)
%74 = getelementptr inbounds [8 x i8], [8 x i8]* %11, 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** %7
%76 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%77 = load i32*, i32** %7
%78 = call i32 @fwrite(i32* %76, i32 2048, i32 1, i32* %77)
%79 = load i32, i32* %5
%80 = add nsw i32 %79, 1
store i32 %80, i32* %5
br label %81
81: ; preds = %71, %68
br label %90
82: ; preds = %51
%83 = load i32, i32* %6
%84 = icmp eq i32 %83, 1
br i1 %84, label %85, label %89
85: ; preds = %82
%86 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%87 = load i32*, i32** %7
%88 = call i32 @fwrite(i32* %86, i32 2048, i32 1, i32* %87)
br label %89
89: ; preds = %85, %82
br label %90
90: ; preds = %89, %81
br label %26
91: ; preds = %26
%92 = load i32*, i32** %7
%93 = icmp ne i32* %92, null
br i1 %93, label %94, label %97
94: ; preds = %91
%95 = load i32*, i32** %7
%96 = call i32 @fclose(i32* %95)
br label %97
97: ; preds = %94, %91
%98 = load i32*, i32** %2
%99 = call i32 @fclose(i32* %98)
store i32 0, i32* %1
br label %100
100: ; preds = %97, %15
%101 = load i32, i32* %1
ret i32 %101
}

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 = [35 x i8] c"Wasn't able to open the cardfile.\0A\00"
@__const.main.checkjpg1 = [4 x i32] [i32 255, i32 216, i32 255, i32 224]
@__const.main.checkjpg2 = [4 x i32] [i32 255, i32 216, i32 255, i32 225]
@.str.3 = [9 x i8] c"%03d.jpg\00"
@.str.4 = [2 x i8] c"w\00"
define i32 @main() {
%1 = alloca [4 x i32]
%2 = alloca [4 x i32]
%3 = alloca [512 x i32]
%4 = bitcast [512 x i32]* %3 to i8*
%5 = alloca [4 x i32]
%6 = bitcast [4 x i32]* %5 to i8*
%7 = alloca [8 x i8]
%8 = 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))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = tail call i32 @fclose(i32* null)
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %62
14: ; preds = %0
%15 = bitcast [4 x i32]* %1 to i8*
%16 = bitcast [4 x i32]* %2 to i8*
%17 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 0
%18 = call i64 @fread(i32* %17, i32 512, i32 1, i32* %8)
%19 = call i64 @fread(i32* %17, i32 512, i32 1, i32* %8)
%20 = icmp sgt i64 %19, 0
%21 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 0
%22 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 0
%23 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%24 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
br label %25
25: ; preds = %14, %50
%26 = phi i32* [ undef, %14 ], [ %53, %50 ]
%27 = phi i32 [ 0, %14 ], [ %52, %50 ]
%28 = phi i32 [ 0, %14 ], [ %51, %50 ]
%29 = call i64 @memcmp(i32* %21, i32* %22, i32 4)
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %25
%32 = call i64 @memcmp(i32* %23, i32* %22, i32 16)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %46
34: ; preds = %31, %25
%35 = call i32 @sprintf(i8* %24, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%36 = icmp eq i32 %27, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%39 = call i32 @fwrite(i32* %17, i32 2048, i32 1, i32* %38)
br label %40
40: ; preds = %34, %37
%41 = phi i32* [ %38, %37 ], [ %26, %34 ]
%42 = call i32 @fclose(i32* %41)
%43 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @fwrite(i32* %17, i32 2048, i32 1, i32* %43)
%45 = add nsw i32 %28, 1
br label %50
46: ; preds = %31
%47 = icmp eq i32 %27, 1
br i1 %47, label %48, label %50
48: ; preds = %46
%49 = call i32 @fwrite(i32* %17, i32 2048, i32 1, i32* %26)
br label %50
50: ; preds = %46, %48, %40
%51 = phi i32 [ %45, %40 ], [ %28, %48 ], [ %28, %46 ]
%52 = phi i32 [ 1, %40 ], [ 1, %48 ], [ 0, %46 ]
%53 = phi i32* [ %43, %40 ], [ %26, %48 ], [ %26, %46 ]
%54 = call i64 @fread(i32* %17, i32 512, i32 1, i32* %8)
%55 = icmp sgt i64 %54, 0
br i1 %55, label %25, label %56
56: ; preds = %50
%57 = icmp eq i32* %53, null
br i1 %57, label %60, label %58
58: ; preds = %56
%59 = call i32 @fclose(i32* %53)
br label %60
60: ; preds = %58, %56
%61 = call i32 @fclose(i32* %8)
br label %62
62: ; preds = %60, %10
%63 = phi i32 [ 1, %10 ], [ 0, %60 ]
ret i32 %63
}