#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_CREAT;
int O_EXCL;
int O_RDWR;
int open(char const *, int, ...);
int openfile(const char *filename) {
  int fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0666);
  if (fd == -1) {
    fd = open(filename, O_RDWR | O_APPEND);
  }
  return fd;
}


x86 assembly (clang, -O0):
<openfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2eb9(%rip),%rax
mov    (%rax),%esi
mov    0x2ec8(%rip),%rax
or     (%rax),%esi
mov    0x2e97(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1030 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1174 <openfile+0x64>
mov    -0x8(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    $0x0,%al
call   1030 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openfile>:
push   %r14
push   %rbx
push   %rax
mov    0x2ec5(%rip),%r14
mov    0x2ed6(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    %rdi,%rbx
mov    0x2e9f(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1030 <open@plt>
cmp    $0xffffffff,%eax
je     114c <openfile+0x3c>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    %rbx,%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <open@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_APPEND = global i32 0
define i32 @openfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = load i32, i32* @O_EXCL
%9 = or i32 %7, %8
%10 = call i32 (i8*, i32, ...) @open(i8* %4, i32 %9, i32 438)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_APPEND
%17 = or i32 %15, %16
%18 = call i32 (i8*, i32, ...) @open(i8* %14, i32 %17)
store i32 %18, i32* %3
br label %19
19: ; preds = %13, %1
%20 = load i32, i32* %3
ret i32 %20
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_APPEND = global i32 0
define i32 @openfile(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @O_EXCL
%6 = or i32 %4, %5
%7 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %6, i32 438)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_APPEND
%12 = or i32 %11, %10
%13 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %12)
br label %14
14: ; preds = %9, %1
%15 = phi i32 [ %13, %9 ], [ %7, %1 ]
ret i32 %15
}