#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 O_CREAT;
int O_EXCL;
int O_WRONLY;
int open(char const *, int, int);
scalar_t__ write(int, char *, int);
int write_file(const char *out_path, char *buf, int size) {
  int to;
  if ((to = open(out_path, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0)
    return -1;
  if (write(to, buf, size) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2eb1(%rip),%rax
or     (%rax),%esi
mov    0x2e88(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1040 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1174 <write_file+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <write_file+0x80>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1199 <write_file+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <write_file+0x80>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rcx
mov    0x2ec7(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    0x2e97(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1a4,%edx
call   1040 <open@plt>
test   %eax,%eax
js     1165 <write_file+0x45>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
sar    $0x3f,%rax
jmp    116a <write_file+0x4a>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
define i32 @write_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @O_EXCL
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %9, i32 %14, i32 420)
store i32 %15, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %26
18: ; preds = %3
%19 = load i32, i32* %8
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = call i64 @write(i32 %19, i8* %20, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 -1, i32* %4
br label %26
25: ; preds = %18
store i32 0, i32* %4
br label %26
26: ; preds = %25, %24, %17
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
define i32 @write_file(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_EXCL
%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 %15, label %11
11: ; preds = %3
%12 = tail call i64 @write(i32 %9, i8* %1, i32 %2)
%13 = ashr i64 %12, 63
%14 = trunc i64 %13 to i32
br label %15
15: ; preds = %11, %3
%16 = phi i32 [ -1, %3 ], [ %14, %11 ]
ret i32 %16
}