#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 /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ on;
typedef int /*<<< orphan*/ local_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_TCP;
int /*<<< orphan*/ TCP_NODELAY;
int connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int std_accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ std_bind(int, struct sockaddr *, int);
int /*<<< orphan*/ std_close(int);
int /*<<< orphan*/ std_getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ std_inet_aton(char *, TYPE_1__ *);
int /*<<< orphan*/ std_listen(int, int);
int /*<<< orphan*/ std_setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                                  int const *, int);
int std_socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void std_tcp_socketpair(int fds[2]) {
  const int on = 1;
  struct sockaddr_in local_addr;
  socklen_t addrlen = sizeof(local_addr);
  int lfd = std_socket(AF_INET, SOCK_STREAM, 0);
  fds[0] = std_socket(AF_INET, SOCK_STREAM, 0);
  memset(&local_addr, 0, sizeof(local_addr));
  local_addr.sin_family = AF_INET;
  local_addr.sin_addr.s_addr = INADDR_ANY;
  local_addr.sin_port = htons(0);
  std_setsockopt(lfd, SOL_TCP, TCP_NODELAY, &on, sizeof(on));
  std_bind(lfd, (struct sockaddr *)&local_addr, addrlen);
  std_listen(lfd, 1);
  memset(&local_addr, 0, sizeof(local_addr));
  std_getsockname(lfd, (struct sockaddr *)&local_addr, &addrlen);
  std_inet_aton("127.0.0.1", &local_addr.sin_addr);
  if (connect(fds[0], (const struct sockaddr *)&local_addr,
              sizeof(local_addr)) == -1) {
    perror("tcpsocketpair");
    exit(1);
  }
  fds[1] = std_accept(lfd, (struct sockaddr *)&local_addr, &addrlen);
  std_close(lfd);
}


x86 assembly (clang, -O0):
<std_tcp_socketpair>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
movl   $0xc,-0x1c(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <std_socket@plt>
mov    %eax,-0x20(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <std_socket@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
xor    %edi,%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
mov    0x2d7c(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1080 <std_setsockopt@plt>
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10b0 <std_bind@plt>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1070 <std_listen@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <std_getsockname@plt>
lea    -0x18(%rbp),%rsi
lea    0xd3c(%rip),%rdi
call   10c0 <std_inet_aton@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12fc <std_tcp_socketpair+0x12c>
lea    0xd1d(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1090 <std_accept@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%edi
call   10a0 <std_close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<std_tcp_socketpair>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
movl   $0x1,0x4(%rsp)
movl   $0xc,(%rsp)
mov    0x2ded(%rip),%r15
mov    (%r15),%edi
mov    0x2dc3(%rip),%rbx
mov    (%rbx),%esi
xor    %edx,%edx
call   1030 <std_socket@plt>
mov    %eax,%ebp
mov    (%r15),%edi
mov    (%rbx),%esi
xor    %edx,%edx
call   1030 <std_socket@plt>
mov    %eax,(%r14)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
xor    %edi,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1080 <std_setsockopt@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <std_bind@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <std_listen@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %rsp,%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <std_getsockname@plt>
lea    0xd54(%rip),%rdi
mov    %rbx,%rsi
call   10c0 <std_inet_aton@plt>
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
cmp    $0xffffffff,%eax
je     12ee <std_tcp_socketpair+0x11e>
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1090 <std_accept@plt>
mov    %eax,0x4(%r14)
mov    %ebp,%edi
call   10a0 <std_close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd15(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@SOL_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [14 x i8] c"tcpsocketpair\00"
define void @std_tcp_socketpair(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %2
store i32 1, i32* %3
store i32 12, i32* %5
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @std_socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %6
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @std_socket(i32 %10, i32 %11, i32 0)
%13 = load i32*, i32** %2
%14 = getelementptr inbounds i32, i32* %13, i64 0
store i32 %12, i32* %14
%15 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = call i32 @htons(i32 0)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* %6
%24 = load i32, i32* @SOL_TCP
%25 = load i32, i32* @TCP_NODELAY
%26 = call i32 @std_setsockopt(i32 %23, i32 %24, i32 %25, i32* %3, i32 4)
%27 = load i32, i32* %6
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%29 = load i32, i32* %5
%30 = call i32 @std_bind(i32 %27, %struct.sockaddr* %28, i32 %29)
%31 = load i32, i32* %6
%32 = call i32 @std_listen(i32 %31, i32 1)
%33 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%34 = load i32, i32* %6
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%36 = call i32 @std_getsockname(i32 %34, %struct.sockaddr* %35, i32* %5)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%38 = call i32 @std_inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.TYPE_2__* %37)
%39 = load i32*, i32** %2
%40 = getelementptr inbounds i32, i32* %39, i64 0
%41 = load i32, i32* %40
%42 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%43 = call i32 @connect(i32 %41, %struct.sockaddr* %42, i32 12)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %1
%46 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %1
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%51 = call i32 @std_accept(i32 %49, %struct.sockaddr* %50, i32* %5)
%52 = load i32*, i32** %2
%53 = getelementptr inbounds i32, i32* %52, i64 1
store i32 %51, i32* %53
%54 = load i32, i32* %6
%55 = call i32 @std_close(i32 %54)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@SOL_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [14 x i8] c"tcpsocketpair\00"
define void @std_tcp_socketpair(i32* nocapture %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 12, i32* %4
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @std_socket(i32 %8, i32 %9, i32 0)
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @std_socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %0
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %17, i32* %19
%20 = call i32 @htons(i32 0)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @SOL_TCP
%23 = load i32, i32* @TCP_NODELAY
%24 = call i32 @std_setsockopt(i32 %10, i32 %22, i32 %23, i32* %2, i32 4)
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @std_bind(i32 %10, %struct.sockaddr* %25, i32 12)
%27 = call i32 @std_listen(i32 %10, i32 1)
%28 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%29 = call i32 @std_getsockname(i32 %10, %struct.sockaddr* %25, i32* %4)
%30 = call i32 @std_inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.TYPE_2__* %18)
%31 = load i32, i32* %0
%32 = call i32 @connect(i32 %31, %struct.sockaddr* %25, i32 12)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %1
%35 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %1
%38 = call i32 @std_accept(i32 %10, %struct.sockaddr* %25, i32* %4)
%39 = getelementptr inbounds i32, i32* %0, i64 1
store i32 %38, i32* %39
%40 = call i32 @std_close(i32 %10)
ret void
}