#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 WEXITSTATUS(int);
int WIFEXITED(int);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int wait(int *);
void fork_test_wait() {
  int n = fork();
  if (n == 0) {
    printf("This is child, my pid is %d!\n", getpid());
    exit(42);
  } else {
    printf("This is parent, my pid is %d, I was told child is %d!\n", getpid(),
           n);
    int status, exitcode;
    int p = wait(&status);
    assert(WIFEXITED(status));
    exitcode = WEXITSTATUS(status);
    printf("Child exit status is %d, exit code %d.\n", status, exitcode);
    assert(exitcode == 42);
    assert(p == n);
  }
}


x86 assembly (clang, -O0):
<fork_test_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    11bd <fork_test_wait+0x3d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2a,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x4(%rbp),%edx
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x8(%rbp),%rdi
call   1090 <wait@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <WIFEXITED@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x2a,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%eax
cmp    -0x4(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_test_wait>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
test   %ebx,%ebx
je     120f <fork_test_wait+0x8f>
lea    0xe7b(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
mov    %eax,%r14d
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%esi
lea    0xe74(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
cmp    $0x2a,%ebp
sete   %dil
call   1050 <assert@plt>
xor    %edi,%edi
cmp    %ebx,%r14d
sete   %dil
call   1050 <assert@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdea(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2a,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"This is child, my pid is %d!\0A\00"
@.str.1 = [55 x i8] c"This is parent, my pid is %d, I was told child is %d!\0A\00"
@.str.2 = [40 x i8] c"Child exit status is %d, exit code %d.\0A\00"
define void @fork_test_wait() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i32 (...) @fork()
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 (...) @getpid()
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 @exit(i32 42)
unreachable
12: ; preds = %0
%13 = call i32 (...) @getpid()
%14 = load i32, i32* %1
%15 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14)
%16 = call i32 @wait(i32* %2)
store i32 %16, i32* %4
%17 = load i32, i32* %2
%18 = call i32 @WIFEXITED(i32 %17)
%19 = call i32 @assert(i32 %18)
%20 = load i32, i32* %2
%21 = call i32 @WEXITSTATUS(i32 %20)
store i32 %21, i32* %3
%22 = load i32, i32* %2
%23 = load i32, i32* %3
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %23)
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, 42
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load i32, i32* %4
%30 = load i32, i32* %1
%31 = icmp eq i32 %29, %30
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
br label %34
34: ; preds = %12
ret void
}

LLVM IR (-O2):
@.str = [30 x i8] c"This is child, my pid is %d!\0A\00"
@.str.1 = [55 x i8] c"This is parent, my pid is %d, I was told child is %d!\0A\00"
@.str.2 = [40 x i8] c"Child exit status is %d, exit code %d.\0A\00"
define void @fork_test_wait() {
%1 = alloca i32
%2 = tail call i32 (...) @fork()
%3 = icmp eq i32 %2, 0
%4 = tail call i32 (...) @getpid()
br i1 %3, label %5, label %8
5: ; preds = %0
%6 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %4)
%7 = tail call i32 @exit(i32 42)
unreachable
8: ; preds = %0
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 %2)
%10 = bitcast i32* %1 to i8*
%11 = call i32 @wait(i32* %1)
%12 = load i32, i32* %1
%13 = call i32 @WIFEXITED(i32 %12)
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* %1
%16 = call i32 @WEXITSTATUS(i32 %15)
%17 = load i32, i32* %1
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %16)
%19 = icmp eq i32 %16, 42
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = icmp eq i32 %11, %2
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
ret void
}