#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 scalar_t__ pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
void test_fork() {
  pid_t pid = fork();
  assert(pid >= 0);
  if (pid == 0) {
    printf("all good in the hood\n");
    exit(42);
  } else {
    int status = 0;
    waitpid(pid, &status, 0);
    assert(WEXITSTATUS(status) == 42);
    printf("splines reticulated\n");
  }
}


x86 assembly (clang, -O0):
<test_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpq   $0x0,-0x8(%rbp)
jne    11a6 <test_fork+0x46>
lea    0xe69(%rip),%rdi
call   1030 <printf@plt>
mov    $0x2a,%edi
call   1070 <exit@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
cmp    $0x2a,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
lea    0xe3b(%rip),%rdi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fork>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
mov    %rax,%rdi
shr    $0x3f,%rdi
xor    $0x1,%edi
call   1040 <assert@plt>
test   %rbx,%rbx
je     11c3 <test_fork+0x63>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
xor    %edi,%edi
cmp    $0x2a,%eax
sete   %dil
call   1040 <assert@plt>
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe4a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2a,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"all good in the hood\0A\00"
@.str.1 = [21 x i8] c"splines reticulated\0A\00"
define void @test_fork() {
%1 = alloca i64
%2 = alloca i32
%3 = call i64 (...) @fork()
store i64 %3, i64* %1
%4 = load i64, i64* %1
%5 = icmp sge i64 %4, 0
%6 = zext i1 %5 to i32
%7 = call i32 @assert(i32 %6)
%8 = load i64, i64* %1
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 42)
unreachable
13: ; preds = %0
store i32 0, i32* %2
%14 = load i64, i64* %1
%15 = call i32 @waitpid(i64 %14, i32* %2, i32 0)
%16 = load i32, i32* %2
%17 = call i32 @WEXITSTATUS(i32 %16)
%18 = icmp eq i32 %17, 42
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %13
ret void
}

LLVM IR (-O2):
@str = [20 x i8] c"splines reticulated\00"
@str.2 = [21 x i8] c"all good in the hood\00"
define void @test_fork() {
%1 = alloca i32
%2 = tail call i64 (...) @fork()
%3 = lshr i64 %2, 63
%4 = trunc i64 %3 to i32
%5 = xor i32 %4, 1
%6 = tail call i32 @assert(i32 %5)
%7 = icmp eq i64 %2, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.2, i64 0, i64 0))
%10 = tail call i32 @exit(i32 42)
unreachable
11: ; preds = %0
%12 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%13 = call i32 @waitpid(i64 %2, i32* %1, i32 0)
%14 = load i32, i32* %1
%15 = call i32 @WEXITSTATUS(i32 %14)
%16 = icmp eq i32 %15, 42
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
ret void
}