#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*/ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_zero;
  scalar_t__ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct pollfd {
  int fd;
  int /*<<< orphan*/ events;
};
struct icmp {
  int /*<<< orphan*/ icmp_cksum;
  int /*<<< orphan*/ icmp_type;
};
typedef int /*<<< orphan*/ in_addr_t;
typedef int /*<<< orphan*/ echo;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ ICMP_ECHO;
int /*<<< orphan*/ INET_ADDRSTRLEN;
int /*<<< orphan*/ IPPROTO_ICMP;
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ bzero(int /*<<< orphan*/, int);
int /*<<< orphan*/ checksum(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(struct icmp *, int /*<<< orphan*/, int);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ sendto(int, struct icmp *, int, int /*<<< orphan*/,
                          struct sockaddr *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int Ping(in_addr_t host_addr) {
  struct sockaddr_in ad;
  ad.sin_family = AF_INET;
  ad.sin_port = 0;
  bzero(ad.sin_zero, sizeof(ad.sin_zero));
  ad.sin_addr.s_addr = host_addr;
  int sk = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  struct icmp echo;
  memset(&echo, 0, sizeof(echo));
  echo.icmp_type = ICMP_ECHO;
  echo.icmp_cksum = checksum((uint16_t *)&echo, sizeof(echo));
  sendto(sk, &echo, sizeof(echo), 0, (struct sockaddr *)&ad, INET_ADDRSTRLEN);
  struct pollfd pfd;
  pfd.fd = sk;
  pfd.events = POLLIN;
  switch (poll(&pfd, 1, 2000)) {
  case 0:
  case -1:
    close(sk);
    return -1;
  case 1:
    close(sk);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<Ping>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e66(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x4,%esi
call   1080 <bzero@plt>
mov    -0x58(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    %ecx,-0x20(%rbp)
mov    (%rax),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
mov    %rdi,-0x50(%rbp)
xor    %esi,%esi
mov    %esi,-0x40(%rbp)
mov    $0x8,%edx
mov    %edx,-0x44(%rbp)
call   1040 <memset@plt>
mov    -0x50(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
call   1030 <checksum@plt>
mov    -0x50(%rbp),%rsi
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%ecx
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%r9d
lea    -0x20(%rbp),%r8
call   1050 <sendto@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x1,%esi
mov    $0x7d0,%edx
call   1070 <poll@plt>
mov    %eax,%ecx
mov    %ecx,%eax
add    $0x1,%eax
mov    %ecx,-0x3c(%rbp)
sub    $0x2,%eax
jb     1269 <Ping+0xf9>
jmp    1258 <Ping+0xe8>
mov    -0x3c(%rbp),%eax
sub    $0x1,%eax
je     127d <Ping+0x10d>
jmp    1291 <Ping+0x121>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <Ping+0x128>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1298 <Ping+0x128>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Ping>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
mov    0x2e69(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    $0x4,%esi
call   1080 <bzero@plt>
mov    %ebx,0x10(%rsp)
mov    0x0(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %rbp,%rdi
mov    $0x8,%esi
call   1030 <checksum@plt>
mov    %eax,0x8(%rsp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%r9d
lea    0x10(%rsp),%r8
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    %ebx,(%rsp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x7d0,%edx
call   1070 <poll@plt>
add    $0x1,%eax
mov    $0xffffffff,%ebp
cmp    $0x2,%eax
ja     124d <Ping+0xdd>
cltq
lea    0xdbd(%rip),%rcx
mov    (%rcx,%rax,4),%ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.icmp = type { i32, i32 }
%struct.pollfd = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_ICMP = global i32 0
@ICMP_ECHO = global i32 0
@INET_ADDRSTRLEN = global i32 0
@POLLIN = global i32 0
define i32 @Ping(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca %struct.icmp
%7 = alloca %struct.pollfd
store i32 %0, i32* %3
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 3
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%12 = load i32, i32* %11
%13 = call i32 @bzero(i32 %12, i32 4)
%14 = load i32, i32* %3
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_RAW
%19 = load i32, i32* @IPPROTO_ICMP
%20 = call i32 @socket(i32 %17, i32 %18, i32 %19)
store i32 %20, i32* %5
%21 = call i32 @memset(%struct.icmp* %6, i32 0, i32 8)
%22 = load i32, i32* @ICMP_ECHO
%23 = getelementptr inbounds %struct.icmp, %struct.icmp* %6, i32 0, i32 1
store i32 %22, i32* %23
%24 = bitcast %struct.icmp* %6 to i32*
%25 = call i32 @checksum(i32* %24, i32 8)
%26 = getelementptr inbounds %struct.icmp, %struct.icmp* %6, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %5
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%29 = load i32, i32* @INET_ADDRSTRLEN
%30 = call i32 @sendto(i32 %27, %struct.icmp* %6, i32 8, i32 0, %struct.sockaddr* %28, i32 %29)
%31 = load i32, i32* %5
%32 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* @POLLIN
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 1
store i32 %33, i32* %34
%35 = call i32 @poll(%struct.pollfd* %7, i32 1, i32 2000)
switch i32 %35, label %42 [
i32 0, label %36
i32 -1, label %36
i32 1, label %39
]
36: ; preds = %1, %1
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %2
br label %43
39: ; preds = %1
%40 = load i32, i32* %5
%41 = call i32 @close(i32 %40)
store i32 0, i32* %2
br label %43
42: ; preds = %1
store i32 -1, i32* %2
br label %43
43: ; preds = %42, %39, %36
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.icmp = type { i32, i32 }
%struct.pollfd = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_ICMP = global i32 0
@ICMP_ECHO = global i32 0
@INET_ADDRSTRLEN = global i32 0
@POLLIN = global i32 0
@switch.table.Ping = [3 x i32] [i32 -1, i32 -1, i32 0]
define i32 @Ping(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.icmp
%4 = alloca %struct.pollfd
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i64 0, i64* %8
%9 = tail call i32 @bzero(i32 undef, i32 4)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %0, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_RAW
%13 = load i32, i32* @IPPROTO_ICMP
%14 = tail call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = bitcast %struct.icmp* %3 to i8*
%16 = call i32 @memset(%struct.icmp* %3, i32 0, i32 8)
%17 = load i32, i32* @ICMP_ECHO
%18 = getelementptr inbounds %struct.icmp, %struct.icmp* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.icmp, %struct.icmp* %3, i64 0, i32 0
%20 = call i32 @checksum(i32* %19, i32 8)
store i32 %20, i32* %19
%21 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%22 = load i32, i32* @INET_ADDRSTRLEN
%23 = call i32 @sendto(i32 %14, %struct.icmp* %3, i32 8, i32 0, %struct.sockaddr* %21, i32 %22)
%24 = bitcast %struct.pollfd* %4 to i8*
%25 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
store i32 %14, i32* %25
%26 = load i32, i32* @POLLIN
%27 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
store i32 %26, i32* %27
%28 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 2000)
%29 = add i32 %28, 1
%30 = icmp ult i32 %29, 3
br i1 %30, label %31, label %36
31: ; preds = %1
%32 = sext i32 %29 to i64
%33 = getelementptr inbounds [3 x i32], [3 x i32]* @switch.table.Ping, i64 0, i64 %32
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %14)
br label %36
36: ; preds = %1, %31
%37 = phi i32 [ -1, %1 ], [ %34, %31 ]
ret i32 %37
}