#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*/ EIO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
void WriteBinData(char *fname, unsigned char *buf, int len) {
  int numBytesWritten;
  FILE *fp;
  fp = fopen(fname, "wb");
  if (!fp) {
    printf("ERROR: failed to open %s for writing data\n", fname);
    exit(EIO);
  }
  numBytesWritten = fwrite(buf, 1, len, fp);
  if (numBytesWritten != len) {
    printf("ERROR: failed to write all data to bin file\n");
    fclose(fp);
    exit(EIO);
  }
  fclose(fp);
  return;
}


x86 assembly (clang, -O0):
<WriteBinData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a2 <WriteBinData+0x52>
mov    -0x8(%rbp),%rsi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11eb <WriteBinData+0x9b>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteBinData>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe8a(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ab <WriteBinData+0x4b>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
cmp    %ebp,%eax
jne    11be <WriteBinData+0x5e>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
lea    0xe51(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11d2 <WriteBinData+0x72>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [43 x i8] c"ERROR: failed to open %s for writing data\0A\00"
@EIO = global i32 0
@.str.2 = [45 x i8] c"ERROR: failed to write all data to bin file\0A\00"
define void @WriteBinData(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp ne i32* %11, null
br i1 %12, label %18, label %13
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EIO
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = load i32, i32* %6
%21 = load i32*, i32** %8
%22 = call i32 @fwrite(i8* %19, i32 1, i32 %20, i32* %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = load i32, i32* %6
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %32
26: ; preds = %18
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32*, i32** %8
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* @EIO
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %18
%33 = load i32*, i32** %8
%34 = call i32 @fclose(i32* %33)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [43 x i8] c"ERROR: failed to open %s for writing data\0A\00"
@EIO = global i32 0
@str = [44 x i8] c"ERROR: failed to write all data to bin file\00"
define void @WriteBinData(i8* %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, 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 %10
6: ; preds = %3
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = load i32, i32* @EIO
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %3
%11 = tail call i32 @fwrite(i8* %1, i32 1, i32 %2, i32* %4)
%12 = icmp eq i32 %11, %2
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @fclose(i32* %4)
%16 = load i32, i32* @EIO
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %10
%19 = tail call i32 @fclose(i32* %4)
ret void
}