#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 *);
int fwrite(char const *, unsigned int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int write_file(const char *dest_fname, const char *file_out_data,
               unsigned file_out_size) {
  FILE *fd = NULL;
  fd = fopen(dest_fname, "wb");
  if (!fd) {
    perror("safeu: "
           "unable to open output file");
    goto error;
  }
  if (file_out_size != 0) {
    if (fwrite(file_out_data, file_out_size, 1, fd) != 1) {
      perror("safeu: "
             "unable to write data to output file");
      goto error;
    }
  }
  fclose(fd);
  return 1;
error:
  if (fd) {
    fclose(fd);
  }
  return 0;
}


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)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9a(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118b <write_file+0x4b>
lea    0xe82(%rip),%rdi
call   1050 <perror@plt>
jmp    11de <write_file+0x9e>
cmpl   $0x0,-0x1c(%rbp)
je     11c9 <write_file+0x89>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
cmp    $0x1,%eax
je     11c4 <write_file+0x84>
lea    0xe6b(%rip),%rdi
call   1050 <perror@plt>
jmp    11de <write_file+0x9e>
jmp    11c9 <write_file+0x89>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f9 <write_file+0xb9>
cmpq   $0x0,-0x28(%rbp)
je     11f2 <write_file+0xb2>
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_file>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
lea    0xeb0(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     118a <write_file+0x4a>
mov    %rax,%rbx
test   %ebp,%ebp
je     1178 <write_file+0x38>
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
cmp    $0x1,%eax
jne    1198 <write_file+0x58>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe72(%rip),%rdi
call   1050 <perror@plt>
jmp    11ac <write_file+0x6c>
lea    0xe86(%rip),%rdi
call   1050 <perror@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1185 <write_file+0x45>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [34 x i8] c"safeu: unable to open output file\00"
@.str.2 = [43 x i8] c"safeu: unable to write data to output file\00"
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
store i32* null, i32** %8
%9 = load i8*, i8** %5
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp ne i32* %11, null
br i1 %12, label %15, label %13
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %30
15: ; preds = %3
%16 = load i32, i32* %7
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = load i8*, i8** %6
%20 = load i32, i32* %7
%21 = load i32*, i32** %8
%22 = call i32 @fwrite(i8* %19, i32 %20, i32 1, i32* %21)
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %30
26: ; preds = %18
br label %27
27: ; preds = %26, %15
%28 = load i32*, i32** %8
%29 = call i32 @fclose(i32* %28)
store i32 1, i32* %4
br label %37
30: ; preds = %24, %13
%31 = load i32*, i32** %8
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32*, i32** %8
%35 = call i32 @fclose(i32* %34)
br label %36
36: ; preds = %33, %30
store i32 0, i32* %4
br label %37
37: ; preds = %36, %27
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [34 x i8] c"safeu: unable to open output file\00"
@.str.2 = [43 x i8] c"safeu: unable to write data to output file\00"
define i32 @write_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 ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %18
8: ; preds = %3
%9 = icmp eq i32 %2, 0
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = tail call i32 @fwrite(i8* %1, i32 %2, i32 1, i32* %4)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %10, %8
%14 = tail call i32 @fclose(i32* %4)
br label %18
15: ; preds = %10
%16 = tail call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @fclose(i32* %4)
br label %18
18: ; preds = %6, %15, %13
%19 = phi i32 [ 1, %13 ], [ 0, %15 ], [ 0, %6 ]
ret i32 %19
}