#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 */
struct sockaddr_in {
  scalar_t__ sin_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ localhost;
typedef int /*<<< orphan*/ listen_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ evutil_closesocket(int);
int getsockname(int, struct sockaddr *, int *);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int pipe(int fds[2]) {
  struct sockaddr_in localhost, listen_addr;
  int cfd, lfd, afd;
  socklen_t len;
  if (inet_pton(AF_INET, "127.0.0.1", &localhost.sin_addr) != 1)
    return -1;
  localhost.sin_family = AF_INET;
  localhost.sin_port = 0;
  lfd = socket(AF_INET, SOCK_STREAM, 0);
  if (lfd == -1) {
    evutil_closesocket(lfd);
    return -1;
  }
  if (bind(lfd, (struct sockaddr *)&localhost, sizeof(localhost)) == -1) {
    evutil_closesocket(lfd);
    return -1;
  }
  if (listen(lfd, 1) == -1) {
    evutil_closesocket(lfd);
    return -1;
  }
  cfd = socket(AF_INET, SOCK_STREAM, 0);
  if (cfd == -1) {
    evutil_closesocket(lfd);
    return -1;
  }
  len = sizeof(listen_addr);
  if (getsockname(lfd, (struct sockaddr *)&listen_addr, &len) == -1) {
    evutil_closesocket(lfd);
    evutil_closesocket(cfd);
    return -1;
  }
  if (connect(cfd, (struct sockaddr *)&listen_addr, sizeof(listen_addr))) {
    evutil_closesocket(lfd);
    evutil_closesocket(cfd);
    return -1;
  }
  afd = accept(lfd, NULL, NULL);
  evutil_closesocket(lfd);
  if (afd == -1) {
    evutil_closesocket(cfd);
    return -1;
  }
  fds[0] = cfd;
  fds[1] = afd;
  return 0;
}


x86 assembly (clang, -O0):
<pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rdx
add    $0xc,%rdx
lea    0xe5c(%rip),%rsi
call   1030 <inet_pton@plt>
cmp    $0x1,%eax
je     11be <pipe+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <pipe+0x1e3>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    120c <pipe+0x8c>
mov    -0x38(%rbp),%edi
call   1050 <evutil_closesocket@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <pipe+0x1e3>
mov    -0x38(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    123a <pipe+0xba>
mov    -0x38(%rbp),%edi
call   1050 <evutil_closesocket@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <pipe+0x1e3>
mov    -0x38(%rbp),%edi
mov    $0x1,%esi
call   1040 <listen@plt>
cmp    $0xffffffff,%eax
jne    1264 <pipe+0xe4>
mov    -0x38(%rbp),%edi
call   1050 <evutil_closesocket@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <pipe+0x1e3>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    129e <pipe+0x11e>
mov    -0x38(%rbp),%edi
call   1050 <evutil_closesocket@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <pipe+0x1e3>
movl   $0x10,-0x40(%rbp)
mov    -0x38(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1080 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    12da <pipe+0x15a>
mov    -0x38(%rbp),%edi
call   1050 <evutil_closesocket@plt>
mov    -0x34(%rbp),%edi
call   1050 <evutil_closesocket@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <pipe+0x1e3>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
je     1311 <pipe+0x191>
mov    -0x38(%rbp),%edi
call   1050 <evutil_closesocket@plt>
mov    -0x34(%rbp),%edi
call   1050 <evutil_closesocket@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <pipe+0x1e3>
mov    -0x38(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1070 <accept@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%edi
call   1050 <evutil_closesocket@plt>
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1349 <pipe+0x1c9>
mov    -0x34(%rbp),%edi
call   1050 <evutil_closesocket@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <pipe+0x1e3>
mov    -0x34(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x3c(%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):
<pipe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    0x2e50(%rip),%rbx
mov    (%rbx),%edi
lea    0x14(%rsp),%rdx
lea    0xe5a(%rip),%rsi
call   1030 <inet_pton@plt>
mov    $0xffffffff,%r13d
cmp    $0x1,%eax
jne    1275 <pipe+0xf5>
mov    (%rbx),%edi
mov    %edi,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2e08(%rip),%r15
mov    (%r15),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1265 <pipe+0xe5>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     1261 <pipe+0xe1>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <listen@plt>
cmp    $0xffffffff,%eax
je     1261 <pipe+0xe1>
mov    (%rbx),%edi
mov    (%r15),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1261 <pipe+0xe1>
mov    %eax,%r15d
movl   $0x10,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1080 <getsockname@plt>
cmp    $0xffffffff,%eax
je     1255 <pipe+0xd5>
lea    0x18(%rsp),%rsi
mov    %r15d,%edi
mov    $0x10,%edx
call   1090 <connect@plt>
test   %rax,%rax
je     1287 <pipe+0x107>
mov    %ebp,%edi
call   1050 <evutil_closesocket@plt>
mov    %r15d,%edi
jmp    1270 <pipe+0xf0>
mov    %ebp,%edi
jmp    1270 <pipe+0xf0>
mov    $0xffffffff,%r13d
mov    $0xffffffff,%edi
call   1050 <evutil_closesocket@plt>
mov    %r13d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <accept@plt>
mov    %eax,%r12d
mov    %ebp,%edi
call   1050 <evutil_closesocket@plt>
cmp    $0xffffffff,%r12d
je     125c <pipe+0xdc>
mov    %r15d,(%r14)
mov    %r12d,0x4(%r14)
xor    %r13d,%r13d
jmp    1275 <pipe+0xf5>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
define i32 @pipe(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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
%10 = call i32 @inet_pton(i32 %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %9)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %60
12: ; preds = %1
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i64 0, i64* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %13, i32 %16, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @evutil_closesocket(i32 -1)
br label %60
21: ; preds = %12
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i32 @bind(i32 %17, %struct.sockaddr* %22, i32 16)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @evutil_closesocket(i32 %17)
br label %60
27: ; preds = %21
%28 = call i32 @listen(i32 %17, i32 1)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @evutil_closesocket(i32 %17)
br label %60
32: ; preds = %27
%33 = load i32, i32* @AF_INET
%34 = load i32, i32* @SOCK_STREAM
%35 = call i32 @socket(i32 %33, i32 %34, i32 0)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @evutil_closesocket(i32 %17)
br label %60
39: ; preds = %32
store i32 16, i32* %4
%40 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%41 = call i32 @getsockname(i32 %17, %struct.sockaddr* %40, i32* %4)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = call i32 @evutil_closesocket(i32 %17)
%45 = call i32 @evutil_closesocket(i32 %35)
br label %60
46: ; preds = %39
%47 = call i64 @connect(i32 %35, %struct.sockaddr* %40, i32 16)
%48 = icmp eq i64 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %46
%50 = call i32 @evutil_closesocket(i32 %17)
%51 = call i32 @evutil_closesocket(i32 %35)
br label %60
52: ; preds = %46
%53 = call i32 @accept(i32 %17, i32* null, i32* null)
%54 = call i32 @evutil_closesocket(i32 %17)
%55 = icmp eq i32 %53, -1
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 @evutil_closesocket(i32 %35)
br label %60
58: ; preds = %52
store i32 %35, i32* %0
%59 = getelementptr inbounds i32, i32* %0, i64 1
store i32 %53, i32* %59
br label %60
60: ; preds = %1, %58, %56, %49, %43, %37, %30, %25, %19
%61 = phi i32 [ -1, %19 ], [ -1, %25 ], [ -1, %30 ], [ -1, %37 ], [ -1, %43 ], [ -1, %49 ], [ -1, %56 ], [ 0, %58 ], [ -1, %1 ]
ret i32 %61
}