#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fatal(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int open(char const *, int, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
FILE *open_file(const char *path, int force) {
  int fd;
  FILE *f;
  int flags = O_WRONLY | 0 | O_CREAT;
  if (!force)
    flags |= O_EXCL;
  fd = open(path, flags, S_IRUSR | S_IWUSR | 0 | 0);
  do {
    if (fd < 0)
      fatal("cannot create '%s': %s\n", path, strerror(errno));
  } while (0);
  f = fdopen(fd, "wb");
  do {
    if (f == NULL)
      fatal("cannot create stream for '%s': %s\n", path, strerror(errno));
  } while (0);
  return f;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
or     $0x0,%eax
mov    0x2e96(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1180 <open_file+0x40>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    0x2e49(%rip),%rax
or     (%rax),%edx
or     $0x0,%edx
or     $0x0,%edx
call   1040 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11d9 <open_file+0x99>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe2c(%rip),%rdi
call   1060 <fatal@plt>
jmp    11de <open_file+0x9e>
mov    -0x10(%rbp),%edi
lea    0xe30(%rip),%rsi
call   1030 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1224 <open_file+0xe4>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xdfc(%rip),%rdi
call   1060 <fatal@plt>
jmp    1229 <open_file+0xe9>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%eax
mov    %rdi,%rbx
mov    0x2e61(%rip),%rcx
mov    0x2ea2(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
xor    %ecx,%ecx
test   %eax,%eax
jne    1169 <open_file+0x29>
mov    0x2e69(%rip),%rax
mov    (%rax),%ecx
or     %ecx,%esi
mov    0x2e4e(%rip),%rax
mov    0x2e67(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1040 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11aa <open_file+0x6a>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe60(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <fatal@plt>
lea    0xe67(%rip),%rsi
mov    %ebp,%edi
call   1030 <fdopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11df <open_file+0x9f>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe46(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <fatal@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [24 x i8] c"cannot create '%s': %s\0A\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [35 x i8] c"cannot create stream for '%s': %s\0A\00"
define i32* @open_file(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* @O_WRONLY
%9 = or i32 %8, 0
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
store i32 %11, i32* %7
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i32, i32* @O_EXCL
%16 = load i32, i32* %7
%17 = or i32 %16, %15
store i32 %17, i32* %7
br label %18
18: ; preds = %14, %2
%19 = load i8*, i8** %3
%20 = load i32, i32* %7
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %21, %22
%24 = or i32 %23, 0
%25 = or i32 %24, 0
%26 = call i32 @open(i8* %19, i32 %20, i32 %25)
store i32 %26, i32* %5
br label %27
27: ; preds = %18
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i8*, i8** %3
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = call i32 @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %31, i32 %33)
br label %35
35: ; preds = %30, %27
br label %36
36: ; preds = %35
%37 = load i32, i32* %5
%38 = call i32* @fdopen(i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %38, i32** %6
br label %39
39: ; preds = %36
%40 = load i32*, i32** %6
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = load i8*, i8** %3
%44 = load i32, i32* @errno
%45 = call i32 @strerror(i32 %44)
%46 = call i32 @fatal(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %43, i32 %45)
br label %47
47: ; preds = %42, %39
br label %48
48: ; preds = %47
%49 = load i32*, i32** %6
ret i32* %49
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [24 x i8] c"cannot create '%s': %s\0A\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [35 x i8] c"cannot create stream for '%s': %s\0A\00"
define noalias i32* @open_file(i8* %0, i32 %1) {
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = icmp eq i32 %1, 0
%7 = load i32, i32* @O_EXCL
%8 = select i1 %6, i32 %7, i32 0
%9 = or i32 %5, %8
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* %0, i32 %9, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %17)
br label %19
19: ; preds = %15, %2
%20 = tail call i32* @fdopen(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* @errno
%24 = tail call i32 @strerror(i32 %23)
%25 = tail call i32 @fatal(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %24)
br label %26
26: ; preds = %22, %19
ret i32* %20
}