#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 O_CLOEXEC;
int O_NOCTTY;
int O_NOFOLLOW;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int);
scalar_t__ write_nointr(int, void const *, size_t);
int write_file(const char *path, const void *buf, size_t count) {
  int fd;
  ssize_t ret;
  fd = open(path, O_WRONLY | O_CLOEXEC | O_NOCTTY | O_NOFOLLOW);
  if (fd < 0)
    return -1;
  ret = write_nointr(fd, buf, count);
  close(fd);
  if (ret < 0 || (size_t)ret != count)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<write_file>:
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    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
mov    0x2e77(%rip),%rax
or     (%rax),%esi
mov    0x2e8e(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    118a <write_file+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <write_file+0xa2>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <write_nointr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
cmpq   $0x0,-0x30(%rbp)
jl     11bf <write_file+0x8f>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11cb <write_file+0x9b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <write_file+0xa2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e8a(%rip),%rcx
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e88(%rip),%rcx
or     (%rcx),%eax
mov    %rdx,%r15
mov    %rsi,%r14
mov    0x2e99(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1050 <open@plt>
test   %eax,%eax
js     119a <write_file+0x6a>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <write_nointr@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1030 <close@plt>
test   %rbx,%rbx
sets   %al
cmp    %r15,%rbx
setne  %cl
or     %al,%cl
movzbl %cl,%eax
neg    %eax
jmp    119f <write_file+0x6f>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CLOEXEC = global i32 0
@O_NOCTTY = global i32 0
@O_NOFOLLOW = 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 = load i32, i32* @O_CLOEXEC
%13 = or i32 %11, %12
%14 = load i32, i32* @O_NOCTTY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_NOFOLLOW
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %10, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 -1, i32* %4
br label %37
22: ; preds = %3
%23 = load i32, i32* %8
%24 = load i8*, i8** %6
%25 = load i64, i64* %7
%26 = call i64 @write_nointr(i32 %23, i8* %24, i64 %25)
store i64 %26, i64* %9
%27 = load i32, i32* %8
%28 = call i32 @close(i32 %27)
%29 = load i64, i64* %9
%30 = icmp slt i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %22
%32 = load i64, i64* %9
%33 = load i64, i64* %7
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31, %22
store i32 -1, i32* %4
br label %37
36: ; preds = %31
store i32 0, i32* %4
br label %37
37: ; preds = %36, %35, %21
%38 = load i32, i32* %4
ret i32 %38
}

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