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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, TYPE_1__ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_connect(char *strIp, int iPort) {
  int fd;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    return -1;
  }
  socklen_t addrlen = sizeof(struct sockaddr_in);
  struct sockaddr_in addr = {
      0,
  };
  addr.sin_family = AF_INET;
  if (NULL == strIp) {
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
  } else {
    if (1 != inet_pton(AF_INET, strIp, &addr.sin_addr)) {
      close(fd);
      return -1;
    }
  }
  addr.sin_port = htons(iPort);
  if (connect(fd, (struct sockaddr *)&addr, addrlen) < 0) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11b1 <tcp_connect+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <tcp_connect+0x106>
movl   $0x10,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    11f6 <tcp_connect+0x86>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x2c(%rbp)
jmp    1238 <tcp_connect+0xc8>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
add    $0x4,%rdx
call   1070 <inet_pton@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1233 <tcp_connect+0xc3>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <tcp_connect+0x106>
jmp    1238 <tcp_connect+0xc8>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1270 <tcp_connect+0x100>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <tcp_connect+0x106>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e71(%rip),%r15
mov    (%r15),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11ef <tcp_connect+0x8f>
mov    %eax,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    (%r15),%edi
mov    %edi,0x8(%rsp)
test   %rbp,%rbp
je     11b7 <tcp_connect+0x57>
lea    0x4(%rsp),%rdx
mov    %rbp,%rsi
call   1060 <inet_pton@plt>
cmp    $0x1,%eax
je     11c9 <tcp_connect+0x69>
jmp    11e8 <tcp_connect+0x88>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1070 <connect@plt>
test   %rax,%rax
jns    11f4 <tcp_connect+0x94>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %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
define i32 @tcp_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %50
15: ; preds = %2
store i32 16, i32* %7
%16 = bitcast %struct.sockaddr_in* %8 to i8*
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i8*, i8** %4
%20 = icmp eq i8* null, %19
br i1 %20, label %21, label %26
21: ; preds = %15
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
br label %36
26: ; preds = %15
%27 = load i32, i32* @AF_INET
%28 = load i8*, i8** %4
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%30 = call i32 @inet_pton(i32 %27, i8* %28, %struct.TYPE_2__* %29)
%31 = icmp ne i32 1, %30
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %3
br label %50
35: ; preds = %26
br label %36
36: ; preds = %35, %21
%37 = load i32, i32* %5
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%42 = load i32, i32* %7
%43 = call i64 @connect(i32 %40, %struct.sockaddr* %41, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %36
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %3
br label %50
48: ; preds = %36
%49 = load i32, i32* %6
store i32 %49, i32* %3
br label %50
50: ; preds = %48, %45, %32, %14
%51 = load i32, i32* %3
ret i32 %51
}

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