#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 const *, char *);
long fwrite(unsigned char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char const *);
long write_file(const char *file_name, unsigned char *data, long length) {
  FILE *out;
  long bytes_written;
  out = fopen(file_name, "wb");
  if (out == NULL) {
    perror(file_name);
    return -1;
  }
  bytes_written = fwrite(data, 1, length, out);
  fclose(out);
  return bytes_written;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1189 <write_file+0x49>
mov    -0x10(%rbp),%rdi
call   1050 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11b4 <write_file+0x74>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xea8(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1192 <write_file+0x52>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r12,%rdi
call   1050 <perror@plt>
mov    $0xffffffffffffffff,%rax
jmp    1186 <write_file+0x46>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
define i64 @write_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %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 %17
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i32 @perror(i8* %15)
store i64 -1, i64* %4
br label %25
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = load i64, i64* %7
%20 = load i32*, i32** %8
%21 = call i64 @fwrite(i8* %18, i32 1, i64 %19, i32* %20)
store i64 %21, i64* %9
%22 = load i32*, i32** %8
%23 = call i32 @fclose(i32* %22)
%24 = load i64, i64* %9
store i64 %24, i64* %4
br label %25
25: ; preds = %17, %14
%26 = load i64, i64* %4
ret i64 %26
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
define i64 @write_file(i8* nocapture %0, i8* nocapture %1, i64 %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 %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* %0)
br label %11
8: ; preds = %3
%9 = tail call i64 @fwrite(i8* %1, i32 1, i64 %2, i32* %4)
%10 = tail call i32 @fclose(i32* %4)
br label %11
11: ; preds = %8, %6
%12 = phi i64 [ -1, %6 ], [ %9, %8 ]
ret i64 %12
}