#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;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_LOOPBACK;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int opentcp(unsigned short port) {
  int s = socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0) {
    return -1;
  }
  struct sockaddr_in addr = {0};
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<opentcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1191 <opentcp+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <opentcp+0xa7>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movzwl -0x6(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    11f1 <opentcp+0xa1>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <opentcp+0xa7>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opentcp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e97(%rip),%r14
mov    (%r14),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11b2 <opentcp+0x72>
mov    %eax,%ebx
movl   $0x0,0xc(%rsp)
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
movzwl %bp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1050 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
jmp    11b7 <opentcp+0x77>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, 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
define i32 @opentcp(i16 zeroext %0) {
%2 = alloca i32
%3 = alloca i16
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i16 %0, i16* %3
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %30
12: ; preds = %1
%13 = bitcast %struct.sockaddr_in* %5 to i8*
%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 i16, i16* %3
%17 = call i32 @htons(i16 zeroext %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_LOOPBACK
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = load i32, i32* %4
%24 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%25 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 16)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %12
store i32 -1, i32* %2
br label %30
28: ; preds = %12
%29 = load i32, i32* %4
store i32 %29, i32* %2
br label %30
30: ; preds = %28, %27, %11
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, 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
define i32 @opentcp(i16 zeroext %0) {
%2 = alloca %struct.sockaddr_in
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %21, label %7
7: ; preds = %1
%8 = bitcast %struct.sockaddr_in* %2 to i8*
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
store i32 0, i32* %9
%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(i16 zeroext %0)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_LOOPBACK
%15 = tail call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%18 = call i64 @connect(i32 %5, %struct.sockaddr* %17, i32 16)
%19 = icmp slt i64 %18, 0
%20 = select i1 %19, i32 -1, i32 %5
br label %21
21: ; preds = %1, %7
%22 = phi i32 [ %20, %7 ], [ -1, %1 ]
ret i32 %22
}