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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_connect(const char *host, int port) {
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1) {
    return -1;
  }
  struct sockaddr_in my_addr;
  my_addr.sin_addr.s_addr = inet_addr(host);
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(port);
  if (connect(fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr_in)) ==
      -1) {
    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    0x2e92(%rip),%rax
mov    (%rax),%edi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1181 <tcp_connect+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <tcp_connect+0x90>
mov    -0x10(%rbp),%rdi
call   1040 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    11ca <tcp_connect+0x8a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <tcp_connect+0x90>
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    0x2e91(%rip),%r15
mov    (%r15),%edi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11a9 <tcp_connect+0x69>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1030 <htons@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    11ae <tcp_connect+0x6e>
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, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = 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 %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %31
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @inet_addr(i8* %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* %5
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %6
%25 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%26 = call i32 @connect(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %14
store i32 -1, i32* %3
br label %31
29: ; preds = %14
%30 = load i32, i32* %6
store i32 %30, i32* %3
br label %31
31: ; preds = %29, %28, %13
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = 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 eq i32 %6, -1
br i1 %7, label %20, label %8
8: ; preds = %2
%9 = bitcast %struct.sockaddr_in* %3 to i8*
%10 = tail call i32 @inet_addr(i8* %0)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %14, i32* %15
%16 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%17 = call i32 @connect(i32 %6, %struct.sockaddr* %16, i32 12)
%18 = icmp eq i32 %17, -1
%19 = select i1 %18, i32 -1, i32 %6
br label %20
20: ; preds = %2, %8
%21 = phi i32 [ %19, %8 ], [ -1, %2 ]
ret i32 %21
}