#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;
  scalar_t__ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int is_network_up(char *chkhost, char *chkport) {
  int sfd, s;
  struct addrinfo *result, *rp;
  struct addrinfo hints;
  if (chkhost == NULL || chkport == NULL) {
    return 1;
  }
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = 0;
  hints.ai_protocol = IPPROTO_TCP;
  if ((s = getaddrinfo(chkhost, chkport, &hints, &result)) != 0) {
    syslog(LOG_ERR, "Error getting addr info: %s", gai_strerror(s));
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sfd == -1)
      continue;
    if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
      break;
    close(sfd);
  }
  freeaddrinfo(result);
  if (rp == NULL) {
    syslog(LOG_ERR, "Could not connect");
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<is_network_up>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a6 <is_network_up+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    11b2 <is_network_up+0x32>
movl   $0x1,-0x4(%rbp)
jmp    12ef <is_network_up+0x16f>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x58(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     1236 <is_network_up+0xb6>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %eax,%edx
lea    0xdd1(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12b1 <is_network_up+0x131>
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   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1275 <is_network_up+0xf5>
jmp    12a0 <is_network_up+0x120>
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     1298 <is_network_up+0x118>
jmp    12b1 <is_network_up+0x131>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    123e <is_network_up+0xbe>
mov    -0x28(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
cmpq   $0x0,-0x30(%rbp)
jne    12e8 <is_network_up+0x168>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
lea    0xd47(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ef <is_network_up+0x16f>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_network_up>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    $0x1,%r14d
test   %rdi,%rdi
je     12ab <is_network_up+0x12b>
mov    %rsi,%r15
test   %rsi,%rsi
je     12ab <is_network_up+0x12b>
mov    %rdi,%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
movq   $0x0,0x30(%rsp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     1225 <is_network_up+0xa5>
mov    0x2db6(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xde6(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <syslog@plt>
mov    0x8(%rsp),%rbx
xor    %r14d,%r14d
test   %rbx,%rbx
jne    1249 <is_network_up+0xc9>
xor    %edi,%edi
jmp    127d <is_network_up+0xfd>
cs nopw 0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1278 <is_network_up+0xf8>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1240 <is_network_up+0xc0>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    129b <is_network_up+0x11b>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1240 <is_network_up+0xc0>
mov    0x8(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    0xd8a(%rip),%rsi
xor    %eax,%eax
call   1060 <syslog@plt>
jmp    12ab <is_network_up+0x12b>
mov    0x8(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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