#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, 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 socketpair(int d, int type, int protocol, int sv[2]) {
  typedef int (*FUNC_PTR)(int d, int type, int protocol, int sv[2]);
  static FUNC_PTR libc_socketpair;
  int ret = -1;
  if (!libc_socketpair) {
    void *handle;
    char *error;
    handle = dlopen("libc.so.6", RTLD_LAZY);
    if (!handle) {
      fputs(dlerror(), stderr);
      exit(1);
    }
    libc_socketpair = (FUNC_PTR)dlsym(handle, "socketpair");
    if ((error = dlerror()) != NULL) {
      fprintf(stderr, "%s\n", error);
      exit(1);
    }
  }
  ret = libc_socketpair(d, type, protocol, sv);
  if (!ret) {
    int fd_flags = fcntl(sv[0], F_GETFD, 0);
    if (fd_flags >= 0) {
      fd_flags |= FD_CLOEXEC;
      fcntl(sv[0], F_SETFD, fd_flags);
    }
    fd_flags = fcntl(sv[1], F_GETFD, 0);
    if (fd_flags >= 0) {
      fd_flags |= FD_CLOEXEC;
      fcntl(sv[1], F_SETFD, fd_flags);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<socketpair>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpq   $0x0,0x2ecc(%rip)
jne    1234 <socketpair+0xc4>
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e0 <socketpair+0x70>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rdi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   1080 <dlsym@plt>
mov    %rax,0x2e69(%rip)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     122f <socketpair+0xbf>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xdf5(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1234 <socketpair+0xc4>
mov    0x2e25(%rip),%rax
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   *%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    12f0 <socketpair+0x180>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     12a0 <socketpair+0x130>
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
or     -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
mov    -0x34(%rbp),%edx
call   1040 <fcntl@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     12eb <socketpair+0x17b>
mov    0x2d05(%rip),%rax
mov    (%rax),%eax
or     -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
mov    -0x34(%rbp),%edx
call   1040 <fcntl@plt>
jmp    12f0 <socketpair+0x180>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketpair>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2ed8(%rip),%rax
test   %rax,%rax
jne    11d8 <socketpair+0x68>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     1258 <socketpair+0xe8>
lea    0xe58(%rip),%rsi
mov    %rax,%rdi
call   1080 <dlsym@plt>
mov    %rax,0x2e9f(%rip)
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    127a <socketpair+0x10a>
mov    0x2e88(%rip),%rax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   *%rax
mov    %eax,%ebp
test   %eax,%eax
jne    124b <socketpair+0xdb>
mov    (%r14),%edi
mov    0x2dd4(%rip),%rbx
mov    (%rbx),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     121d <socketpair+0xad>
mov    0x2dc8(%rip),%rcx
or     (%rcx),%eax
mov    (%r14),%edi
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edx
call   1040 <fcntl@plt>
mov    0x4(%r14),%edi
mov    (%rbx),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     124b <socketpair+0xdb>
mov    0x2d9b(%rip),%rcx
or     (%rcx),%eax
mov    0x4(%r14),%edi
mov    0x2d96(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edx
call   1040 <fcntl@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    0x2d7a(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d5f(%rip),%rcx
mov    (%rcx),%edi
lea    0xd8b(%rip),%rsi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@socketpair.libc_socketpair = internal global i32 (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 = [11 x i8] c"socketpair\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 @socketpair(i32 %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
store i32 -1, i32* %9
%13 = load i32 (i32, i32, i32, i32*)*, i32 (i32, i32, i32, i32*)** @socketpair.libc_socketpair
%14 = icmp ne i32 (i32, i32, i32, i32*)* %13, null
br i1 %14, label %37, label %15
15: ; preds = %4
%16 = load i32, i32* @RTLD_LAZY
%17 = call i8* @dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %16)
store i8* %17, i8** %10
%18 = load i8*, i8** %10
%19 = icmp ne i8* %18, null
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = call i8* (...) @dlerror()
%22 = load i32, i32* @stderr
%23 = call i32 @fputs(i8* %21, i32 %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %15
%26 = load i8*, i8** %10
%27 = call i64 @dlsym(i8* %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%28 = inttoptr i64 %27 to i32 (i32, i32, i32, i32*)*
store i32 (i32, i32, i32, i32*)* %28, i32 (i32, i32, i32, i32*)** @socketpair.libc_socketpair
%29 = call i8* (...) @dlerror()
store i8* %29, i8** %11
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %36
31: ; preds = %25
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %11
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %25
br label %37
37: ; preds = %36, %4
%38 = load i32 (i32, i32, i32, i32*)*, i32 (i32, i32, i32, i32*)** @socketpair.libc_socketpair
%39 = load i32, i32* %5
%40 = load i32, i32* %6
%41 = load i32, i32* %7
%42 = load i32*, i32** %8
%43 = call i32 %38(i32 %39, i32 %40, i32 %41, i32* %42)
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = icmp ne i32 %44, 0
br i1 %45, label %83, label %46
46: ; preds = %37
%47 = load i32*, i32** %8
%48 = getelementptr inbounds i32, i32* %47, i64 0
%49 = load i32, i32* %48
%50 = load i32, i32* @F_GETFD
%51 = call i32 @fcntl(i32 %49, i32 %50, i32 0)
store i32 %51, i32* %12
%52 = load i32, i32* %12
%53 = icmp sge i32 %52, 0
br i1 %53, label %54, label %64
54: ; preds = %46
%55 = load i32, i32* @FD_CLOEXEC
%56 = load i32, i32* %12
%57 = or i32 %56, %55
store i32 %57, i32* %12
%58 = load i32*, i32** %8
%59 = getelementptr inbounds i32, i32* %58, i64 0
%60 = load i32, i32* %59
%61 = load i32, i32* @F_SETFD
%62 = load i32, i32* %12
%63 = call i32 @fcntl(i32 %60, i32 %61, i32 %62)
br label %64
64: ; preds = %54, %46
%65 = load i32*, i32** %8
%66 = getelementptr inbounds i32, i32* %65, i64 1
%67 = load i32, i32* %66
%68 = load i32, i32* @F_GETFD
%69 = call i32 @fcntl(i32 %67, i32 %68, i32 0)
store i32 %69, i32* %12
%70 = load i32, i32* %12
%71 = icmp sge i32 %70, 0
br i1 %71, label %72, label %82
72: ; preds = %64
%73 = load i32, i32* @FD_CLOEXEC
%74 = load i32, i32* %12
%75 = or i32 %74, %73
store i32 %75, i32* %12
%76 = load i32*, i32** %8
%77 = getelementptr inbounds i32, i32* %76, i64 1
%78 = load i32, i32* %77
%79 = load i32, i32* @F_SETFD
%80 = load i32, i32* %12
%81 = call i32 @fcntl(i32 %78, i32 %79, i32 %80)
br label %82
82: ; preds = %72, %64
br label %83
83: ; preds = %82, %37
%84 = load i32, i32* %9
ret i32 %84
}

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