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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(int argc, char *argv[]) {
  FILE *memory = fopen("card.raw", "r");
  if (memory) {
    char buffer[512] = {0};
    char jpg_sign[3] = {0xff, 0xd8, 0xff};
    int count = 0;
    FILE *current_out = NULL;
    while (fread(buffer, 512, 1, memory)) {
      if ((memcmp(buffer, jpg_sign, sizeof(jpg_sign)) == 0) &&
          (((buffer[3] & 0x000000ff) >> 4) == 0x0e)) {
        if (current_out != NULL) {
          fclose(current_out);
        }
        char filename[8] = {0};
        sprintf(filename, "%03d.jpg", count);
        current_out = fopen(filename, "w");
        count++;
      }
      if (current_out) {
        fwrite(buffer, 512, 1, current_out);
      }
      memset(buffer, 0, sizeof(buffer));
    }
  } else {
    printf("Could not open card.raw\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe60(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1312 <main+0x192>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    0xe58(%rip),%ax
mov    %ax,-0x223(%rbp)
mov    0xe4d(%rip),%al
mov    %al,-0x221(%rbp)
movl   $0x0,-0x228(%rbp)
movq   $0x0,-0x230(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
je     130d <main+0x18d>
lea    -0x220(%rbp),%rdi
lea    -0x223(%rbp),%rsi
mov    $0x3,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
jne    12ca <main+0x14a>
movsbl -0x21d(%rbp),%eax
and    $0xff,%eax
sar    $0x4,%eax
cmp    $0xe,%eax
jne    12ca <main+0x14a>
cmpq   $0x0,-0x230(%rbp)
je     1275 <main+0xf5>
mov    -0x230(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x238(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
lea    -0x238(%rbp),%rdi
mov    -0x228(%rbp),%edx
lea    0xd6f(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x238(%rbp),%rdi
lea    0xd58(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x230(%rbp)
mov    -0x228(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x228(%rbp)
cmpq   $0x0,-0x230(%rbp)
je     12f5 <main+0x175>
lea    -0x220(%rbp),%rdi
mov    -0x230(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
jmp    11fd <main+0x7d>
jmp    131e <main+0x19e>
lea    0xcfb(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%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    0xe82(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12c3 <main+0x143>
mov    %rax,%r15
lea    0x10(%rsp),%rbx
xor    %r14d,%r14d
mov    $0x200,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
movb   $0xff,0x6(%rsp)
movw   $0xd8ff,0x4(%rsp)
mov    %rbx,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     12cf <main+0x14f>
lea    0x10(%rsp),%rbx
lea    0x4(%rsp),%r12
lea    0x8(%rsp),%rbp
xor    %r13d,%r13d
jmp    123d <main+0xbd>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     12cf <main+0x14f>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x3,%edx
call   1070 <memcmp@plt>
test   %rax,%rax
jne    12a0 <main+0x120>
movzbl 0x13(%rsp),%eax
and    $0xf0,%al
cmp    $0xe0,%al
jne    12a0 <main+0x120>
test   %r13,%r13
je     126a <main+0xea>
mov    %r13,%rdi
call   1050 <fclose@plt>
movq   $0x0,0x8(%rsp)
mov    %rbp,%rdi
lea    0xd85(%rip),%rsi
mov    %r14d,%edx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
lea    0xd92(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r13
add    $0x1,%r14d
nopl   0x0(%rax,%rax,1)
test   %r13,%r13
je     1210 <main+0x90>
mov    %rbx,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   10a0 <fwrite@plt>
jmp    1210 <main+0x90>
lea    0xd41(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%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"
@__const.main.jpg_sign = [3 x i8] c"\FF\D8\FF"
@.str.2 = [9 x i8] c"%03d.jpg\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [25 x i8] c"Could not open card.raw\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [512 x i8]
%8 = alloca [3 x i8]
%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
%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** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %61
15: ; preds = %2
%16 = bitcast [512 x i8]* %7 to i8*
%17 = bitcast [3 x i8]* %8 to i8*
store i32 0, i32* %9
store i32* null, i32** %10
br label %18
18: ; preds = %57, %15
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%20 = load i32*, i32** %6
%21 = call i64 @fread(i8* %19, i32 512, i32 1, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %60
23: ; preds = %18
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%25 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%26 = call i64 @memcmp(i8* %24, i8* %25, i32 3)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %50
28: ; preds = %23
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 3
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = and i32 %31, 255
%33 = ashr i32 %32, 4
%34 = icmp eq i32 %33, 14
br i1 %34, label %35, label %50
35: ; preds = %28
%36 = load i32*, i32** %10
%37 = icmp ne i32* %36, null
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32*, i32** %10
%40 = call i32 @fclose(i32* %39)
br label %41
41: ; preds = %38, %35
%42 = bitcast [8 x i8]* %11 to i8*
%43 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%44 = load i32, i32* %9
%45 = call i32 @sprintf(i8* %43, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%47 = call i32* @fopen(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %47, i32** %10
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
br label %50
50: ; preds = %41, %28, %23
%51 = load i32*, i32** %10
%52 = icmp ne i32* %51, null
br i1 %52, label %53, label %57
53: ; preds = %50
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%55 = load i32*, i32** %10
%56 = call i32 @fwrite(i8* %54, i32 512, i32 1, i32* %55)
br label %57
57: ; preds = %53, %50
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%59 = call i32 @memset(i8* %58, i32 0, i32 512)
br label %18
60: ; preds = %18
br label %63
61: ; preds = %2
%62 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %63
63: ; preds = %61, %60
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"card.raw\00"
@.str.1 = [2 x i8] c"r\00"
@__const.main.jpg_sign = [3 x i8] c"\FF\D8\FF"
@.str.2 = [9 x i8] c"%03d.jpg\00"
@.str.3 = [2 x i8] c"w\00"
@str = [24 x i8] c"Could not open card.raw\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [512 x i8]
%4 = alloca [3 x i8]
%5 = alloca i64
%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 %44, label %8
8: ; preds = %2
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%10 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%11 = call i64 @fread(i8* %9, i32 512, i32 1, i32* %6)
%12 = icmp eq i64 %11, 0
br i1 %12, label %43, label %13
13: ; preds = %8
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 3
%15 = bitcast i64* %5 to i8*
br label %16
16: ; preds = %13, %39
%17 = phi i32* [ null, %13 ], [ %35, %39 ]
%18 = phi i32 [ 0, %13 ], [ %34, %39 ]
%19 = call i64 @memcmp(i8* %9, i8* %10, i32 3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %16
%22 = load i8, i8* %14
%23 = and i8 %22, -16
%24 = icmp eq i8 %23, -32
br i1 %24, label %25, label %33
25: ; preds = %21
%26 = icmp eq i32* %17, null
br i1 %26, label %29, label %27
27: ; preds = %25
%28 = call i32 @fclose(i32* %17)
br label %29
29: ; preds = %27, %25
store i64 0, i64* %5
%30 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%31 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%32 = add nsw i32 %18, 1
br label %33
33: ; preds = %29, %21, %16
%34 = phi i32 [ %32, %29 ], [ %18, %21 ], [ %18, %16 ]
%35 = phi i32* [ %31, %29 ], [ %17, %21 ], [ %17, %16 ]
%36 = icmp eq i32* %35, null
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = call i32 @fwrite(i8* %9, i32 512, i32 1, i32* %35)
br label %39
39: ; preds = %37, %33
%40 = call i32 @memset(i8* %9, i32 0, i32 512)
%41 = call i64 @fread(i8* %9, i32 512, i32 1, i32* %6)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %16
43: ; preds = %39, %8
br label %46
44: ; preds = %2
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %46
46: ; preds = %44, %43
ret i32 0
}