#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 syscall(int, int *, int /*<<< orphan*/);
int wait() {
  int child, exitCode;
  printf("proc %d enter Kernel to wait for a child to die\n", getpid());
  child = syscall(5, &exitCode, 0);
  printf("proc %d back from wait, dead child=%d", getpid(), child);
  if (child >= 0)
    printf("exitValue=%d", exitCode);
  printf("\n");
}


x86 assembly (clang, -O0):
<wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1050 <syscall@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jl     1197 <wait+0x67>
mov    -0xc(%rbp),%esi
lea    0xec7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xead(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rsi
mov    $0x5,%edi
xor    %edx,%edx
call   1060 <syscall@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xeb4(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
js     119e <wait+0x5e>
mov    0xc(%rsp),%esi
lea    0xec0(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [49 x i8] c"proc %d enter Kernel to wait for a child to die\0A\00"
@.str.1 = [38 x i8] c"proc %d back from wait, dead child=%d\00"
@.str.2 = [13 x i8] c"exitValue=%d\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @wait() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @getpid()
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32 @syscall(i32 5, i32* %3, i32 0)
store i32 %6, i32* %2
%7 = call i32 (...) @getpid()
%8 = load i32, i32* %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %8)
%10 = load i32, i32* %2
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = load i32, i32* %3
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %12, %0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%17 = load i32, i32* %1
ret i32 %17
}

LLVM IR (-O2):
@.str = [49 x i8] c"proc %d enter Kernel to wait for a child to die\0A\00"
@.str.1 = [38 x i8] c"proc %d back from wait, dead child=%d\00"
@.str.2 = [13 x i8] c"exitValue=%d\00"
define i32 @wait() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 @syscall(i32 5, i32* %1, i32 0)
%6 = call i32 (...) @getpid()
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %5)
%8 = icmp sgt i32 %5, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* %1
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %9, %0
%13 = call i32 @putchar(i32 10)
ret i32 undef
}