#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*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int write_output_file(char *fname, char *buf, size_t bufsize) {
  FILE *fp;
  size_t num_bytes;
  fp = fopen(fname, "w");
  if (fp == NULL) {
    perror(fname);
    return (-1);
  }
  num_bytes = fwrite(buf, 1, bufsize, fp);
  if (ferror(fp) || (num_bytes != bufsize)) {
    perror(fname);
    return (-1);
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<write_output_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1198 <write_output_file+0x48>
mov    -0x10(%rbp),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <write_output_file+0xa8>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    11d3 <write_output_file+0x83>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11e8 <write_output_file+0x98>
mov    -0x10(%rbp),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <write_output_file+0xa8>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_output_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe98(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11b3 <write_output_file+0x63>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1030 <ferror@plt>
cmp    %r15,%r12
jne    11b3 <write_output_file+0x63>
test   %rax,%rax
jne    11b3 <write_output_file+0x63>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11a7 <write_output_file+0x57>

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
define i32 @write_output_file(i8* nocapture %0, i8* nocapture %1, i64 %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 %6, label %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* %0)
br label %18
8: ; preds = %3
%9 = tail call i64 @fwrite(i8* %1, i32 1, i64 %2, i32* %4)
%10 = tail call i64 @ferror(i32* %4)
%11 = icmp eq i64 %10, 0
%12 = icmp eq i64 %9, %2
%13 = and i1 %12, %11
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = tail call i32 @perror(i8* %0)
br label %18
16: ; preds = %8
%17 = tail call i32 @fclose(i32* %4)
br label %18
18: ; preds = %16, %14, %6
%19 = phi i32 [ -1, %6 ], [ -1, %14 ], [ 0, %16 ]
ret i32 %19
}