#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*/ printf(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);
  printf("%s\n", command);
  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    0xe50(%rip),%rsi
lea    0xe54(%rip),%rdx
call   1080 <asprintf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    121b <ping+0x9b>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <ping+0x117>
jmp    1220 <ping+0xa0>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
cmp    $0x0,%rax
je     1275 <ping+0xf5>
lea    -0x420(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     1270 <ping+0xf0>
movl   $0x0,-0x4(%rbp)
jmp    1297 <ping+0x117>
jmp    1220 <ping+0xa0>
mov    -0x428(%rbp),%rdi
call   1060 <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    0xe61(%rip),%rsi
lea    0xe65(%rip),%rdx
lea    0x8(%rsp),%rdi
call   1080 <asprintf@plt>
mov    0x8(%rsp),%rsi
lea    0xe54(%rip),%rdi
call   1050 <printf@plt>
mov    0x8(%rsp),%rdi
lea    0xe47(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
je     121e <ping+0x9e>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
lea    0xe51(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     123a <ping+0xba>
mov    %r14,%rdi
mov    %r15,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     11f0 <ping+0x70>
xor    %ebx,%ebx
jmp    1251 <ping+0xd1>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1247 <ping+0xc7>
mov    %rbx,%rdi
call   1060 <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 = [4 x i8] c"%s\0A\00"
@.str.3 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.4 = [33 x i8] c"Failed to execute fping command\0A\00"
@.str.5 = [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 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%12 = load i8*, i8** %4
%13 = call i32* @popen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %21
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
%19 = load i8*, i8** %4
%20 = call i32 @free(i8* %19)
store i32 -1, i32* %2
br label %38
21: ; preds = %1
br label %22
22: ; preds = %32, %21
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%24 = load i32*, i32** %6
%25 = call i64 @fread(i8* %23, i32 1, i32 1024, i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%29 = call i64 @strstr(i8* %28, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 0, i32* %2
br label %38
32: ; preds = %27
br label %22
33: ; preds = %22
%34 = load i32*, i32** %6
%35 = call i32 @pclose(i32* %34)
store i32 %35, i32* %7
%36 = load i8*, i8** %4
%37 = call i32 @free(i8* %36)
store i32 1, i32* %2
br label %38
38: ; preds = %33, %31, %16
%39 = load i32, i32* %2
ret i32 %39
}

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