#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_EXCL;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int close(int);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
int write(int, char const *, size_t);
int write_to_file(const char *data, size_t len, const char *filename) {
  int written = 0;
  int fd = -1;
  int rv = 0;
  fd = open(filename, O_RDWR | O_CREAT | O_EXCL,
            S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (-1 == fd) {
    perror("Failed to create file");
    return 1;
  }
  while (len != 0) {
    written = write(fd, data, len);
    if (-1 == written) {
      perror("Write failed");
      rv = 1;
      break;
    }
    len -= written;
  }
  if (-1 == close(fd)) {
    perror("Close failed");
    rv = 1;
  }
  return rv;
}


x86 assembly (clang, -O0):
<write_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
mov    0x2e7b(%rip),%rax
or     (%rax),%esi
mov    0x2e42(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    0x2e40(%rip),%rax
or     (%rax),%edx
mov    0x2e1f(%rip),%rax
or     (%rax),%edx
mov    0x2e0e(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x28(%rbp)
mov    $0xffffffff,%eax
cmp    -0x28(%rbp),%eax
jne    11da <write_to_file+0x9a>
lea    0xe37(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1267 <write_to_file+0x127>
jmp    11df <write_to_file+0x9f>
cmpq   $0x0,-0x18(%rbp)
je     1237 <write_to_file+0xf7>
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
mov    $0xffffffff,%eax
cmp    -0x24(%rbp),%eax
jne    1223 <write_to_file+0xe3>
lea    0xe04(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x2c(%rbp)
jmp    1237 <write_to_file+0xf7>
movslq -0x24(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    11df <write_to_file+0x9f>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1261 <write_to_file+0x121>
lea    0xdce(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e92(%rip),%rcx
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e68(%rip),%rcx
or     (%rcx),%eax
mov    0x2e47(%rip),%rbp
mov    0x2e68(%rip),%rcx
mov    (%rcx),%ecx
or     0x0(%rbp),%ecx
mov    0x2e44(%rip),%rbp
or     0x0(%rbp),%ecx
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    0x2e2c(%rip),%rsi
or     (%rsi),%ecx
mov    %rdx,%rdi
mov    %eax,%esi
mov    %ecx,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11e5 <write_to_file+0xa5>
mov    %eax,%r15d
xor    %r14d,%r14d
test   %rbp,%rbp
je     11ca <write_to_file+0x8a>
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1202 <write_to_file+0xc2>
cltq
sub    %rax,%rbp
jne    11b0 <write_to_file+0x70>
mov    %r15d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     11ee <write_to_file+0xae>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe14(%rip),%rdi
jmp    11f5 <write_to_file+0xb5>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%r14d
jmp    11d7 <write_to_file+0x97>
lea    0xe0d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%r14d
jmp    11ca <write_to_file+0x8a>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [22 x i8] c"Failed to create file\00"
@.str.1 = [13 x i8] c"Write failed\00"
@.str.2 = [13 x i8] c"Close failed\00"
define i32 @write_to_file(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 0, i32* %8
store i32 -1, i32* %9
store i32 0, i32* %10
%11 = load i8*, i8** %7
%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 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRGRP
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IROTH
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %11, i32 %16, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp eq i32 -1, %25
br i1 %26, label %27, label %29
27: ; preds = %3
%28 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %55
29: ; preds = %3
br label %30
30: ; preds = %42, %29
%31 = load i64, i64* %6
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %30
%34 = load i32, i32* %9
%35 = load i8*, i8** %5
%36 = load i64, i64* %6
%37 = call i32 @write(i32 %34, i8* %35, i64 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp eq i32 -1, %38
br i1 %39, label %40, label %42
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %10
br label %47
42: ; preds = %33
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = load i64, i64* %6
%46 = sub i64 %45, %44
store i64 %46, i64* %6
br label %30
47: ; preds = %40, %30
%48 = load i32, i32* %9
%49 = call i32 @close(i32 %48)
%50 = icmp eq i32 -1, %49
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %10
br label %53
53: ; preds = %51, %47
%54 = load i32, i32* %10
store i32 %54, i32* %4
br label %55
55: ; preds = %53, %27
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [22 x i8] c"Failed to create file\00"
@.str.1 = [13 x i8] c"Write failed\00"
@.str.2 = [13 x i8] c"Close failed\00"
define i32 @write_to_file(i8* nocapture %0, i64 %1, i8* nocapture %2) {
%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 = 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* %2, i32 %8, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %34, label %18
18: ; preds = %3
%19 = icmp eq i64 %1, 0
br i1 %19, label %30, label %22
20: ; preds = %22
%21 = icmp eq i64 %27, 0
br i1 %21, label %30, label %22
22: ; preds = %18, %20
%23 = phi i64 [ %27, %20 ], [ %1, %18 ]
%24 = tail call i32 @write(i32 %16, i8* %0, i64 %23)
%25 = icmp eq i32 %24, -1
%26 = sext i32 %24 to i64
%27 = sub i64 %23, %26
br i1 %25, label %28, label %20
28: ; preds = %22
%29 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %20, %18, %28
%31 = phi i32 [ 1, %28 ], [ 0, %18 ], [ 0, %20 ]
%32 = tail call i32 @close(i32 %16)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %30, %3
%35 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), %30 ]
%36 = tail call i32 @perror(i8* %35)
br label %37
37: ; preds = %34, %30
%38 = phi i32 [ %31, %30 ], [ 1, %34 ]
ret i32 %38
}