#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 /*<<< orphan*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_pipe(int port) {
  int sock;
  struct sockaddr_in sin;
  sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sock == -1)
    return -1;
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  sin.sin_addr.s_addr = inet_addr("127.0.0.1");
  if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
    return -1;
  return sock;
}


x86 assembly (clang, -O0):
<open_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1184 <open_pipe+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <open_pipe+0x96>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    0xe5e(%rip),%rdi
call   1040 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    11d0 <open_pipe+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <open_pipe+0x96>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_pipe>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    0x2e7f(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11b0 <open_pipe+0x70>
mov    %eax,%ebx
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0xe72(%rip),%rdi
call   1040 <inet_addr@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
jmp    11b5 <open_pipe+0x75>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
define i32 @open_pipe(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = load i32, i32* @IPPROTO_TCP
%9 = call i32 @socket(i32 %6, i32 %7, i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %29
13: ; preds = %1
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* %3
%17 = call i32 @htons(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %17, i32* %18
%19 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = load i32, i32* %4
%23 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%24 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %13
store i32 -1, i32* %2
br label %29
27: ; preds = %13
%28 = load i32, i32* %4
store i32 %28, i32* %2
br label %29
29: ; preds = %27, %26, %12
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
define i32 @open_pipe(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = load i32, i32* @IPPROTO_TCP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %20, label %9
9: ; preds = %1
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 %0)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %14, i32* %15
%16 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%17 = call i32 @connect(i32 %7, %struct.sockaddr* %16, i32 12)
%18 = icmp eq i32 %17, -1
%19 = select i1 %18, i32 -1, i32 %7
br label %20
20: ; preds = %9, %1
%21 = phi i32 [ -1, %1 ], [ %19, %9 ]
ret i32 %21
}