#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: ./copy forensicimage\n");
    return 1;
  }
  char *infile = argv[1];
  FILE *inptr = fopen(infile, "r");
  if (inptr == NULL) {
    fprintf(stderr, "Could not open card.raw");
    return 2;
  }
  uint8_t block[512];
  int count = 0;
  FILE *img = NULL;
  while (fread(block, 512, 1, inptr) == 1) {
    if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff &&
        (block[3] & 0xf0) == 0xe0) {
      if (img != NULL) {
        fclose(img);
      }
      char filename[8];
      sprintf(filename, "%03i.jpg", count);
      img = fopen(filename, "w");
      if (img == NULL) {
        fprintf(stderr, "Error opening JPEG");
        return 3;
      }
      count++;
      fwrite(block, 512, 1, img);
    } else {
      if (img != NULL) {
        fwrite(block, 512, 1, img);
      }
    }
  }
  fclose(inptr);
  fclose(img);
}


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)
cmpl   $0x2,-0x8(%rbp)
je     11a4 <main+0x44>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1365 <main+0x205>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f0 <main+0x90>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1365 <main+0x205>
movl   $0x0,-0x824(%rbp)
movq   $0x0,-0x830(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1350 <main+0x1f0>
cmpl   $0xff,-0x820(%rbp)
jne    131b <main+0x1bb>
cmpl   $0xd8,-0x81c(%rbp)
jne    131b <main+0x1bb>
cmpl   $0xff,-0x818(%rbp)
jne    131b <main+0x1bb>
mov    -0x814(%rbp),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
jne    131b <main+0x1bb>
cmpq   $0x0,-0x830(%rbp)
je     1288 <main+0x128>
mov    -0x830(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x838(%rbp),%rdi
mov    -0x824(%rbp),%edx
lea    0xd9b(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x838(%rbp),%rdi
lea    0xd86(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x830(%rbp)
cmpq   $0x0,-0x830(%rbp)
jne    12ea <main+0x18a>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
jmp    1365 <main+0x205>
mov    -0x824(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x824(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x830(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
jmp    134b <main+0x1eb>
cmpq   $0x0,-0x830(%rbp)
je     1346 <main+0x1e6>
lea    -0x820(%rbp),%rdi
mov    -0x830(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
jmp    134b <main+0x1eb>
jmp    1205 <main+0xa5>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x830(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%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
cmp    $0x2,%edi
jne    1288 <main+0x128>
mov    0x8(%rsi),%rdi
lea    0xe98(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12b7 <main+0x157>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
xor    %r14d,%r14d
xor    %r15d,%r15d
cmp    $0x1,%eax
jne    12a5 <main+0x145>
lea    0x8(%rsp),%r12
lea    0x10(%rsp),%rbp
xor    %r13d,%r13d
jmp    120c <main+0xac>
nopl   0x0(%rax,%rax,1)
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   1030 <fread@plt>
cmp    $0x1,%eax
jne    12a5 <main+0x145>
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
mov    $0xf0,%ecx
and    %ecx,%eax
cmp    $0xe0,%eax
jne    11d0 <main+0x70>
test   %r15,%r15
je     1249 <main+0xe9>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
lea    0xde4(%rip),%rsi
mov    %r13d,%edx
call   1070 <sprintf@plt>
mov    %r12,%rdi
lea    0xdd0(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12e7 <main+0x187>
mov    %rax,%r15
add    $0x1,%r13d
jmp    11d9 <main+0x79>
nopl   0x0(%rax,%rax,1)
xor    %r15d,%r15d
jmp    11ee <main+0x8e>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
lea    0xd68(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%r14d
jmp    12d2 <main+0x172>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    12d2 <main+0x172>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
lea    0xd58(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%r14d
mov    %r14d,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
lea    0xd49(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x3,%r14d
jmp    12d2 <main+0x172>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"Usage: ./copy forensicimage\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"Could not open card.raw\00"
@.str.3 = [9 x i8] c"%03i.jpg\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [19 x i8] c"Error opening JPEG\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [512 x 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
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %88
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%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** %7
%23 = load i32*, i32** %7
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %3
br label %88
28: ; preds = %17
store i32 0, i32* %9
store i32* null, i32** %10
br label %29
29: ; preds = %82, %28
%30 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%31 = load i32*, i32** %7
%32 = call i32 @fread(i32* %30, i32 512, i32 1, i32* %31)
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %83
34: ; preds = %29
%35 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%36 = load i32, i32* %35
%37 = icmp eq i32 %36, 255
br i1 %37, label %38, label %74
38: ; preds = %34
%39 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 1
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, 216
br i1 %41, label %42, label %74
42: ; preds = %38
%43 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 2
%44 = load i32, i32* %43
%45 = icmp eq i32 %44, 255
br i1 %45, label %46, label %74
46: ; preds = %42
%47 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 3
%48 = load i32, i32* %47
%49 = and i32 %48, 240
%50 = icmp eq i32 %49, 224
br i1 %50, label %51, label %74
51: ; preds = %46
%52 = load i32*, i32** %10
%53 = icmp ne i32* %52, null
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32*, i32** %10
%56 = call i32 @fclose(i32* %55)
br label %57
57: ; preds = %54, %51
%58 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%59 = load i32, i32* %9
%60 = call i32 @sprintf(i8* %58, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %59)
%61 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%62 = call i32* @fopen(i8* %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %62, i32** %10
%63 = load i32*, i32** %10
%64 = icmp eq i32* %63, null
br i1 %64, label %65, label %68
65: ; preds = %57
%66 = load i32, i32* @stderr
%67 = call i32 @fprintf(i32 %66, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
store i32 3, i32* %3
br label %88
68: ; preds = %57
%69 = load i32, i32* %9
%70 = add nsw i32 %69, 1
store i32 %70, i32* %9
%71 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%72 = load i32*, i32** %10
%73 = call i32 @fwrite(i32* %71, i32 512, i32 1, i32* %72)
br label %82
74: ; preds = %46, %42, %38, %34
%75 = load i32*, i32** %10
%76 = icmp ne i32* %75, null
br i1 %76, label %77, label %81
77: ; preds = %74
%78 = getelementptr inbounds [512 x i32], [512 x i32]* %8, i64 0, i64 0
%79 = load i32*, i32** %10
%80 = call i32 @fwrite(i32* %78, i32 512, i32 1, i32* %79)
br label %81
81: ; preds = %77, %74
br label %82
82: ; preds = %81, %68
br label %29
83: ; preds = %29
%84 = load i32*, i32** %7
%85 = call i32 @fclose(i32* %84)
%86 = load i32*, i32** %10
%87 = call i32 @fclose(i32* %86)
br label %88
88: ; preds = %83, %65, %25, %14
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"Usage: ./copy forensicimage\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"Could not open card.raw\00"
@.str.3 = [9 x i8] c"%03i.jpg\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [19 x i8] c"Error opening JPEG\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i32]
%4 = alloca [8 x i8]
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %71
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %71
17: ; preds = %9
%18 = bitcast [512 x i32]* %3 to i8*
%19 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 0
%20 = call i32 @fread(i32* %19, i32 512, i32 1, i32* %12)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %65
22: ; preds = %17
%23 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 1
%24 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 2
%25 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 3
%26 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
br label %27
27: ; preds = %22, %60
%28 = phi i32* [ null, %22 ], [ %62, %60 ]
%29 = phi i32 [ 0, %22 ], [ %61, %60 ]
%30 = load i32, i32* %19
%31 = icmp eq i32 %30, 255
%32 = load i32, i32* %23
%33 = icmp eq i32 %32, 216
%34 = select i1 %31, i1 %33, i1 false
%35 = load i32, i32* %24
%36 = icmp eq i32 %35, 255
%37 = select i1 %34, i1 %36, i1 false
br i1 %37, label %38, label %56
38: ; preds = %27
%39 = load i32, i32* %25
%40 = and i32 %39, 240
%41 = icmp eq i32 %40, 224
br i1 %41, label %42, label %56
42: ; preds = %38
%43 = icmp eq i32* %28, null
br i1 %43, label %46, label %44
44: ; preds = %42
%45 = tail call i32 @fclose(i32* %28)
br label %46
46: ; preds = %44, %42
%47 = call i32 @sprintf(i8* %26, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%48 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = tail call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %69
53: ; preds = %46
%54 = add nsw i32 %29, 1
%55 = call i32 @fwrite(i32* %19, i32 512, i32 1, i32* %48)
br label %60
56: ; preds = %38, %27
%57 = icmp eq i32* %28, null
br i1 %57, label %60, label %58
58: ; preds = %56
%59 = call i32 @fwrite(i32* %19, i32 512, i32 1, i32* %28)
br label %60
60: ; preds = %53, %56, %58
%61 = phi i32 [ %54, %53 ], [ %29, %58 ], [ %29, %56 ]
%62 = phi i32* [ %48, %53 ], [ %28, %58 ], [ null, %56 ]
%63 = call i32 @fread(i32* %19, i32 512, i32 1, i32* %12)
%64 = icmp eq i32 %63, 1
br i1 %64, label %27, label %65
65: ; preds = %60, %17
%66 = phi i32* [ null, %17 ], [ %62, %60 ]
%67 = tail call i32 @fclose(i32* %12)
%68 = tail call i32 @fclose(i32* %66)
br label %69
69: ; preds = %50, %65
%70 = phi i32 [ 0, %65 ], [ 3, %50 ]
br label %71
71: ; preds = %14, %69, %6
%72 = phi i32 [ 1, %6 ], [ 2, %14 ], [ %70, %69 ]
ret i32 %72
}