#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__ EINTR;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
int open(char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, void *, size_t);
int zwritefile(char *filename, void *buf, size_t nb, size_t *sizeret) {
  size_t nleft;
  ssize_t res;
  int fd;
  fd = open(filename, O_WRONLY | O_TRUNC);
  if (fd < 0) {
    fprintf(stderr, "failed to create %s\n", filename);
    return -1;
  }
  nleft = nb;
  if (nleft) {
    buf = malloc(nleft);
    if (buf) {
      while (nleft) {
        res = write(fd, buf, nleft);
        if (res < 0) {
          if (errno == EINTR) {
            continue;
          } else {
            free(buf);
            return -1;
          }
        } else {
          nleft -= res;
        }
      }
    }
  }
  close(fd);
  if (sizeret) {
    *sizeret = nb;
  }
  return 0;
}


x86 assembly (clang, -O0):
<zwritefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e6c(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11c5 <zwritefile+0x65>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4c(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <zwritefile+0x136>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1271 <zwritefile+0x111>
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     126c <zwritefile+0x10c>
jmp    11f5 <zwritefile+0x95>
cmpq   $0x0,-0x30(%rbp)
je     1267 <zwritefile+0x107>
mov    -0x3c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    1253 <zwritefile+0xf3>
mov    0x2daa(%rip),%rax
mov    (%rax),%rax
mov    0x2d98(%rip),%rcx
cmp    (%rcx),%rax
jne    123e <zwritefile+0xde>
jmp    11f5 <zwritefile+0x95>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <zwritefile+0x136>
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    11f5 <zwritefile+0x95>
jmp    126c <zwritefile+0x10c>
jmp    1271 <zwritefile+0x111>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
cmpq   $0x0,-0x28(%rbp)
je     128f <zwritefile+0x12f>
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zwritefile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rdi,%rbx
mov    0x2e3d(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1204 <zwritefile+0xa4>
mov    %eax,%ebp
mov    %r14,(%rsp)
test   %r15,%r15
je     11ed <zwritefile+0x8d>
mov    %r15,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11ed <zwritefile+0x8d>
mov    %rax,%r12
mov    0x2e1f(%rip),%r13
mov    0x2e10(%rip),%r14
mov    %r15,%rbx
jmp    11c8 <zwritefile+0x68>
nopl   (%rax)
sub    %rax,%rbx
test   %rbx,%rbx
je     11ed <zwritefile+0x8d>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
test   %rax,%rax
jns    11c0 <zwritefile+0x60>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     11c3 <zwritefile+0x63>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    121c <zwritefile+0xbc>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    (%rsp),%rcx
test   %rcx,%rcx
je     1221 <zwritefile+0xc1>
mov    %r15,(%rcx)
jmp    1221 <zwritefile+0xc1>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"failed to create %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @zwritefile(i8* %0, i8* %1, i64 %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64* %3, i64** %9
%13 = load i8*, i8** %6
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %13, i32 %16)
store i32 %17, i32* %12
%18 = load i32, i32* %12
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %4
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %6
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 -1, i32* %5
br label %68
24: ; preds = %4
%25 = load i64, i64* %8
store i64 %25, i64* %10
%26 = load i64, i64* %10
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %59
28: ; preds = %24
%29 = load i64, i64* %10
%30 = call i8* @malloc(i64 %29)
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %58
33: ; preds = %28
br label %34
34: ; preds = %56, %48, %33
%35 = load i64, i64* %10
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %57
37: ; preds = %34
%38 = load i32, i32* %12
%39 = load i8*, i8** %7
%40 = load i64, i64* %10
%41 = call i64 @write(i32 %38, i8* %39, i64 %40)
store i64 %41, i64* %11
%42 = load i64, i64* %11
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %52
44: ; preds = %37
%45 = load i64, i64* @errno
%46 = load i64, i64* @EINTR
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %44
br label %34
49: ; preds = %44
%50 = load i8*, i8** %7
%51 = call i32 @free(i8* %50)
store i32 -1, i32* %5
br label %68
52: ; preds = %37
%53 = load i64, i64* %11
%54 = load i64, i64* %10
%55 = sub i64 %54, %53
store i64 %55, i64* %10
br label %56
56: ; preds = %52
br label %34
57: ; preds = %34
br label %58
58: ; preds = %57, %28
br label %59
59: ; preds = %58, %24
%60 = load i32, i32* %12
%61 = call i32 @close(i32 %60)
%62 = load i64*, i64** %9
%63 = icmp ne i64* %62, null
br i1 %63, label %64, label %67
64: ; preds = %59
%65 = load i64, i64* %8
%66 = load i64*, i64** %9
store i64 %65, i64* %66
br label %67
67: ; preds = %64, %59
store i32 0, i32* %5
br label %68
68: ; preds = %67, %49, %20
%69 = load i32, i32* %5
ret i32 %69
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"failed to create %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @zwritefile(i8* %0, i8* nocapture readnone %1, i64 %2, i64* writeonly %3) {
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %37
13: ; preds = %4
%14 = icmp eq i64 %2, 0
br i1 %14, label %33, label %15
15: ; preds = %13
%16 = tail call i8* @malloc(i64 %2)
%17 = icmp eq i8* %16, null
br i1 %17, label %33, label %21
18: ; preds = %25, %31
%19 = phi i64 [ %22, %25 ], [ %32, %31 ]
%20 = icmp eq i64 %19, 0
br i1 %20, label %33, label %21
21: ; preds = %15, %18
%22 = phi i64 [ %19, %18 ], [ %2, %15 ]
%23 = tail call i64 @write(i32 %8, i8* %16, i64 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br i1 %28, label %18, label %29
29: ; preds = %25
%30 = tail call i32 @free(i8* %16)
br label %37
31: ; preds = %21
%32 = sub i64 %22, %23
br label %18
33: ; preds = %18, %15, %13
%34 = tail call i32 @close(i32 %8)
%35 = icmp eq i64* %3, null
br i1 %35, label %37, label %36
36: ; preds = %33
store i64 %2, i64* %3
br label %37
37: ; preds = %33, %36, %29, %10
%38 = phi i32 [ -1, %10 ], [ -1, %29 ], [ 0, %36 ], [ 0, %33 ]
ret i32 %38
}