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

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fwrite(void *, int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ unlink(char *);
int write_buf_to_file(char *name, void *buf, ssize_t buflen) {
  FILE *f;
  size_t done;
  int ret = -1;
  f = fopen(name, "w");
  if (f == NULL)
    goto out;
  errno = 0;
  done = fwrite(buf, buflen, 1, f);
  if (done != 1)
    goto close;
  ret = 0;
close:
  fflush(f);
  fclose(f);
  if (ret)
    unlink(name);
out:
  return ret;
}


x86 assembly (clang, -O0):
<write_buf_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118e <write_buf_to_file+0x3e>
jmp    11f6 <write_buf_to_file+0xa6>
mov    0x2e4b(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x1,-0x28(%rbp)
je     11c5 <write_buf_to_file+0x75>
jmp    11cc <write_buf_to_file+0x7c>
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <fflush@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x2c(%rbp)
je     11f1 <write_buf_to_file+0xa1>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
jmp    11f6 <write_buf_to_file+0xa6>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_buf_to_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    $0xffffffff,%eax
test   %rbx,%rbx
je     11c2 <write_buf_to_file+0x72>
mov    0x2e60(%rip),%rax
movq   $0x0,(%rax)
mov    %rbp,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <fflush@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
cmp    $0x1,%rbp
je     11c2 <write_buf_to_file+0x72>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@errno = global i64 0
define i32 @write_buf_to_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 -1, i32* %9
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
br label %35
15: ; preds = %3
store i64 0, i64* @errno
%16 = load i8*, i8** %5
%17 = load i32, i32* %6
%18 = load i32*, i32** %7
%19 = call i64 @fwrite(i8* %16, i32 %17, i32 1, i32* %18)
store i64 %19, i64* %8
%20 = load i64, i64* %8
%21 = icmp ne i64 %20, 1
br i1 %21, label %22, label %23
22: ; preds = %15
br label %24
23: ; preds = %15
store i32 0, i32* %9
br label %24
24: ; preds = %23, %22
%25 = load i32*, i32** %7
%26 = call i32 @fflush(i32* %25)
%27 = load i32*, i32** %7
%28 = call i32 @fclose(i32* %27)
%29 = load i32, i32* %9
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i8*, i8** %4
%33 = call i32 @unlink(i8* %32)
br label %34
34: ; preds = %31, %24
br label %35
35: ; preds = %34, %14
%36 = load i32, i32* %9
ret i32 %36
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@errno = global i64 0
define i32 @write_buf_to_file(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %13, label %6
6: ; preds = %3
store i64 0, i64* @errno
%7 = tail call i64 @fwrite(i8* %1, i32 %2, i32 1, i32* %4)
%8 = icmp eq i64 %7, 1
%9 = tail call i32 @fflush(i32* %4)
%10 = tail call i32 @fclose(i32* %4)
br i1 %8, label %13, label %11
11: ; preds = %6
%12 = tail call i32 @unlink(i8* %0)
br label %13
13: ; preds = %6, %11, %3
%14 = phi i32 [ -1, %3 ], [ -1, %11 ], [ 0, %6 ]
ret i32 %14
}