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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, int /*<<< orphan*/ *,
                         int /*<<< orphan*/);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *, ...);
int /*<<< orphan*/ warnx(char *, char *);
int tcp_connect(char *host, int port) {
  int sock;
  struct hostent *hent;
  struct sockaddr_in addr;
  if ((hent = gethostbyname(host)) == NULL) {
    warnx("%s: hostname lookup failed", host);
    return -1;
  }
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    warn("socket() call failed");
    return -1;
  }
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  bcopy(hent->h_addr, &addr.sin_addr.s_addr, hent->h_length);
  addr.sin_port = htons(port);
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    close(sock);
    warn("failed to connect to %s:%i", host, port);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11d2 <tcp_connect+0x42>
mov    -0x10(%rbp),%rsi
lea    0xe3f(%rip),%rdi
call   1050 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <tcp_connect+0x109>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1211 <tcp_connect+0x81>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <tcp_connect+0x109>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x30(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1080 <bcopy@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1293 <tcp_connect+0x103>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <tcp_connect+0x109>
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   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     123d <tcp_connect+0xad>
mov    %rax,%rbx
mov    0x2e2c(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     124e <tcp_connect+0xbe>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    (%rbx),%edx
mov    0x4(%rbx),%edi
lea    0xc(%rsp),%rsi
call   1080 <bcopy@plt>
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1261 <tcp_connect+0xd1>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xe02(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <warn@plt>
jmp    125c <tcp_connect+0xcc>
lea    0xdbc(%rip),%rdi
mov    %r15,%rsi
call   1050 <warnx@plt>
jmp    125c <tcp_connect+0xcc>
lea    0xdc6(%rip),%rdi
xor    %eax,%eax
call   1030 <warn@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
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 = [27 x i8] c"%s: hostname lookup failed\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"socket() call failed\00"
@.str.2 = [27 x i8] c"failed to connect to %s:%i\00"
define i32 @tcp_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.hostent*
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %7
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @warnx(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %3
br label %50
15: ; preds = %2
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
22: ; preds = %15
%23 = call i32 @bzero(%struct.sockaddr_in* %8, 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 = load %struct.hostent*, %struct.hostent** %7
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
%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 @bcopy(i32 %28, i32* %30, i32 %33)
%35 = load i32, i32* %5
%36 = call i32 @htons(i32 %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 i32 @connect(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %22
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
%45 = load i8*, i8** %4
%46 = load i32, i32* %5
%47 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %45, i32 %46)
store i32 -1, i32* %3
br label %50
48: ; preds = %22
%49 = load i32, i32* %6
store i32 %49, i32* %3
br label %50
50: ; preds = %48, %42, %20, %12
%51 = load i32, i32* %3
ret i32 %51
}

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 = [27 x i8] c"%s: hostname lookup failed\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"socket() call failed\00"
@.str.2 = [27 x i8] c"failed to connect to %s:%i\00"
define i32 @tcp_connect(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @warnx(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %34
9: ; preds = %2
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %34
16: ; preds = %9
%17 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 1
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @bcopy(i32 %21, i32* %22, i32 %24)
%26 = call i32 @htons(i32 %1)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @connect(i32 %12, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %16
%32 = call i32 @close(i32 %12)
%33 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %1)
br label %34
34: ; preds = %16, %31, %14, %7
%35 = phi i32 [ -1, %7 ], [ -1, %14 ], [ -1, %31 ], [ %12, %16 ]
ret i32 %35
}