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

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int TCP_Connect(char *host, unsigned long int port) {
  struct sockaddr_in server;
  struct hostent *hptr;
  int sock;
  int ret;
  hptr = gethostbyname(host);
  if (!hptr) {
    fprintf(stderr, "Invalid Host Name\n");
    return -1;
  }
  sock = socket(AF_INET, SOCK_STREAM, 0);
  memcpy((char *)&server.sin_addr, hptr->h_addr_list[0], hptr->h_length);
  server.sin_family = AF_INET;
  server.sin_port = htons((short)port);
  ret = connect(sock, (struct sockaddr *)&server, sizeof(server));
  if (ret < 0) {
    fprintf(stderr, "Unable to connect %d\n", ret);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<TCP_Connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ab <TCP_Connect+0x4b>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <TCP_Connect+0xe9>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x34(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movswl %ax,%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1243 <TCP_Connect+0xe3>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%edx
lea    0xde3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <TCP_Connect+0xe9>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TCP_Connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11ec <TCP_Connect+0x8c>
mov    %rax,%rbx
mov    0x2e67(%rip),%r15
mov    (%r15),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rdi
mov    0x8(%rbx),%rax
mov    (%rax),%esi
mov    (%rbx),%edx
call   1060 <memcpy@plt>
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
movswl %r14w,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %eax,%eax
jns    1208 <TCP_Connect+0xa8>
mov    0x2e00(%rip),%rcx
mov    (%rcx),%edi
lea    0xe32(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1203 <TCP_Connect+0xa3>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"Invalid Host Name\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"Unable to connect %d\0A\00"
define i32 @TCP_Connect(i8* %0, i64 %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 %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %33
10: ; preds = %2
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%15 = bitcast i32* %14 to i8*
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 1
%17 = load i32*, i32** %16
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @memcpy(i8* %15, i32 %18, i32 %20)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %22, i32* %23
%24 = trunc i64 %1 to i16
%25 = call i32 @htons(i16 signext %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i32 @connect(i32 %13, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %10
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %28)
br label %33
33: ; preds = %10, %30, %7
%34 = phi i32 [ -1, %30 ], [ -1, %7 ], [ %13, %10 ]
ret i32 %34
}