#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_APPEND;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int open(char *, int, ...);
int /*<<< orphan*/ strcmp(char *, char *);
int open_file(char *file_name, char *mode) {
  if (!file_name || !mode)
    return -1;
  int fd;
  mode_t mode1 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  if (!strcmp(mode, "r"))
    fd = open(file_name, O_RDONLY);
  else if (!strcmp(mode, "w"))
    fd = open(file_name, O_WRONLY | O_CREAT, mode1);
  else if (!strcmp(mode, "a"))
    fd = open(file_name, O_APPEND | O_CREAT, mode1);
  else
    return -1;
  return (uintptr_t)fd;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <open_file+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1152 <open_file+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <open_file+0x12d>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    0x2e76(%rip),%rcx
or     (%rcx),%eax
mov    0x2e5d(%rip),%rcx
or     (%rcx),%eax
mov    0x2e4c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    11ae <open_file+0x8e>
mov    -0x10(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
jmp    1246 <open_file+0x126>
mov    -0x18(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    11ef <open_file+0xcf>
mov    -0x10(%rbp),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    0x2e1d(%rip),%rax
or     (%rax),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
jmp    1241 <open_file+0x121>
mov    -0x18(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    1230 <open_file+0x110>
mov    -0x10(%rbp),%rdi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
or     (%rax),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
jmp    123c <open_file+0x11c>
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <open_file+0x12d>
jmp    1241 <open_file+0x121>
jmp    1246 <open_file+0x126>
movslq -0x1c(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1191 <open_file+0x71>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1191 <open_file+0x71>
mov    %rdi,%r14
mov    0x2e7d(%rip),%rax
mov    0x2e96(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x2e7b(%rip),%rax
or     (%rax),%ebp
mov    0x2e6a(%rip),%rax
or     (%rax),%ebp
lea    0xea1(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
je     119b <open_file+0x7b>
lea    0xe90(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
je     11b2 <open_file+0x92>
lea    0xe7f(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
je     11bb <open_file+0x9b>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <open@plt>
mov    0x2def(%rip),%rax
jmp    11c2 <open_file+0xa2>
mov    0x2dde(%rip),%rax
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
mov    %ebp,%edx
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <open@plt>

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [2 x i8] c"r\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"w\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [2 x i8] c"a\00"
@O_APPEND = global i32 0
define i32 @open_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %10, %2
store i32 -1, i32* %3
br label %58
14: ; preds = %10
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRGRP
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IROTH
%21 = or i32 %19, %20
store i32 %21, i32* %7
%22 = load i8*, i8** %5
%23 = call i32 @strcmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = icmp ne i32 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %14
%26 = load i8*, i8** %4
%27 = load i32, i32* @O_RDONLY
%28 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %27)
store i32 %28, i32* %6
br label %54
29: ; preds = %14
%30 = load i8*, i8** %5
%31 = call i32 @strcmp(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp ne i32 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %29
%34 = load i8*, i8** %4
%35 = load i32, i32* @O_WRONLY
%36 = load i32, i32* @O_CREAT
%37 = or i32 %35, %36
%38 = load i32, i32* %7
%39 = call i32 (i8*, i32, ...) @open(i8* %34, i32 %37, i32 %38)
store i32 %39, i32* %6
br label %53
40: ; preds = %29
%41 = load i8*, i8** %5
%42 = call i32 @strcmp(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%43 = icmp ne i32 %42, 0
br i1 %43, label %51, label %44
44: ; preds = %40
%45 = load i8*, i8** %4
%46 = load i32, i32* @O_APPEND
%47 = load i32, i32* @O_CREAT
%48 = or i32 %46, %47
%49 = load i32, i32* %7
%50 = call i32 (i8*, i32, ...) @open(i8* %45, i32 %48, i32 %49)
store i32 %50, i32* %6
br label %52
51: ; preds = %40
store i32 -1, i32* %3
br label %58
52: ; preds = %44
br label %53
53: ; preds = %52, %33
br label %54
54: ; preds = %53, %25
%55 = load i32, i32* %6
%56 = sext i32 %55 to i64
%57 = trunc i64 %56 to i32
store i32 %57, i32* %3
br label %58
58: ; preds = %54, %51, %13
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [2 x i8] c"r\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"w\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [2 x i8] c"a\00"
@O_APPEND = global i32 0
define i32 @open_file(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i8* %1, null
%5 = and i1 %3, %4
br i1 %5, label %6, label %35
6: ; preds = %2
%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 @strcmp(i8* %1, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %6
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %17)
br label %35
19: ; preds = %6
%20 = tail call i32 @strcmp(i8* %1, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %25, i32 %13)
br label %35
27: ; preds = %19
%28 = tail call i32 @strcmp(i8* %1, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* @O_APPEND
%32 = load i32, i32* @O_CREAT
%33 = or i32 %32, %31
%34 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %33, i32 %13)
br label %35
35: ; preds = %27, %22, %30, %16, %2
%36 = phi i32 [ -1, %2 ], [ -1, %27 ], [ %34, %30 ], [ %26, %22 ], [ %18, %16 ]
ret i32 %36
}