#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int s_addr;
};
struct sockaddr_in {
  int sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ listen_addr;
typedef int /*<<< orphan*/ connect_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_LOOPBACK;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int getsockname(int, struct sockaddr *, int *);
int htonl(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_socketpair(int fd[2]) {
  int listener = -1;
  int connector = -1;
  int acceptor = -1;
  struct sockaddr_in listen_addr;
  struct sockaddr_in connect_addr;
  int size;
  int saved_errno = -1;
  listener = socket(AF_INET, SOCK_STREAM, 0);
  if (listener < 0)
    return -1;
  memset(&listen_addr, 0, sizeof(listen_addr));
  listen_addr.sin_family = AF_INET;
  listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  listen_addr.sin_port = 0;
  if (bind(listener, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) ==
      -1)
    return -1;
  if (listen(listener, 1) == -1)
    return -1;
  connector = socket(AF_INET, SOCK_STREAM, 0);
  if (connector < 0)
    return -1;
  size = sizeof(connect_addr);
  if (getsockname(listener, (struct sockaddr *)&connect_addr, &size) == -1)
    return -1;
  if (size != sizeof(connect_addr))
    return -1;
  printf("listener addr %d port %d\n", connect_addr.sin_addr.s_addr,
         connect_addr.sin_port);
  if (connect(connector, (struct sockaddr *)&connect_addr,
              sizeof(connect_addr)) == -1)
    return -1;
  size = sizeof(listen_addr);
  acceptor = accept(listener, (struct sockaddr *)&listen_addr, &size);
  if (acceptor < 0)
    return -1;
  if (size != sizeof(listen_addr))
    return -1;
  close(listener);
  if (getsockname(connector, (struct sockaddr *)&connect_addr, &size) == -1)
    return -1;
  printf("connector addr %d port %d\n", connect_addr.sin_addr.s_addr,
         connect_addr.sin_port);
  fd[0] = connector;
  fd[1] = acceptor;
  return 0;
}


x86 assembly (clang, -O0):
<create_socketpair>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x40(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fa <create_socketpair+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <create_socketpair+0x229>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1254 <create_socketpair+0xb4>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <create_socketpair+0x229>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    1276 <create_socketpair+0xd6>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <create_socketpair+0x229>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12a8 <create_socketpair+0x108>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <create_socketpair+0x229>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10a0 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    12d4 <create_socketpair+0x134>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <create_socketpair+0x229>
movslq -0x3c(%rbp),%rax
cmp    $0xc,%rax
je     12ee <create_socketpair+0x14e>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <create_socketpair+0x229>
mov    -0x34(%rbp),%esi
mov    -0x38(%rbp),%edx
lea    0xd05(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1326 <create_socketpair+0x186>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <create_socketpair+0x229>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1356 <create_socketpair+0x1b6>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <create_socketpair+0x229>
movslq -0x3c(%rbp),%rax
cmp    $0xc,%rax
je     1370 <create_socketpair+0x1d0>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <create_socketpair+0x229>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10a0 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    139d <create_socketpair+0x1fd>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <create_socketpair+0x229>
mov    -0x34(%rbp),%esi
mov    -0x38(%rbp),%edx
lea    0xc70(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socketpair>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    0x2e30(%rip),%r15
mov    (%r15),%edi
mov    0x2e0e(%rip),%r12
mov    (%r12),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    $0xffffffff,%r13d
test   %eax,%eax
js     1313 <create_socketpair+0x173>
mov    %eax,%ebp
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x20(%rsp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x1c(%rsp)
movl   $0x0,0x18(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1313 <create_socketpair+0x173>
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     1313 <create_socketpair+0x173>
mov    (%r15),%edi
mov    (%r12),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1313 <create_socketpair+0x173>
mov    %eax,%r15d
movl   $0xc,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   10a0 <getsockname@plt>
cmp    $0xffffffff,%eax
je     1313 <create_socketpair+0x173>
cmpl   $0xc,0x4(%rsp)
jne    1313 <create_socketpair+0x173>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%esi
lea    0xd6c(%rip),%rdi
call   1030 <printf@plt>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
je     1313 <create_socketpair+0x173>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1090 <accept@plt>
test   %eax,%eax
js     1313 <create_socketpair+0x173>
cmpl   $0xc,0x4(%rsp)
jne    1313 <create_socketpair+0x173>
mov    %eax,%r12d
mov    %ebp,%edi
call   1060 <close@plt>
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r15d,%edi
call   10a0 <getsockname@plt>
cmp    $0xffffffff,%eax
je     1313 <create_socketpair+0x173>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%esi
lea    0xd16(%rip),%rdi
call   1030 <printf@plt>
mov    %r15d,(%r14)
mov    %r12d,0x4(%r14)
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_LOOPBACK = global i32 0
@.str = [26 x i8] c"listener addr %d port %d\0A\00"
@.str.1 = [27 x i8] c"connector addr %d port %d\0A\00"
define i32 @create_socketpair(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %3
store i32 -1, i32* %4
store i32 -1, i32* %5
store i32 -1, i32* %6
store i32 -1, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 -1, i32* %2
br label %99
17: ; preds = %1
%18 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_LOOPBACK
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 0, i32* %25
%26 = load i32, i32* %4
%27 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%28 = call i32 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %17
store i32 -1, i32* %2
br label %99
31: ; preds = %17
%32 = load i32, i32* %4
%33 = call i32 @listen(i32 %32, i32 1)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 -1, i32* %2
br label %99
36: ; preds = %31
%37 = load i32, i32* @AF_INET
%38 = load i32, i32* @SOCK_STREAM
%39 = call i32 @socket(i32 %37, i32 %38, i32 0)
store i32 %39, i32* %5
%40 = load i32, i32* %5
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 -1, i32* %2
br label %99
43: ; preds = %36
store i32 12, i32* %9
%44 = load i32, i32* %4
%45 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%46 = call i32 @getsockname(i32 %44, %struct.sockaddr* %45, i32* %9)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %49
48: ; preds = %43
store i32 -1, i32* %2
br label %99
49: ; preds = %43
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = icmp ne i64 %51, 12
br i1 %52, label %53, label %54
53: ; preds = %49
store i32 -1, i32* %2
br label %99
54: ; preds = %49
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%56 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %57, i32 %59)
%61 = load i32, i32* %5
%62 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%63 = call i32 @connect(i32 %61, %struct.sockaddr* %62, i32 12)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %66
65: ; preds = %54
store i32 -1, i32* %2
br label %99
66: ; preds = %54
store i32 12, i32* %9
%67 = load i32, i32* %4
%68 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%69 = call i32 @accept(i32 %67, %struct.sockaddr* %68, i32* %9)
store i32 %69, i32* %6
%70 = load i32, i32* %6
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %66
store i32 -1, i32* %2
br label %99
73: ; preds = %66
%74 = load i32, i32* %9
%75 = sext i32 %74 to i64
%76 = icmp ne i64 %75, 12
br i1 %76, label %77, label %78
77: ; preds = %73
store i32 -1, i32* %2
br label %99
78: ; preds = %73
%79 = load i32, i32* %4
%80 = call i32 @close(i32 %79)
%81 = load i32, i32* %5
%82 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%83 = call i32 @getsockname(i32 %81, %struct.sockaddr* %82, i32* %9)
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %86
85: ; preds = %78
store i32 -1, i32* %2
br label %99
86: ; preds = %78
%87 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%88 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %87, i32 0, i32 0
%89 = load i32, i32* %88
%90 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%91 = load i32, i32* %90
%92 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %89, i32 %91)
%93 = load i32, i32* %5
%94 = load i32*, i32** %3
%95 = getelementptr inbounds i32, i32* %94, i64 0
store i32 %93, i32* %95
%96 = load i32, i32* %6
%97 = load i32*, i32** %3
%98 = getelementptr inbounds i32, i32* %97, i64 1
store i32 %96, i32* %98
store i32 0, i32* %2
br label %99
99: ; preds = %86, %85, %77, %72, %65, %53, %48, %42, %35, %30, %16
%100 = load i32, i32* %2
ret i32 %100
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_LOOPBACK = global i32 0
@.str = [26 x i8] c"listener addr %d port %d\0A\00"
@.str.1 = [27 x i8] c"connector addr %d port %d\0A\00"
define i32 @create_socketpair(i32* nocapture writeonly %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %61, label %12
12: ; preds = %1
%13 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_LOOPBACK
%17 = call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 0, i32* %19
%20 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%21 = call i32 @bind(i32 %10, %struct.sockaddr* %20, i32 12)
%22 = icmp eq i32 %21, -1
br i1 %22, label %61, label %23
23: ; preds = %12
%24 = call i32 @listen(i32 %10, i32 1)
%25 = icmp eq i32 %24, -1
br i1 %25, label %61, label %26
26: ; preds = %23
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
%30 = icmp slt i32 %29, 0
br i1 %30, label %61, label %31
31: ; preds = %26
store i32 12, i32* %4
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i32 @getsockname(i32 %10, %struct.sockaddr* %32, i32* %4)
%34 = icmp ne i32 %33, -1
%35 = load i32, i32* %4
%36 = icmp eq i32 %35, 12
%37 = select i1 %34, i1 %36, i1 false
br i1 %37, label %38, label %61
38: ; preds = %31
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %40, i32 %42)
%44 = call i32 @connect(i32 %29, %struct.sockaddr* %32, i32 12)
%45 = icmp eq i32 %44, -1
br i1 %45, label %61, label %46
46: ; preds = %38
store i32 12, i32* %4
%47 = call i32 @accept(i32 %10, %struct.sockaddr* %20, i32* %4)
%48 = icmp sgt i32 %47, -1
%49 = load i32, i32* %4
%50 = icmp eq i32 %49, 12
%51 = select i1 %48, i1 %50, i1 false
br i1 %51, label %52, label %61
52: ; preds = %46
%53 = call i32 @close(i32 %10)
%54 = call i32 @getsockname(i32 %29, %struct.sockaddr* %32, i32* %4)
%55 = icmp eq i32 %54, -1
br i1 %55, label %61, label %56
56: ; preds = %52
%57 = load i32, i32* %39
%58 = load i32, i32* %41
%59 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %57, i32 %58)
store i32 %29, i32* %0
%60 = getelementptr inbounds i32, i32* %0, i64 1
store i32 %47, i32* %60
br label %61
61: ; preds = %52, %46, %38, %31, %26, %23, %12, %1, %56
%62 = phi i32 [ 0, %56 ], [ -1, %1 ], [ -1, %12 ], [ -1, %23 ], [ -1, %26 ], [ -1, %31 ], [ -1, %38 ], [ -1, %46 ], [ -1, %52 ]
ret i32 %62
}