#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 unsigned int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, char const *);
int open(char const *, int, int);
unsigned int write(int, unsigned char const *, unsigned int);
void write_to_file(const char *filename, const char *error,
                   const unsigned char *data, unsigned int size) {
  ssize_t n;
  int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  if (fd < 0)
    err(EXIT_FAILURE, "cannot open '%s'", filename);
  n = write(fd, data, size);
  if (n < 0)
    err(EXIT_FAILURE, "cannot write %s", error);
  else if (n != size)
    errx(EXIT_FAILURE, "cannot write %s", error);
  close(fd);
}


x86 assembly (clang, -O0):
<write_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e7d(%rip),%rax
or     (%rax),%esi
mov    0x2e6c(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b6 <write_to_file+0x66>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe4f(%rip),%rsi
call   1060 <err@plt>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jae    11f0 <write_to_file+0xa0>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe2b(%rip),%rsi
call   1060 <err@plt>
jmp    121a <write_to_file+0xca>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1215 <write_to_file+0xc5>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe01(%rip),%rsi
call   1040 <errx@plt>
jmp    121a <write_to_file+0xca>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e61(%rip),%rbp
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
or     0x0(%rbp),%eax
mov    %ecx,%r12d
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e72(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11aa <write_to_file+0x5a>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
mov    %rbx,%rdx
call   1060 <err@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12d,%edx
call   1030 <write@plt>
cmp    %r12d,%eax
je     11d4 <write_to_file+0x84>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
mov    %r14,%rdx
call   1040 <errx@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [17 x i8] c"cannot open '%s'\00"
@.str.1 = [16 x i8] c"cannot write %s\00"
define void @write_to_file(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %11, i32 %16, i32 438)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %4
%21 = load i32, i32* @EXIT_FAILURE
%22 = load i8*, i8** %5
%23 = call i32 @err(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %20, %4
%25 = load i32, i32* %10
%26 = load i8*, i8** %7
%27 = load i32, i32* %8
%28 = call i32 @write(i32 %25, i8* %26, i32 %27)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = icmp ult i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = load i32, i32* @EXIT_FAILURE
%33 = load i8*, i8** %6
%34 = call i32 @err(i32 %32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %33)
br label %44
35: ; preds = %24
%36 = load i32, i32* %9
%37 = load i32, i32* %8
%38 = icmp ne i32 %36, %37
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i32, i32* @EXIT_FAILURE
%41 = load i8*, i8** %6
%42 = call i32 @errx(i32 %40, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %39, %35
br label %44
44: ; preds = %43, %31
%45 = load i32, i32* %10
%46 = call i32 @close(i32 %45)
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [17 x i8] c"cannot open '%s'\00"
@.str.1 = [16 x i8] c"cannot write %s\00"
define void @write_to_file(i8* %0, i8* %1, i8* nocapture %2, i32 %3) {
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 438)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %4
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @err(i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %12, %4
%16 = tail call i32 @write(i32 %10, i8* %2, i32 %3)
%17 = icmp eq i32 %16, %3
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @errx(i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %21
21: ; preds = %15, %18
%22 = tail call i32 @close(i32 %10)
ret void
}