#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 /*<<< orphan*/ *fopen(char const *, char *);
int fwrite(unsigned char *, int, unsigned int, int /*<<< orphan*/ *);
int file_write(const char *file, unsigned char *buf, unsigned int length) {
  FILE *fd = NULL;
  fd = fopen(file, "wb");
  if (fd == NULL) {
    return -1;
  }
  int bytes = fwrite(buf, 1, length, fd);
  if (bytes != length) {
    fclose(fd);
    return -1;
  }
  fclose(fd);
  return bytes;
}


x86 assembly (clang, -O0):
<file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeaa(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1176 <file_write+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <file_write+0x8e>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     11af <file_write+0x7f>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <file_write+0x8e>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_write>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
lea    0xec0(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1177 <file_write+0x47>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1050 <fwrite@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    %ebp,%r14d
mov    $0xffffffff,%eax
cmove  %ebp,%eax
jmp    117c <file_write+0x4c>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
define i32 @file_write(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %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 eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %30
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = load i32, i32* %7
%18 = load i32*, i32** %8
%19 = call i32 @fwrite(i8* %16, i32 1, i32 %17, i32* %18)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = load i32, i32* %7
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = load i32*, i32** %8
%25 = call i32 @fclose(i32* %24)
store i32 -1, i32* %4
br label %30
26: ; preds = %15
%27 = load i32*, i32** %8
%28 = call i32 @fclose(i32* %27)
%29 = load i32, i32* %9
store i32 %29, i32* %4
br label %30
30: ; preds = %26, %23, %14
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
define i32 @file_write(i8* nocapture %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 %11, label %6
6: ; preds = %3
%7 = tail call i32 @fwrite(i8* %1, i32 1, i32 %2, i32* %4)
%8 = icmp eq i32 %7, %2
%9 = tail call i32 @fclose(i32* %4)
%10 = select i1 %8, i32 %2, i32 -1
br label %11
11: ; preds = %6, %3
%12 = phi i32 [ -1, %3 ], [ %10, %6 ]
ret i32 %12
}