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

/* Variables and functions */
int comp(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int main(void) {
  FILE *card = fopen("card.raw", "r");
  if (card == NULL) {
    printf("File does not exist.\n");
    return 1;
  }
  char buffer[512] = "0000";
  int jcounter = 0;
  char title[8];
  int stream = 0;
  while (stream == 0) {
    if (comp(buffer) == true) {
      sprintf(title, "%03d.jpg", jcounter);
      FILE *out = fopen(title, "w");
      if (out == NULL) {
        fclose(out);
        fprintf(stderr, "Could not create %s.\n", title);
        return 2;
      }
      stream = 1;
      fwrite(&buffer, sizeof(buffer), 1, out);
      while (stream == 1) {
        fread(&buffer, sizeof(buffer), 1, card);
        if (comp(buffer) == true) {
          jcounter++;
          fclose(out);
          stream = 0;
        } else if (feof(card)) {
          fclose(out);
          break;
        } else
          fwrite(&buffer, sizeof(buffer), 1, out);
      }
    } else {
      fread(&buffer, sizeof(buffer), 1, card);
    }
  }
  fclose(card);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe47(%rip),%rdi
lea    0xe49(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ec <main+0x4c>
lea    0xe30(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d2 <main+0x232>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
movb   $0x30,-0x210(%rbp)
movb   $0x30,-0x20f(%rbp)
movb   $0x30,-0x20e(%rbp)
movb   $0x30,-0x20d(%rbp)
movl   $0x0,-0x214(%rbp)
movl   $0x0,-0x220(%rbp)
cmpl   $0x0,-0x220(%rbp)
jne    13c2 <main+0x222>
lea    -0x210(%rbp),%rdi
call   1040 <comp@plt>
cmp    $0x1,%eax
jne    13a3 <main+0x203>
lea    -0x21c(%rbp),%rdi
mov    -0x214(%rbp),%edx
lea    0xdbc(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x21c(%rbp),%rdi
lea    0xd8f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
jne    12c6 <main+0x126>
mov    -0x228(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
lea    -0x21c(%rbp),%rdx
lea    0xd75(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13d2 <main+0x232>
movl   $0x1,-0x220(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x228(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   10c0 <fwrite@plt>
cmpl   $0x1,-0x220(%rbp)
jne    139e <main+0x1fe>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
lea    -0x210(%rbp),%rdi
call   1040 <comp@plt>
cmp    $0x1,%eax
jne    1353 <main+0x1b3>
mov    -0x214(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x214(%rbp)
mov    -0x228(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x220(%rbp)
jmp    1399 <main+0x1f9>
mov    -0x10(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%rax
je     1377 <main+0x1d7>
mov    -0x228(%rbp),%rdi
call   1050 <fclose@plt>
jmp    139e <main+0x1fe>
lea    -0x210(%rbp),%rdi
mov    -0x228(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   10c0 <fwrite@plt>
jmp    1399 <main+0x1f9>
jmp    12ed <main+0x14d>
jmp    13bd <main+0x21d>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
jmp    122f <main+0x8f>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%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    0xe48(%rip),%rdi
lea    0xe4a(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12f7 <main+0x157>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
xor    %r15d,%r15d
mov    $0x200,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
movl   $0x30303030,0x10(%rsp)
30
lea    0xe15(%rip),%r13
lea    0x8(%rsp),%rbp
lea    0xe05(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1050 <comp@plt>
cmp    $0x1,%eax
je     1240 <main+0xa0>
mov    %r14,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
jmp    1210 <main+0x70>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r15d,%r13d
mov    %r15d,%edx
call   10b0 <sprintf@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
call   10a0 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
je     131a <main+0x17a>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10c0 <fwrite@plt>
mov    %r14,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %r14,%rdi
call   1050 <comp@plt>
cmp    $0x1,%eax
je     12c0 <main+0x120>
mov    %rbx,%rdi
call   1090 <feof@plt>
test   %rax,%rax
je     1270 <main+0xd0>
jmp    12e3 <main+0x143>
cs nopw 0x0(%rax,%rax,1)
mov    %r13d,%r12d
add    $0x1,%r12d
mov    %r15,%rdi
call   1060 <fclose@plt>
lea    0xd35(%rip),%r13
lea    0x8(%rsp),%rbp
mov    %r12d,%r15d
jmp    11fb <main+0x5b>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
jmp    1308 <main+0x168>
lea    0xd2c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1060 <fclose@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
lea    0xce3(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1080 <fprintf@plt>
mov    $0x2,%eax
jmp    1308 <main+0x168>

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

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"
@stderr = global i32 0
@.str.5 = [22 x i8] c"Could not create %s.\0A\00"
@str = [21 x i8] c"File does not exist.\00"
define i32 @main() {
%1 = alloca [512 x i8]
%2 = alloca [8 x i8]
%3 = 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))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %43
7: ; preds = %0
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%9 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%10 = bitcast [512 x i8]* %1 to i32*
store i32 808464432, i32* %10
%11 = bitcast [512 x i8]* %1 to i8**
br label %12
12: ; preds = %33, %7
%13 = phi i32 [ %34, %33 ], [ 0, %7 ]
br label %14
14: ; preds = %12, %36
%15 = call i32 @comp(i8* %8)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %36
17: ; preds = %14
%18 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%19 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = call i32 @fclose(i32* null)
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %9)
br label %41
25: ; preds = %17, %30
%26 = call i32 @fwrite(i8** %11, i32 512, i32 1, i32* %19)
%27 = call i32 @fread(i8** %11, i32 512, i32 1, i32* %3)
%28 = call i32 @comp(i8* %8)
%29 = icmp eq i32 %28, 1
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = call i64 @feof(i32* %3)
%32 = icmp eq i64 %31, 0
br i1 %32, label %25, label %38
33: ; preds = %25
%34 = add nuw nsw i32 %13, 1
%35 = call i32 @fclose(i32* %19)
br label %12
36: ; preds = %14
%37 = call i32 @fread(i8** %11, i32 512, i32 1, i32* %3)
br label %14
38: ; preds = %30
%39 = call i32 @fclose(i32* %19)
%40 = call i32 @fclose(i32* %3)
br label %41
41: ; preds = %21, %38
%42 = phi i32 [ 0, %38 ], [ 2, %21 ]
br label %43
43: ; preds = %41, %5
%44 = phi i32 [ 1, %5 ], [ %42, %41 ]
ret i32 %44
}