#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fread(char *, long, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int fwrite(char *, long, size_t, int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ stderr;
int dump_bin(FILE *in, const char *out_filename, size_t len) {
  FILE *out;
  char *buf;
  int res;
  out = fopen(out_filename, "wb");
  if (!out) {
    perror(out_filename);
    return 0;
  }
  buf = malloc(len);
  res = fread(buf, 1l, len, in);
  if (res < 0) {
    perror(out_filename);
    free(buf);
    return 0;
  }
  if ((unsigned)res != len) {
    fprintf(stderr, "%s:short read while copying.\n", out_filename);
    free(buf);
    return 0;
  }
  res = fwrite(buf, 1l, len, out);
  if (res < 0) {
    perror(out_filename);
    free(buf);
    return 0;
  }
  if ((unsigned)res != len) {
    fprintf(stderr, "%s:short write while copying.\n", out_filename);
    free(buf);
    return 0;
  }
  free(buf);
  fprintf(stderr, "Wrote %s\n", out_filename);
  return 1;
}


x86 assembly (clang, -O0):
<dump_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b8 <dump_bin+0x48>
mov    -0x18(%rbp),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <dump_bin+0x176>
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1206 <dump_bin+0x96>
mov    -0x18(%rbp),%rdi
call   1080 <perror@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <dump_bin+0x176>
mov    -0x34(%rbp),%eax
cmp    -0x20(%rbp),%rax
je     1241 <dump_bin+0xd1>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xddc(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <dump_bin+0x176>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1282 <dump_bin+0x112>
mov    -0x18(%rbp),%rdi
call   1080 <perror@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <dump_bin+0x176>
mov    -0x34(%rbp),%eax
cmp    -0x20(%rbp),%rax
je     12bd <dump_bin+0x14d>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd7e(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <dump_bin+0x176>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd66(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_bin>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r13
lea    0xe77(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1257 <dump_bin+0xe7>
mov    %rax,%r12
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    $0x1,%esi
mov    %rax,%rdi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1040 <fread@plt>
test   %eax,%eax
js     124d <dump_bin+0xdd>
mov    %eax,%eax
cmp    %rbx,%rax
jne    120f <dump_bin+0x9f>
mov    $0x1,%esi
mov    %r15,%rdi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1090 <fwrite@plt>
test   %eax,%eax
js     124d <dump_bin+0xdd>
mov    %eax,%eax
cmp    %rbx,%rax
jne    1221 <dump_bin+0xb1>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    1243 <dump_bin+0xd3>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
jmp    1231 <dump_bin+0xc1>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1080 <perror@plt>
jmp    1239 <dump_bin+0xc9>
mov    %r14,%rdi
call   1080 <perror@plt>
jmp    1241 <dump_bin+0xd1>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"%s:short read while copying.\0A\00"
@.str.2 = [31 x i8] c"%s:short write while copying.\0A\00"
@.str.3 = [10 x i8] c"Wrote %s\0A\00"
define i32 @dump_bin(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = load i32*, i32** %8
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = call i32 @perror(i8* %16)
store i32 0, i32* %4
br label %72
18: ; preds = %3
%19 = load i64, i64* %7
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %9
%21 = load i8*, i8** %9
%22 = load i64, i64* %7
%23 = load i32*, i32** %5
%24 = call i32 @fread(i8* %21, i64 1, i64 %22, i32* %23)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = load i8*, i8** %6
%29 = call i32 @perror(i8* %28)
%30 = load i8*, i8** %9
%31 = call i32 @free(i8* %30)
store i32 0, i32* %4
br label %72
32: ; preds = %18
%33 = load i32, i32* %10
%34 = zext i32 %33 to i64
%35 = load i64, i64* %7
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %43
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %6
%40 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = load i8*, i8** %9
%42 = call i32 @free(i8* %41)
store i32 0, i32* %4
br label %72
43: ; preds = %32
%44 = load i8*, i8** %9
%45 = load i64, i64* %7
%46 = load i32*, i32** %8
%47 = call i32 @fwrite(i8* %44, i64 1, i64 %45, i32* %46)
store i32 %47, i32* %10
%48 = load i32, i32* %10
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %43
%51 = load i8*, i8** %6
%52 = call i32 @perror(i8* %51)
%53 = load i8*, i8** %9
%54 = call i32 @free(i8* %53)
store i32 0, i32* %4
br label %72
55: ; preds = %43
%56 = load i32, i32* %10
%57 = zext i32 %56 to i64
%58 = load i64, i64* %7
%59 = icmp ne i64 %57, %58
br i1 %59, label %60, label %66
60: ; preds = %55
%61 = load i32, i32* @stderr
%62 = load i8*, i8** %6
%63 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %62)
%64 = load i8*, i8** %9
%65 = call i32 @free(i8* %64)
store i32 0, i32* %4
br label %72
66: ; preds = %55
%67 = load i8*, i8** %9
%68 = call i32 @free(i8* %67)
%69 = load i32, i32* @stderr
%70 = load i8*, i8** %6
%71 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %70)
store i32 1, i32* %4
br label %72
72: ; preds = %66, %60, %50, %37, %27, %15
%73 = load i32, i32* %4
ret i32 %73
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"%s:short read while copying.\0A\00"
@.str.2 = [31 x i8] c"%s:short write while copying.\0A\00"
@.str.3 = [10 x i8] c"Wrote %s\0A\00"
define i32 @dump_bin(i32* nocapture %0, i8* %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* %1)
br label %39
8: ; preds = %3
%9 = tail call i8* @malloc(i64 %2)
%10 = tail call i32 @fread(i8* %9, i64 1, i64 %2, i32* %0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 @perror(i8* %1)
%14 = tail call i32 @free(i8* %9)
br label %39
15: ; preds = %8
%16 = zext i32 %10 to i64
%17 = icmp eq i64 %16, %2
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%21 = tail call i32 @free(i8* %9)
br label %39
22: ; preds = %15
%23 = tail call i32 @fwrite(i8* %9, i64 1, i64 %2, i32* %4)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = tail call i32 @perror(i8* %1)
%27 = tail call i32 @free(i8* %9)
br label %39
28: ; preds = %22
%29 = zext i32 %23 to i64
%30 = icmp eq i64 %29, %2
br i1 %30, label %35, label %31
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%34 = tail call i32 @free(i8* %9)
br label %39
35: ; preds = %28
%36 = tail call i32 @free(i8* %9)
%37 = load i32, i32* @stderr
%38 = tail call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %39
39: ; preds = %35, %31, %25, %18, %12, %6
%40 = phi i32 [ 0, %12 ], [ 0, %18 ], [ 0, %25 ], [ 0, %31 ], [ 1, %35 ], [ 0, %6 ]
ret i32 %40
}