#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_NOFOLLOW;
int O_RDONLY;
int errno;
int open(char const *, int);
int openat(int, char const *, int);
int open_helper(int dirfd, const char *name) {
  int fd;
  if (dirfd >= 0) {
    fd = openat(dirfd, name, O_RDONLY | O_NOFOLLOW);
  } else {
    fd = open(name, O_RDONLY | O_NOFOLLOW);
  }
  if (fd >= 0) {
    return fd;
  }
  return -errno;
}


x86 assembly (clang, -O0):
<open_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     115f <open_helper+0x3f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e99(%rip),%rax
mov    (%rax),%edx
mov    0x2ea8(%rip),%rax
or     (%rax),%edx
call   1030 <openat@plt>
mov    %eax,-0x14(%rbp)
jmp    117d <open_helper+0x5d>
mov    -0x10(%rbp),%rdi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    0x2e85(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1192 <open_helper+0x72>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a0 <open_helper+0x80>
xor    %eax,%eax
mov    0x2e35(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_helper>:
push   %rax
mov    0x2eb8(%rip),%rax
mov    0x2ec9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
test   %edi,%edi
js     113e <open_helper+0x1e>
call   1030 <openat@plt>
jmp    1148 <open_helper+0x28>
mov    %rsi,%rdi
mov    %edx,%esi
call   1040 <open@plt>
mov    0x2e81(%rip),%rdx
xor    %ecx,%ecx
sub    (%rdx),%ecx
test   %eax,%eax
cmovns %eax,%ecx
mov    %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_NOFOLLOW = global i32 0
@errno = global i32 0
define i32 @open_helper(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i32, i32* %4
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %2
%10 = load i32, i32* %4
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDONLY
%13 = load i32, i32* @O_NOFOLLOW
%14 = or i32 %12, %13
%15 = call i32 @openat(i32 %10, i8* %11, i32 %14)
store i32 %15, i32* %6
br label %22
16: ; preds = %2
%17 = load i8*, i8** %5
%18 = load i32, i32* @O_RDONLY
%19 = load i32, i32* @O_NOFOLLOW
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %17, i32 %20)
store i32 %21, i32* %6
br label %22
22: ; preds = %16, %9
%23 = load i32, i32* %6
%24 = icmp sge i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = load i32, i32* %6
store i32 %26, i32* %3
br label %30
27: ; preds = %22
%28 = load i32, i32* @errno
%29 = sub nsw i32 0, %28
store i32 %29, i32* %3
br label %30
30: ; preds = %27, %25
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_NOFOLLOW = global i32 0
@errno = global i32 0
define i32 @open_helper(i32 %0, i8* %1) {
%3 = icmp sgt i32 %0, -1
%4 = load i32, i32* @O_RDONLY
%5 = load i32, i32* @O_NOFOLLOW
%6 = or i32 %5, %4
br i1 %3, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @openat(i32 %0, i8* %1, i32 %6)
br label %11
9: ; preds = %2
%10 = tail call i32 @open(i8* %1, i32 %6)
br label %11
11: ; preds = %9, %7
%12 = phi i32 [ %8, %7 ], [ %10, %9 ]
%13 = icmp sgt i32 %12, -1
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
%16 = select i1 %13, i32 %12, i32 %15
ret i32 %16
}