#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*/ *);
int ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int DataWrite(char *filename, char *buf, int length) {
  FILE *file;
  file = fopen(filename, "wb");
  if (file == NULL) {
    printf("Unable to open output file %s\n", filename);
    return -1;
  }
  if (fwrite(buf, 1, length, file) < length) {
    printf("Unable to write %d bytes to output file %s (0x%X).\n", length,
           filename, ferror(file));
    fclose(file);
    return -1;
  }
  fflush(file);
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<DataWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b0 <DataWrite+0x50>
mov    -0x10(%rbp),%rsi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <DataWrite+0xca>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
cmp    -0x1c(%rbp),%eax
jge    1211 <DataWrite+0xb1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <DataWrite+0xca>
mov    -0x28(%rbp),%rdi
call   1060 <fflush@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<DataWrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe8a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c5 <DataWrite+0x65>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
cmp    %ebp,%eax
jge    11dd <DataWrite+0x7d>
call   1030 <ferror@plt>
lea    0xe7a(%rip),%rdi
mov    %ebp,%esi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%eax
jmp    11ec <DataWrite+0x8c>
lea    0xe37(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
jmp    11ec <DataWrite+0x8c>
call   1060 <fflush@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [31 x i8] c"Unable to open output file %s\0A\00"
@.str.2 = [52 x i8] c"Unable to write %d bytes to output file %s (0x%X).\0A\00"
define i32 @DataWrite(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%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 %16
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 -1, i32* %4
br label %36
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = load i32, i32* %7
%19 = load i32*, i32** %8
%20 = call i32 @fwrite(i8* %17, i32 1, i32 %18, i32* %19)
%21 = load i32, i32* %7
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %31
23: ; preds = %16
%24 = load i32, i32* %7
%25 = load i8*, i8** %5
%26 = load i32*, i32** %8
%27 = call i32 @ferror(i32* %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %24, i8* %25, i32 %27)
%29 = load i32*, i32** %8
%30 = call i32 @fclose(i32* %29)
store i32 -1, i32* %4
br label %36
31: ; preds = %16
%32 = load i32*, i32** %8
%33 = call i32 @fflush(i32* %32)
%34 = load i32*, i32** %8
%35 = call i32 @fclose(i32* %34)
store i32 0, i32* %4
br label %36
36: ; preds = %31, %23, %13
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [31 x i8] c"Unable to open output file %s\0A\00"
@.str.2 = [52 x i8] c"Unable to write %d bytes to output file %s (0x%X).\0A\00"
define i32 @DataWrite(i8* %0, i8* nocapture %1, i32 %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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %18
8: ; preds = %3
%9 = tail call i32 @fwrite(i8* %1, i32 1, i32 %2, i32* %4)
%10 = icmp slt i32 %9, %2
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = tail call i32 @ferror(i32* %4)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %2, i8* %0, i32 %12)
%14 = tail call i32 @fclose(i32* %4)
br label %18
15: ; preds = %8
%16 = tail call i32 @fflush(i32* %4)
%17 = tail call i32 @fclose(i32* %4)
br label %18
18: ; preds = %15, %11, %6
%19 = phi i32 [ -1, %6 ], [ -1, %11 ], [ 0, %15 ]
ret i32 %19
}