#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ ENOSPC;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(scalar_t__);
int /*<<< orphan*/ unlink(char *);
scalar_t__ write(int, char *, size_t);
int eslib_file_write_full(char *filename, char *buf, size_t size) {
  int fd;
  ssize_t r;
  size_t bytes_written = 0;
  errno = 0;
  fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0750);
  if (fd == -1)
    return -1;
  do {
    printf("write iterate %d\n", bytes_written);
    r = write(fd, buf + bytes_written, size - bytes_written);
    if (r < 0) {
      goto werror;
    } else if (r == 0) {
      errno = ENOSPC;
      goto werror;
    }
    bytes_written += (size_t)r;
  } while (bytes_written < size);
  if (bytes_written > size)
    goto werror;
  close(fd);
  return 0;
werror:
  printf("write error: %s\n", strerror(errno));
  close(fd);
  unlink(filename);
  return -1;
}


x86 assembly (clang, -O0):
<eslib_file_write_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2e45(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
or     (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
mov    $0x1e8,%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11cc <eslib_file_write_full+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <eslib_file_write_full+0x151>
jmp    11d1 <eslib_file_write_full+0x71>
mov    -0x38(%rbp),%rsi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x38(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    120f <eslib_file_write_full+0xaf>
jmp    1279 <eslib_file_write_full+0x119>
cmpq   $0x0,-0x30(%rbp)
jne    1233 <eslib_file_write_full+0xd3>
mov    0x2d9f(%rip),%rax
mov    (%rax),%rcx
mov    0x2d9d(%rip),%rax
mov    %rcx,(%rax)
jmp    1279 <eslib_file_write_full+0x119>
jmp    1238 <eslib_file_write_full+0xd8>
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     11d1 <eslib_file_write_full+0x71>
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1265 <eslib_file_write_full+0x105>
jmp    1279 <eslib_file_write_full+0x119>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b1 <eslib_file_write_full+0x151>
mov    0x2d48(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eslib_file_write_full>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r15
mov    0x2e4d(%rip),%rdx
mov    0x2e5e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
movq   $0x0,(%rdx)
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    $0x1e8,%edx
call   1070 <open@plt>
mov    %eax,%ebp
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ebp
je     1252 <eslib_file_write_full+0xf2>
lea    0xe42(%rip),%r13
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    (%r12,%r14,1),%rsi
mov    %rbx,%rdx
sub    %r14,%rdx
mov    %ebp,%edi
call   1040 <write@plt>
test   %rax,%rax
js     121e <eslib_file_write_full+0xbe>
je     120a <eslib_file_write_full+0xaa>
add    %rax,%r14
cmp    %rbx,%r14
jb     11d0 <eslib_file_write_full+0x70>
ja     121e <eslib_file_write_full+0xbe>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    1252 <eslib_file_write_full+0xf2>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    0x2dad(%rip),%rcx
mov    %rax,(%rcx)
mov    0x2da3(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xdde(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r15,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [18 x i8] c"write iterate %d\0A\00"
@ENOSPC = global i64 0
@.str.1 = [17 x i8] c"write error: %s\0A\00"
define i32 @eslib_file_write_full(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %10
store i64 0, i64* @errno
%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_EXCL
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %11, i32 %16, i32 488)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %66
21: ; preds = %3
br label %22
22: ; preds = %46, %21
%23 = load i64, i64* %10
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %23)
%25 = load i32, i32* %8
%26 = load i8*, i8** %6
%27 = load i64, i64* %10
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i64, i64* %7
%30 = load i64, i64* %10
%31 = sub i64 %29, %30
%32 = call i64 @write(i32 %25, i8* %28, i64 %31)
store i64 %32, i64* %9
%33 = load i64, i64* %9
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %22
br label %58
36: ; preds = %22
%37 = load i64, i64* %9
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = load i64, i64* @ENOSPC
store i64 %40, i64* @errno
br label %58
41: ; preds = %36
br label %42
42: ; preds = %41
%43 = load i64, i64* %9
%44 = load i64, i64* %10
%45 = add i64 %44, %43
store i64 %45, i64* %10
br label %46
46: ; preds = %42
%47 = load i64, i64* %10
%48 = load i64, i64* %7
%49 = icmp ult i64 %47, %48
br i1 %49, label %22, label %50
50: ; preds = %46
%51 = load i64, i64* %10
%52 = load i64, i64* %7
%53 = icmp ugt i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50
br label %58
55: ; preds = %50
%56 = load i32, i32* %8
%57 = call i32 @close(i32 %56)
store i32 0, i32* %4
br label %66
58: ; preds = %54, %39, %35
%59 = load i64, i64* @errno
%60 = call i8* @strerror(i64 %59)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %60)
%62 = load i32, i32* %8
%63 = call i32 @close(i32 %62)
%64 = load i8*, i8** %5
%65 = call i32 @unlink(i8* %64)
store i32 -1, i32* %4
br label %66
66: ; preds = %58, %55, %20
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
@errno = global i64 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [18 x i8] c"write iterate %d\0A\00"
@ENOSPC = global i64 0
@.str.1 = [17 x i8] c"write error: %s\0A\00"
define i32 @eslib_file_write_full(i8* nocapture %0, i8* nocapture %1, i64 %2) {
store i64 0, i64* @errno
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_EXCL
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8, i32 488)
%10 = icmp eq i32 %9, -1
br i1 %10, label %35, label %11
11: ; preds = %3, %22
%12 = phi i64 [ %23, %22 ], [ 0, %3 ]
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %12)
%14 = getelementptr inbounds i8, i8* %1, i64 %12
%15 = sub i64 %2, %12
%16 = tail call i64 @write(i32 %9, i8* %14, i64 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %29, label %18
18: ; preds = %11
%19 = icmp eq i64 %16, 0
br i1 %19, label %20, label %22
20: ; preds = %18
%21 = load i64, i64* @ENOSPC
store i64 %21, i64* @errno
br label %29
22: ; preds = %18
%23 = add i64 %16, %12
%24 = icmp ult i64 %23, %2
br i1 %24, label %11, label %25
25: ; preds = %22
%26 = icmp ugt i64 %23, %2
br i1 %26, label %29, label %27
27: ; preds = %25
%28 = tail call i32 @close(i32 %9)
br label %35
29: ; preds = %11, %25, %20
%30 = load i64, i64* @errno
%31 = tail call i8* @strerror(i64 %30)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = tail call i32 @close(i32 %9)
%34 = tail call i32 @unlink(i8* %0)
br label %35
35: ; preds = %3, %29, %27
%36 = phi i32 [ -1, %29 ], [ 0, %27 ], [ -1, %3 ]
ret i32 %36
}