#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_RDWR;
int O_TRUNC;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char const *);
int write(int, void *, int);
int file_store(const char *name, void *buf, int sz) {
  int fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
  int off, n;
  if (fd < 0) {
    return -1;
  }
  off = 0;
  while (off < sz) {
    n = write(fd, buf + off, sz - off);
    if (n < 0) {
      unlink(name);
      close(fd);
      return -1;
    }
    off += n;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<file_store>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    0x2e91(%rip),%rax
or     (%rax),%esi
mov    0x2e80(%rip),%rax
or     (%rax),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1199 <file_store+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <file_store+0xcc>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11fd <file_store+0xbd>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
sub    -0x24(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11ef <file_store+0xaf>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <file_store+0xcc>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11a0 <file_store+0x60>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_store>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e79(%rip),%rax
mov    0x2e9a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e87(%rip),%rax
or     (%rax),%esi
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
call   1060 <open@plt>
test   %eax,%eax
js     11b1 <file_store+0x71>
mov    %eax,%r12d
test   %ebp,%ebp
jle    11ad <file_store+0x6d>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%r15,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1040 <write@plt>
test   %eax,%eax
js     11b8 <file_store+0x78>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1190 <file_store+0x50>
xor    %ebp,%ebp
jmp    11c5 <file_store+0x85>
mov    $0xffffffff,%ebp
jmp    11cd <file_store+0x8d>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%ebp
mov    %r12d,%edi
call   1050 <close@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
define i32 @file_store(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRWXU
%18 = call i32 @open(i8* %11, i32 %16, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 -1, i32* %4
br label %51
22: ; preds = %3
store i32 0, i32* %9
br label %23
23: ; preds = %44, %22
%24 = load i32, i32* %9
%25 = load i32, i32* %7
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %48
27: ; preds = %23
%28 = load i32, i32* %8
%29 = load i8*, i8** %6
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = getelementptr i8, i8* %29, i64 %31
%33 = load i32, i32* %7
%34 = load i32, i32* %9
%35 = sub nsw i32 %33, %34
%36 = call i32 @write(i32 %28, i8* %32, i32 %35)
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %27
%40 = load i8*, i8** %5
%41 = call i32 @unlink(i8* %40)
%42 = load i32, i32* %8
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %4
br label %51
44: ; preds = %27
%45 = load i32, i32* %10
%46 = load i32, i32* %9
%47 = add nsw i32 %46, %45
store i32 %47, i32* %9
br label %23
48: ; preds = %23
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
store i32 0, i32* %4
br label %51
51: ; preds = %48, %39, %21
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
define i32 @file_store(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRWXU
%10 = tail call i32 @open(i8* %0, i32 %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %29, label %12
12: ; preds = %3
%13 = icmp sgt i32 %2, 0
br i1 %13, label %16, label %26
14: ; preds = %16
%15 = icmp slt i32 %23, %2
br i1 %15, label %16, label %26
16: ; preds = %12, %14
%17 = phi i32 [ %23, %14 ], [ 0, %12 ]
%18 = sext i32 %17 to i64
%19 = getelementptr i8, i8* %1, i64 %18
%20 = sub nsw i32 %2, %17
%21 = tail call i32 @write(i32 %10, i8* %19, i32 %20)
%22 = icmp slt i32 %21, 0
%23 = add nsw i32 %21, %17
br i1 %22, label %24, label %14
24: ; preds = %16
%25 = tail call i32 @unlink(i8* %0)
br label %26
26: ; preds = %14, %12, %24
%27 = phi i32 [ -1, %24 ], [ 0, %12 ], [ 0, %14 ]
%28 = tail call i32 @close(i32 %10)
br label %29
29: ; preds = %26, %3
%30 = phi i32 [ -1, %3 ], [ %27, %26 ]
ret i32 %30
}