#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_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int open(unsigned char const *, int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, void const *, unsigned long long);
int filewrite(const unsigned char *fname, const void *buf,
              unsigned long long len) {
  int fd = 0;
  if ((fd = open(fname, O_RDWR | O_CREAT,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
    perror("open");
    return 0x80001104;
  }
  if (write(fd, buf, len) < 0) {
    perror("write");
    return 0x80001106;
  }
  return 0;
}


x86 assembly (clang, -O0):
<filewrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e99(%rip),%rax
or     (%rax),%esi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    0x2e67(%rip),%rax
or     (%rax),%edx
mov    0x2e4e(%rip),%rax
or     (%rax),%edx
mov    0x2e3d(%rip),%rax
or     (%rax),%edx
call   1040 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11ae <filewrite+0x7e>
lea    0xe63(%rip),%rdi
call   1050 <perror@plt>
movl   $0x80001104,-0x4(%rbp)
jmp    11e7 <filewrite+0xb7>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11e0 <filewrite+0xb0>
lea    0xe36(%rip),%rdi
call   1050 <perror@plt>
movl   $0x80001106,-0x4(%rbp)
jmp    11e7 <filewrite+0xb7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filewrite>:
push   %r14
push   %rbx
push   %rax
mov    0x2ea5(%rip),%rcx
mov    0x2eb6(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e6b(%rip),%rbx
mov    0x2e84(%rip),%rcx
mov    (%rcx),%ecx
or     (%rbx),%ecx
mov    0x2e69(%rip),%rbx
or     (%rbx),%ecx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2e52(%rip),%rdx
or     (%rdx),%ecx
mov    %eax,%esi
mov    %ecx,%edx
call   1040 <open@plt>
test   %eax,%eax
js     119b <filewrite+0x6b>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
xor    %ebx,%ebx
test   %rax,%rax
js     11a9 <filewrite+0x79>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x80001104,%ebx
lea    0xe59(%rip),%rdi
jmp    11b5 <filewrite+0x85>
mov    $0x80001106,%ebx
lea    0xe50(%rip),%rdi
call   1050 <perror@plt>
jmp    1191 <filewrite+0x61>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [6 x i8] c"write\00"
define i32 @filewrite(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IROTH
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %9, i32 %12, i32 %19)
store i32 %20, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %3
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -2147479292, i32* %4
br label %33
24: ; preds = %3
%25 = load i32, i32* %8
%26 = load i8*, i8** %6
%27 = load i64, i64* %7
%28 = call i64 @write(i32 %25, i8* %26, i64 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -2147479290, i32* %4
br label %33
32: ; preds = %24
store i32 0, i32* %4
br label %33
33: ; preds = %32, %30, %22
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [6 x i8] c"write\00"
define i32 @filewrite(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* %0, i32 %6, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = tail call i64 @write(i32 %14, i8* %1, i64 %2)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16, %3
%20 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %16 ]
%21 = phi i32 [ -2147479292, %3 ], [ -2147479290, %16 ]
%22 = tail call i32 @perror(i8* %20)
br label %23
23: ; preds = %19, %16
%24 = phi i32 [ 0, %16 ], [ %21, %19 ]
ret i32 %24
}