#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);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WNOHANG;
int WTERMSIG(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int waitpid(int, int *, int /*<<< orphan*/);
void check_background_processes(int *status) {
  int temp_pid = 0;
  int temp_count = 0;
  while (temp_count < 100000) {
    temp_pid = waitpid(-1, status, WNOHANG);
    if (temp_pid > 0) {
      if (WIFEXITED(*status)) {
        printf("process %d exited with exit value %d", temp_pid,
               WEXITSTATUS(*status));
      }
      if (WIFSIGNALED(*status)) {
        printf("process %d terminated by signal %d", temp_pid,
               WTERMSIG(*status));
      }
      printf("\n");
      fflush(stdout);
    }
    temp_count++;
  }
}


x86 assembly (clang, -O0):
<check_background_processes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x186a0,-0x10(%rbp)
jge    1257 <check_background_processes+0xe7>
mov    -0x8(%rbp),%rsi
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
call   1080 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1249 <check_background_processes+0xd9>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11f4 <check_background_processes+0x84>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     122d <check_background_processes+0xbd>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1060 <WTERMSIG@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    118a <check_background_processes+0x1a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_background_processes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
mov    $0x186a0,%ebx
mov    0x2e4e(%rip),%r13
mov    0x2e37(%rip),%r14
lea    0xe58(%rip),%r15
jmp    11c7 <check_background_processes+0x47>
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    (%r14),%edi
call   1060 <fflush@plt>
add    $0xffffffff,%ebx
je     122e <check_background_processes+0xae>
mov    0x0(%r13),%edx
mov    $0xffffffff,%edi
mov    %r12,%rsi
call   1090 <waitpid@plt>
test   %eax,%eax
jle    11c2 <check_background_processes+0x42>
mov    %eax,%ebp
mov    (%r12),%edi
call   10a0 <WIFEXITED@plt>
test   %rax,%rax
je     1203 <check_background_processes+0x83>
mov    (%r12),%edi
call   1080 <WEXITSTATUS@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r12),%edi
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
je     11b0 <check_background_processes+0x30>
mov    (%r12),%edi
call   1070 <WTERMSIG@plt>
lea    0xe04(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b0 <check_background_processes+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@WNOHANG = global i32 0
@.str = [37 x i8] c"process %d exited with exit value %d\00"
@.str.1 = [35 x i8] c"process %d terminated by signal %d\00"
@stdout = global i32 0
define void @check_background_processes(i32* %0) {
br label %2
2: ; preds = %1, %27
%3 = phi i32 [ 0, %1 ], [ %28, %27 ]
%4 = load i32, i32* @WNOHANG
%5 = tail call i32 @waitpid(i32 -1, i32* %0, i32 %4)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %27
7: ; preds = %2
%8 = load i32, i32* %0
%9 = tail call i64 @WIFEXITED(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = load i32, i32* %0
%13 = tail call i32 @WEXITSTATUS(i32 %12)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %13)
br label %15
15: ; preds = %11, %7
%16 = load i32, i32* %0
%17 = tail call i64 @WIFSIGNALED(i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i32, i32* %0
%21 = tail call i32 @WTERMSIG(i32 %20)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 %21)
br label %23
23: ; preds = %19, %15
%24 = tail call i32 @putchar(i32 10)
%25 = load i32, i32* @stdout
%26 = tail call i32 @fflush(i32 %25)
br label %27
27: ; preds = %23, %2
%28 = add nuw nsw i32 %3, 1
%29 = icmp eq i32 %28, 100000
br i1 %29, label %30, label %2
30: ; preds = %27
ret void
}