#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;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ alarm(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ set_socket_opt(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_to_host_port_by_ip(char *hostip, int portno) {
  int fserver;
  struct sockaddr_in server;
  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(portno);
  server.sin_addr.s_addr = inet_addr(hostip);
  alarm(3);
  if (connect(fserver, (struct sockaddr *)&server, sizeof(server)) < 0) {
    fprintf(stderr, "Connection to Server Failed, the server may be down! \n");
    alarm(0);
    return (-1);
  }
  alarm(0);
  return (fserver);
}


x86 assembly (clang, -O0):
<connect_to_host_port_by_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11c5 <connect_to_host_port_by_ip+0x55>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <connect_to_host_port_by_ip+0xda>
mov    -0x18(%rbp),%edi
call   1070 <set_socket_opt@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    $0x3,%edi
call   1040 <alarm@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    123d <connect_to_host_port_by_ip+0xcd>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1040 <alarm@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <connect_to_host_port_by_ip+0xda>
xor    %edi,%edi
call   1040 <alarm@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_host_port_by_ip>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e62(%rip),%r15
mov    (%r15),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11ed <connect_to_host_port_by_ip+0x7d>
mov    %eax,%ebx
mov    %eax,%edi
call   1070 <set_socket_opt@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r14,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    $0x3,%edi
call   1040 <alarm@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     1204 <connect_to_host_port_by_ip+0x94>
xor    %edi,%edi
call   1040 <alarm@plt>
jmp    1225 <connect_to_host_port_by_ip+0xb5>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1220 <connect_to_host_port_by_ip+0xb0>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1040 <alarm@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 { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"Can not acquire a socket stream ! \0A\00"
@.str.1 = [55 x i8] c"Connection to Server Failed, the server may be down! \0A\00"
define i32 @connect_to_host_port_by_ip(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %39
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i32 @set_socket_opt(i32 %16)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* %5
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i8*, i8** %4
%24 = call i32 @inet_addr(i8* %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = call i32 @alarm(i32 3)
%28 = load i32, i32* %6
%29 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%30 = call i64 @connect(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %15
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @alarm(i32 0)
store i32 -1, i32* %3
br label %39
36: ; preds = %15
%37 = call i32 @alarm(i32 0)
%38 = load i32, i32* %6
store i32 %38, i32* %3
br label %39
39: ; preds = %36, %32, %12
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"Can not acquire a socket stream ! \0A\00"
@.str.1 = [55 x i8] c"Connection to Server Failed, the server may be down! \0A\00"
define i32 @connect_to_host_port_by_ip(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 slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %30
12: ; preds = %2
%13 = tail call i32 @set_socket_opt(i32 %7)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 %1)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = tail call i32 @inet_addr(i8* %0)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %18, i32* %19
%20 = tail call i32 @alarm(i32 3)
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i64 @connect(i32 %7, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %12
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @alarm(i32 0)
br label %30
28: ; preds = %12
%29 = call i32 @alarm(i32 0)
br label %30
30: ; preds = %28, %24, %9
%31 = phi i32 [ -1, %9 ], [ -1, %24 ], [ %7, %28 ]
ret i32 %31
}