#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ printf(char *, ...);
int waitpid(int, int *, int /*<<< orphan*/);
int waiting_for_child(void) {
  int status;
  int err = waitpid(-1, &status, 0);
  if (err == 0)
    return 0;
  if (err == -1) {
    printf("error in waitpid\n");
    return 3;
  }
  if (WIFEXITED(status)) {
    printf("Child exited, status=%d\n", WEXITSTATUS(status));
    if (WEXITSTATUS(status))
      return 2;
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<waiting_for_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1171 <waiting_for_child+0x31>
movl   $0x0,-0x4(%rbp)
jmp    11ef <waiting_for_child+0xaf>
cmpl   $0xffffffff,-0xc(%rbp)
jne    1195 <waiting_for_child+0x55>
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    11ef <waiting_for_child+0xaf>
mov    -0x8(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11e8 <waiting_for_child+0xa8>
mov    -0x8(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     11dc <waiting_for_child+0x9c>
movl   $0x2,-0x4(%rbp)
jmp    11ef <waiting_for_child+0xaf>
movl   $0x1,-0x4(%rbp)
jmp    11ef <waiting_for_child+0xaf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waiting_for_child>:
push   %rax
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %eax,%eax
je     11b9 <waiting_for_child+0x69>
cmp    $0xffffffff,%eax
jne    117e <waiting_for_child+0x2e>
lea    0xea7(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%eax
pop    %rcx
ret
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
je     11bb <waiting_for_child+0x6b>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xe64(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
cmp    $0x1,%eax
mov    $0x2,%eax
sbb    $0x0,%eax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"error in waitpid\0A\00"
@.str.1 = [25 x i8] c"Child exited, status=%d\0A\00"
define i32 @waiting_for_child() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @waitpid(i32 -1, i32* %2, i32 0)
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 0, i32* %1
br label %27
8: ; preds = %0
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 3, i32* %1
br label %27
13: ; preds = %8
%14 = load i32, i32* %2
%15 = call i64 @WIFEXITED(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %13
%18 = load i32, i32* %2
%19 = call i32 @WEXITSTATUS(i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %2
%22 = call i32 @WEXITSTATUS(i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 2, i32* %1
br label %27
25: ; preds = %17
store i32 1, i32* %1
br label %27
26: ; preds = %13
store i32 0, i32* %1
br label %27
27: ; preds = %26, %25, %24, %11, %7
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str.1 = [25 x i8] c"Child exited, status=%d\0A\00"
@str = [17 x i8] c"error in waitpid\00"
define i32 @waiting_for_child() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @waitpid(i32 -1, i32* %1, i32 0)
switch i32 %3, label %6 [
i32 0, label %18
i32 -1, label %4
]
4: ; preds = %0
%5 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %18
6: ; preds = %0
%7 = load i32, i32* %1
%8 = call i64 @WIFEXITED(i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %6
%11 = load i32, i32* %1
%12 = call i32 @WEXITSTATUS(i32 %11)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %1
%15 = call i32 @WEXITSTATUS(i32 %14)
%16 = icmp eq i32 %15, 0
%17 = select i1 %16, i32 1, i32 2
br label %18
18: ; preds = %6, %10, %0, %4
%19 = phi i32 [ 3, %4 ], [ %3, %0 ], [ %17, %10 ], [ 0, %6 ]
ret i32 %19
}