#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 */
typedef int /*<<< orphan*/ xdrproc_t;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  scalar_t__ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef enum clnt_stat { ____Placeholder_clnt_stat } clnt_stat;
typedef int /*<<< orphan*/ addr;
typedef int /*<<< orphan*/ CLIENT;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int RPC_ANYSOCK;
int RPC_SUCCESS;
int clnt_call(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
              int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
              struct timeval);
int /*<<< orphan*/ clnt_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ clnt_pcreateerror(char *);
int /*<<< orphan*/ *clnttcp_create(struct sockaddr_in *, int, int, int *,
                                   int /*<<< orphan*/, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ xdr_void;
int ping_tcp(char *hostname, int prog_num, int version, int timeout) {
  struct sockaddr_in addr;
  struct hostent *hent;
  CLIENT *clnt;
  struct timeval val;
  enum clnt_stat ans;
  int s = RPC_ANYSOCK, ret;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = 0;
  hent = gethostbyname(hostname);
  if (hent != NULL) {
    memcpy(&addr.sin_addr.s_addr, hent->h_addr_list[0], hent->h_length);
  } else {
    herror("Could not lookup hostname:");
    return -1;
  }
  val.tv_sec = timeout;
  val.tv_usec = 0;
  clnt = clnttcp_create(&addr, prog_num, version, &s, 0, 0);
  if (clnt == NULL) {
    clnt_pcreateerror("Could not create tcp client handle");
    return -1;
  }
  ans = clnt_call(clnt, 0, (xdrproc_t)xdr_void, 0, (xdrproc_t)xdr_void, 0, val);
  if (ans != RPC_SUCCESS) {
    ret = -1;
    printf("No answer from the specified program's TCP service\n");
  } else {
    ret = 0;
    printf("Specified program's TCP service is alive\n");
  }
  clnt_destroy(clnt);
  return ret;
}


x86 assembly (clang, -O0):
<ping_tcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     120e <ping_tcp+0x7e>
lea    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
jmp    1226 <ping_tcp+0x96>
lea    0xdeb(%rip),%rdi
call   1080 <herror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <ping_tcp+0x172>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    -0x38(%rbp),%rdi
lea    -0x60(%rbp),%rcx
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1070 <clnttcp_create@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1274 <ping_tcp+0xe4>
lea    0xdb8(%rip),%rdi
call   10a0 <clnt_pcreateerror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <ping_tcp+0x172>
mov    -0x48(%rbp),%rdi
mov    0x2d59(%rip),%rax
mov    (%rax),%rax
mov    %eax,%edx
mov    0x2d4d(%rip),%rax
mov    (%rax),%rax
mov    %eax,%r8d
xor    %r9d,%r9d
lea    -0x58(%rbp),%rax
mov    %r9d,%esi
mov    %r9d,%ecx
mov    (%rax),%r10
mov    %r10,(%rsp)
mov    0x8(%rax),%rax
mov    %rax,0x8(%rsp)
call   1090 <clnt_call@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
mov    0x2d10(%rip),%rcx
cmp    (%rcx),%eax
je     12e0 <ping_tcp+0x150>
movl   $0xffffffff,-0x64(%rbp)
lea    0xd68(%rip),%rdi
call   1030 <printf@plt>
jmp    12f3 <ping_tcp+0x163>
movl   $0x0,-0x64(%rbp)
lea    0xd84(%rip),%rdi
call   1030 <printf@plt>
mov    -0x48(%rbp),%rdi
call   10b0 <clnt_destroy@plt>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ping_tcp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbp
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x30(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x40(%rsp)
movq   $0x0,0x38(%rsp)
mov    %rbp,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1283 <ping_tcp+0xf3>
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
lea    0x30(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <memcpy@plt>
mov    %ebx,0x20(%rsp)
movq   $0x0,0x28(%rsp)
lea    0x1c(%rsp),%rcx
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1070 <clnttcp_create@plt>
test   %rax,%rax
je     1291 <ping_tcp+0x101>
mov    %rax,%rbx
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
movups 0x20(%rsp),%xmm0
movups %xmm0,(%rsp)
mov    %rbx,%rdi
xor    %esi,%esi
xor    %ecx,%ecx
mov    %edx,%r8d
xor    %r9d,%r9d
call   1090 <clnt_call@plt>
mov    0x2d78(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%eax
setne  %bpl
lea    0xdd7(%rip),%rax
lea    0xdf9(%rip),%rdi
cmove  %rax,%rdi
neg    %ebp
call   1030 <puts@plt>
mov    %rbx,%rdi
call   10b0 <clnt_destroy@plt>
jmp    12a2 <ping_tcp+0x112>
lea    0xd76(%rip),%rdi
call   1080 <herror@plt>
jmp    129d <ping_tcp+0x10d>
lea    0xd83(%rip),%rdi
call   10a0 <clnt_pcreateerror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32* }
%struct.timeval = type { i32, i64 }
@RPC_ANYSOCK = global i32 0
@AF_INET = global i32 0
@.str = [27 x i8] c"Could not lookup hostname:\00"
@.str.1 = [35 x i8] c"Could not create tcp client handle\00"
@xdr_void = global i64 0
@RPC_SUCCESS = global i32 0
@.str.2 = [52 x i8] c"No answer from the specified program's TCP service\0A\00"
@.str.3 = [42 x i8] c"Specified program's TCP service is alive\0A\00"
define i32 @ping_tcp(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.hostent*
%12 = alloca i32*
%13 = alloca %struct.timeval
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%17 = load i32, i32* @RPC_ANYSOCK
store i32 %17, i32* %15
%18 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 24)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i64 0, i64* %21
%22 = load i8*, i8** %6
%23 = call %struct.hostent* @gethostbyname(i8* %22)
store %struct.hostent* %23, %struct.hostent** %11
%24 = load %struct.hostent*, %struct.hostent** %11
%25 = icmp ne %struct.hostent* %24, null
br i1 %25, label %26, label %38
26: ; preds = %4
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
%29 = load %struct.hostent*, %struct.hostent** %11
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 1
%31 = load i32*, i32** %30
%32 = getelementptr inbounds i32, i32* %31, i64 0
%33 = load i32, i32* %32
%34 = load %struct.hostent*, %struct.hostent** %11
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @memcpy(i32* %28, i32 %33, i32 %36)
br label %40
38: ; preds = %4
%39 = call i32 @herror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %69
40: ; preds = %26
%41 = load i32, i32* %9
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i64 0, i64* %43
%44 = load i32, i32* %7
%45 = load i32, i32* %8
%46 = call i32* @clnttcp_create(%struct.sockaddr_in* %10, i32 %44, i32 %45, i32* %15, i32 0, i32 0)
store i32* %46, i32** %12
%47 = load i32*, i32** %12
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %51
49: ; preds = %40
%50 = call i32 @clnt_pcreateerror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %69
51: ; preds = %40
%52 = load i32*, i32** %12
%53 = load i64, i64* @xdr_void
%54 = trunc i64 %53 to i32
%55 = load i64, i64* @xdr_void
%56 = trunc i64 %55 to i32
%57 = call i32 @clnt_call(i32* %52, i32 0, i32 %54, i32 0, i32 %56, i32 0, %struct.timeval* byval(%struct.timeval) align 8 %13)
store i32 %57, i32* %14
%58 = load i32, i32* %14
%59 = load i32, i32* @RPC_SUCCESS
%60 = icmp ne i32 %58, %59
br i1 %60, label %61, label %63
61: ; preds = %51
store i32 -1, i32* %16
%62 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0))
br label %65
63: ; preds = %51
store i32 0, i32* %16
%64 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
br label %65
65: ; preds = %63, %61
%66 = load i32*, i32** %12
%67 = call i32 @clnt_destroy(i32* %66)
%68 = load i32, i32* %16
store i32 %68, i32* %5
br label %69
69: ; preds = %65, %49, %38
%70 = load i32, i32* %5
ret i32 %70
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.hostent = type { i32, i32* }
@RPC_ANYSOCK = global i32 0
@AF_INET = global i32 0
@.str = [27 x i8] c"Could not lookup hostname:\00"
@.str.1 = [35 x i8] c"Could not create tcp client handle\00"
@xdr_void = global i64 0
@RPC_SUCCESS = global i32 0
@str = [41 x i8] c"Specified program's TCP service is alive\00"
@str.4 = [51 x i8] c"No answer from the specified program's TCP service\00"
define i32 @ping_tcp(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = bitcast %struct.timeval* %6 to i8*
%10 = bitcast i32* %7 to i8*
%11 = load i32, i32* @RPC_ANYSOCK
store i32 %11, i32* %7
%12 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 24)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i64 0, i64* %15
%16 = call %struct.hostent* @gethostbyname(i8* %0)
%17 = icmp eq %struct.hostent* %16, null
br i1 %17, label %30, label %18
18: ; preds = %4
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i64 0, i32 1
%21 = load i32*, i32** %20
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @memcpy(i32* %19, i32 %22, i32 %24)
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %3, i32* %26
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %27
%28 = call i32* @clnttcp_create(%struct.sockaddr_in* %5, i32 %1, i32 %2, i32* %7, i32 0, i32 0)
%29 = icmp eq i32* %28, null
br i1 %29, label %32, label %34
30: ; preds = %4
%31 = call i32 @herror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %45
32: ; preds = %18
%33 = call i32 @clnt_pcreateerror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %45
34: ; preds = %18
%35 = load i64, i64* @xdr_void
%36 = trunc i64 %35 to i32
%37 = call i32 @clnt_call(i32* %28, i32 0, i32 %36, i32 0, i32 %36, i32 0, %struct.timeval* byval(%struct.timeval) align 8 %6)
%38 = load i32, i32* @RPC_SUCCESS
%39 = icmp eq i32 %37, %38
%40 = select i1 %39, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([51 x i8], [51 x i8]* @str.4, i64 0, i64 0)
%41 = xor i1 %39, true
%42 = sext i1 %41 to i32
%43 = call i32 @puts(i8* dereferenceable(1) %40)
%44 = call i32 @clnt_destroy(i32* %28)
br label %45
45: ; preds = %34, %32, %30
%46 = phi i32 [ -1, %32 ], [ %42, %34 ], [ -1, %30 ]
ret i32 %46
}