#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_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int write(int, void *, unsigned int);
int save_file(void *buf, unsigned int size, char *filename) {
  int fd;
  int ret;
  fd = open(filename, O_WRONLY | O_CREAT, 0644);
  if (fd < 0)
    return -1;
  ret = write(fd, buf, size);
  if (ret != size)
    goto out_error;
  close(fd);
  return 0;
out_error:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<save_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2ea1(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    117c <save_file+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <save_file+0x92>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     119f <save_file+0x6f>
jmp    11b3 <save_file+0x83>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c2 <save_file+0x92>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e8c(%rip),%rax
mov    0x2ead(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdx,%rdi
mov    $0x1a4,%edx
call   1050 <open@plt>
test   %eax,%eax
js     1183 <save_file+0x53>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
xor    %ebp,%ebp
cmp    %r15d,%eax
setne  %bpl
mov    %ebx,%edi
call   1040 <close@plt>
neg    %ebp
jmp    1188 <save_file+0x58>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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