#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ delete_bg_pid(int);
int /*<<< orphan*/ printf(char *, int, int);
int waitpid(int, int *, int /*<<< orphan*/);
void lookup_all_bg_process() {
  pid_t bg_pid = -1;
  int bg_exit_status;
  bg_pid = waitpid(-1, &bg_exit_status, WNOHANG);
  if (bg_pid != 0 && bg_pid != -1) {
    if (bg_pid > 0) {
      if (bg_exit_status != 0)
        printf("background pid %d is done: terminated by sig %d\n", bg_pid,
               bg_exit_status);
      else
        printf("background pid %d exited with code %d\n", bg_pid,
               bg_exit_status);
      delete_bg_pid(bg_pid);
    }
    lookup_all_bg_process();
  }
}


x86 assembly (clang, -O0):
<lookup_all_bg_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0xffffffff,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
call   1060 <waitpid@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11c7 <lookup_all_bg_process+0x87>
cmpl   $0xffffffff,-0x4(%rbp)
je     11c7 <lookup_all_bg_process+0x87>
cmpl   $0x0,-0x4(%rbp)
jle    11c2 <lookup_all_bg_process+0x82>
cmpl   $0x0,-0x8(%rbp)
je     11a8 <lookup_all_bg_process+0x68>
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe62(%rip),%rdi
call   1040 <printf@plt>
jmp    11ba <lookup_all_bg_process+0x7a>
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
call   1050 <delete_bg_pid@plt>
call   1030 <lookup_all_bg_process@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_all_bg_process>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e9c(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
call   1060 <waitpid@plt>
mov    %eax,%ebx
lea    -0x1(%rbx),%eax
cmp    $0xfffffffd,%eax
ja     119a <lookup_all_bg_process+0x5a>
test   %ebx,%ebx
jle    1195 <lookup_all_bg_process+0x55>
mov    0xc(%rsp),%edx
test   %edx,%edx
je     117e <lookup_all_bg_process+0x3e>
lea    0xe86(%rip),%rdi
mov    %ebx,%esi
jmp    1189 <lookup_all_bg_process+0x49>
lea    0xeac(%rip),%rdi
mov    %ebx,%esi
xor    %edx,%edx
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <delete_bg_pid@plt>
call   1030 <lookup_all_bg_process@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@.str = [49 x i8] c"background pid %d is done: terminated by sig %d\0A\00"
@.str.1 = [39 x i8] c"background pid %d exited with code %d\0A\00"
define void @lookup_all_bg_process() {
%1 = alloca i32
%2 = alloca i32
store i32 -1, i32* %1
%3 = load i32, i32* @WNOHANG
%4 = call i32 @waitpid(i32 -1, i32* %2, i32 %3)
store i32 %4, i32* %1
%5 = load i32, i32* %1
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %28
7: ; preds = %0
%8 = load i32, i32* %1
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %28
10: ; preds = %7
%11 = load i32, i32* %1
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %10
%14 = load i32, i32* %2
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* %1
%18 = load i32, i32* %2
%19 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18)
br label %24
20: ; preds = %13
%21 = load i32, i32* %1
%22 = load i32, i32* %2
%23 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %22)
br label %24
24: ; preds = %20, %16
%25 = load i32, i32* %1
%26 = call i32 @delete_bg_pid(i32 %25)
br label %27
27: ; preds = %24, %10
br label %28
28: ; preds = %27, %7, %0
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@.str = [49 x i8] c"background pid %d is done: terminated by sig %d\0A\00"
@.str.1 = [39 x i8] c"background pid %d exited with code %d\0A\00"
define void @lookup_all_bg_process() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @WNOHANG
%4 = call i32 @waitpid(i32 -1, i32* %1, i32 %3)
%5 = add i32 %4, -1
%6 = icmp ult i32 %5, -2
br i1 %6, label %7, label %19
7: ; preds = %0
%8 = icmp sgt i32 %4, 0
br i1 %8, label %9, label %18
9: ; preds = %7
%10 = load i32, i32* %1
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %10)
br label %16
14: ; preds = %9
%15 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 0)
br label %16
16: ; preds = %14, %12
%17 = call i32 @delete_bg_pid(i32 %4)
br label %18
18: ; preds = %16, %7
br label %19
19: ; preds = %18, %0
ret void
}