#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*/ exit(int /*<<< orphan*/);
int fwrite(void *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int host_flash_write(void *buf, size_t size, void *handle) {
  int status;
  FILE *file;
  file = (FILE *)handle;
  if (file == NULL) {
    printf("NULL write\n");
    exit(0);
  }
  status = fwrite(buf, 1, size, file);
  if (status == size)
    return 0;
  else
    return 1;
}


x86 assembly (clang, -O0):
<host_flash_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    116a <host_flash_write+0x3a>
lea    0xea2(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    119d <host_flash_write+0x6d>
movl   $0x0,-0x4(%rbp)
jmp    11a4 <host_flash_write+0x74>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_flash_write>:
push   %rbx
test   %rdx,%rdx
je     1156 <host_flash_write+0x26>
mov    %rdx,%rcx
mov    %rsi,%rbx
mov    $0x1,%esi
mov    %rbx,%rdx
call   1050 <fwrite@plt>
movslq %eax,%rcx
xor    %eax,%eax
cmp    %rbx,%rcx
setne  %al
pop    %rbx
ret
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"NULL write\0A\00"
define i32 @host_flash_write(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %7
%11 = bitcast i8* %10 to i32*
store i32* %11, i32** %9
%12 = load i32*, i32** %9
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = load i64, i64* %6
%20 = load i32*, i32** %9
%21 = call i32 @fwrite(i8* %18, i32 1, i64 %19, i32* %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = load i64, i64* %6
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %17
store i32 0, i32* %4
br label %28
27: ; preds = %17
store i32 1, i32* %4
br label %28
28: ; preds = %27, %26
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
@str = [11 x i8] c"NULL write\00"
define i32 @host_flash_write(i8* nocapture %0, i64 %1, i8* %2) {
%4 = icmp eq i8* %2, null
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %3
%9 = bitcast i8* %2 to i32*
%10 = tail call i32 @fwrite(i8* %0, i32 1, i64 %1, i32* %9)
%11 = sext i32 %10 to i64
%12 = icmp ne i64 %11, %1
%13 = zext i1 %12 to i32
ret i32 %13
}