#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 const *, char *);
int fwrite(int /*<<< orphan*/ const *, size_t, int, int /*<<< orphan*/ *);
int write_file(const uint8_t *buf, size_t size, const char *path) {
  FILE *fp;
  fp = fopen(path, "wb");
  if (fp == NULL)
    return -1;
  if (fwrite(buf, size, 1, fp) != 1) {
    fclose(fp);
    return -1;
  }
  fclose(fp);
  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    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    116f <write_file+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <write_file+0x83>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fwrite@plt>
cmp    $0x1,%eax
je     11a3 <write_file+0x73>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <write_file+0x83>
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    %rsi,%r14
mov    %rdi,%rbp
lea    0xebf(%rip),%rsi
mov    %rdx,%rdi
call   1040 <fopen@plt>
test   %rax,%rax
je     1179 <write_file+0x49>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1050 <fwrite@plt>
xor    %ebp,%ebp
cmp    $0x1,%eax
setne  %bpl
mov    %rbx,%rdi
call   1030 <fclose@plt>
neg    %ebp
jmp    117e <write_file+0x4e>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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