#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_TRUNC;
int O_WRONLY;
int close(int);
int open(char *, int);
int write(int, char *, int);
int write_file(char *fn, char *data, int data_len) {
  int fd;
  if ((fd = open(fn, O_WRONLY | O_TRUNC)) < 0) {
    return -1;
  }
  if (write(fd, data, data_len) != data_len) {
    if (close(fd) == -1) {
      return -1;
    }
    return -1;
  }
  if (close(fd) == -1) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2ea1(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1176 <write_file+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <write_file+0xab>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     11b7 <write_file+0x87>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11ab <write_file+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <write_file+0xab>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <write_file+0xab>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11d4 <write_file+0xa4>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <write_file+0xab>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    0x2e8c(%rip),%rax
mov    0x2ead(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1185 <write_file+0x55>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1040 <close@plt>
cmp    %r15d,%r14d
jne    1185 <write_file+0x55>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
sete   %bl
neg    %ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
define i32 @write_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12)
store i32 %13, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %35
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load i8*, i8** %6
%19 = load i32, i32* %7
%20 = call i32 @write(i32 %17, i8* %18, i32 %19)
%21 = load i32, i32* %7
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %29
23: ; preds = %16
%24 = load i32, i32* %8
%25 = call i32 @close(i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 -1, i32* %4
br label %35
28: ; preds = %23
store i32 -1, i32* %4
br label %35
29: ; preds = %16
%30 = load i32, i32* %8
%31 = call i32 @close(i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 -1, i32* %4
br label %35
34: ; preds = %29
store i32 0, i32* %4
br label %35
35: ; preds = %34, %33, %28, %27, %15
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
define i32 @write_file(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %3
%10 = tail call i32 @write(i32 %7, i8* %1, i32 %2)
%11 = icmp eq i32 %10, %2
%12 = tail call i32 @close(i32 %7)
br i1 %11, label %13, label %16
13: ; preds = %9
%14 = icmp eq i32 %12, -1
%15 = sext i1 %14 to i32
br label %16
16: ; preds = %9, %13, %3
%17 = phi i32 [ -1, %3 ], [ %15, %13 ], [ -1, %9 ]
ret i32 %17
}