#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 *);
size_t fwrite(void const *, size_t, int, int /*<<< orphan*/ *);
int memory_write(const char *datafile, const void *data, size_t size) {
  int rc = (-1);
  FILE *fp = fopen(datafile, "wb");
  if (fp != NULL) {
    size_t n = fwrite(data, size, 1, fp);
    if (n == 1) {
      rc = (0);
    }
    fclose(fp);
  }
  return (rc);
}


x86 assembly (clang, -O0):
<memory_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeaa(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     119f <memory_write+0x6f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fwrite@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
jne    1196 <memory_write+0x66>
movl   $0x0,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

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

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

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