#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;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sa_log(int /*<<< orphan*/, char *, char *, unsigned short);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_tcp(char *host, unsigned short port) {
  int sockfd;
  struct hostent *server;
  struct sockaddr_in serv_addr;
  sa_log(0, "Connecting to %s %hd\n", host, port);
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  if ((server = gethostbyname(host)) == NULL) {
    return -1;
  }
  memset(&serv_addr, 0, sizeof(struct sockaddr_in));
  serv_addr.sin_family = AF_INET;
  memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
  serv_addr.sin_port = htons(port);
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_tcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
lea    0xe70(%rip),%rsi
movzwl -0x12(%rbp),%ecx
call   1050 <sa_log@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11ca <connect_tcp+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <connect_tcp+0xec>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ed <connect_tcp+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <connect_tcp+0xec>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x4,%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1256 <connect_tcp+0xe6>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <connect_tcp+0xec>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_tcp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xe77(%rip),%rsi
xor    %edi,%edi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1050 <sa_log@plt>
mov    0x2e4b(%rip),%r12
mov    (%r12),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     121e <connect_tcp+0xae>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     121e <connect_tcp+0xae>
mov    %rax,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
lea    0x4(%rsp),%rdi
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
call   1070 <memcpy@plt>
movzwl %r14w,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%ebp
cmovns %r15d,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"Connecting to %s %hd\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @connect_tcp(i8* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca i32
%7 = alloca %struct.hostent*
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i16 %1, i16* %5
%9 = load i8*, i8** %4
%10 = load i16, i16* %5
%11 = call i32 @sa_log(i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %9, i16 zeroext %10)
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %45
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call %struct.hostent* @gethostbyname(i8* %18)
store %struct.hostent* %19, %struct.hostent** %7
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -1, i32* %3
br label %45
22: ; preds = %17
%23 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
%28 = load %struct.hostent*, %struct.hostent** %7
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load %struct.hostent*, %struct.hostent** %7
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @memcpy(i32* %27, i32 %30, i32 %33)
%35 = load i16, i16* %5
%36 = call i32 @htons(i16 zeroext %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%40 = call i64 @connect(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %22
store i32 -1, i32* %3
br label %45
43: ; preds = %22
%44 = load i32, i32* %6
store i32 %44, i32* %3
br label %45
45: ; preds = %43, %42, %21, %16
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"Connecting to %s %hd\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @connect_tcp(i8* %0, i16 zeroext %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call i32 @sa_log(i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0, i16 zeroext %1)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %29, label %10
10: ; preds = %2
%11 = tail call %struct.hostent* @gethostbyname(i8* %0)
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %29, label %13
13: ; preds = %10
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 1
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @memcpy(i32* %17, i32 %19, i32 %21)
%23 = call i32 @htons(i16 zeroext %1)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @connect(i32 %8, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
%28 = select i1 %27, i32 -1, i32 %8
br label %29
29: ; preds = %13, %10, %2
%30 = phi i32 [ -1, %2 ], [ -1, %10 ], [ %28, %13 ]
ret i32 %30
}