#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*/ cmd;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int /*<<< orphan*/);
int system(char *);
int connect_ip(char *ping_ip) {
  char cmd[100];
  int retval;
  int n = 0;
  snprintf(cmd, sizeof(cmd), "ping -c 1 %s", ping_ip, n++);
  printf("%s\n", cmd);
  n = 0;
  int net_up = 0;
  while (1) {
    retval = system(cmd);
    if (net_up) {
      if (retval == 0) {
        printf("Net up:  %s\n", ping_ip);
      } else {
        printf("------------- Holly cow, we lost the network!!!!!\n");
        system("date");
        net_up = 0;
      }
    } else {
      if (retval == 0) {
        printf("************** Network is back up!!!!!\n");
        system("date");
        net_up = 1;
      } else {
        printf("Net still down:  %s\n", ping_ip);
      }
    }
    sleep(2);
  }
  printf("Connected!!!!!!!!!\n", ping_ip, n++);
  return 0;
}


x86 assembly (clang, -O0):
<connect_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x78(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x78(%rbp),%r8d
mov    %r8d,%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
mov    $0x64,%esi
lea    0xe89(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x70(%rbp),%rsi
lea    0xf04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x78(%rbp)
movl   $0x0,-0x7c(%rbp)
lea    -0x70(%rbp),%rdi
call   1030 <system@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x7c(%rbp)
je     11f9 <connect_ip+0xb9>
cmpl   $0x0,-0x74(%rbp)
jne    11d3 <connect_ip+0x93>
mov    -0x8(%rbp),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f4 <connect_ip+0xb4>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe65(%rip),%rdi
call   1030 <system@plt>
movl   $0x0,-0x7c(%rbp)
jmp    1240 <connect_ip+0x100>
cmpl   $0x0,-0x74(%rbp)
jne    1229 <connect_ip+0xe9>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe35(%rip),%rdi
call   1030 <system@plt>
movl   $0x1,-0x7c(%rbp)
jmp    123b <connect_ip+0xfb>
mov    -0x8(%rbp),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1240 <connect_ip+0x100>
mov    $0x2,%edi
call   1060 <sleep@plt>
jmp    119c <connect_ip+0x5c>

x86 assembly (clang, -O2):
<connect_ip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%r14
lea    0xe98(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %r14,%rcx
xor    %r8d,%r8d
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
lea    0xe8d(%rip),%r12
lea    0xe79(%rip),%rbp
lea    0xec0(%rip),%r15
xor    %r13d,%r13d
jmp    11bd <connect_ip+0x6d>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
call   1040 <system@plt>
xor    %r13d,%r13d
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
test   %r13d,%r13d
je     11f0 <connect_ip+0xa0>
test   %eax,%eax
jne    11a0 <connect_ip+0x50>
mov    %rbp,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r13d
jmp    11b3 <connect_ip+0x63>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %eax,%eax
je     120a <connect_ip+0xba>
xor    %r13d,%r13d
lea    0xe21(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11b3 <connect_ip+0x63>
lea    0xe23(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
call   1040 <system@plt>
mov    $0x1,%r13d
jmp    11b3 <connect_ip+0x63>

LLVM IR (-O0):
@.str = [13 x i8] c"ping -c 1 %s\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [13 x i8] c"Net up: %s\0A\00"
@.str.3 = [51 x i8] c"------------- Holly cow, we lost the network!!!!!\0A\00"
@.str.4 = [5 x i8] c"date\00"
@.str.5 = [40 x i8] c"************** Network is back up!!!!!\0A\00"
@.str.6 = [21 x i8] c"Net still down: %s\0A\00"
define i32 @connect_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%8 = load i8*, i8** %2
%9 = load i32, i32* %5
%10 = add nsw i32 %9, 1
store i32 %10, i32* %5
%11 = call i32 @snprintf(i8* %7, i32 100, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %9)
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i32 0, i32* %5
store i32 0, i32* %6
br label %14
14: ; preds = %1, %39
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%16 = call i32 @system(i8* %15)
store i32 %16, i32* %4
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %14
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i8*, i8** %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %28
25: ; preds = %19
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %6
br label %28
28: ; preds = %25, %22
br label %39
29: ; preds = %14
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
%34 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %6
br label %38
35: ; preds = %29
%36 = load i8*, i8** %2
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %35, %32
br label %39
39: ; preds = %38, %28
%40 = call i32 @sleep(i32 2)
br label %14
}

LLVM IR (-O2):
@.str = [13 x i8] c"ping -c 1 %s\00"
@.str.2 = [13 x i8] c"Net up: %s\0A\00"
@.str.4 = [5 x i8] c"date\00"
@.str.6 = [21 x i8] c"Net still down: %s\0A\00"
@str = [39 x i8] c"************** Network is back up!!!!!\00"
@str.7 = [50 x i8] c"------------- Holly cow, we lost the network!!!!!\00"
define i32 @connect_ip(i8* %0) {
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 100, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0, i32 0)
%5 = call i32 @puts(i8* %3)
br label %6
6: ; preds = %23, %1
%7 = phi i32 [ 0, %1 ], [ %24, %23 ]
%8 = call i32 @system(i8* %3)
%9 = icmp eq i32 %7, 0
%10 = icmp eq i32 %8, 0
br i1 %9, label %17, label %11
11: ; preds = %6
br i1 %10, label %12, label %14
12: ; preds = %11
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %23
14: ; preds = %11
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.7, i64 0, i64 0))
%16 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %23
17: ; preds = %6
br i1 %10, label %18, label %21
18: ; preds = %17
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%20 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %23
21: ; preds = %17
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %0)
br label %23
23: ; preds = %18, %21, %12, %14
%24 = phi i32 [ 1, %12 ], [ 0, %14 ], [ 1, %18 ], [ 0, %21 ]
%25 = call i32 @sleep(i32 2)
br label %6
}