#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 */
scalar_t__ EINTR;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fsync(int);
int open(char const *, int, int);
int /*<<< orphan*/ printf(char *, char const *, char *);
char *strerror(scalar_t__);
int write(int, unsigned char const *, size_t);
int file_write(const char *filename, const void *data, size_t sz) {
  int fd;
  int ret;
  const unsigned char *what = (const unsigned char *)data;
  fd = open(filename, O_RDWR | O_CREAT | O_TRUNC,
            S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd < 0) {
    printf("file_write: Can't open file %s: %s\n", filename, strerror(errno));
    return -1;
  }
  while (sz) {
    ret = write(fd, what, sz);
    if (ret <= 0 && errno != EINTR) {
      printf("file_write: Failed to write to %s: %s\n", filename,
             strerror(errno));
      close(fd);
      return -1;
    }
    what += ret;
    sz -= ret;
  }
  fsync(fd);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    0x2e25(%rip),%rax
or     (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
mov    0x2df3(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1207 <file_write+0xa7>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <file_write+0x168>
jmp    120c <file_write+0xac>
cmpq   $0x0,-0x20(%rbp)
je     12b1 <file_write+0x151>
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jg     128c <file_write+0x12c>
mov    0x2d85(%rip),%rax
mov    (%rax),%rax
mov    0x2d63(%rip),%rcx
cmp    (%rcx),%rax
je     128c <file_write+0x12c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d63(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xdb1(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <file_write+0x168>
mov    -0x28(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
movslq -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    120c <file_write+0xac>
mov    -0x24(%rbp),%edi
call   1060 <fsync@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e66(%rip),%rcx
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e6c(%rip),%rcx
or     (%rcx),%eax
mov    0x2e13(%rip),%rbp
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%ecx
or     0x0(%rbp),%ecx
mov    0x2e18(%rip),%rbp
or     0x0(%rbp),%ecx
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%r14
mov    0x2dfd(%rip),%rdx
or     (%rdx),%ecx
mov    %eax,%esi
mov    %ecx,%edx
call   1070 <open@plt>
test   %eax,%eax
js     123b <file_write+0xdb>
mov    %eax,%r15d
test   %rbx,%rbx
je     1227 <file_write+0xc7>
mov    0x2def(%rip),%r12
mov    0x2dd0(%rip),%r13
jmp    11ea <file_write+0x8a>
nopw   0x0(%rax,%rax,1)
cltq
add    %rax,%rbp
sub    %rax,%rbx
je     1227 <file_write+0xc7>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %eax,%eax
jg     11e0 <file_write+0x80>
mov    (%r12),%rdi
cmp    0x0(%r13),%rdi
je     11e0 <file_write+0x80>
call   1080 <strerror@plt>
lea    0xe12(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    %r15d,%edi
call   1050 <close@plt>
jmp    125c <file_write+0xfc>
mov    %r15d,%edi
call   1060 <fsync@plt>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1261 <file_write+0x101>
mov    0x2d7e(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xdaf(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [36 x i8] c"file_write: Can't open file %s: %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [39 x i8] c"file_write: Failed to write to %s: %s\0A\00"
define i32 @file_write(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %10
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IROTH
%24 = or i32 %22, %23
%25 = call i32 @open(i8* %12, i32 %17, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %3
%29 = load i8*, i8** %5
%30 = load i64, i64* @errno
%31 = call i8* @strerror(i64 %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %29, i8* %31)
store i32 -1, i32* %4
br label %69
33: ; preds = %3
br label %34
34: ; preds = %55, %33
%35 = load i64, i64* %7
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %64
37: ; preds = %34
%38 = load i32, i32* %8
%39 = load i8*, i8** %10
%40 = load i64, i64* %7
%41 = call i32 @write(i32 %38, i8* %39, i64 %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp sle i32 %42, 0
br i1 %43, label %44, label %55
44: ; preds = %37
%45 = load i64, i64* @errno
%46 = load i64, i64* @EINTR
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %55
48: ; preds = %44
%49 = load i8*, i8** %5
%50 = load i64, i64* @errno
%51 = call i8* @strerror(i64 %50)
%52 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %49, i8* %51)
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
store i32 -1, i32* %4
br label %69
55: ; preds = %44, %37
%56 = load i32, i32* %9
%57 = load i8*, i8** %10
%58 = sext i32 %56 to i64
%59 = getelementptr inbounds i8, i8* %57, i64 %58
store i8* %59, i8** %10
%60 = load i32, i32* %9
%61 = sext i32 %60 to i64
%62 = load i64, i64* %7
%63 = sub i64 %62, %61
store i64 %63, i64* %7
br label %34
64: ; preds = %34
%65 = load i32, i32* %8
%66 = call i32 @fsync(i32 %65)
%67 = load i32, i32* %8
%68 = call i32 @close(i32 %67)
store i32 0, i32* %4
br label %69
69: ; preds = %64, %48, %28
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [36 x i8] c"file_write: Can't open file %s: %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [39 x i8] c"file_write: Failed to write to %s: %s\0A\00"
define i32 @file_write(i8* %0, i8* nocapture %1, i64 %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_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IRGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IROTH
%15 = or i32 %13, %14
%16 = tail call i32 @open(i8* %0, i32 %8, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %3
%19 = icmp eq i64 %2, 0
br i1 %19, label %42, label %24
20: ; preds = %3
%21 = load i64, i64* @errno
%22 = tail call i8* @strerror(i64 %21)
%23 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %22)
br label %45
24: ; preds = %18, %37
%25 = phi i8* [ %39, %37 ], [ %1, %18 ]
%26 = phi i64 [ %40, %37 ], [ %2, %18 ]
%27 = tail call i32 @write(i32 %16, i8* %25, i64 %26)
%28 = icmp slt i32 %27, 1
br i1 %28, label %29, label %37
29: ; preds = %24
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = tail call i8* @strerror(i64 %30)
%35 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %34)
%36 = tail call i32 @close(i32 %16)
br label %45
37: ; preds = %29, %24
%38 = sext i32 %27 to i64
%39 = getelementptr inbounds i8, i8* %25, i64 %38
%40 = sub i64 %26, %38
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %24
42: ; preds = %37, %18
%43 = tail call i32 @fsync(i32 %16)
%44 = tail call i32 @close(i32 %16)
br label %45
45: ; preds = %42, %33, %20
%46 = phi i32 [ -1, %20 ], [ -1, %33 ], [ 0, %42 ]
ret i32 %46
}