#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_APPEND;
int O_RDONLY;
int O_RDWR;
int O_WRONLY;
int errno;
int open64(char *, int);
int /*<<< orphan*/ panic(char *);
int open_file(char *path, int r, int w, int append) {
  int mode = 0, fd;
  if (r && !w)
    mode = O_RDONLY;
  else if (!r && w)
    mode = O_WRONLY;
  else if (r && w)
    mode = O_RDWR;
  else
    panic("Impossible mode in open_file");
  if (append)
    mode |= O_APPEND;
  fd = open64(path, mode);
  if (fd < 0)
    return -errno;
  else
    return fd;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1161 <open_file+0x41>
cmpl   $0x0,-0x18(%rbp)
jne    1161 <open_file+0x41>
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    11c1 <open_file+0xa1>
cmpl   $0x0,-0x14(%rbp)
jne    1186 <open_file+0x66>
cmpl   $0x0,-0x18(%rbp)
je     1186 <open_file+0x66>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    11bc <open_file+0x9c>
cmpl   $0x0,-0x14(%rbp)
je     11ab <open_file+0x8b>
cmpl   $0x0,-0x18(%rbp)
je     11ab <open_file+0x8b>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    11b7 <open_file+0x97>
lea    0xe4e(%rip),%rdi
call   1030 <panic@plt>
jmp    11bc <open_file+0x9c>
jmp    11c1 <open_file+0xa1>
cmpl   $0x0,-0x1c(%rbp)
je     11da <open_file+0xba>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1040 <open64@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1206 <open_file+0xe6>
xor    %eax,%eax
mov    0x2dd4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    120c <open_file+0xec>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %rdi,%r14
test   %esi,%esi
je     113a <open_file+0x1a>
test   %edx,%edx
jne    113a <open_file+0x1a>
mov    0x2ea8(%rip),%rax
jmp    115a <open_file+0x3a>
test   %esi,%esi
jne    114b <open_file+0x2b>
test   %edx,%edx
je     114b <open_file+0x2b>
mov    0x2e77(%rip),%rax
jmp    115a <open_file+0x3a>
test   %esi,%esi
je     118d <open_file+0x6d>
test   %edx,%edx
je     118d <open_file+0x6d>
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
test   %ebx,%ebx
je     1169 <open_file+0x49>
mov    0x2e51(%rip),%rcx
mov    (%rcx),%ebx
or     %eax,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <open64@plt>
mov    0x2e54(%rip),%rcx
xor    %edx,%edx
sub    (%rcx),%edx
test   %eax,%eax
cmovs  %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6c(%rip),%rdi
call   1030 <panic@plt>
xor    %eax,%eax
test   %ebx,%ebx
je     1169 <open_file+0x49>
jmp    1160 <open_file+0x40>

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
@.str = [29 x i8] c"Impossible mode in open_file\00"
@O_APPEND = global i32 0
@errno = global i32 0
define i32 @open_file(i8* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %1, 0
%6 = icmp ne i32 %2, 0
%7 = or i1 %5, %6
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = load i32, i32* @O_RDONLY
br label %21
10: ; preds = %4
%11 = and i1 %5, %6
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = load i32, i32* @O_WRONLY
br label %21
14: ; preds = %10
%15 = icmp ne i32 %1, 0
%16 = and i1 %15, %6
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = load i32, i32* @O_RDWR
br label %21
19: ; preds = %14
%20 = tail call i32 @panic(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %12, %19, %17, %8
%22 = phi i32 [ %13, %12 ], [ %18, %17 ], [ 0, %19 ], [ %9, %8 ]
%23 = icmp eq i32 %3, 0
%24 = load i32, i32* @O_APPEND
%25 = select i1 %23, i32 0, i32 %24
%26 = or i32 %25, %22
%27 = tail call i32 @open64(i8* %0, i32 %26)
%28 = icmp slt i32 %27, 0
%29 = load i32, i32* @errno
%30 = sub nsw i32 0, %29
%31 = select i1 %28, i32 %30, i32 %27
ret i32 %31
}