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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
scalar_t__ write(int, void const *, size_t);
int write_file(const char *path, const void *data, size_t size) {
  int fd = open(path, O_WRONLY);
  if (fd == -1) {
    return -1;
  }
  ssize_t n = write(fd, data, size);
  close(fd);
  return (n >= 0 && (size_t)n == size) ? 0 : -1;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    116f <write_file+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <write_file+0x8c>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x31(%rbp)
jl     11a9 <write_file+0x79>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%dl
mov    $0xffffffff,%eax
xor    %ecx,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     117e <write_file+0x4e>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
test   %rbx,%rbx
sets   %al
cmp    %r15,%rbx
setne  %cl
or     %al,%cl
movzbl %cl,%eax
neg    %eax
jmp    1183 <write_file+0x53>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
define i32 @write_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
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 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %33
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load i8*, i8** %6
%19 = load i64, i64* %7
%20 = call i64 @write(i32 %17, i8* %18, i64 %19)
store i64 %20, i64* %9
%21 = load i32, i32* %8
%22 = call i32 @close(i32 %21)
%23 = load i64, i64* %9
%24 = icmp sge i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = load i64, i64* %9
%27 = load i64, i64* %7
%28 = icmp eq i64 %26, %27
br label %29
29: ; preds = %25, %16
%30 = phi i1 [ false, %16 ], [ %28, %25 ]
%31 = zext i1 %30 to i64
%32 = select i1 %30, i32 0, i32 -1
store i32 %32, i32* %4
br label %33
33: ; preds = %29, %15
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
define i32 @write_file(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %14, label %7
7: ; preds = %3
%8 = tail call i64 @write(i32 %5, i8* %1, i64 %2)
%9 = tail call i32 @close(i32 %5)
%10 = icmp slt i64 %8, 0
%11 = icmp ne i64 %8, %2
%12 = or i1 %10, %11
%13 = sext i1 %12 to i32
br label %14
14: ; preds = %3, %7
%15 = phi i32 [ %13, %7 ], [ -1, %3 ]
ret i32 %15
}