#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__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  scalar_t__ s_addr;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ INADDR_NONE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ htons(int const);
scalar_t__ inet_addr(char const *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_connect_ipv4(const char *hostname, const int port) {
  struct sockaddr_in serv_addr;
  struct hostent *host = NULL;
  int fd, ret;
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket() failed");
    return -1;
  }
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr(hostname);
  serv_addr.sin_port = htons(port);
  if (serv_addr.sin_addr.s_addr == INADDR_NONE) {
    if ((host = gethostbyname(hostname)) == NULL) {
      herror("gethostbyname() failed");
      return -1;
    }
    serv_addr.sin_addr.s_addr =
        ((struct in_addr *)host->h_addr_list[0])->s_addr;
  }
  ret = connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (ret == -1) {
    perror("connect() failed");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<tcp_connect_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    11e4 <tcp_connect_ipv4+0x54>
lea    0xe2d(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <tcp_connect_ipv4+0x120>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%rax
jne    126c <tcp_connect_ipv4+0xdc>
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    125b <tcp_connect_ipv4+0xcb>
lea    0xdc6(%rip),%rdi
call   1080 <herror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <tcp_connect_ipv4+0x120>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   10a0 <connect@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    12aa <tcp_connect_ipv4+0x11a>
lea    0xd96(%rip),%rdi
call   1090 <perror@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <tcp_connect_ipv4+0x120>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect_ipv4>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e42(%rip),%r15
mov    (%r15),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1259 <tcp_connect_ipv4+0xc9>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x14(%rsp)
mov    %r14,%rdi
call   1060 <inet_addr@plt>
mov    %rax,0x8(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <tcp_connect_ipv4+0x90>
mov    %r14,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     124b <tcp_connect_ipv4+0xbb>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    126a <tcp_connect_ipv4+0xda>
lea    0xdea(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1265 <tcp_connect_ipv4+0xd5>
lea    0xdbe(%rip),%rdi
call   1080 <herror@plt>
jmp    1265 <tcp_connect_ipv4+0xd5>
lea    0xda0(%rip),%rdi
call   1090 <perror@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 { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64* }
%struct.in_addr = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@INADDR_NONE = global i64 0
@.str.1 = [23 x i8] c"gethostbyname() failed\00"
@.str.2 = [17 x i8] c"connect() failed\00"
define i32 @tcp_connect_ipv4(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store %struct.hostent* null, %struct.hostent** %7
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %61
16: ; preds = %2
%17 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 16)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i8*, i8** %4
%21 = call i64 @inet_addr(i8* %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i64 %21, i64* %23
%24 = load i32, i32* %5
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* @INADDR_NONE
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %49
32: ; preds = %16
%33 = load i8*, i8** %4
%34 = call %struct.hostent* @gethostbyname(i8* %33)
store %struct.hostent* %34, %struct.hostent** %7
%35 = icmp eq %struct.hostent* %34, null
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @herror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %61
38: ; preds = %32
%39 = load %struct.hostent*, %struct.hostent** %7
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %39, i32 0, i32 0
%41 = load i64*, i64** %40
%42 = getelementptr inbounds i64, i64* %41, i64 0
%43 = load i64, i64* %42
%44 = inttoptr i64 %43 to %struct.in_addr*
%45 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %44, i32 0, i32 0
%46 = load i64, i64* %45
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
store i64 %46, i64* %48
br label %49
49: ; preds = %38, %16
%50 = load i32, i32* %8
%51 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%52 = call i32 @connect(i32 %50, %struct.sockaddr* %51, i32 16)
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %59
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%57 = load i32, i32* %8
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %3
br label %61
59: ; preds = %49
%60 = load i32, i32* %8
store i32 %60, i32* %3
br label %61
61: ; preds = %59, %55, %36, %14
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64* }
%struct.in_addr = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@INADDR_NONE = global i64 0
@.str.1 = [23 x i8] c"gethostbyname() failed\00"
@.str.2 = [17 x i8] c"connect() failed\00"
define i32 @tcp_connect_ipv4(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %41
11: ; preds = %2
%12 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 16)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i64 @inet_addr(i8* %0)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i64 %15, i64* %16
%17 = call i32 @htons(i32 %1)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i64, i64* %16
%20 = load i64, i64* @INADDR_NONE
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %34
22: ; preds = %11
%23 = call %struct.hostent* @gethostbyname(i8* %0)
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @herror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %41
27: ; preds = %22
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i64 0, i32 0
%29 = load i64*, i64** %28
%30 = load i64, i64* %29
%31 = inttoptr i64 %30 to %struct.in_addr*
%32 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %31, i64 0, i32 0
%33 = load i64, i64* %32
store i64 %33, i64* %16
br label %34
34: ; preds = %27, %11
%35 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%36 = call i32 @connect(i32 %7, %struct.sockaddr* %35, i32 16)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @close(i32 %7)
br label %41
41: ; preds = %34, %38, %25, %9
%42 = phi i32 [ -1, %9 ], [ -1, %25 ], [ -1, %38 ], [ %7, %34 ]
ret i32 %42
}