#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_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 savefile(const char *filepath, const void *buf, size_t size) {
  int fd = open(filepath, O_CREAT | O_WRONLY | O_TRUNC,
                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):
<savefile>:
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    0x2e50(%rip),%rax
or     (%rax),%esi
mov    0x2e8f(%rip),%rax
or     (%rax),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
mov    0x2e65(%rip),%rax
or     (%rax),%edx
mov    0x2e4c(%rip),%rax
or     (%rax),%edx
mov    0x2e3b(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11a5 <savefile+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <savefile+0x96>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <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):
<savefile>:
push   %rbp
push   %r14
push   %rbx
mov    0x2ebd(%rip),%rcx
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2ea3(%rip),%rcx
or     (%rcx),%eax
mov    0x2e62(%rip),%rbp
mov    0x2e7b(%rip),%rcx
mov    (%rcx),%ecx
or     0x0(%rbp),%ecx
mov    0x2e5f(%rip),%rbp
or     0x0(%rbp),%ecx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2e47(%rip),%rdx
or     (%rdx),%ecx
mov    %eax,%esi
mov    %ecx,%edx
call   1050 <open@plt>
test   %eax,%eax
js     11a4 <savefile+0x74>
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    11a9 <savefile+0x79>
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
define i32 @savefile(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* @O_TRUNC
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRGRP
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IROTH
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %10, i32 %15, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %3
store i32 -1, i32* %4
br label %35
27: ; preds = %3
%28 = load i32, i32* %8
%29 = load i8*, i8** %6
%30 = load i64, i64* %7
%31 = call i32 @write(i32 %28, i8* %29, i64 %30)
store i32 %31, i32* %9
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %9
store i32 %34, i32* %4
br label %35
35: ; preds = %27, %26
%36 = load i32, i32* %4
ret i32 %36
}

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
define i32 @savefile(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_WRONLY
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IRGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IROTH
%15 = or i32 %13, %14
%16 = tail call i32 @open(i8* %0, i32 %8, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %3
%19 = tail call i32 @write(i32 %16, i8* %1, i64 %2)
%20 = tail call i32 @close(i32 %16)
br label %21
21: ; preds = %3, %18
%22 = phi i32 [ %19, %18 ], [ -1, %3 ]
ret i32 %22
}