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

/* Variables and functions */
scalar_t__ ECHILD;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
scalar_t__ errno;
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ ksft_print_msg(char *, int /*<<< orphan*/, int);
scalar_t__ wait(int *);
int children_cleanup(void) {
  unsigned ret = 0;
  while (1) {
    int status;
    pid_t p = wait(&status);
    if ((p < 0) && errno == ECHILD)
      break;
    if (p < 0) {
      ksft_print_msg("%d[%u] "
                     "wait()"
                     ": %m"
                     "\n",
                     getpid(), 2064);
      return 1;
    }
    if (!WIFEXITED(status)) {
      ret = 1;
      continue;
    }
    if (WEXITSTATUS(status) == 1)
      ret = 1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<children_cleanup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <wait@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1196 <children_cleanup+0x46>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%rax
jne    1196 <children_cleanup+0x46>
jmp    1201 <children_cleanup+0xb1>
cmpq   $0x0,-0x18(%rbp)
jge    11c7 <children_cleanup+0x77>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe4f(%rip),%rdi
mov    $0x810,%edx
call   1040 <ksft_print_msg@plt>
movl   $0x1,-0x4(%rbp)
jmp    1207 <children_cleanup+0xb7>
mov    -0xc(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    11e4 <children_cleanup+0x94>
movl   $0x1,-0x8(%rbp)
jmp    115f <children_cleanup+0xf>
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
cmp    $0x1,%eax
jne    11fc <children_cleanup+0xac>
movl   $0x1,-0x8(%rbp)
jmp    115f <children_cleanup+0xf>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<children_cleanup>:
push   %r14
push   %rbx
push   %rax
lea    0x4(%rsp),%rdi
call   1070 <wait@plt>
xor    %ebx,%ebx
test   %rax,%rax
js     11a0 <children_cleanup+0x50>
lea    0x4(%rsp),%r14
jmp    118c <children_cleanup+0x3c>
nopl   0x0(%rax)
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
cmp    $0x1,%eax
cmove  %eax,%ebx
mov    %r14,%rdi
call   1070 <wait@plt>
test   %rax,%rax
js     11a0 <children_cleanup+0x50>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %eax,%eax
jne    1170 <children_cleanup+0x20>
mov    $0x1,%ebx
jmp    117f <children_cleanup+0x2f>
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
je     11d5 <children_cleanup+0x85>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe3c(%rip),%rdi
mov    %eax,%esi
mov    $0x810,%edx
call   1040 <ksft_print_msg@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i64 0
@ECHILD = global i64 0
@.str = [19 x i8] c"%d[%u] wait(): %m\0A\00"
define i32 @children_cleanup() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
store i32 0, i32* %2
br label %5
5: ; preds = %0, %24, %30
%6 = call i64 @wait(i32* %3)
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %5
%10 = load i64, i64* @errno
%11 = load i64, i64* @ECHILD
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %9
br label %31
14: ; preds = %9, %5
%15 = load i64, i64* %4
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %18, i32 2064)
store i32 1, i32* %1
br label %33
20: ; preds = %14
%21 = load i32, i32* %3
%22 = call i32 @WIFEXITED(i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %20
store i32 1, i32* %2
br label %5
25: ; preds = %20
%26 = load i32, i32* %3
%27 = call i32 @WEXITSTATUS(i32 %26)
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 1, i32* %2
br label %30
30: ; preds = %29, %25
br label %5
31: ; preds = %13
%32 = load i32, i32* %2
store i32 %32, i32* %1
br label %33
33: ; preds = %31, %17
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@errno = global i64 0
@ECHILD = global i64 0
@.str = [19 x i8] c"%d[%u] wait(): %m\0A\00"
define i32 @children_cleanup() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i64 @wait(i32* %1)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %13
5: ; preds = %23, %0
%6 = phi i32 [ 0, %0 ], [ %24, %23 ]
%7 = load i64, i64* @errno
%8 = load i64, i64* @ECHILD
%9 = icmp eq i64 %7, %8
br i1 %9, label %27, label %10
10: ; preds = %5
%11 = call i32 (...) @getpid()
%12 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %11, i32 2064)
br label %28
13: ; preds = %0, %23
%14 = phi i32 [ %24, %23 ], [ 0, %0 ]
%15 = load i32, i32* %1
%16 = call i32 @WIFEXITED(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = load i32, i32* %1
%20 = call i32 @WEXITSTATUS(i32 %19)
%21 = icmp eq i32 %20, 1
%22 = select i1 %21, i32 1, i32 %14
br label %23
23: ; preds = %13, %18
%24 = phi i32 [ %22, %18 ], [ 1, %13 ]
%25 = call i64 @wait(i32* %1)
%26 = icmp slt i64 %25, 0
br i1 %26, label %5, label %13
27: ; preds = %5
br label %28
28: ; preds = %10, %27
%29 = phi i32 [ %6, %27 ], [ 1, %10 ]
ret i32 %29
}