#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*/ WNOHANG;
int /*<<< orphan*/ printf(char *, int);
int waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
void checkBgStatus() {
  int pid = waitpid(-1, NULL, WNOHANG);
  while (pid != -1 && pid != 0) {
    printf("BG_PID %d Exited.\n", pid);
    pid = waitpid(-1, NULL, WNOHANG);
  }
}


x86 assembly (clang, -O0):
<checkBgStatus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2eb9(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <waitpid@plt>
mov    %eax,-0x4(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x4(%rbp)
mov    %al,-0x5(%rbp)
je     115b <checkBgStatus+0x3b>
cmpl   $0x0,-0x4(%rbp)
setne  %al
mov    %al,-0x5(%rbp)
mov    -0x5(%rbp),%al
test   $0x1,%al
jne    116b <checkBgStatus+0x4b>
jmp    1199 <checkBgStatus+0x79>
mov    -0x4(%rbp),%esi
lea    0xe8b(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <waitpid@plt>
mov    %eax,-0x4(%rbp)
jmp    1142 <checkBgStatus+0x22>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkBgStatus>:
push   %r14
push   %rbx
push   %rax
mov    0x2ebd(%rip),%r14
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1040 <waitpid@plt>
lea    -0x1(%rax),%ecx
cmp    $0xfffffffd,%ecx
ja     1173 <checkBgStatus+0x53>
lea    0xeb7(%rip),%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1040 <waitpid@plt>
mov    %eax,%ecx
add    $0xffffffff,%ecx
cmp    $0xfffffffe,%ecx
jb     1150 <checkBgStatus+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@.str = [19 x i8] c"BG_PID %d Exited.\0A\00"
define void @checkBgStatus() {
%1 = alloca i32
%2 = load i32, i32* @WNOHANG
%3 = call i32 @waitpid(i32 -1, i32* null, i32 %2)
store i32 %3, i32* %1
br label %4
4: ; preds = %12, %0
%5 = load i32, i32* %1
%6 = icmp ne i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = load i32, i32* %1
%9 = icmp ne i32 %8, 0
br label %10
10: ; preds = %7, %4
%11 = phi i1 [ false, %4 ], [ %9, %7 ]
br i1 %11, label %12, label %17
12: ; preds = %10
%13 = load i32, i32* %1
%14 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @WNOHANG
%16 = call i32 @waitpid(i32 -1, i32* null, i32 %15)
store i32 %16, i32* %1
br label %4
17: ; preds = %10
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@.str = [19 x i8] c"BG_PID %d Exited.\0A\00"
define void @checkBgStatus() {
%1 = load i32, i32* @WNOHANG
%2 = tail call i32 @waitpid(i32 -1, i32* null, i32 %1)
%3 = add i32 %2, -1
%4 = icmp ult i32 %3, -2
br i1 %4, label %5, label %12
5: ; preds = %0, %5
%6 = phi i32 [ %9, %5 ], [ %2, %0 ]
%7 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @WNOHANG
%9 = tail call i32 @waitpid(i32 -1, i32* null, i32 %8)
%10 = add i32 %9, -1
%11 = icmp ult i32 %10, -2
br i1 %11, label %5, label %12
12: ; preds = %5, %0
ret void
}