#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
unsigned long long fwrite(char *, int, unsigned long long,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ print_error(char *, char *);
int /*<<< orphan*/ stderr;
unsigned long long writeFile(char *outfile, char *buffer,
                             unsigned long long bufflen) {
  FILE *file = NULL;
  file = fopen(outfile, "wb");
  if (!file) {
    print_error("Unable to find or create file %s", outfile);
    return 0;
  }
  unsigned long long count = fwrite(buffer, sizeof(char), bufflen, file);
  if (ferror(file))
    fprintf(stderr, "ERROR writing to %s\n", outfile);
  fclose(file);
  return count;
}


x86 assembly (clang, -O0):
<writeFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b8 <writeFile+0x58>
mov    -0x10(%rbp),%rsi
lea    0xe5d(%rip),%rdi
call   1040 <print_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    120f <writeFile+0xaf>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11fe <writeFile+0x9e>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe2b(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe88(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ca <writeFile+0x6a>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rax,%rcx
call   1080 <fwrite@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11c0 <writeFile+0x60>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    11dc <writeFile+0x7c>
lea    0xe32(%rip),%rdi
mov    %r14,%rsi
call   1040 <print_error@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [33 x i8] c"Unable to find or create file %s\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"ERROR writing to %s\0A\00"
define i64 @writeFile(i8* %0, i8* %1, i64 %2) {
%4 = alloca i64
%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
store i32* null, i32** %8
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i32 @print_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i64 0, i64* %4
br label %33
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 %25, label %29
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %5
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %25, %17
%30 = load i32*, i32** %8
%31 = call i32 @fclose(i32* %30)
%32 = load i64, i64* %9
store i64 %32, i64* %4
br label %33
33: ; preds = %29, %14
%34 = load i64, i64* %4
ret i64 %34
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [33 x i8] c"Unable to find or create file %s\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"ERROR writing to %s\0A\00"
define i64 @writeFile(i8* %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 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 @print_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %17
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
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %12, %8
%16 = tail call i32 @fclose(i32* %4)
br label %17
17: ; preds = %15, %6
%18 = phi i64 [ %9, %15 ], [ 0, %6 ]
ret i64 %18
}