#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 getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int wait(int *);
void wait_for_pid(int pid) {
  int status;
  int corpse;
  int curpid = getpid();
  printf("%d: waiting for children to die\n", curpid);
  while ((corpse = wait(&status)) >= 0 && corpse != pid)
    printf("%d: Unexpected child %d exited with status 0x%.4X\n", curpid,
           corpse, status);
  if (corpse == pid)
    printf("%d: Child %d exited with status 0x%.4X\n", curpid, corpse, status);
  else
    printf("%d: Child %d died without its death being tracked\n", curpid, pid);
}


x86 assembly (clang, -O0):
<wait_for_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x8(%rbp),%rdi
call   1050 <wait@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x11(%rbp)
jl     117e <wait_for_pid+0x4e>
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    118e <wait_for_pid+0x5e>
jmp    11aa <wait_for_pid+0x7a>
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1156 <wait_for_pid+0x26>
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    11d2 <wait_for_pid+0xa2>
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e6 <wait_for_pid+0xb6>
mov    -0x10(%rbp),%esi
mov    -0x4(%rbp),%edx
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
lea    0xeb7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x4(%rsp),%rdi
call   1050 <wait@plt>
test   %eax,%eax
js     1192 <wait_for_pid+0x62>
cmp    %ebx,%eax
je     1192 <wait_for_pid+0x62>
lea    0xeb6(%rip),%r14
lea    0x4(%rsp),%r15
mov    0x4(%rsp),%ecx
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
call   1050 <wait@plt>
test   %eax,%eax
js     1192 <wait_for_pid+0x62>
cmp    %ebx,%eax
jne    1170 <wait_for_pid+0x40>
cmp    %ebx,%eax
jne    11ae <wait_for_pid+0x7e>
mov    0x4(%rsp),%ecx
lea    0xeb3(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c0 <wait_for_pid+0x90>
lea    0xec7(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"%d: waiting for children to die\0A\00"
@.str.1 = [51 x i8] c"%d: Unexpected child %d exited with status 0x%.4X\0A\00"
@.str.2 = [40 x i8] c"%d: Child %d exited with status 0x%.4X\0A\00"
@.str.3 = [51 x i8] c"%d: Child %d died without its death being tracked\0A\00"
define void @wait_for_pid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = call i32 (...) @getpid()
store i32 %6, i32* %5
%7 = load i32, i32* %5
%8 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %18, %1
%10 = call i32 @wait(i32* %3)
store i32 %10, i32* %4
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i32, i32* %4
%14 = load i32, i32* %2
%15 = icmp ne i32 %13, %14
br label %16
16: ; preds = %12, %9
%17 = phi i1 [ false, %9 ], [ %15, %12 ]
br i1 %17, label %18, label %23
18: ; preds = %16
%19 = load i32, i32* %5
%20 = load i32, i32* %4
%21 = load i32, i32* %3
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %20, i32 %21)
br label %9
23: ; preds = %16
%24 = load i32, i32* %4
%25 = load i32, i32* %2
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i32, i32* %5
%29 = load i32, i32* %4
%30 = load i32, i32* %3
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 %29, i32 %30)
br label %36
32: ; preds = %23
%33 = load i32, i32* %5
%34 = load i32, i32* %2
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %33, i32 %34)
br label %36
36: ; preds = %32, %27
ret void
}

LLVM IR (-O2):
@.str = [33 x i8] c"%d: waiting for children to die\0A\00"
@.str.1 = [51 x i8] c"%d: Unexpected child %d exited with status 0x%.4X\0A\00"
@.str.2 = [40 x i8] c"%d: Child %d exited with status 0x%.4X\0A\00"
@.str.3 = [51 x i8] c"%d: Child %d died without its death being tracked\0A\00"
define void @wait_for_pid(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 (...) @getpid()
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32 @wait(i32* %2)
%7 = icmp sgt i32 %6, -1
%8 = icmp ne i32 %6, %0
%9 = and i1 %7, %8
br i1 %9, label %10, label %18
10: ; preds = %1, %10
%11 = phi i32 [ %14, %10 ], [ %6, %1 ]
%12 = load i32, i32* %2
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 %11, i32 %12)
%14 = call i32 @wait(i32* %2)
%15 = icmp sgt i32 %14, -1
%16 = icmp ne i32 %14, %0
%17 = and i1 %15, %16
br i1 %17, label %10, label %18
18: ; preds = %10, %1
%19 = phi i32 [ %6, %1 ], [ %14, %10 ]
%20 = icmp eq i32 %19, %0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* %2
%23 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %4, i32 %0, i32 %22)
br label %26
24: ; preds = %18
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %4, i32 %0)
br label %26
26: ; preds = %24, %21
ret void
}