#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 (*FUNC_PTR)(char const *, int);

/* Variables and functions */
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ RTLD_LAZY;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int open(const char *pathname, int flags, ...) {
  typedef int (*FUNC_PTR)(const char *pathname, int flags, ...);
  static FUNC_PTR libc_open;
  int fd = -1;
  if (!libc_open) {
    void *handle;
    char *error;
    handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
    if (!handle) {
      fputs(dlerror(), stderr);
      exit(1);
    }
    libc_open = (FUNC_PTR)dlsym(handle, "open");
    if ((error = dlerror()) != NULL) {
      fprintf(stderr, "%s\n", error);
      exit(1);
    }
  }
  fd = libc_open(pathname, flags);
  if (fd >= 0) {
    int fd_flags = fcntl(fd, F_GETFD, 0);
    if (fd_flags >= 0) {
      fd_flags |= FD_CLOEXEC;
      fcntl(fd, F_SETFD, fd_flags);
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
cmpq   $0x0,0x2ed2(%rip)
jne    122e <open+0xbe>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11da <open+0x6a>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1080 <dlsym@plt>
mov    %rax,0x2e6f(%rip)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1229 <open+0xb9>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdfa(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    122e <open+0xbe>
mov    0x2e2b(%rip),%rcx
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x0,%al
call   *%rcx
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1295 <open+0x125>
mov    -0x10(%rbp),%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1290 <open+0x120>
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
call   1040 <fcntl@plt>
jmp    1295 <open+0x125>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2ee1(%rip),%rcx
test   %rcx,%rcx
jne    11c7 <open+0x57>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     1210 <open+0xa0>
lea    0xe6a(%rip),%rsi
mov    %rax,%rdi
call   1080 <dlsym@plt>
mov    %rax,0x2eac(%rip)
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    1232 <open+0xc2>
mov    0x2e99(%rip),%rcx
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   *%rcx
mov    %eax,%ebx
test   %eax,%eax
js     1207 <open+0x97>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     1207 <open+0x97>
mov    0x2ddd(%rip),%rcx
or     (%rcx),%eax
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2da7(%rip),%rcx
mov    (%rcx),%edi
lea    0xdd2(%rip),%rsi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@open.libc_open = internal global i32 (i8*, i32, ...)* null
@.str = [15 x i8] c"/lib/libc.so.6\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @open(i8* %0, i32 %1, ...) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 -1, i32* %5
%9 = load i32 (i8*, i32, ...)*, i32 (i8*, i32, ...)** @open.libc_open
%10 = icmp ne i32 (i8*, i32, ...)* %9, null
br i1 %10, label %33, label %11
11: ; preds = %2
%12 = load i32, i32* @RTLD_LAZY
%13 = call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = call i8* (...) @dlerror()
%18 = load i32, i32* @stderr
%19 = call i32 @fputs(i8* %17, i32 %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %11
%22 = load i8*, i8** %6
%23 = call i64 @dlsym(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = inttoptr i64 %23 to i32 (i8*, i32, ...)*
store i32 (i8*, i32, ...)* %24, i32 (i8*, i32, ...)** @open.libc_open
%25 = call i8* (...) @dlerror()
store i8* %25, i8** %7
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %32
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %7
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
br label %33
33: ; preds = %32, %2
%34 = load i32 (i8*, i32, ...)*, i32 (i8*, i32, ...)** @open.libc_open
%35 = load i8*, i8** %3
%36 = load i32, i32* %4
%37 = call i32 (i8*, i32, ...) %34(i8* %35, i32 %36)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = icmp sge i32 %38, 0
br i1 %39, label %40, label %55
40: ; preds = %33
%41 = load i32, i32* %5
%42 = load i32, i32* @F_GETFD
%43 = call i32 @fcntl(i32 %41, i32 %42, i32 0)
store i32 %43, i32* %8
%44 = load i32, i32* %8
%45 = icmp sge i32 %44, 0
br i1 %45, label %46, label %54
46: ; preds = %40
%47 = load i32, i32* @FD_CLOEXEC
%48 = load i32, i32* %8
%49 = or i32 %48, %47
store i32 %49, i32* %8
%50 = load i32, i32* %5
%51 = load i32, i32* @F_SETFD
%52 = load i32, i32* %8
%53 = call i32 @fcntl(i32 %50, i32 %51, i32 %52)
br label %54
54: ; preds = %46, %40
br label %55
55: ; preds = %54, %33
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
@open.libc_open = internal unnamed_addr global i32 (i8*, i32, ...)* null
@.str = [15 x i8] c"/lib/libc.so.6\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @open(i8* %0, i32 %1, ...) {
%3 = load i32 (i8*, i32, ...)*, i32 (i8*, i32, ...)** @open.libc_open
%4 = icmp eq i32 (i8*, i32, ...)* %3, null
br i1 %4, label %5, label %25
5: ; preds = %2
%6 = load i32, i32* @RTLD_LAZY
%7 = tail call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %14
9: ; preds = %5
%10 = tail call i8* (...) @dlerror()
%11 = load i32, i32* @stderr
%12 = tail call i32 @fputs(i8* %10, i32 %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %5
%15 = tail call i64 @dlsym(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = inttoptr i64 %15 to i32 (i8*, i32, ...)*
store i32 (i8*, i32, ...)* %16, i32 (i8*, i32, ...)** @open.libc_open
%17 = tail call i8* (...) @dlerror()
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = load i32 (i8*, i32, ...)*, i32 (i8*, i32, ...)** @open.libc_open
br label %25
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %19, %2
%26 = phi i32 (i8*, i32, ...)* [ %20, %19 ], [ %3, %2 ]
%27 = tail call i32 (i8*, i32, ...) %26(i8* %0, i32 %1)
%28 = icmp sgt i32 %27, -1
br i1 %28, label %29, label %38
29: ; preds = %25
%30 = load i32, i32* @F_GETFD
%31 = tail call i32 @fcntl(i32 %27, i32 %30, i32 0)
%32 = icmp sgt i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32, i32* @FD_CLOEXEC
%35 = or i32 %34, %31
%36 = load i32, i32* @F_SETFD
%37 = tail call i32 @fcntl(i32 %27, i32 %36, i32 %35)
br label %38
38: ; preds = %29, %33, %25
ret i32 %27
}