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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
void *malloc(int);
int open(char *, int, int);
void *bvfs_unistd_open(char *path, int flags) {
  int fd;
  void *handle;
  fd = open(path, ((flags & 1) ? O_CREAT : 0) | O_RDWR, 0644);
  if (fd == -1)
    return NULL;
  handle = malloc(sizeof(fd));
  *(int *)handle = fd;
  return handle;
}


x86 assembly (clang, -O0):
<bvfs_unistd_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1157 <bvfs_unistd_open+0x37>
mov    0x2eab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1161 <bvfs_unistd_open+0x41>
xor    %eax,%eax
mov    %eax,-0x2c(%rbp)
jmp    1161 <bvfs_unistd_open+0x41>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    0x2e71(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1040 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1195 <bvfs_unistd_open+0x75>
movq   $0x0,-0x8(%rbp)
jmp    11b4 <bvfs_unistd_open+0x94>
mov    $0x4,%edi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bvfs_unistd_open>:
push   %rbx
and    $0x1,%esi
neg    %esi
mov    0x2ecb(%rip),%rax
and    (%rax),%esi
mov    0x2eaa(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     1157 <bvfs_unistd_open+0x37>
mov    %eax,%ebx
mov    $0x4,%edi
call   1030 <malloc@plt>
mov    %ebx,(%rax)
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
define i8* @bvfs_unistd_open(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = and i32 %9, 1
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* @O_CREAT
br label %15
14: ; preds = %2
br label %15
15: ; preds = %14, %12
%16 = phi i32 [ %13, %12 ], [ 0, %14 ]
%17 = load i32, i32* @O_RDWR
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %8, i32 %18, i32 420)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %15
store i8* null, i8** %3
br label %29
23: ; preds = %15
%24 = call i8* @malloc(i32 4)
store i8* %24, i8** %7
%25 = load i32, i32* %6
%26 = load i8*, i8** %7
%27 = bitcast i8* %26 to i32*
store i32 %25, i32* %27
%28 = load i8*, i8** %7
store i8* %28, i8** %3
br label %29
29: ; preds = %23, %22
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
define noalias i8* @bvfs_unistd_open(i8* nocapture %0, i32 %1) {
%3 = and i32 %1, 1
%4 = icmp eq i32 %3, 0
%5 = load i32, i32* @O_CREAT
%6 = select i1 %4, i32 0, i32 %5
%7 = load i32, i32* @O_RDWR
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8, i32 420)
%10 = icmp eq i32 %9, -1
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = tail call dereferenceable_or_null(4) i8* @malloc(i32 4)
%13 = bitcast i8* %12 to i32*
store i32 %9, i32* %13
br label %14
14: ; preds = %2, %11
%15 = phi i8* [ %12, %11 ], [ null, %2 ]
ret i8* %15
}