#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
};
typedef int /*<<< orphan*/ sport;
typedef int /*<<< orphan*/ service;
typedef int /*<<< orphan*/ node;

/* Variables and functions */
int /*<<< orphan*/ NI_NUMERICHOST;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ getnameinfo(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                               int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int is_up(const char *chkhost, const char *chkport) {
  int sock = -1;
  struct addrinfo *res, *rp;
  int ret = 0;
  char sport[10];
  snprintf(sport, sizeof(sport), "%s", chkport);
  struct addrinfo hints = {.ai_socktype = SOCK_STREAM};
  if (getaddrinfo(chkhost, sport, &hints, &res)) {
    perror("gai");
    return 0;
  }
  for (rp = res; rp && !ret; rp = rp->ai_next) {
    sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sock == -1)
      continue;
    if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) {
      char node[200], service[100];
      getnameinfo(res->ai_addr, res->ai_addrlen, node, sizeof(node), service,
                  sizeof(service), NI_NUMERICHOST);
      ret = 1;
    }
    close(sock);
  }
  freeaddrinfo(res);
  return ret;
}


x86 assembly (clang, -O0):
<is_up>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x0,-0x34(%rbp)
lea    -0x3e(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0xa,%esi
lea    0xe3b(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x3e(%rbp),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1090 <getaddrinfo@plt>
cmp    $0x0,%rax
je     121d <is_up+0x8d>
lea    0xdf7(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    130a <is_up+0x17a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x1a1(%rbp)
je     1247 <is_up+0xb7>
cmpl   $0x0,-0x34(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x1a1(%rbp)
mov    -0x1a1(%rbp),%al
test   $0x1,%al
jne    125a <is_up+0xca>
jmp    12fb <is_up+0x16b>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1286 <is_up+0xf6>
jmp    12ea <is_up+0x15a>
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     12e2 <is_up+0x152>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
lea    -0x130(%rbp),%rdx
lea    -0x1a0(%rbp),%r8
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
mov    $0xc8,%ecx
mov    $0x64,%r9d
mov    %eax,(%rsp)
call   1060 <getnameinfo@plt>
movl   $0x1,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    1225 <is_up+0x95>
mov    -0x28(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_up>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x178,%rsp
mov    %rsi,%rcx
mov    %rdi,%rbx
lea    0xe62(%rip),%rdx
lea    0x36(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xa,%esi
call   1030 <snprintf@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x20(%rsp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <getaddrinfo@plt>
test   %rax,%rax
jne    12c2 <is_up+0x142>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
je     12a3 <is_up+0x123>
mov    0x2ddb(%rip),%r13
lea    0xb0(%rsp),%r14
lea    0x40(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1270 <is_up+0xf0>
mov    %eax,%ebx
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %eax,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     1280 <is_up+0x100>
mov    0x8(%rsp),%rax
mov    (%rax),%esi
mov    0x4(%rax),%edi
mov    0x0(%r13),%eax
mov    %eax,(%rsp)
mov    %r14,%rdx
mov    $0xc8,%ecx
mov    %r15,%r8
mov    $0x64,%r9d
call   1050 <getnameinfo@plt>
mov    $0x1,%r12d
jmp    1283 <is_up+0x103>
cs nopw 0x0(%rax,%rax,1)
xor    %r12d,%r12d
test   %r12d,%r12d
je     128f <is_up+0x10f>
jmp    129c <is_up+0x11c>
nopw   0x0(%rax,%rax,1)
xor    %r12d,%r12d
mov    %ebx,%edi
call   1040 <close@plt>
test   %r12d,%r12d
jne    129c <is_up+0x11c>
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
jne    1210 <is_up+0x90>
mov    0x8(%rsp),%rdi
jmp    12a8 <is_up+0x128>
xor    %edi,%edi
xor    %r12d,%r12d
call   1090 <freeaddrinfo@plt>
mov    %r12d,%eax
add    $0x178,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3a(%rip),%rdi
call   1060 <perror@plt>
xor    %r12d,%r12d
jmp    12ad <is_up+0x12d>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@.str = [3 x i8] c"%s\00"
@SOCK_STREAM = global i32 0
@.str.1 = [4 x i8] c"gai\00"
@NI_NUMERICHOST = global i32 0
define i32 @is_up(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca i32
%10 = alloca [10 x i8]
%11 = alloca %struct.addrinfo
%12 = alloca [200 x i8]
%13 = alloca [100 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 -1, i32* %6
store i32 0, i32* %9
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%15 = load i8*, i8** %5
%16 = call i32 @snprintf(i8* %14, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = bitcast %struct.addrinfo* %11 to i8*
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 3
%19 = load i32, i32* @SOCK_STREAM
store i32 %19, i32* %18
%20 = load i8*, i8** %4
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%22 = call i64 @getaddrinfo(i8* %20, i8* %21, %struct.addrinfo* %11, %struct.addrinfo** %7)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %83
26: ; preds = %2
%27 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %27, %struct.addrinfo** %8
br label %28
28: ; preds = %75, %26
%29 = load %struct.addrinfo*, %struct.addrinfo** %8
%30 = icmp ne %struct.addrinfo* %29, null
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i32, i32* %9
%33 = icmp ne i32 %32, 0
%34 = xor i1 %33, true
br label %35
35: ; preds = %31, %28
%36 = phi i1 [ false, %28 ], [ %34, %31 ]
br i1 %36, label %37, label %79
37: ; preds = %35
%38 = load %struct.addrinfo*, %struct.addrinfo** %8
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 4
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %8
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 3
%43 = load i32, i32* %42
%44 = load %struct.addrinfo*, %struct.addrinfo** %8
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 2
%46 = load i32, i32* %45
%47 = call i32 @socket(i32 %40, i32 %43, i32 %46)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %51
50: ; preds = %37
br label %75
51: ; preds = %37
%52 = load i32, i32* %6
%53 = load %struct.addrinfo*, %struct.addrinfo** %8
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = load %struct.addrinfo*, %struct.addrinfo** %8
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @connect(i32 %52, i32 %55, i32 %58)
%60 = icmp ne i32 %59, -1
br i1 %60, label %61, label %72
61: ; preds = %51
%62 = load %struct.addrinfo*, %struct.addrinfo** %7
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 1
%64 = load i32, i32* %63
%65 = load %struct.addrinfo*, %struct.addrinfo** %7
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = getelementptr inbounds [200 x i8], [200 x i8]* %12, i64 0, i64 0
%69 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%70 = load i32, i32* @NI_NUMERICHOST
%71 = call i32 @getnameinfo(i32 %64, i32 %67, i8* %68, i32 200, i8* %69, i32 100, i32 %70)
store i32 1, i32* %9
br label %72
72: ; preds = %61, %51
%73 = load i32, i32* %6
%74 = call i32 @close(i32 %73)
br label %75
75: ; preds = %72, %50
%76 = load %struct.addrinfo*, %struct.addrinfo** %8
%77 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %76, i32 0, i32 5
%78 = load %struct.addrinfo*, %struct.addrinfo** %77
store %struct.addrinfo* %78, %struct.addrinfo** %8
br label %28
79: ; preds = %35
%80 = load %struct.addrinfo*, %struct.addrinfo** %7
%81 = call i32 @freeaddrinfo(%struct.addrinfo* %80)
%82 = load i32, i32* %9
store i32 %82, i32* %3
br label %83
83: ; preds = %79, %24
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@.str = [3 x i8] c"%s\00"
@SOCK_STREAM = global i32 0
@.str.1 = [4 x i8] c"gai\00"
@NI_NUMERICHOST = global i32 0
define i32 @is_up(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca [10 x i8]
%5 = alloca %struct.addrinfo
%6 = alloca [200 x i8]
%7 = alloca [100 x i8]
%8 = bitcast %struct.addrinfo** %3 to i8*
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%10 = call i32 @snprintf(i8* %9, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
%11 = bitcast %struct.addrinfo* %5 to i8*
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 3
%13 = load i32, i32* @SOCK_STREAM
store i32 %13, i32* %12
%14 = call i64 @getaddrinfo(i8* %0, i8* %9, %struct.addrinfo* %5, %struct.addrinfo** %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load %struct.addrinfo*, %struct.addrinfo** %3
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %61, label %19
19: ; preds = %16
%20 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
br label %24
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %65
24: ; preds = %19, %52
%25 = phi %struct.addrinfo* [ %17, %19 ], [ %55, %52 ]
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 4
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 3
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 2
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %27, i32 %29, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %52, label %34
34: ; preds = %24
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @connect(i32 %32, i32 %36, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %49, label %41
41: ; preds = %34
%42 = load %struct.addrinfo*, %struct.addrinfo** %3
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i64 0, i32 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i64 0, i32 0
%46 = load i32, i32* %45
%47 = load i32, i32* @NI_NUMERICHOST
%48 = call i32 @getnameinfo(i32 %44, i32 %46, i8* %20, i32 200, i8* %21, i32 100, i32 %47)
br label %49
49: ; preds = %41, %34
%50 = phi i32 [ 1, %41 ], [ 0, %34 ]
%51 = call i32 @close(i32 %32)
br label %52
52: ; preds = %24, %49
%53 = phi i32 [ 0, %24 ], [ %50, %49 ]
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 5
%55 = load %struct.addrinfo*, %struct.addrinfo** %54
%56 = icmp ne %struct.addrinfo* %55, null
%57 = icmp eq i32 %53, 0
%58 = and i1 %57, %56
br i1 %58, label %24, label %59
59: ; preds = %52
%60 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %61
61: ; preds = %59, %16
%62 = phi %struct.addrinfo* [ null, %16 ], [ %60, %59 ]
%63 = phi i32 [ 0, %16 ], [ %53, %59 ]
%64 = call i32 @freeaddrinfo(%struct.addrinfo* %62)
br label %65
65: ; preds = %61, %22
%66 = phi i32 [ 0, %22 ], [ %63, %61 ]
ret i32 %66
}