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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fwrite(unsigned char const *, int /*<<< orphan*/ const, int,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char const *);
int write_data(const unsigned char *buf, const uint32_t sz, const char *fn) {
  printf("Writing to %s\n", fn);
  FILE *handle = fopen(fn, "wb");
  if (NULL == handle)
    return 1;
  fwrite(buf, sz, 1, handle);
  fclose(handle);
  return 0;
}


x86 assembly (clang, -O0):
<write_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xea2(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    118f <write_data+0x4f>
movl   $0x1,-0x4(%rbp)
jmp    11b4 <write_data+0x74>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_data>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xeac(%rip),%rdi
mov    %rdx,%rsi
call   1040 <printf@plt>
lea    0xeac(%rip),%rsi
mov    %rbp,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1192 <write_data+0x52>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1197 <write_data+0x57>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"Writing to %s\0A\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @write_data(i8* nocapture %0, i32 %1, i8* %2) {
%4 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %2)
%5 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = tail call i32 @fwrite(i8* %0, i32 %1, i32 1, i32* %5)
%9 = tail call i32 @fclose(i32* %5)
br label %10
10: ; preds = %3, %7
%11 = phi i32 [ 0, %7 ], [ 1, %3 ]
ret i32 %11
}