#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 ssize_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int open(char const *, int, int);
int /*<<< orphan*/ stderr;
int write(int, void const *, size_t);
int write_buf(const char *filename, const void *buf, size_t nbyte) {
  int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (fd < 0) {
    fprintf(stderr, "Failed to open %s\n", filename);
    return -1;
  }
  ssize_t ret = write(fd, buf, nbyte);
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<write_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
or     (%rax),%esi
mov    0x2e7f(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11af <write_buf+0x6f>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe62(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <write_buf+0x90>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e72(%rip),%rcx
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e87(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
test   %eax,%eax
js     1197 <write_buf+0x57>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
jmp    11b4 <write_buf+0x74>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"Failed to open %s\0A\00"
define i32 @write_buf(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15, i32 420)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %5
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 -1, i32* %4
br label %31
23: ; preds = %3
%24 = load i32, i32* %8
%25 = load i8*, i8** %6
%26 = load i64, i64* %7
%27 = call i32 @write(i32 %24, i8* %25, i64 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %8
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %9
store i32 %30, i32* %4
br label %31
31: ; preds = %23, %19
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"Failed to open %s\0A\00"
define i32 @write_buf(i8* %0, i8* nocapture %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 420)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %17
14: ; preds = %3
%15 = tail call i32 @write(i32 %9, i8* %1, i64 %2)
%16 = tail call i32 @close(i32 %9)
br label %17
17: ; preds = %14, %11
%18 = phi i32 [ -1, %11 ], [ %15, %14 ]
ret i32 %18
}