#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fwrite(unsigned char const *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printerr(char *, char const *);
int /*<<< orphan*/ printerrno(char *, char const *);
int buffer_to_file(const char *filename, const unsigned char *buffer,
                   int size_to_write) {
  FILE *f;
  int res;
  if ((f = fopen(filename, "wb")) == NULL) {
    printerrno("fopen(%s)", filename);
    return -1;
  }
  if ((res = fwrite(buffer, size_to_write, 1, f)) != 1) {
    if (ferror(f))
      printerrno("write(%s)", filename);
    else
      printerr("Could not write to file %s\n", filename);
    return -1;
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<buffer_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ad <buffer_to_file+0x4d>
mov    -0x10(%rbp),%rsi
lea    0xe67(%rip),%rdi
call   1060 <printerrno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <buffer_to_file+0xc2>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x1,%eax
je     1212 <buffer_to_file+0xb2>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11f6 <buffer_to_file+0x96>
mov    -0x10(%rbp),%rsi
lea    0xe21(%rip),%rdi
call   1060 <printerrno@plt>
jmp    1206 <buffer_to_file+0xa6>
mov    -0x10(%rbp),%rsi
lea    0xe16(%rip),%rdi
call   1050 <printerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <buffer_to_file+0xc2>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_to_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a8 <buffer_to_file+0x48>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
cmp    $0x1,%eax
jne    11b1 <buffer_to_file+0x51>
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    11cf <buffer_to_file+0x6f>
lea    0xe54(%rip),%rdi
jmp    11c2 <buffer_to_file+0x62>
call   1030 <ferror@plt>
test   %rax,%rax
je     11da <buffer_to_file+0x7a>
lea    0xe4b(%rip),%rdi
mov    %r14,%rsi
call   1060 <printerrno@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe36(%rip),%rdi
mov    %r14,%rsi
call   1050 <printerr@plt>
jmp    11ca <buffer_to_file+0x6a>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [10 x i8] c"fopen(%s)\00"
@.str.2 = [10 x i8] c"write(%s)\00"
@.str.3 = [28 x i8] c"Could not write to file %s\0A\00"
define i32 @buffer_to_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 @printerrno(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 -1, i32* %4
br label %36
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = load i32, i32* %7
%19 = load i32*, i32** %8
%20 = call i32 @fwrite(i8* %17, i32 %18, i32 1, i32* %19)
store i32 %20, i32* %9
%21 = icmp ne i32 %20, 1
br i1 %21, label %22, label %33
22: ; preds = %16
%23 = load i32*, i32** %8
%24 = call i64 @ferror(i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i8*, i8** %5
%28 = call i32 @printerrno(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %27)
br label %32
29: ; preds = %22
%30 = load i8*, i8** %5
%31 = call i32 @printerr(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %29, %26
store i32 -1, i32* %4
br label %36
33: ; preds = %16
%34 = load i32*, i32** %8
%35 = call i32 @fclose(i32* %34)
store i32 0, i32* %4
br label %36
36: ; preds = %33, %32, %13
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [10 x i8] c"fopen(%s)\00"
@.str.2 = [10 x i8] c"write(%s)\00"
@.str.3 = [28 x i8] c"Could not write to file %s\0A\00"
define i32 @buffer_to_file(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 %8
6: ; preds = %3
%7 = tail call i32 @printerrno(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %20
8: ; preds = %3
%9 = tail call i32 @fwrite(i8* %1, i32 %2, i32 1, i32* %4)
%10 = icmp eq i32 %9, 1
br i1 %10, label %18, label %11
11: ; preds = %8
%12 = tail call i64 @ferror(i32* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @printerrno(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %20
16: ; preds = %11
%17 = tail call i32 @printerr(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %20
18: ; preds = %8
%19 = tail call i32 @fclose(i32* %4)
br label %20
20: ; preds = %14, %16, %18, %6
%21 = phi i32 [ -1, %6 ], [ 0, %18 ], [ -1, %16 ], [ -1, %14 ]
ret i32 %21
}