#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*/ filebench_log(int, char *, int);
int getpid();
int waitpid(int, int *, int /*<<< orphan*/);
void procflow_wait(pid_t pid) {
  pid_t wpid;
  int stat;
  (void)waitpid(pid, &stat, 0);
  while ((wpid = waitpid(getpid() * -1, &stat, WNOHANG)) > 0)
    filebench_log(6, "Waited for pid %d", (int)wpid);
}


x86 assembly (clang, -O0):
<procflow_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
imul   $0xffffffff,%eax,%edi
mov    0x2e8e(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   1050 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    118a <procflow_wait+0x5a>
mov    -0x8(%rbp),%edx
mov    $0x6,%edi
lea    0xe80(%rip),%rsi
call   1040 <filebench_log@plt>
jmp    1149 <procflow_wait+0x19>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<procflow_wait>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
neg    %eax
mov    0x2e90(%rip),%r15
mov    (%r15),%edx
mov    %eax,%edi
mov    %rbx,%rsi
call   1050 <waitpid@plt>
test   %eax,%eax
jle    11a9 <procflow_wait+0x79>
lea    0xe90(%rip),%r14
lea    0xc(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x6,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <filebench_log@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
neg    %eax
mov    (%r15),%edx
mov    %eax,%edi
mov    %rbx,%rsi
call   1050 <waitpid@plt>
test   %eax,%eax
jg     1180 <procflow_wait+0x50>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@.str = [18 x i8] c"Waited for pid %d\00"
define void @procflow_wait(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32 @waitpid(i32 %5, i32* %4, i32 0)
br label %7
7: ; preds = %13, %1
%8 = call i32 (...) @getpid()
%9 = mul nsw i32 %8, -1
%10 = load i32, i32* @WNOHANG
%11 = call i32 @waitpid(i32 %9, i32* %4, i32 %10)
store i32 %11, i32* %3
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = load i32, i32* %3
%15 = call i32 @filebench_log(i32 6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %7
16: ; preds = %7
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@.str = [18 x i8] c"Waited for pid %d\00"
define void @procflow_wait(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%5 = call i32 (...) @getpid()
%6 = sub nsw i32 0, %5
%7 = load i32, i32* @WNOHANG
%8 = call i32 @waitpid(i32 %6, i32* %2, i32 %7)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %1, %10
%11 = phi i32 [ %16, %10 ], [ %8, %1 ]
%12 = call i32 @filebench_log(i32 6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i32 (...) @getpid()
%14 = sub nsw i32 0, %13
%15 = load i32, i32* @WNOHANG
%16 = call i32 @waitpid(i32 %14, i32* %2, i32 %15)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %10, label %18
18: ; preds = %10, %1
ret void
}