#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*/ erase_flash(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
long fwrite(unsigned char *, int, long, int /*<<< orphan*/ *);
int write_file(const char *name, unsigned char *data, long size) {
  FILE *f;
  int fd;
  f = fopen(name, "w");
  if (f == NULL)
    return -1;
  fd = fileno(f);
  if (fd >= 0)
    erase_flash(fd);
  if (fwrite(data, sizeof(char), size, f) != size) {
    fclose(f);
    return -1;
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<write_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   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118f <write_file+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <write_file+0xa2>
mov    -0x28(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     11ad <write_file+0x5d>
mov    -0x2c(%rbp),%edi
call   1040 <erase_flash@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
cmp    -0x20(%rbp),%rax
je     11e2 <write_file+0x92>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <write_file+0xa2>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ac <write_file+0x5c>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fileno@plt>
test   %eax,%eax
js     1184 <write_file+0x34>
mov    %eax,%edi
call   1040 <erase_flash@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
xor    %ebp,%ebp
cmp    %r14,%rax
setne  %bpl
mov    %rbx,%rdi
call   1030 <fclose@plt>
neg    %ebp
jmp    11b1 <write_file+0x61>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
define i32 @write_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 %16, label %6
6: ; preds = %3
%7 = tail call i32 @fileno(i32* %4)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @erase_flash(i32 %7)
br label %11
11: ; preds = %9, %6
%12 = tail call i64 @fwrite(i8* %1, i32 1, i64 %2, i32* %4)
%13 = icmp ne i64 %12, %2
%14 = tail call i32 @fclose(i32* %4)
%15 = sext i1 %13 to i32
br label %16
16: ; preds = %11, %3
%17 = phi i32 [ -1, %3 ], [ %15, %11 ]
ret i32 %17
}