#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_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char const *, int, int);
int write(int, void const *, size_t);
ssize_t qfile_save(const char *filepath, const void *buf, size_t size,
                   bool append) {
  int fd;
  if (append == false) {
    fd = open(filepath, O_CREAT | O_WRONLY | O_TRUNC,
              (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
  } else {
    fd = open(filepath, O_CREAT | O_WRONLY | O_APPEND,
              (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
  }
  if (fd < 0)
    return -1;
  ssize_t count = write(fd, buf, size);
  close(fd);
  return count;
}


x86 assembly (clang, -O0):
<qfile_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11a1 <qfile_save+0x71>
mov    -0x10(%rbp),%rdi
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
or     (%rax),%esi
mov    0x2e82(%rip),%rax
or     (%rax),%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    0x2e58(%rip),%rax
or     (%rax),%edx
mov    0x2e3f(%rip),%rax
or     (%rax),%edx
mov    0x2e2e(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x28(%rbp)
jmp    11ec <qfile_save+0xbc>
mov    -0x10(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    0x2df3(%rip),%rax
or     (%rax),%esi
mov    0x2de2(%rip),%rax
or     (%rax),%esi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    0x2e08(%rip),%rax
or     (%rax),%edx
mov    0x2def(%rip),%rax
or     (%rax),%edx
mov    0x2dde(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1202 <qfile_save+0xd2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <qfile_save+0xf3>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qfile_save>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2eb7(%rip),%rax
mov    0x2e60(%rip),%rdx
mov    (%rdx),%esi
or     (%rax),%esi
test   %ecx,%ecx
jne    1159 <qfile_save+0x29>
mov    0x2e99(%rip),%rax
jmp    1160 <qfile_save+0x30>
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    0x2e4f(%rip),%rax
mov    0x2e68(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e4d(%rip),%rax
or     (%rax),%edx
mov    0x2e3c(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
test   %eax,%eax
js     11ab <qfile_save+0x7b>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
jmp    11b0 <qfile_save+0x80>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@O_APPEND = global i32 0
define i32 @qfile_save(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%12 = load i32, i32* %9
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %4
%15 = load i8*, i8** %6
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_WRONLY
%18 = or i32 %16, %17
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRGRP
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IROTH
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %15, i32 %20, i32 %27)
store i32 %28, i32* %10
br label %44
29: ; preds = %4
%30 = load i8*, i8** %6
%31 = load i32, i32* @O_CREAT
%32 = load i32, i32* @O_WRONLY
%33 = or i32 %31, %32
%34 = load i32, i32* @O_APPEND
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IRUSR
%37 = load i32, i32* @S_IWUSR
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IRGRP
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IROTH
%42 = or i32 %40, %41
%43 = call i32 @open(i8* %30, i32 %35, i32 %42)
store i32 %43, i32* %10
br label %44
44: ; preds = %29, %14
%45 = load i32, i32* %10
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %44
store i32 -1, i32* %5
br label %56
48: ; preds = %44
%49 = load i32, i32* %10
%50 = load i8*, i8** %7
%51 = load i64, i64* %8
%52 = call i32 @write(i32 %49, i8* %50, i64 %51)
store i32 %52, i32* %11
%53 = load i32, i32* %10
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %11
store i32 %55, i32* %5
br label %56
56: ; preds = %48, %47
%57 = load i32, i32* %5
ret i32 %57
}

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