#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_APPEND;
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
scalar_t__ S_ISCHR(int /*<<< orphan*/);
scalar_t__ S_ISFIFO(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ msg_Err(int /*<<< orphan*/ *, char *, char const *,
                           int /*<<< orphan*/);
int open(char const *, int, int);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int OpenFile(const char *psz_arg, bool b_read, bool b_append) {
  struct stat sb;
  int i_fd;
  int i_mode = b_read ? O_RDONLY : O_WRONLY;
  if (stat(psz_arg, &sb) < 0) {
    if (b_read) {
      msg_Err(NULL, "file %s doesn't exist (%s)", psz_arg, strerror(errno));
      exit(EXIT_FAILURE);
    }
    i_mode |= O_CREAT;
  } else if (!(S_ISCHR(sb.st_mode) || S_ISFIFO(sb.st_mode)) && !b_read) {
    if (b_append)
      i_mode |= O_APPEND;
    else
      i_mode |= O_TRUNC;
  }
  if ((i_fd = open(psz_arg, i_mode, 0644)) < 0) {
    msg_Err(NULL, "couldn't open file %s (%s)", psz_arg, strerror(errno));
    exit(EXIT_FAILURE);
  }
  return i_fd;
}


x86 assembly (clang, -O0):
<OpenFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     119d <OpenFile+0x2d>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    11a9 <OpenFile+0x39>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    121e <OpenFile+0xae>
cmpl   $0x0,-0xc(%rbp)
je     120a <OpenFile+0x9a>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
xor    %eax,%eax
mov    %eax,%edi
lea    0xe09(%rip),%rsi
call   1040 <msg_Err@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1283 <OpenFile+0x113>
mov    -0x18(%rbp),%edi
call   1060 <S_ISCHR@plt>
cmp    $0x0,%rax
jne    127e <OpenFile+0x10e>
mov    -0x18(%rbp),%edi
call   1030 <S_ISFIFO@plt>
cmp    $0x0,%rax
jne    127e <OpenFile+0x10e>
cmpl   $0x0,-0xc(%rbp)
jne    127e <OpenFile+0x10e>
cmpl   $0x0,-0x10(%rbp)
je     126a <OpenFile+0xfa>
mov    0x2d4b(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1279 <OpenFile+0x109>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    127e <OpenFile+0x10e>
jmp    1283 <OpenFile+0x113>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    12da <OpenFile+0x16a>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
xor    %eax,%eax
mov    %eax,%edi
lea    0xd54(%rip),%rsi
call   1040 <msg_Err@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OpenFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,%r15
test   %esi,%esi
je     118c <OpenFile+0x1c>
mov    0x2e4e(%rip),%rax
jmp    1193 <OpenFile+0x23>
mov    0x2e1d(%rip),%rax
mov    (%rax),%ebp
mov    %rsp,%rsi
mov    %r15,%rdi
call   1050 <stat@plt>
test   %rax,%rax
js     11d1 <OpenFile+0x61>
mov    (%rsp),%edi
call   1060 <S_ISCHR@plt>
test   %rax,%rax
jne    11de <OpenFile+0x6e>
mov    (%rsp),%edi
call   1030 <S_ISFIFO@plt>
test   %ebx,%ebx
jne    11de <OpenFile+0x6e>
test   %rax,%rax
jne    11de <OpenFile+0x6e>
test   %r14d,%r14d
je     11fc <OpenFile+0x8c>
mov    0x2dd9(%rip),%rax
jmp    11dc <OpenFile+0x6c>
test   %ebx,%ebx
jne    121c <OpenFile+0xac>
mov    0x2e1c(%rip),%rax
or     (%rax),%ebp
mov    %r15,%rdi
mov    %ebp,%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     1205 <OpenFile+0x95>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ded(%rip),%rax
jmp    11dc <OpenFile+0x6c>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe01(%rip),%rsi
jmp    1231 <OpenFile+0xc1>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdcf(%rip),%rsi
xor    %edi,%edi
mov    %r15,%rdx
mov    %eax,%ecx
call   1040 <msg_Err@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str = [27 x i8] c"file %s doesn't exist (%s)\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [27 x i8] c"couldn't open file %s (%s)\00"
define i32 @OpenFile(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = load i32, i32* @O_RDONLY
br label %16
14: ; preds = %3
%15 = load i32, i32* @O_WRONLY
br label %16
16: ; preds = %14, %12
%17 = phi i32 [ %13, %12 ], [ %15, %14 ]
store i32 %17, i32* %9
%18 = load i8*, i8** %4
%19 = call i64 @stat(i8* %18, %struct.stat* %7)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %35
21: ; preds = %16
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = load i8*, i8** %4
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 @msg_Err(i32* null, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %21
%32 = load i32, i32* @O_CREAT
%33 = load i32, i32* %9
%34 = or i32 %33, %32
store i32 %34, i32* %9
br label %61
35: ; preds = %16
%36 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i64 @S_ISCHR(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %60, label %40
40: ; preds = %35
%41 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @S_ISFIFO(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %60, label %45
45: ; preds = %40
%46 = load i32, i32* %5
%47 = icmp ne i32 %46, 0
br i1 %47, label %60, label %48
48: ; preds = %45
%49 = load i32, i32* %6
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = load i32, i32* @O_APPEND
%53 = load i32, i32* %9
%54 = or i32 %53, %52
store i32 %54, i32* %9
br label %59
55: ; preds = %48
%56 = load i32, i32* @O_TRUNC
%57 = load i32, i32* %9
%58 = or i32 %57, %56
store i32 %58, i32* %9
br label %59
59: ; preds = %55, %51
br label %60
60: ; preds = %59, %45, %40, %35
br label %61
61: ; preds = %60, %31
%62 = load i8*, i8** %4
%63 = load i32, i32* %9
%64 = call i32 @open(i8* %62, i32 %63, i32 420)
store i32 %64, i32* %8
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %73
66: ; preds = %61
%67 = load i8*, i8** %4
%68 = load i32, i32* @errno
%69 = call i32 @strerror(i32 %68)
%70 = call i32 @msg_Err(i32* null, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %67, i32 %69)
%71 = load i32, i32* @EXIT_FAILURE
%72 = call i32 @exit(i32 %71)
unreachable
73: ; preds = %61
%74 = load i32, i32* %8
ret i32 %74
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str = [27 x i8] c"file %s doesn't exist (%s)\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [27 x i8] c"couldn't open file %s (%s)\00"
define i32 @OpenFile(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = icmp ne i32 %1, 0
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_WRONLY
%9 = select i1 %6, i32 %7, i32 %8
%10 = call i64 @stat(i8* %0, %struct.stat* %4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %3
br i1 %6, label %13, label %19
13: ; preds = %12
%14 = load i32, i32* @errno
%15 = call i32 @strerror(i32 %14)
%16 = call i32 @msg_Err(i32* null, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %9
br label %40
22: ; preds = %3
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i64 @S_ISCHR(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %40
27: ; preds = %22
%28 = load i32, i32* %23
%29 = call i64 @S_ISFIFO(i32 %28)
%30 = icmp ne i64 %29, 0
%31 = or i1 %6, %30
br i1 %31, label %40, label %32
32: ; preds = %27
%33 = icmp eq i32 %2, 0
br i1 %33, label %37, label %34
34: ; preds = %32
%35 = load i32, i32* @O_APPEND
%36 = or i32 %35, %9
br label %40
37: ; preds = %32
%38 = load i32, i32* @O_TRUNC
%39 = or i32 %38, %9
br label %40
40: ; preds = %22, %27, %37, %34, %19
%41 = phi i32 [ %21, %19 ], [ %9, %22 ], [ %9, %27 ], [ %36, %34 ], [ %39, %37 ]
%42 = call i32 @open(i8* %0, i32 %41, i32 420)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %40
%45 = load i32, i32* @errno
%46 = call i32 @strerror(i32 %45)
%47 = call i32 @msg_Err(i32* null, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %46)
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %40
ret i32 %42
}