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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write_all(int, char *, size_t, char const *);
void write_file(const char *filename, char *buf, size_t buf_size) {
  int fd;
  fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  if (fd < 0) {
    perror("ERROR: open ");
    fprintf(stderr, "ERROR: Couldn't open file \"%s\" for writing \n",
            filename);
    exit(EXIT_FAILURE);
  }
  write_all(fd, buf, buf_size, filename);
  close(fd);
  return;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11dd <write_file+0x7d>
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe43(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x8(%rbp),%rcx
call   1050 <write_all@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e4a(%rip),%rcx
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e67(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11be <write_file+0x5e>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1050 <write_all@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <close@plt>
lea    0xe3b(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [13 x i8] c"ERROR: open \00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"ERROR: Couldn't open file \22%s\22 for writing \0A\00"
@EXIT_FAILURE = global i32 0
define void @write_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %8, i32 %13, i32 438)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %4
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %3
%25 = load i32, i32* %7
%26 = load i8*, i8** %5
%27 = load i64, i64* %6
%28 = load i8*, i8** %4
%29 = call i32 @write_all(i32 %25, i8* %26, i64 %27, i8* %28)
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [13 x i8] c"ERROR: open \00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"ERROR: Couldn't open file \22%s\22 for writing \0A\00"
@EXIT_FAILURE = global i32 0
define void @write_file(i8* %0, i8* %1, i64 %2) {
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8, i32 438)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %3
%18 = tail call i32 @write_all(i32 %9, i8* %1, i64 %2, i8* %0)
%19 = tail call i32 @close(i32 %9)
ret void
}