#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_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int io_write(int, void const *, size_t);
int open(char const *, int, int);
ssize_t io_file_write(const char *filename, const void *buf, size_t len) {
  int fd;
  ssize_t count;
  fd =
      open(filename, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd < 0) {
    return -1;
  }
  count = io_write(fd, buf, len);
  close(fd);
  return count;
}


x86 assembly (clang, -O0):
<io_file_write>:
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    0x2ea9(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
mov    0x2e76(%rip),%rax
or     (%rax),%edx
mov    0x2e5d(%rip),%rax
or     (%rax),%edx
mov    0x2e4c(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    119c <io_file_write+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bd <io_file_write+0x8d>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <io_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):
<io_file_write>:
push   %rbp
push   %r14
push   %rbx
mov    0x2ebd(%rip),%rcx
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e73(%rip),%rbp
mov    0x2e8c(%rip),%rcx
mov    (%rcx),%ecx
or     0x0(%rbp),%ecx
mov    0x2e70(%rip),%rbp
or     0x0(%rbp),%ecx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2e58(%rip),%rdx
or     (%rdx),%ecx
mov    %eax,%esi
mov    %ecx,%edx
call   1050 <open@plt>
test   %eax,%eax
js     119b <io_file_write+0x6b>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <io_write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
jmp    11a0 <io_file_write+0x70>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @io_file_write(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_CREAT
%12 = load i32, i32* @O_WRONLY
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRGRP
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IROTH
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %10, i32 %13, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %3
store i32 -1, i32* %4
br label %33
25: ; preds = %3
%26 = load i32, i32* %8
%27 = load i8*, i8** %6
%28 = load i64, i64* %7
%29 = call i32 @io_write(i32 %26, i8* %27, i64 %28)
store i32 %29, i32* %9
%30 = load i32, i32* %8
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %9
store i32 %32, i32* %4
br label %33
33: ; preds = %25, %24
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @io_file_write(i8* nocapture %0, i8* %1, i64 %2) {
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_WRONLY
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* %0, i32 %6, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = tail call i32 @io_write(i32 %14, i8* %1, i64 %2)
%18 = tail call i32 @close(i32 %14)
br label %19
19: ; preds = %3, %16
%20 = phi i32 [ %17, %16 ], [ -1, %3 ]
ret i32 %20
}