#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 */

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int open(char const *, int, int);
int write(int, void const *, int);
int file_append(const char *file, const void *data, int size) {
  int fd, ret = 0;
  if ((fd = open(file, O_RDWR | O_APPEND | O_CREAT, 0644)) == -1) {
    return -1;
  }
  if (write(fd, data, size) != size) {
    ret = -1;
  }
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<file_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
or     (%rax),%esi
mov    0x2e91(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    118b <file_append+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <file_append+0x88>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     11aa <file_append+0x7a>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_append>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2ea2(%rip),%rcx
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %edx,%r15d
mov    %rsi,%r14
mov    0x2ea2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     118b <file_append+0x5b>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
xor    %ebx,%ebx
cmp    %r15d,%eax
setne  %bl
neg    %ebx
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1190 <file_append+0x60>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
define i32 @file_append(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_APPEND
%13 = or i32 %11, %12
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15, i32 420)
store i32 %16, i32* %8
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %31
19: ; preds = %3
%20 = load i32, i32* %8
%21 = load i8*, i8** %6
%22 = load i32, i32* %7
%23 = call i32 @write(i32 %20, i8* %21, i32 %22)
%24 = load i32, i32* %7
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %19
store i32 -1, i32* %9
br label %27
27: ; preds = %26, %19
%28 = load i32, i32* %8
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %9
store i32 %30, i32* %4
br label %31
31: ; preds = %27, %18
%32 = load i32, i32* %4
ret i32 %32
}

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