#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 EINTR;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int errno;
int open(char const *, int, int);
int write(int, char const *, int);
int zfile_put_contents(const char *pathname, const void *data, int len) {
  int fd;
  int ret;
  int wlen = 0;
  int errno2;
  while (((fd = open(pathname, O_CREAT | O_RDWR | O_TRUNC, 0777)) == -1) &&
         (errno == EINTR)) {
    continue;
  }
  if (fd == -1) {
    return -1;
  }
  while (len > wlen) {
    ret = write(fd, (const char *)data + wlen, len - wlen);
    if (ret > -1) {
      wlen += ret;
      continue;
    }
    errno2 = errno;
    if (errno == EINTR) {
      continue;
    }
    close(fd);
    errno = errno2;
    return -1;
  }
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<zfile_put_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
mov    0x2e7a(%rip),%rax
or     (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1050 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x2d(%rbp)
jne    119e <zfile_put_contents+0x6e>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    11ae <zfile_put_contents+0x7e>
jmp    11b3 <zfile_put_contents+0x83>
jmp    114a <zfile_put_contents+0x1a>
cmpl   $0xffffffff,-0x20(%rbp)
jne    11c9 <zfile_put_contents+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <zfile_put_contents+0x136>
jmp    11ce <zfile_put_contents+0x9e>
mov    -0x1c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    1257 <zfile_put_contents+0x127>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
sub    -0x28(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jle    120e <zfile_put_contents+0xde>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11ce <zfile_put_contents+0x9e>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%eax
jne    1237 <zfile_put_contents+0x107>
jmp    11ce <zfile_put_contents+0x9e>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2d7f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <zfile_put_contents+0x136>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zfile_put_contents>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
mov    %rdi,%rbp
mov    0x2ea7(%rip),%r12
mov    0x2e80(%rip),%r13
mov    0x2e91(%rip),%r14
mov    0x2e5a(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%r13),%esi
or     (%r12),%esi
or     (%r14),%esi
mov    %rbp,%rdi
mov    $0x1ff,%edx
call   1050 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    119d <zfile_put_contents+0x6d>
mov    (%r15),%eax
mov    0x2e2f(%rip),%rcx
cmp    %eax,(%rcx)
je     1170 <zfile_put_contents+0x40>
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ebx
je     121d <zfile_put_contents+0xed>
mov    0xc(%rsp),%r14d
test   %r14d,%r14d
jle    11fa <zfile_put_contents+0xca>
xor    %r12d,%r12d
mov    0x10(%rsp),%r13
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r13,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    11e1 <zfile_put_contents+0xb1>
mov    0x2df0(%rip),%rax
mov    (%rax),%ebp
cmp    (%r15),%ebp
je     11c0 <zfile_put_contents+0x90>
jmp    1208 <zfile_put_contents+0xd8>
add    %eax,%r12d
movslq %r12d,%rax
mov    0x10(%rsp),%rcx
lea    (%rcx,%rax,1),%r13
mov    0xc(%rsp),%r14d
sub    %eax,%r14d
jg     11c0 <zfile_put_contents+0x90>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0x1,%eax
jmp    121d <zfile_put_contents+0xed>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2db2(%rip),%rax
mov    %ebp,(%rax)
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define i32 @zfile_put_contents(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
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %10
br label %12
12: ; preds = %27, %3
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_RDWR
%16 = or i32 %14, %15
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %13, i32 %18, i32 511)
store i32 %19, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %12
%22 = load i32, i32* @errno
%23 = load i32, i32* @EINTR
%24 = icmp eq i32 %22, %23
br label %25
25: ; preds = %21, %12
%26 = phi i1 [ false, %12 ], [ %24, %21 ]
br i1 %26, label %27, label %28
27: ; preds = %25
br label %12
28: ; preds = %25
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 -1, i32* %4
br label %66
32: ; preds = %28
br label %33
33: ; preds = %58, %49, %32
%34 = load i32, i32* %7
%35 = load i32, i32* %10
%36 = icmp sgt i32 %34, %35
br i1 %36, label %37, label %63
37: ; preds = %33
%38 = load i32, i32* %8
%39 = load i8*, i8** %6
%40 = load i32, i32* %10
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i32, i32* %7
%44 = load i32, i32* %10
%45 = sub nsw i32 %43, %44
%46 = call i32 @write(i32 %38, i8* %42, i32 %45)
store i32 %46, i32* %9
%47 = load i32, i32* %9
%48 = icmp sgt i32 %47, -1
br i1 %48, label %49, label %53
49: ; preds = %37
%50 = load i32, i32* %9
%51 = load i32, i32* %10
%52 = add nsw i32 %51, %50
store i32 %52, i32* %10
br label %33
53: ; preds = %37
%54 = load i32, i32* @errno
store i32 %54, i32* %11
%55 = load i32, i32* @errno
%56 = load i32, i32* @EINTR
%57 = icmp eq i32 %55, %56
br i1 %57, label %58, label %59
58: ; preds = %53
br label %33
59: ; preds = %53
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %11
store i32 %62, i32* @errno
store i32 -1, i32* %4
br label %66
63: ; preds = %33
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
store i32 1, i32* %4
br label %66
66: ; preds = %63, %59, %31
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define i32 @zfile_put_contents(i8* nocapture %0, i8* nocapture %1, i32 %2) {
br label %4
4: ; preds = %4, %3
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 511)
%11 = icmp eq i32 %10, -1
%12 = load i32, i32* @errno
%13 = load i32, i32* @EINTR
%14 = icmp eq i32 %12, %13
%15 = select i1 %11, i1 %14, i1 false
br i1 %15, label %4, label %16
16: ; preds = %4
br i1 %11, label %40, label %17
17: ; preds = %16
%18 = icmp sgt i32 %2, 0
br i1 %18, label %19, label %38
19: ; preds = %17, %26
%20 = phi i32 [ %31, %26 ], [ %2, %17 ]
%21 = phi i8* [ %30, %26 ], [ %1, %17 ]
%22 = phi i32 [ %27, %26 ], [ 0, %17 ]
br label %23
23: ; preds = %19, %32
%24 = tail call i32 @write(i32 %10, i8* %21, i32 %20)
%25 = icmp sgt i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = add nsw i32 %24, %22
%28 = icmp slt i32 %27, %2
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %1, i64 %29
%31 = sub nsw i32 %2, %27
br i1 %28, label %19, label %38
32: ; preds = %23
%33 = load i32, i32* @errno
%34 = load i32, i32* @EINTR
%35 = icmp eq i32 %33, %34
br i1 %35, label %23, label %36
36: ; preds = %32
%37 = tail call i32 @close(i32 %10)
store i32 %33, i32* @errno
br label %40
38: ; preds = %26, %17
%39 = tail call i32 @close(i32 %10)
br label %40
40: ; preds = %16, %38, %36
%41 = phi i32 [ -1, %36 ], [ 1, %38 ], [ -1, %16 ]
ret i32 %41
}