#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 int mode_t;

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int open(char *, int, ...);
scalar_t__ strcmp(char *, char *);
int fastio_open(char *path, char *mode) {
  mode_t md;
  if (strcmp(mode, "w") == 0) {
    md = O_CREAT | O_TRUNC | O_WRONLY;
    return open(path, md, S_IRUSR | S_IWUSR);
  } else if (strcmp(mode, "r") == 0) {
    md = O_RDONLY;
    return open(path, md);
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<fastio_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xec5(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    1190 <fastio_open+0x70>
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
mov    0x2e96(%rip),%rcx
or     (%rcx),%eax
mov    0x2e4d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    0x2e51(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x4(%rbp)
jmp    11d3 <fastio_open+0xb3>
mov    -0x18(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    11cc <fastio_open+0xac>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x4(%rbp)
jmp    11d3 <fastio_open+0xb3>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fastio_open>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xecf(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
je     115f <fastio_open+0x3f>
lea    0xebd(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
je     119d <fastio_open+0x7d>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e92(%rip),%rax
mov    0x2e83(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
mov    0x2e3f(%rip),%rax
mov    0x2e48(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <open@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <open@plt>

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

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