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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
bool save_from_buffer(uint8_t *buf, int len, char *fn) {
  FILE *outfile = NULL;
  bool result = false;
  outfile = fopen(fn, "wb");
  if (outfile == NULL) {
    printf("Error opening output file!\n");
    goto FINALLY;
  };
  if (fwrite(buf, 1, len, outfile) != len) {
    printf("Error writing output file!\n");
    goto FINALLY;
  };
  result = true;
FINALLY:
  if (outfile != NULL)
    fclose(outfile);
  return result;
}


x86 assembly (clang, -O0):
<save_from_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe93(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1192 <save_from_buffer+0x52>
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
jmp    11c8 <save_from_buffer+0x88>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
cmp    -0xc(%rbp),%eax
je     11c1 <save_from_buffer+0x81>
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
jmp    11c8 <save_from_buffer+0x88>
movl   $0x1,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11dc <save_from_buffer+0x9c>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_from_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r15
lea    0xead(%rip),%rsi
mov    %rdx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1198 <save_from_buffer+0x58>
mov    %rax,%rbx
mov    $0x1,%r14d
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
cmp    %ebp,%eax
je     118e <save_from_buffer+0x4e>
lea    0xe7d(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11a7 <save_from_buffer+0x67>
lea    0xe7f(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [28 x i8] c"Error opening output file!\0A\00"
@.str.2 = [28 x i8] c"Error writing output file!\0A\00"
define i32 @save_from_buffer(i32* %0, i32 %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32* null, i32** %7
store i32 0, i32* %8
%9 = load i8*, i8** %6
%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 = load i32*, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %25
15: ; preds = %3
%16 = load i32*, i32** %4
%17 = load i32, i32* %5
%18 = load i32*, i32** %7
%19 = call i32 @fwrite(i32* %16, i32 1, i32 %17, i32* %18)
%20 = load i32, i32* %5
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %25
24: ; preds = %15
store i32 1, i32* %8
br label %25
25: ; preds = %24, %22, %13
%26 = load i32*, i32** %7
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32*, i32** %7
%30 = call i32 @fclose(i32* %29)
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* %8
ret i32 %32
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@str = [27 x i8] c"Error writing output file!\00"
@str.3 = [27 x i8] c"Error opening output file!\00"
define i32 @save_from_buffer(i32* nocapture %0, i32 %1, i8* nocapture %2) {
%4 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %11, label %6
6: ; preds = %3
%7 = tail call i32 @fwrite(i32* %0, i32 1, i32 %1, i32* %4)
%8 = icmp eq i32 %7, %1
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %13
11: ; preds = %3
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.3, i64 0, i64 0))
br label %16
13: ; preds = %6, %9
%14 = phi i32 [ 1, %6 ], [ 0, %9 ]
%15 = tail call i32 @fclose(i32* %4)
br label %16
16: ; preds = %11, %13
%17 = phi i32 [ %14, %13 ], [ 0, %11 ]
ret i32 %17
}