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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ GetHostName(char *, int);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int get_ip_address(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ set_socket_opt(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strstr(char *, char *);
int connect_to_hostport(char *target2) {
  char buf[1024];
  int fserver;
  int b0, b1, b2, b3, sizeserver;
  int ipaddr;
  char *portno;
  char *target;
  struct sockaddr_in server;
  sizeserver = sizeof(server);
  target = target2;
  if (NULL == strstr(target, ":")) {
    portno = target;
    if (GetHostName(buf, sizeof buf)) {
      fprintf(stderr, "Can't find hostname\n");
      return (-1);
    }
    ipaddr = get_ip_address(buf);
  } else {
    portno = strstr(target, ":");
    *portno = '\0';
    portno++;
    ipaddr = get_ip_address(target);
  }
  b0 = ipaddr >> 24;
  b1 = ipaddr >> 16;
  b2 = ipaddr >> 8;
  b3 = ipaddr;
  b0 &= 255;
  b1 &= 255;
  b2 &= 255;
  b3 &= 255;
  snprintf(buf, sizeof(buf), "%d.%d.%d.%d", b0, b1, b2, b3);
  if ((fserver = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "Can not acquire a socket stream ! \n");
    return (-1);
  }
  set_socket_opt(fserver);
  server.sin_family = AF_INET;
  server.sin_port = htons(atoi(portno));
  server.sin_addr.s_addr = inet_addr(buf);
  if (connect(fserver, (struct sockaddr *)&server, sizeserver) < 0) {
    fprintf(stderr, "Connection to Server Failed, the server may be down! \n");
    return (-1);
  }
  return (fserver);
}


x86 assembly (clang, -O0):
<connect_to_hostport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xc,-0x428(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x440(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   10c0 <strstr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1256 <connect_to_hostport+0xa6>
mov    -0x440(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1070 <GetHostName@plt>
cmp    $0x0,%rax
je     123f <connect_to_hostport+0x8f>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    142a <connect_to_hostport+0x27a>
lea    -0x410(%rbp),%rdi
call   10a0 <get_ip_address@plt>
mov    %eax,-0x42c(%rbp)
jmp    129e <connect_to_hostport+0xee>
mov    -0x440(%rbp),%rdi
lea    0xd9c(%rip),%rsi
call   10c0 <strstr@plt>
mov    %rax,-0x438(%rbp)
mov    -0x438(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x438(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x438(%rbp)
mov    -0x440(%rbp),%rdi
call   10a0 <get_ip_address@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
sar    $0x18,%eax
mov    %eax,-0x418(%rbp)
mov    -0x42c(%rbp),%eax
sar    $0x10,%eax
mov    %eax,-0x41c(%rbp)
mov    -0x42c(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x420(%rbp)
mov    -0x42c(%rbp),%eax
mov    %eax,-0x424(%rbp)
mov    -0x418(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x418(%rbp)
mov    -0x41c(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x41c(%rbp)
mov    -0x420(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x420(%rbp)
mov    -0x424(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x424(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%ecx
mov    -0x41c(%rbp),%r8d
mov    -0x420(%rbp),%r9d
mov    -0x424(%rbp),%eax
mov    $0x400,%esi
lea    0xccf(%rip),%rdx
mov    %eax,(%rsp)
call   1040 <snprintf@plt>
mov    0x2c91(%rip),%rax
mov    (%rax),%edi
mov    0x2c70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jge    1399 <connect_to_hostport+0x1e9>
mov    0x2c59(%rip),%rax
mov    (%rax),%edi
lea    0xc9b(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    142a <connect_to_hostport+0x27a>
mov    -0x414(%rbp),%edi
call   1080 <set_socket_opt@plt>
mov    0x2c3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x448(%rbp)
mov    -0x438(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x44c(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x450(%rbp)
mov    -0x414(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    -0x428(%rbp),%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    1421 <connect_to_hostport+0x271>
mov    0x2bd1(%rip),%rax
mov    (%rax),%edi
lea    0xc37(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    142a <connect_to_hostport+0x27a>
mov    -0x414(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_hostport>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    %rdi,%r14
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1282 <connect_to_hostport+0xd2>
mov    %rax,%rbx
movb   $0x0,(%rax)
add    $0x1,%rbx
mov    %r14,%rdi
call   10b0 <get_ip_address@plt>
mov    %rbx,%r14
mov    %eax,%ecx
shr    $0x18,%ecx
mov    %eax,%edx
shr    $0x10,%edx
movzbl %dl,%r8d
movzbl %ah,%ebx
movzbl %al,%eax
mov    %eax,(%rsp)
lea    0xe11(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
mov    %ebx,%r9d
call   1050 <snprintf@plt>
mov    0x2dcb(%rip),%rbx
mov    (%rbx),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     129f <connect_to_hostport+0xef>
mov    %eax,%ebp
mov    %eax,%edi
call   1090 <set_socket_opt@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    %r14,%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0x20(%rsp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
jns    12b9 <connect_to_hostport+0x109>
lea    0xdc5(%rip),%rsi
jmp    12a6 <connect_to_hostport+0xf6>
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
call   1080 <GetHostName@plt>
test   %rax,%rax
je     12c7 <connect_to_hostport+0x117>
lea    0xd63(%rip),%rsi
jmp    12a6 <connect_to_hostport+0xf6>
lea    0xd7b(%rip),%rsi
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0x20(%rsp),%rdi
call   10b0 <get_ip_address@plt>
jmp    11e6 <connect_to_hostport+0x36>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [2 x i8] c":\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Can't find hostname\0A\00"
@.str.2 = [12 x i8] c"%d.%d.%d.%d\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [36 x i8] c"Can not acquire a socket stream ! \0A\00"
@.str.4 = [55 x i8] c"Connection to Server Failed, the server may be down! \0A\00"
define i32 @connect_to_hostport(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca %struct.sockaddr_in
store i8* %0, i8** %3
store i32 12, i32* %10
%15 = load i8*, i8** %3
store i8* %15, i8** %13
%16 = load i8*, i8** %13
%17 = call i8* @strstr(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i8* null, %17
br i1 %18, label %19, label %30
19: ; preds = %1
%20 = load i8*, i8** %13
store i8* %20, i8** %12
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%22 = call i64 @GetHostName(i8* %21, i32 1024)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %90
27: ; preds = %19
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%29 = call i32 @get_ip_address(i8* %28)
store i32 %29, i32* %11
br label %38
30: ; preds = %1
%31 = load i8*, i8** %13
%32 = call i8* @strstr(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %32, i8** %12
%33 = load i8*, i8** %12
store i8 0, i8* %33
%34 = load i8*, i8** %12
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %12
%36 = load i8*, i8** %13
%37 = call i32 @get_ip_address(i8* %36)
store i32 %37, i32* %11
br label %38
38: ; preds = %30, %27
%39 = load i32, i32* %11
%40 = ashr i32 %39, 24
store i32 %40, i32* %6
%41 = load i32, i32* %11
%42 = ashr i32 %41, 16
store i32 %42, i32* %7
%43 = load i32, i32* %11
%44 = ashr i32 %43, 8
store i32 %44, i32* %8
%45 = load i32, i32* %11
store i32 %45, i32* %9
%46 = load i32, i32* %6
%47 = and i32 %46, 255
store i32 %47, i32* %6
%48 = load i32, i32* %7
%49 = and i32 %48, 255
store i32 %49, i32* %7
%50 = load i32, i32* %8
%51 = and i32 %50, 255
store i32 %51, i32* %8
%52 = load i32, i32* %9
%53 = and i32 %52, 255
store i32 %53, i32* %9
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%55 = load i32, i32* %6
%56 = load i32, i32* %7
%57 = load i32, i32* %8
%58 = load i32, i32* %9
%59 = call i32 @snprintf(i8* %54, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %55, i32 %56, i32 %57, i32 %58)
%60 = load i32, i32* @AF_INET
%61 = load i32, i32* @SOCK_STREAM
%62 = call i32 @socket(i32 %60, i32 %61, i32 0)
store i32 %62, i32* %5
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %38
%65 = load i32, i32* @stderr
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %90
67: ; preds = %38
%68 = load i32, i32* %5
%69 = call i32 @set_socket_opt(i32 %68)
%70 = load i32, i32* @AF_INET
%71 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 2
store i32 %70, i32* %71
%72 = load i8*, i8** %12
%73 = call i32 @atoi(i8* %72)
%74 = call i32 @htons(i32 %73)
%75 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
store i32 %74, i32* %75
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%77 = call i32 @inet_addr(i8* %76)
%78 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
%79 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %78, i32 0, i32 0
store i32 %77, i32* %79
%80 = load i32, i32* %5
%81 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%82 = load i32, i32* %10
%83 = call i64 @connect(i32 %80, %struct.sockaddr* %81, i32 %82)
%84 = icmp slt i64 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %67
%86 = load i32, i32* @stderr
%87 = call i32 @fprintf(i32 %86, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %90
88: ; preds = %67
%89 = load i32, i32* %5
store i32 %89, i32* %2
br label %90
90: ; preds = %88, %85, %64, %24
%91 = load i32, i32* %2
ret i32 %91
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str.1 = [21 x i8] c"Can't find hostname\0A\00"
@.str.2 = [12 x i8] c"%d.%d.%d.%d\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [36 x i8] c"Can not acquire a socket stream ! \0A\00"
@.str.4 = [55 x i8] c"Connection to Server Failed, the server may be down! \0A\00"
define i32 @connect_to_hostport(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.sockaddr_in
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = tail call i8* @strchr(i8* dereferenceable(1) %0, i32 58)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = call i64 @GetHostName(i8* %4, i32 1024)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %42
11: ; preds = %8
%12 = call i32 @get_ip_address(i8* %4)
br label %16
13: ; preds = %1
store i8 0, i8* %6
%14 = getelementptr inbounds i8, i8* %6, i64 1
%15 = tail call i32 @get_ip_address(i8* %0)
br label %16
16: ; preds = %13, %11
%17 = phi i32 [ %12, %11 ], [ %15, %13 ]
%18 = phi i8* [ %0, %11 ], [ %14, %13 ]
%19 = lshr i32 %17, 24
%20 = lshr i32 %17, 16
%21 = lshr i32 %17, 8
%22 = and i32 %20, 255
%23 = and i32 %21, 255
%24 = and i32 %17, 255
%25 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %22, i32 %23, i32 %24)
%26 = load i32, i32* @AF_INET
%27 = load i32, i32* @SOCK_STREAM
%28 = call i32 @socket(i32 %26, i32 %27, i32 0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %42, label %30
30: ; preds = %16
%31 = call i32 @set_socket_opt(i32 %28)
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %32, i32* %33
%34 = call i32 @atoi(i8* %18)
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %35, i32* %36
%37 = call i32 @inet_addr(i8* %4)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %37, i32* %38
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i64 @connect(i32 %28, %struct.sockaddr* %39, i32 12)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %30, %16, %8
%43 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), %8 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), %16 ], [ getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), %30 ]
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* %43)
br label %46
46: ; preds = %42, %30
%47 = phi i32 [ %28, %30 ], [ -1, %42 ]
ret i32 %47
}