#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_APPEND;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int);
int write(int, char *, int);
ssize_t write_text(const char *path, char *buf, ssize_t len) {
  int fd;
  fd = open(path, O_WRONLY | O_APPEND);
  if (fd < 0)
    return fd;
  len = write(fd, buf, len);
  if (len < 0) {
    close(fd);
    return len;
  }
  close(fd);
  return len;
}


x86 assembly (clang, -O0):
<write_text>:
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    0x2e79(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1176 <write_text+0x46>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b3 <write_text+0x83>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a5 <write_text+0x75>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b3 <write_text+0x83>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_text>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    0x2e97(%rip),%rax
mov    0x2e88(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1171 <write_text+0x41>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
jmp    1173 <write_text+0x43>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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