#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 scalar_t__ uint8_t;
typedef scalar_t__ uint16_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(scalar_t__ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(scalar_t__ *);
int /*<<< orphan*/ fwrite(scalar_t__ *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(int argc, char *argv[]) {
  if (argc > 1) {
    printf("Usage: no command-line arguments required.");
    return 1;
  }
  FILE *inptr = fopen("card.raw", "r");
  FILE *outptr = NULL;
  if (inptr == NULL) {
    printf("Could not open card.raw for reading.\n");
    return 1;
  }
  uint16_t signatureA = 0xff + 0xd8 + 0xff + 0xe0;
  uint16_t signatureB = 0xff + 0xd8 + 0xff + 0xe1;
  bool foundFirstSignature = false;
  int fnumber = 0;
  while (true) {
    uint8_t *buffer = (uint8_t *)malloc(512);
    if (fread(buffer, 512, 1, inptr) != 1) {
      fclose(outptr);
      free(buffer);
      break;
    }
    uint16_t signature = buffer[0] + buffer[1] + buffer[2] + buffer[3];
    if (signature == signatureA || signature == signatureB) {
      foundFirstSignature = true;
      if (outptr != NULL) {
        fclose(outptr);
      }
      char fname[8];
      sprintf(fname, "%03d.jpg", fnumber);
      outptr = fopen(fname, "w");
      if (outptr == NULL) {
        printf("Could not open %s for writing.\n", fname);
        fclose(inptr);
        free(buffer);
        return 1;
      }
      fnumber += 1;
    }
    if (foundFirstSignature) {
      fwrite(buffer, 512, 1, outptr);
    }
    free(buffer);
  }
  fclose(inptr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11ba <main+0x3a>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1365 <main+0x1e5>
lea    0xe6a(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11fe <main+0x7e>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1365 <main+0x1e5>
movq   $0x3b6,-0x28(%rbp)
movq   $0x3b7,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    $0x200,%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     1261 <main+0xe1>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    1355 <main+0x1d5>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    -0x40(%rbp),%rcx
add    0x8(%rcx),%rax
mov    -0x40(%rbp),%rcx
add    0x10(%rcx),%rax
mov    -0x40(%rbp),%rcx
add    0x18(%rcx),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     12a0 <main+0x120>
mov    -0x48(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    1326 <main+0x1a6>
movl   $0x1,-0x34(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12bb <main+0x13b>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x50(%rbp),%rdi
mov    -0x38(%rbp),%edx
lea    0xd93(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xd59(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    131d <main+0x19d>
lea    -0x50(%rbp),%rsi
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1365 <main+0x1e5>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1347 <main+0x1c7>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    121c <main+0x9c>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     11b9 <main+0x29>
lea    0xe59(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%r15d
jmp    1314 <main+0x184>
lea    0xe6b(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12e5 <main+0x155>
mov    %rax,%r14
mov    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
xor    %r15d,%r15d
xor    %ebp,%ebp
cmp    $0x1,%eax
jne    12fc <main+0x16c>
mov    %rsp,%r13
xor    %r12d,%r12d
xor    %r15d,%r15d
jmp    1271 <main+0xe1>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %r15d,%r15d
je     12dd <main+0x14d>
mov    %rbx,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    12f9 <main+0x169>
movdqu (%rbx),%xmm0
movdqu 0x10(%rbx),%xmm1
paddq  %xmm0,%xmm1
pshufd $0xee,%xmm1,%xmm0
paddq  %xmm1,%xmm0
movq   %xmm0,%rax
and    $0xfffffffffffffffe,%rax
cmp    $0x3b6,%rax
jne    1220 <main+0x90>
test   %rbp,%rbp
je     12a5 <main+0x115>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %r13,%rdi
lea    0xd87(%rip),%rsi
mov    %r12d,%edx
call   10a0 <sprintf@plt>
mov    %r13,%rdi
lea    0xd71(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1326 <main+0x196>
mov    %rax,%rbp
add    $0x1,%r12d
mov    $0x1,%r15d
jmp    1229 <main+0x99>
xor    %r15d,%r15d
jmp    123e <main+0xae>
lea    0xd73(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r15d
jmp    1314 <main+0x184>
xor    %r15d,%r15d
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd12(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%r15d
jmp    1314 <main+0x184>

LLVM IR (-O0):
@.str = [43 x i8] c"Usage: no command-line arguments required.\00"
@.str.1 = [9 x i8] c"card.raw\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [38 x i8] c"Could not open card.raw for reading.\0A\00"
@.str.4 = [9 x i8] c"%03d.jpg\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [32 x i8] c"Could not open %s for writing.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64*
%13 = alloca i64
%14 = alloca [8 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %98
19: ; preds = %2
%20 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %6
store i32* null, i32** %7
%21 = load i32*, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %98
25: ; preds = %19
store i64 950, i64* %8
store i64 951, i64* %9
store i32 0, i32* %10
store i32 0, i32* %11
br label %26
26: ; preds = %25, %92
%27 = call i64 @malloc(i32 512)
%28 = inttoptr i64 %27 to i64*
store i64* %28, i64** %12
%29 = load i64*, i64** %12
%30 = load i32*, i32** %6
%31 = call i32 @fread(i64* %29, i32 512, i32 1, i32* %30)
%32 = icmp ne i32 %31, 1
br i1 %32, label %33, label %38
33: ; preds = %26
%34 = load i32*, i32** %7
%35 = call i32 @fclose(i32* %34)
%36 = load i64*, i64** %12
%37 = call i32 @free(i64* %36)
br label %95
38: ; preds = %26
%39 = load i64*, i64** %12
%40 = getelementptr inbounds i64, i64* %39, i64 0
%41 = load i64, i64* %40
%42 = load i64*, i64** %12
%43 = getelementptr inbounds i64, i64* %42, i64 1
%44 = load i64, i64* %43
%45 = add nsw i64 %41, %44
%46 = load i64*, i64** %12
%47 = getelementptr inbounds i64, i64* %46, i64 2
%48 = load i64, i64* %47
%49 = add nsw i64 %45, %48
%50 = load i64*, i64** %12
%51 = getelementptr inbounds i64, i64* %50, i64 3
%52 = load i64, i64* %51
%53 = add nsw i64 %49, %52
store i64 %53, i64* %13
%54 = load i64, i64* %13
%55 = load i64, i64* %8
%56 = icmp eq i64 %54, %55
br i1 %56, label %61, label %57
57: ; preds = %38
%58 = load i64, i64* %13
%59 = load i64, i64* %9
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %85
61: ; preds = %57, %38
store i32 1, i32* %10
%62 = load i32*, i32** %7
%63 = icmp ne i32* %62, null
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = load i32*, i32** %7
%66 = call i32 @fclose(i32* %65)
br label %67
67: ; preds = %64, %61
%68 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 0
%69 = load i32, i32* %11
%70 = call i32 @sprintf(i8* %68, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %69)
%71 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 0
%72 = call i32* @fopen(i8* %71, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %72, i32** %7
%73 = load i32*, i32** %7
%74 = icmp eq i32* %73, null
br i1 %74, label %75, label %82
75: ; preds = %67
%76 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 0
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %76)
%78 = load i32*, i32** %6
%79 = call i32 @fclose(i32* %78)
%80 = load i64*, i64** %12
%81 = call i32 @free(i64* %80)
store i32 1, i32* %3
br label %98
82: ; preds = %67
%83 = load i32, i32* %11
%84 = add nsw i32 %83, 1
store i32 %84, i32* %11
br label %85
85: ; preds = %82, %57
%86 = load i32, i32* %10
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %92
88: ; preds = %85
%89 = load i64*, i64** %12
%90 = load i32*, i32** %7
%91 = call i32 @fwrite(i64* %89, i32 512, i32 1, i32* %90)
br label %92
92: ; preds = %88, %85
%93 = load i64*, i64** %12
%94 = call i32 @free(i64* %93)
br label %26
95: ; preds = %33
%96 = load i32*, i32** %6
%97 = call i32 @fclose(i32* %96)
store i32 0, i32* %3
br label %98
98: ; preds = %95, %75, %23, %17
%99 = load i32, i32* %3
ret i32 %99
}

LLVM IR (-O2):
@.str = [43 x i8] c"Usage: no command-line arguments required.\00"
@.str.1 = [9 x i8] c"card.raw\00"
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [9 x i8] c"%03d.jpg\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [32 x i8] c"Could not open %s for writing.\0A\00"
@str = [37 x i8] c"Could not open card.raw for reading.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [8 x i8]
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %69
7: ; preds = %2
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %17, label %10
10: ; preds = %7
%11 = tail call i64 @malloc(i32 512)
%12 = inttoptr i64 %11 to i64*
%13 = tail call i32 @fread(i64* %12, i32 512, i32 1, i32* %8)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %63
15: ; preds = %10
%16 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
br label %19
17: ; preds = %7
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
br label %69
19: ; preds = %15, %52
%20 = phi i64* [ %12, %15 ], [ %58, %52 ]
%21 = phi i64 [ %11, %15 ], [ %57, %52 ]
%22 = phi i32* [ null, %15 ], [ %53, %52 ]
%23 = phi i32 [ 0, %15 ], [ %54, %52 ]
%24 = phi i32 [ 0, %15 ], [ %55, %52 ]
%25 = bitcast i64* %20 to <4 x i64>*
%26 = load <4 x i64>, <4 x i64>* %25
%27 = call i64 @llvm.vector.reduce.add.v4i64(<4 x i64> %26)
%28 = and i64 %27, -2
%29 = icmp eq i64 %28, 950
br i1 %29, label %30, label %45
30: ; preds = %19
%31 = icmp eq i32* %22, null
br i1 %31, label %34, label %32
32: ; preds = %30
%33 = tail call i32 @fclose(i32* %22)
br label %34
34: ; preds = %32, %30
%35 = call i32 @sprintf(i8* %16, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%36 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %43
38: ; preds = %34
%39 = inttoptr i64 %21 to i64*
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %16)
%41 = call i32 @fclose(i32* %8)
%42 = call i32 @free(i64* %39)
br label %69
43: ; preds = %34
%44 = add nsw i32 %23, 1
br label %47
45: ; preds = %19
%46 = icmp eq i32 %24, 0
br i1 %46, label %52, label %47
47: ; preds = %43, %45
%48 = phi i32* [ %36, %43 ], [ %22, %45 ]
%49 = phi i32 [ %44, %43 ], [ %23, %45 ]
%50 = phi i32 [ 1, %43 ], [ %24, %45 ]
%51 = tail call i32 @fwrite(i64* %20, i32 512, i32 1, i32* %48)
br label %52
52: ; preds = %45, %47
%53 = phi i32* [ %48, %47 ], [ %22, %45 ]
%54 = phi i32 [ %49, %47 ], [ %23, %45 ]
%55 = phi i32 [ %50, %47 ], [ 0, %45 ]
%56 = tail call i32 @free(i64* %20)
%57 = tail call i64 @malloc(i32 512)
%58 = inttoptr i64 %57 to i64*
%59 = tail call i32 @fread(i64* %58, i32 512, i32 1, i32* %8)
%60 = icmp eq i32 %59, 1
br i1 %60, label %19, label %61
61: ; preds = %52
%62 = inttoptr i64 %57 to i64*
br label %63
63: ; preds = %61, %10
%64 = phi i64* [ %12, %10 ], [ %62, %61 ]
%65 = phi i32* [ null, %10 ], [ %53, %61 ]
%66 = tail call i32 @fclose(i32* %65)
%67 = tail call i32 @free(i64* %64)
%68 = tail call i32 @fclose(i32* %8)
br label %69
69: ; preds = %38, %17, %63, %5
%70 = phi i32 [ 1, %5 ], [ 1, %17 ], [ 0, %63 ], [ 1, %38 ]
ret i32 %70
}