#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 */
scalar_t__ EINTR;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WNOHANG;
int WTERMSIG(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int waitpid(int, int *, int /*<<< orphan*/);
void collect_zombies() {
  pid_t pid;
  int status;
  while (((pid = waitpid(-1, &status, WNOHANG))) != 0) {
    if (pid == -1) {
      if (errno == EINTR)
        continue;
      else
        break;
    }
    fprintf(stderr, "BG process %d terminated: ", pid);
    if (WIFEXITED(status))
      fprintf(stderr, "exited with code %d\n", WEXITSTATUS(status));
    else if (WIFSIGNALED(status))
      fprintf(stderr, "killed by signal %d\n", WTERMSIG(status));
    else
      fprintf(stderr, "unknown reason\n");
  }
}


x86 assembly (clang, -O0):
<collect_zombies>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
call   1070 <waitpid@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
je     1270 <collect_zombies+0x110>
cmpl   $0xffffffff,-0x4(%rbp)
jne    11b9 <collect_zombies+0x59>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%rax
jne    11b4 <collect_zombies+0x54>
jmp    1168 <collect_zombies+0x8>
jmp    1270 <collect_zombies+0x110>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1211 <collect_zombies+0xb1>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    -0xc(%rbp),%edi
mov    %eax,%edx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    126b <collect_zombies+0x10b>
mov    -0x8(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     124f <collect_zombies+0xef>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%edx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1266 <collect_zombies+0x106>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    126b <collect_zombies+0x10b>
jmp    1168 <collect_zombies+0x8>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<collect_zombies>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e76(%rip),%r15
lea    0x4(%rsp),%rbx
mov    0x2e4a(%rip),%r13
mov    0x2e53(%rip),%r12
lea    0xe74(%rip),%r14
jmp    11a4 <collect_zombies+0x44>
xchg   %ax,%ax
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    1237 <collect_zombies+0xd7>
mov    (%r15),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1190 <collect_zombies+0x30>
test   %eax,%eax
je     1237 <collect_zombies+0xd7>
mov    (%r12),%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11f3 <collect_zombies+0x93>
mov    (%r12),%ebp
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %ebp,%edi
lea    0xe2a(%rip),%rsi
jmp    1217 <collect_zombies+0xb7>
mov    0x4(%rsp),%edi
call   1030 <WIFSIGNALED@plt>
mov    (%r12),%ebp
test   %rax,%rax
je     1222 <collect_zombies+0xc2>
mov    0x4(%rsp),%edi
call   1050 <WTERMSIG@plt>
mov    %ebp,%edi
lea    0xe19(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11a4 <collect_zombies+0x44>
mov    %ebp,%edi
lea    0xe1a(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11a4 <collect_zombies+0x44>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [27 x i8] c"BG process %d terminated: \00"
@.str.1 = [21 x i8] c"exited with code %d\0A\00"
@.str.2 = [21 x i8] c"killed by signal %d\0A\00"
@.str.3 = [16 x i8] c"unknown reason\0A\00"
define void @collect_zombies() {
%1 = alloca i32
%2 = alloca i32
br label %3
3: ; preds = %41, %14, %0
%4 = load i32, i32* @WNOHANG
%5 = call i32 @waitpid(i32 -1, i32* %2, i32 %4)
store i32 %5, i32* %1
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %42
7: ; preds = %3
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10
br label %3
15: ; preds = %10
br label %42
16: ; preds = %7
%17 = load i32, i32* @stderr
%18 = load i32, i32* %1
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %2
%21 = call i64 @WIFEXITED(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = load i32, i32* %2
%26 = call i32 @WEXITSTATUS(i32 %25)
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %41
28: ; preds = %16
%29 = load i32, i32* %2
%30 = call i64 @WIFSIGNALED(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = load i32, i32* %2
%35 = call i32 @WTERMSIG(i32 %34)
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %40
37: ; preds = %28
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %37, %32
br label %41
41: ; preds = %40, %23
br label %3
42: ; preds = %15, %3
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [27 x i8] c"BG process %d terminated: \00"
@.str.1 = [21 x i8] c"exited with code %d\0A\00"
@.str.2 = [21 x i8] c"killed by signal %d\0A\00"
@.str.3 = [16 x i8] c"unknown reason\0A\00"
define void @collect_zombies() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
br label %3
3: ; preds = %21, %0
%4 = load i32, i32* @WNOHANG
%5 = call i32 @waitpid(i32 -1, i32* %1, i32 %4)
switch i32 %5, label %10 [
i32 0, label %33
i32 -1, label %6
]
6: ; preds = %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
br i1 %9, label %21, label %33
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %5)
%13 = load i32, i32* %1
%14 = call i64 @WIFEXITED(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = load i32, i32* %1
%19 = call i32 @WEXITSTATUS(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %16, %31, %27, %6
br label %3
22: ; preds = %10
%23 = load i32, i32* %1
%24 = call i64 @WIFSIGNALED(i32 %23)
%25 = icmp eq i64 %24, 0
%26 = load i32, i32* @stderr
br i1 %25, label %31, label %27
27: ; preds = %22
%28 = load i32, i32* %1
%29 = call i32 @WTERMSIG(i32 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %29)
br label %21
31: ; preds = %22
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %21
33: ; preds = %3, %6
ret void
}