#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 STDOUT_FILENO;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char const *, int, int);
scalar_t__ strcmp(char *, char const *);
scalar_t__ write(int, void *, size_t);
int to_file(const char *filename, void *buf, size_t len) {
  int fd;
  if (strcmp("-", filename)) {
    fd = open(filename, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
  } else {
    fd = STDOUT_FILENO;
  }
  if (fd > 0) {
    ssize_t res = write(fd, buf, len);
    close(fd);
    return res == (ssize_t)len;
  } else {
    return fd;
  }
}


x86 assembly (clang, -O0):
<to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xea1(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11a3 <to_file+0x63>
mov    -0x10(%rbp),%rdi
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    0x2e44(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
jmp    11af <to_file+0x6f>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11ed <to_file+0xad>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    11f3 <to_file+0xb3>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xea9(%rip),%rdi
mov    %rbp,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11c3 <to_file+0x83>
mov    0x2e8d(%rip),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4b(%rip),%rax
mov    0x2e54(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbp,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %ebp,%ebp
jle    11b6 <to_file+0x76>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
cmp    %r15,%rbx
sete   %bpl
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e1e(%rip),%rax
mov    (%rax),%ebp
test   %ebp,%ebp
jg     1196 <to_file+0x56>
jmp    11b6 <to_file+0x76>

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @to_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_WRONLY
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %14, i32 %17, i32 %20)
store i32 %21, i32* %8
br label %24
22: ; preds = %3
%23 = load i32, i32* @STDOUT_FILENO
store i32 %23, i32* %8
br label %24
24: ; preds = %22, %13
%25 = load i32, i32* %8
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %38
27: ; preds = %24
%28 = load i32, i32* %8
%29 = load i8*, i8** %6
%30 = load i64, i64* %7
%31 = call i64 @write(i32 %28, i8* %29, i64 %30)
store i64 %31, i64* %9
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
%34 = load i64, i64* %9
%35 = load i64, i64* %7
%36 = icmp eq i64 %34, %35
%37 = zext i1 %36 to i32
store i32 %37, i32* %4
br label %40
38: ; preds = %24
%39 = load i32, i32* %8
store i32 %39, i32* %4
br label %40
40: ; preds = %38, %27
%41 = load i32, i32* %4
ret i32 %41
}

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