#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_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int open(char const *, int, int);
int slp_openfile(const char *filename, char perm) {
  int flags = (perm == 'w') ? (O_RDWR | O_CREAT) : O_RDONLY;
  mode_t mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  return open(filename, flags, mode);
}


x86 assembly (clang, -O0):
<slp_openfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0x77,%eax
jne    1149 <slp_openfile+0x39>
mov    0x2eaa(%rip),%rax
mov    (%rax),%eax
mov    0x2eb9(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
jmp    1155 <slp_openfile+0x45>
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    0x2e65(%rip),%rcx
or     (%rcx),%eax
mov    0x2e4c(%rip),%rcx
or     (%rcx),%eax
mov    0x2e3b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1030 <open@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slp_openfile>:
cmp    $0x77,%sil
je     1121 <slp_openfile+0x11>
mov    0x2ebb(%rip),%rax
mov    (%rax),%esi
jmp    1133 <slp_openfile+0x23>
mov    0x2eb8(%rip),%rax
mov    0x2ec9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e76(%rip),%rax
mov    0x2e8f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e74(%rip),%rax
or     (%rax),%edx
mov    0x2e63(%rip),%rax
or     (%rax),%edx
jmp    1030 <open@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @slp_openfile(i8* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8 %1, i8* %4
%7 = load i8, i8* %4
%8 = sext i8 %7 to i32
%9 = icmp eq i32 %8, 119
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
br label %16
14: ; preds = %2
%15 = load i32, i32* @O_RDONLY
br label %16
16: ; preds = %14, %10
%17 = phi i32 [ %13, %10 ], [ %15, %14 ]
store i32 %17, i32* %5
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IROTH
%24 = or i32 %22, %23
store i32 %24, i32* %6
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = call i32 @open(i8* %25, i32 %26, i32 %27)
ret i32 %28
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @slp_openfile(i8* nocapture %0, i8 signext %1) {
%3 = icmp eq i8 %1, 119
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_RDONLY
%8 = select i1 %3, i32 %6, i32 %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IRGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IROTH
%15 = or i32 %13, %14
%16 = tail call i32 @open(i8* %0, i32 %8, i32 %15)
ret i32 %16
}