#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 EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fwrite(unsigned char const *, unsigned int const, int,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ stdout;
int writebuf(const unsigned char *buf, const unsigned int size,
             const char *filename) {
  if (1)
    fprintf(stdout, "\t%s\n", filename);
  if (1) {
    FILE *file = fopen(filename, "wb");
    if (file == NULL) {
      perror(filename);
      return EXIT_FAILURE;
    }
    fwrite(buf, size, 1, file);
    if (fclose(file) < 0) {
      perror("fclose");
      return EXIT_FAILURE;
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<writebuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe89(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe7e(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b5 <writebuf+0x65>
mov    -0x20(%rbp),%rdi
call   1060 <perror@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <writebuf+0xb6>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
jge    11fa <writebuf+0xaa>
lea    0xe24(%rip),%rdi
call   1060 <perror@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <writebuf+0xb6>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writebuf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xe89(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11c7 <writebuf+0x77>
mov    %rax,%rbp
mov    %r15,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
js     11c0 <writebuf+0x70>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe41(%rip),%rbx
mov    %rbx,%rdi
call   1060 <perror@plt>
mov    0x2e0a(%rip),%rax
jmp    11b3 <writebuf+0x63>

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

LLVM IR (-O2):
@stdout = global i32 0
@.str = [5 x i8] c"\09%s\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"fclose\00"
@EXIT_SUCCESS = global i32 0
define i32 @writebuf(i8* nocapture %0, i32 %1, i8* %2) {
%4 = load i32, i32* @stdout
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %2)
%6 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %12, label %8
8: ; preds = %3
%9 = tail call i32 @fwrite(i8* %0, i32 %1, i32 1, i32* %6)
%10 = tail call i64 @fclose(i32* %6)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8, %3
%13 = phi i8* [ %2, %3 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %8 ]
%14 = tail call i32 @perror(i8* %13)
br label %15
15: ; preds = %12, %8
%16 = phi i32* [ @EXIT_SUCCESS, %8 ], [ @EXIT_FAILURE, %12 ]
%17 = load i32, i32* %16
ret i32 %17
}