#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 const O_CREAT;
int const O_RDONLY;
int const O_TRUNC;
int const O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *const, int,
                           char *);
int open(char const *const, int const, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int open_file(const char *const path, const int ext_flags, const int rw) {
  int fd;
  int err;
  if (rw == 2) {
    fd =
        open(path, O_WRONLY | O_TRUNC | O_CREAT | ext_flags, S_IRUSR | S_IWUSR);
  } else {
    fd = open(path, O_RDONLY | ext_flags, 0);
  }
  if (fd == -1) {
    err = errno;
    fprintf(stderr, "ERR open: %s : (%d)%s\n", path, err, strerror(err));
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x2,-0x10(%rbp)
jne    118d <open_file+0x5d>
mov    -0x8(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
or     (%rax),%esi
mov    0x2e8f(%rip),%rax
or     (%rax),%esi
or     -0xc(%rbp),%esi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
mov    0x2e52(%rip),%rax
or     (%rax),%edx
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
jmp    11a7 <open_file+0x77>
mov    -0x8(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
or     -0xc(%rbp),%esi
xor    %edx,%edx
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f8 <open_file+0xc8>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    %rax,%r8
lea    0xe1d(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e87(%rip),%rax
mov    0x2ea0(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %eax,%eax
cmp    $0x2,%edx
cmove  %ecx,%eax
mov    %eax,%edx
call   1040 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1196 <open_file+0x66>
mov    0x2e67(%rip),%rax
mov    (%rax),%r14d
mov    0x2e65(%rip),%rax
mov    (%rax),%r15d
mov    %r14d,%edi
call   1050 <strerror@plt>
lea    0xe8b(%rip),%rsi
mov    %r15d,%edi
mov    %rbx,%rdx
mov    %r14d,%ecx
mov    %rax,%r8
call   1030 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = constant i32 0
@O_TRUNC = constant i32 0
@O_CREAT = constant i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@O_RDONLY = constant i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"ERR open: %s : (%d)%s\0A\00"
define i32 @open_file(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, 2
br i1 %10, label %11, label %24
11: ; preds = %3
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* %5
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %12, i32 %19, i32 %22)
store i32 %23, i32* %7
br label %30
24: ; preds = %3
%25 = load i8*, i8** %4
%26 = load i32, i32* @O_RDONLY
%27 = load i32, i32* %5
%28 = or i32 %26, %27
%29 = call i32 @open(i8* %25, i32 %28, i32 0)
store i32 %29, i32* %7
br label %30
30: ; preds = %24, %11
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %41
33: ; preds = %30
%34 = load i32, i32* @errno
store i32 %34, i32* %8
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %4
%37 = load i32, i32* %8
%38 = load i32, i32* %8
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %36, i32 %37, i8* %39)
br label %41
41: ; preds = %33, %30
%42 = load i32, i32* %7
ret i32 %42
}

LLVM IR (-O2):
@O_WRONLY = constant i32 0
@O_TRUNC = constant i32 0
@O_CREAT = constant i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@O_RDONLY = constant i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"ERR open: %s : (%d)%s\0A\00"
define i32 @open_file(i8* %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %2, 2
%5 = load i32, i32* @S_IRUSR
%6 = load i32, i32* @S_IWUSR
%7 = or i32 %6, %5
%8 = select i1 %4, i32 %7, i32 0
%9 = tail call i32 @open(i8* %0, i32 %1, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %3
%12 = load i32, i32* @errno
%13 = load i32, i32* @stderr
%14 = tail call i8* @strerror(i32 %12)
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %12, i8* %14)
br label %16
16: ; preds = %11, %3
ret i32 %9
}