#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_TRUNC;
int O_WRONLY;
int STDOUT_FILENO;
int S_IRUSR;
int S_IWUSR;
int open(char *, int, int);
int strLen(char *);
int open_or_stdout(char *filename) {
  if (strLen(filename) == 1 && filename[0] == '-') {
    return STDOUT_FILENO;
  } else {
    return open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
  }
}


x86 assembly (clang, -O0):
<open_or_stdout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strLen@plt>
cmp    $0x1,%eax
jne    115f <open_or_stdout+0x3f>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    115f <open_or_stdout+0x3f>
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1198 <open_or_stdout+0x78>
mov    -0x10(%rbp),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    0x2e7d(%rip),%rax
or     (%rax),%esi
mov    0x2e7c(%rip),%rax
or     (%rax),%esi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
mov    0x2e42(%rip),%rax
or     (%rax),%edx
call   1040 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_or_stdout>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strLen@plt>
cmp    $0x1,%eax
jne    113e <open_or_stdout+0x1e>
cmpb   $0x2d,(%rbx)
jne    113e <open_or_stdout+0x1e>
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
mov    0x2e6b(%rip),%rax
mov    0x2ea4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2ea1(%rip),%rax
or     (%rax),%esi
mov    0x2e60(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
pop    %rbx
jmp    1040 <open@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @open_or_stdout(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
store i8* %0, i8** %3
%4 = load i8*, i8** %3
%5 = call i32 @strLen(i8* %4)
%6 = icmp eq i32 %5, 1
br i1 %6, label %7, label %15
7: ; preds = %1
%8 = load i8*, i8** %3
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 45
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = load i32, i32* @STDOUT_FILENO
store i32 %14, i32* %2
br label %26
15: ; preds = %7, %1
%16 = load i8*, i8** %3
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRUSR
%23 = load i32, i32* @S_IWUSR
%24 = or i32 %22, %23
%25 = call i32 @open(i8* %16, i32 %21, i32 %24)
store i32 %25, i32* %2
br label %26
26: ; preds = %15, %13
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @open_or_stdout(i8* %0) {
%2 = tail call i32 @strLen(i8* %0)
%3 = icmp eq i32 %2, 1
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 45
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = load i32, i32* @STDOUT_FILENO
br label %19
9: ; preds = %4, %1
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %11, %10
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %16, %15
%18 = tail call i32 @open(i8* %0, i32 %14, i32 %17)
br label %19
19: ; preds = %9, %7
%20 = phi i32 [ %8, %7 ], [ %18, %9 ]
ret i32 %20
}