#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 */
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ stderr;
int waitpid(int, int *, int /*<<< orphan*/);
void wait_children() {
  int status = -1;
  int pid = -1;
  while ((pid = waitpid(-1, &status, 0)) > 0) {
    if (WIFSIGNALED(status)) {
      int signo = WTERMSIG(status);
      fprintf(stderr, "child [%d] exit by signal %d\n", pid, signo);
    } else {
      fprintf(stderr, "child [%d] exit \n", pid);
    }
  }
}


x86 assembly (clang, -O0):
<wait_children>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0xffffffff,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
mov    $0xffffffff,%edi
lea    -0x4(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    11d0 <wait_children+0x90>
mov    -0x4(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11b1 <wait_children+0x71>
mov    -0x4(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe5b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11cb <wait_children+0x8b>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1156 <wait_children+0x16>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_children>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %eax,%eax
jle    11e0 <wait_children+0xa0>
mov    %eax,%ebp
mov    0x2e6e(%rip),%r12
lea    0xea5(%rip),%r14
lea    0xc(%rsp),%rbx
lea    0xe7b(%rip),%r15
jmp    11b5 <wait_children+0x75>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11e0 <wait_children+0xa0>
mov    0xc(%rsp),%edi
call   1030 <WIFSIGNALED@plt>
test   %rax,%rax
je     1190 <wait_children+0x50>
mov    0xc(%rsp),%edi
call   1050 <WTERMSIG@plt>
mov    (%r12),%edi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11a0 <wait_children+0x60>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"child [%d] exit by signal %d\0A\00"
@.str.1 = [18 x i8] c"child [%d] exit \0A\00"
define void @wait_children() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 -1, i32* %1
store i32 -1, i32* %2
br label %4
4: ; preds = %22, %0
%5 = call i32 @waitpid(i32 -1, i32* %1, i32 0)
store i32 %5, i32* %2
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %23
7: ; preds = %4
%8 = load i32, i32* %1
%9 = call i64 @WIFSIGNALED(i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %7
%12 = load i32, i32* %1
%13 = call i32 @WTERMSIG(i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* @stderr
%15 = load i32, i32* %2
%16 = load i32, i32* %3
%17 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
br label %22
18: ; preds = %7
%19 = load i32, i32* @stderr
%20 = load i32, i32* %2
%21 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %18, %11
br label %4
23: ; preds = %4
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"child [%d] exit by signal %d\0A\00"
@.str.1 = [18 x i8] c"child [%d] exit \0A\00"
define void @wait_children() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 -1, i32* %1
%3 = call i32 @waitpid(i32 -1, i32* %1, i32 0)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %21
5: ; preds = %0, %18
%6 = phi i32 [ %19, %18 ], [ %3, %0 ]
%7 = load i32, i32* %1
%8 = call i64 @WIFSIGNALED(i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %5
%11 = load i32, i32* %1
%12 = call i32 @WTERMSIG(i32 %11)
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %12)
br label %18
15: ; preds = %5
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %18
18: ; preds = %15, %10
%19 = call i32 @waitpid(i32 -1, i32* %1, i32 0)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %5, label %21
21: ; preds = %18, %0
ret void
}