#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *, char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strstr(char *, char *);
int ping(char *ipaddr) {
  char *command = NULL;
  char buffer[1024];
  FILE *fp;
  int stat = 0;
  asprintf(&command, "%s %s -c 1", "ping", ipaddr);
  fp = popen(command, "r");
  if (fp == NULL) {
    fprintf(stderr, "Failed to execute fping command\n");
    free(command);
    return -1;
  }
  while (fread(buffer, sizeof(char), 1024, fp)) {
    if (strstr(buffer, "1 received"))
      return 0;
  }
  stat = pclose(fp);
  free(command);
  return 1;
}


x86 assembly (clang, -O0):
<ping>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x42c(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x18(%rbp),%rdi
lea    0xe60(%rip),%rsi
lea    0xe64(%rip),%rdx
call   1070 <asprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    11fb <ping+0x8b>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <ping+0x107>
jmp    1200 <ping+0x90>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
cmp    $0x0,%rax
je     1255 <ping+0xe5>
lea    -0x420(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     1250 <ping+0xe0>
movl   $0x0,-0x4(%rbp)
jmp    1277 <ping+0x107>
jmp    1200 <ping+0x90>
mov    -0x428(%rbp),%rdi
call   1050 <pclose@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ping>:
push   %r15
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %rdi,%rcx
movq   $0x0,0x8(%rsp)
lea    0xe71(%rip),%rsi
lea    0xe75(%rip),%rdx
lea    0x8(%rsp),%rdi
call   1070 <asprintf@plt>
mov    0x8(%rsp),%rdi
lea    0xe64(%rip),%rsi
call   1080 <popen@plt>
test   %rax,%rax
je     11fe <ping+0x8e>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
lea    0xe6e(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     121a <ping+0xaa>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11d0 <ping+0x60>
xor    %ebx,%ebx
jmp    1231 <ping+0xc1>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1227 <ping+0xb7>
mov    %rbx,%rdi
call   1050 <pclose@plt>
mov    $0x1,%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%s %s -c 1\00"
@.str.1 = [5 x i8] c"ping\00"
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.3 = [33 x i8] c"Failed to execute fping command\0A\00"
@.str.4 = [11 x i8] c"1 received\00"
define i32 @ping(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %4
store i32 0, i32* %7
%8 = load i8*, i8** %3
%9 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %4
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%17 = load i8*, i8** %4
%18 = call i32 @free(i8* %17)
store i32 -1, i32* %2
br label %36
19: ; preds = %1
br label %20
20: ; preds = %30, %19
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = load i32*, i32** %6
%23 = call i64 @fread(i8* %21, i32 1, i32 1024, i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%27 = call i64 @strstr(i8* %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 0, i32* %2
br label %36
30: ; preds = %25
br label %20
31: ; preds = %20
%32 = load i32*, i32** %6
%33 = call i32 @pclose(i32* %32)
store i32 %33, i32* %7
%34 = load i8*, i8** %4
%35 = call i32 @free(i8* %34)
store i32 1, i32* %2
br label %36
36: ; preds = %31, %29, %14
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [11 x i8] c"%s %s -c 1\00"
@.str.1 = [5 x i8] c"ping\00"
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.3 = [33 x i8] c"Failed to execute fping command\0A\00"
@.str.4 = [11 x i8] c"1 received\00"
define i32 @ping(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = load i8*, i8** %2
%8 = call i32* @popen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %21
13: ; preds = %1, %16
%14 = call i64 @fread(i8* %5, i32 1, i32 1024, i32* %8)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = call i64 @strstr(i8* %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %13, label %25
19: ; preds = %13
%20 = call i32 @pclose(i32* %8)
br label %21
21: ; preds = %10, %19
%22 = phi i32 [ 1, %19 ], [ -1, %10 ]
%23 = load i8*, i8** %2
%24 = call i32 @free(i8* %23)
br label %25
25: ; preds = %16, %21
%26 = phi i32 [ %22, %21 ], [ 0, %16 ]
ret i32 %26
}