#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int write_to_fd(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int write_file(char *filename, FILE *fin, int size) {
  FILE *fout;
  int ret;
  fout = fopen(filename, "w+");
  if (fout == NULL) {
    fprintf(stderr, "Failed to open file %s for writing!\n", filename);
    return -1;
  }
  ret = write_to_fd(fout, fin, size);
  fclose(fout);
  return ret;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1197 <write_file+0x57>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe7d(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <write_file+0x79>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <write_to_fd@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1180 <write_file+0x40>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <write_to_fd@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
jmp    119d <write_file+0x5d>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"Failed to open file %s for writing!\0A\00"
define i32 @write_file(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %5
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %16)
store i32 -1, i32* %4
br label %26
18: ; preds = %3
%19 = load i32*, i32** %8
%20 = load i32*, i32** %6
%21 = load i32, i32* %7
%22 = call i32 @write_to_fd(i32* %19, i32* %20, i32 %21)
store i32 %22, i32* %9
%23 = load i32*, i32** %8
%24 = call i32 @fclose(i32* %23)
%25 = load i32, i32* %9
store i32 %25, i32* %4
br label %26
26: ; preds = %18, %14
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"Failed to open file %s for writing!\0A\00"
define i32 @write_file(i8* %0, i32* %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %12
9: ; preds = %3
%10 = tail call i32 @write_to_fd(i32* %4, i32* %1, i32 %2)
%11 = tail call i32 @fclose(i32* %4)
br label %12
12: ; preds = %9, %6
%13 = phi i32 [ -1, %6 ], [ %10, %9 ]
ret i32 %13
}