#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;
  if ((fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0644)) == -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    $0x1a4,%edx
mov    $0x0,%al
call   1030 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1173 <openfile+0x63>
mov    -0x8(%rbp),%rdi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%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    $0x1a4,%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 420)
store i32 %10, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_APPEND
%16 = or i32 %14, %15
%17 = call i32 (i8*, i32, ...) @open(i8* %13, i32 %16)
store i32 %17, i32* %3
br label %18
18: ; preds = %12, %1
%19 = load i32, i32* %3
ret i32 %19
}

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 420)
%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
}