#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)(int, int, 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 socket(int domain, int type, int protocol) {
  typedef int (*FUNC_PTR)(int domain, int type, int protocol);
  static FUNC_PTR libc_socket;
  int fd = -1;
  if (!libc_socket) {
    void *handle;
    char *error;
    handle = dlopen("libc.so.6", RTLD_LAZY);
    if (!handle) {
      fputs(dlerror(), stderr);
      exit(1);
    }
    libc_socket = (FUNC_PTR)dlsym(handle, "socket");
    if ((error = dlerror()) != NULL) {
      fprintf(stderr, "%s\n", error);
      exit(1);
    }
  }
  fd = libc_socket(domain, type, protocol);
  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):
<socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
cmpq   $0x0,0x2ed0(%rip)
jne    1230 <socket+0xc0>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11dc <socket+0x6c>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xe23(%rip),%rsi
call   1080 <dlsym@plt>
mov    %rax,0x2e6d(%rip)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     122b <socket+0xbb>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdf5(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1230 <socket+0xc0>
mov    0x2e29(%rip),%rax
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   *%rax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1297 <socket+0x127>
mov    -0x10(%rbp),%edi
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1292 <socket+0x122>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
call   1040 <fcntl@plt>
jmp    1297 <socket+0x127>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2ede(%rip),%rax
test   %rax,%rax
jne    11ca <socket+0x5a>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     1211 <socket+0xa1>
lea    0xe62(%rip),%rsi
mov    %rax,%rdi
call   1080 <dlsym@plt>
mov    %rax,0x2ea9(%rip)
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    1233 <socket+0xc3>
mov    0x2e96(%rip),%rax
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r14d,%edx
call   *%rax
mov    %eax,%ebx
test   %eax,%eax
js     120a <socket+0x9a>
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     120a <socket+0x9a>
mov    0x2dda(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2da6(%rip),%rcx
mov    (%rcx),%edi
lea    0xdce(%rip),%rsi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

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