#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 fwrite(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
void write_memory_to_file(char *filename, char *binary_data,
                          int buffer_length) {
  FILE *outfile;
  int bytes_written;
  if ((outfile = fopen(filename, "wb")) == NULL) {
    perror("\n\nUnable to open file for writing\n");
    return;
  }
  bytes_written = fwrite((void *)binary_data, 1, buffer_length, outfile);
  fclose(outfile);
}


x86 assembly (clang, -O0):
<write_memory_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1182 <write_memory_to_file+0x42>
lea    0xe8b(%rip),%rdi
call   1050 <perror@plt>
jmp    11a3 <write_memory_to_file+0x63>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_memory_to_file>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
lea    0xeaf(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     117d <write_memory_to_file+0x3d>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %rbp,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fclose@plt>
lea    0xe7f(%rip),%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <perror@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [35 x i8] c"\0A\0AUnable to open file for writing\0A\00"
define void @write_memory_to_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %21
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i32, i32* %6
%17 = load i32*, i32** %7
%18 = call i32 @fwrite(i8* %15, i32 1, i32 %16, i32* %17)
store i32 %18, i32* %8
%19 = load i32*, i32** %7
%20 = call i32 @fclose(i32* %19)
br label %21
21: ; preds = %14, %12
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [35 x i8] c"\0A\0AUnable to open file for writing\0A\00"
define void @write_memory_to_file(i8* nocapture %0, i8* nocapture %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 %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %11
8: ; preds = %3
%9 = tail call i32 @fwrite(i8* %1, i32 1, i32 %2, i32* %4)
%10 = tail call i32 @fclose(i32* %4)
br label %11
11: ; preds = %8, %6
ret void
}