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

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
scalar_t__ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ kill(int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
int waitForChild(int child_pid, int timeout) {
  int status;
  int pid;
  while (timeout != 0) {
    pid = waitpid(child_pid, &status, WNOHANG);
    if (pid == 0) {
      sleep(1);
      timeout--;
    } else if (pid == -1) {
      syslog(LOG_ERR, strerror(errno));
      exit(1);
    } else {
      if (WIFEXITED(status)) {
        if (WEXITSTATUS(status) == 0) {
          printf("File read successfully!\n");
          return 1;
        }
      }
      printf("File NOT read successfully.\n");
      return 0;
    }
  }
  kill(child_pid, 9);
  printf("Timeout reading the file!\n");
  return 0;
}


x86 assembly (clang, -O0):
<waitForChild>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1278 <waitForChild+0xe8>
mov    -0x8(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rsi
call   1070 <waitpid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11e2 <waitForChild+0x52>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1273 <waitForChild+0xe3>
cmpl   $0xffffffff,-0x14(%rbp)
jne    121a <waitForChild+0x8a>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%esi
call   1050 <syslog@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     125b <waitForChild+0xcb>
mov    -0x10(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x0,%rax
jne    1256 <waitForChild+0xc6>
lea    0xdbb(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1298 <waitForChild+0x108>
jmp    125b <waitForChild+0xcb>
lea    0xdb7(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1298 <waitForChild+0x108>
jmp    119e <waitForChild+0xe>
mov    -0x8(%rbp),%edi
mov    $0x9,%esi
call   1040 <kill@plt>
lea    0xdaa(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitForChild>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
test   %esi,%esi
je     11d0 <waitForChild+0x40>
mov    %esi,%ebp
mov    0x2e42(%rip),%r15
lea    0x4(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    (%r15),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1070 <waitpid@plt>
test   %eax,%eax
jne    11e7 <waitForChild+0x57>
mov    $0x1,%edi
call   10b0 <sleep@plt>
add    $0xffffffff,%ebp
jne    11b0 <waitForChild+0x20>
mov    %ebx,%edi
mov    $0x9,%esi
call   1040 <kill@plt>
xor    %ebx,%ebx
lea    0xe1b(%rip),%rdi
jmp    121f <waitForChild+0x8f>
cmp    $0xffffffff,%eax
je     1231 <waitForChild+0xa1>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     1208 <waitForChild+0x78>
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
test   %rax,%rax
je     1213 <waitForChild+0x83>
xor    %ebx,%ebx
lea    0xe09(%rip),%rdi
jmp    121f <waitForChild+0x8f>
mov    $0x1,%ebx
lea    0xe17(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d90(%rip),%rax
mov    (%rax),%ebx
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1050 <syslog@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@WNOHANG = global i32 0
@LOG_ERR = global i32 0
@errno = global i32 0
@.str = [25 x i8] c"File read successfully!\0A\00"
@.str.1 = [29 x i8] c"File NOT read successfully.\0A\00"
@.str.2 = [27 x i8] c"Timeout reading the file!\0A\00"
define i32 @waitForChild(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %8
8: ; preds = %43, %2
%9 = load i32, i32* %5
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %44
11: ; preds = %8
%12 = load i32, i32* %4
%13 = load i32, i32* @WNOHANG
%14 = call i32 @waitpid(i32 %12, i32* %6, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = call i32 @sleep(i32 1)
%19 = load i32, i32* %5
%20 = add nsw i32 %19, -1
store i32 %20, i32* %5
br label %43
21: ; preds = %11
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = load i32, i32* @LOG_ERR
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 @syslog(i32 %25, i32 %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = load i32, i32* %6
%32 = call i64 @WIFEXITED(i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %30
%35 = load i32, i32* %6
%36 = call i64 @WEXITSTATUS(i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %48
40: ; preds = %34
br label %41
41: ; preds = %40, %30
%42 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %48
43: ; preds = %17
br label %8
44: ; preds = %8
%45 = load i32, i32* %4
%46 = call i32 @kill(i32 %45, i32 9)
%47 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %48
48: ; preds = %44, %41, %38
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@LOG_ERR = global i32 0
@errno = global i32 0
@str = [26 x i8] c"Timeout reading the file!\00"
@str.3 = [28 x i8] c"File NOT read successfully.\00"
@str.4 = [24 x i8] c"File read successfully!\00"
define i32 @waitForChild(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %1, 0
br i1 %5, label %29, label %6
6: ; preds = %2, %10
%7 = phi i32 [ %12, %10 ], [ %1, %2 ]
%8 = load i32, i32* @WNOHANG
%9 = call i32 @waitpid(i32 %0, i32* %3, i32 %8)
switch i32 %9, label %20 [
i32 0, label %10
i32 -1, label %14
]
10: ; preds = %6
%11 = call i32 @sleep(i32 1)
%12 = add nsw i32 %7, -1
%13 = icmp eq i32 %12, 0
br i1 %13, label %29, label %6
14: ; preds = %6
%15 = load i32, i32* @LOG_ERR
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 @syslog(i32 %15, i32 %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %6
%21 = load i32, i32* %3
%22 = call i64 @WIFEXITED(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i32, i32* %3
%26 = call i64 @WEXITSTATUS(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %24, %20
br label %31
29: ; preds = %10, %2
%30 = call i32 @kill(i32 %0, i32 9)
br label %31
31: ; preds = %24, %29, %28
%32 = phi i8* [ getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0), %29 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @str.3, i64 0, i64 0), %28 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @str.4, i64 0, i64 0), %24 ]
%33 = phi i32 [ 0, %29 ], [ 0, %28 ], [ 1, %24 ]
%34 = call i32 @puts(i8* dereferenceable(1) %32)
ret i32 %33
}