#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 scalar_t__ ssize_t;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char const *, int, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, void *, size_t);
void write_file(const char *filename, void *data, size_t size) {
  int fd = open(filename, O_WRONLY | O_CREAT, 0644);
  if (fd < 0) {
    fprintf(stderr, "Cannot open %s for writing\n", filename);
    goto cleanup;
  }
  if (write(fd, data, size) != (ssize_t)size) {
    fprintf(stderr, "Cannot write data to file\n");
    goto cleanup;
  }
cleanup:
  if (fd >= 0)
    close(fd);
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a1 <write_file+0x61>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe6b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11dc <write_file+0x9c>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x18(%rbp),%rax
je     11d7 <write_file+0x97>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11dc <write_file+0x9c>
jmp    11dc <write_file+0x9c>
cmpl   $0x0,-0x1c(%rbp)
jl     11ee <write_file+0xae>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    0x2e70(%rip),%rax
mov    0x2e99(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11ae <write_file+0x6e>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r12,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
cmp    %r14,%rax
je     119c <write_file+0x5c>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <close@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <fprintf@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Cannot open %s for writing\0A\00"
@.str.1 = [27 x i8] c"Cannot write data to file\0A\00"
define void @write_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %8, i32 %11, i32 420)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %30
19: ; preds = %3
%20 = load i32, i32* %7
%21 = load i8*, i8** %5
%22 = load i64, i64* %6
%23 = call i64 @write(i32 %20, i8* %21, i64 %22)
%24 = load i64, i64* %6
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %30
29: ; preds = %19
br label %30
30: ; preds = %29, %26, %15
%31 = load i32, i32* %7
%32 = icmp sge i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %30
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Cannot open %s for writing\0A\00"
@.str.1 = [27 x i8] c"Cannot write data to file\0A\00"
define void @write_file(i8* %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6, i32 420)
%8 = icmp slt i32 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %3
%10 = tail call i64 @write(i32 %7, i8* %1, i64 %2)
%11 = icmp eq i64 %10, %2
br i1 %11, label %18, label %12
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %18
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %20
18: ; preds = %9, %12
%19 = tail call i32 @close(i32 %7)
br label %20
20: ; preds = %15, %18
ret void
}