#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ lg(char *, ...);
void dump_bin(char *path, void *data, int len) {
  FILE *fp;
  lg("Writing %s\n", path);
  fp = fopen(path, "wb");
  if (len != fwrite(data, 1, len, fp)) {
    lg("Failed to write all data\n");
    exit(1);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<dump_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <lg@plt>
mov    -0x8(%rbp),%rdi
lea    0xe8c(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
je     11c9 <dump_bin+0x79>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <lg@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_bin>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe9d(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <lg@plt>
lea    0xe98(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
cmp    %ebp,%eax
jne    11a1 <dump_bin+0x51>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fclose@plt>
lea    0xe67(%rip),%rdi
xor    %eax,%eax
call   1040 <lg@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"Writing %s\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [26 x i8] c"Failed to write all data\0A\00"
define void @dump_bin(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = call i32 (i8*, ...) @lg(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32, i32* %6
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = load i32*, i32** %7
%16 = call i32 @fwrite(i8* %13, i32 1, i32 %14, i32* %15)
%17 = icmp ne i32 %12, %16
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = call i32 (i8*, ...) @lg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %3
%22 = load i32*, i32** %7
%23 = call i32 @fclose(i32* %22)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"Writing %s\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [26 x i8] c"Failed to write all data\0A\00"
define void @dump_bin(i8* %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 (i8*, ...) @lg(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @fwrite(i8* %1, i32 1, i32 %2, i32* %5)
%7 = icmp eq i32 %6, %2
br i1 %7, label %11, label %8
8: ; preds = %3
%9 = tail call i32 (i8*, ...) @lg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = tail call i32 @fclose(i32* %5)
ret void
}