#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int write(int, char const *, size_t);
int write_file(const char *path, const char *buf, size_t buflen) {
  int fd;
  ssize_t numwritten;
  fd = open(path, O_WRONLY);
  if (fd == -1)
    return 0;
  numwritten = write(fd, buf, buflen - 1);
  close(fd);
  if (numwritten < 1)
    return 0;
  return (unsigned int)numwritten;
}


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    0x2e89(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    116f <write_file+0x3f>
movl   $0x0,-0x4(%rbp)
jmp    11aa <write_file+0x7a>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x1,-0x28(%rbp)
jge    11a4 <write_file+0x74>
movl   $0x0,-0x4(%rbp)
jmp    11aa <write_file+0x7a>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1172 <write_file+0x42>
mov    %eax,%ebp
add    $0xffffffffffffffff,%rbx
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
test   %ebx,%ebx
cmovns %ebx,%eax
jmp    1174 <write_file+0x44>
xor    %eax,%eax
pop    %rbx
pop    %r14
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 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 = 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 0, i32* %4
br label %29
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load i8*, i8** %6
%19 = load i64, i64* %7
%20 = sub i64 %19, 1
%21 = call i32 @write(i32 %17, i8* %18, i64 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %8
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 1
br i1 %25, label %26, label %27
26: ; preds = %16
store i32 0, i32* %4
br label %29
27: ; preds = %16
%28 = load i32, i32* %9
store i32 %28, i32* %4
br label %29
29: ; preds = %27, %26, %15
%30 = load i32, i32* %4
ret i32 %30
}

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 %13, label %7
7: ; preds = %3
%8 = add i64 %2, -1
%9 = tail call i32 @write(i32 %5, i8* %1, i64 %8)
%10 = tail call i32 @close(i32 %5)
%11 = icmp sgt i32 %9, 0
%12 = select i1 %11, i32 %9, i32 0
br label %13
13: ; preds = %7, %3
%14 = phi i32 [ 0, %3 ], [ %12, %7 ]
ret i32 %14
}