int wait() {
  int child, exitValue;
  printf("proc %d enter Kernel to wait for a child to die (addr. %x)\n",
         getpid(), &exitValue);
  child = syscall(5, &exitValue, 0);
  printf("proc %d back from wait, dead child=%d", getpid(), child);
  if (child >= 0) {
    printf("exitValue=%d", exitValue);
  }
  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
lea    -0xc(%rbp),%rdx
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
mov    $0x0,%al
call   1050 <syscall@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jl     119d <wait+0x6d>
mov    -0xc(%rbp),%esi
lea    0xecc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe96(%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
lea    0xc(%rsp),%rbx
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1060 <syscall@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xeb7(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
js     11a6 <wait+0x66>
mov    0xc(%rsp),%esi
lea    0xec3(%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 = [60 x i8] c"proc %d enter Kernel to wait for a child to die (addr. %x)\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 ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %4, i32* %3)
%6 = call i32 (i32, i32*, i32, ...) bitcast (i32 (...)* @syscall to i32 (i32, i32*, i32, ...)*)(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 = [60 x i8] c"proc %d enter Kernel to wait for a child to die (addr. %x)\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 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %3, i32* %1)
%5 = call i32 (i32, i32*, i32, ...) bitcast (i32 (...)* @syscall to i32 (i32, i32*, i32, ...)*)(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
}