#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 */

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int /*<<< orphan*/);
int write(int, char *, unsigned int);
int write_memory(char *file, char *begin, unsigned size) {
  int handle, bytes;
  handle = open(file, O_CREAT | O_WRONLY, 0);
  if (handle == -1)
    return 0;
  bytes = write(handle, begin, size);
  close(handle);
  if (bytes != size)
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<write_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1179 <write_memory+0x49>
movl   $0x0,-0x4(%rbp)
jmp    11b2 <write_memory+0x82>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     11ab <write_memory+0x7b>
movl   $0x0,-0x4(%rbp)
jmp    11b2 <write_memory+0x82>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_memory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    0x2eb4(%rip),%rax
mov    0x2e85(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     117f <write_memory+0x4f>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
cmp    %r15d,%r14d
sete   %bpl
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
define i32 @write_memory(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
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_WRONLY
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13, i32 0)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 0, i32* %4
br label %30
18: ; preds = %3
%19 = load i32, i32* %8
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = call i32 @write(i32 %19, i8* %20, i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %8
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %9
%26 = load i32, i32* %7
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %18
store i32 0, i32* %4
br label %30
29: ; preds = %18
store i32 1, i32* %4
br label %30
30: ; preds = %29, %28, %17
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
define i32 @write_memory(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_WRONLY
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %14, label %9
9: ; preds = %3
%10 = tail call i32 @write(i32 %7, i8* %1, i32 %2)
%11 = tail call i32 @close(i32 %7)
%12 = icmp eq i32 %10, %2
%13 = zext i1 %12 to i32
br label %14
14: ; preds = %9, %3
%15 = phi i32 [ 0, %3 ], [ %13, %9 ]
ret i32 %15
}