#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);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int *);
int main() {
  int status;
  if (fork()) {
    printf("parent waiting for child ...\n");
    wait(&status);
    if (WIFEXITED(status))
      printf("child ended normally, exit status = %d\n", WEXITSTATUS(status));
    if (WIFSIGNALED(status))
      printf("child terminated by signal %d\n", WTERMSIG(status));
  } else {
    printf("child running -- PID is %d\n", getpid());
    *(int *)0 = 42;
    sleep(500);
    exit(3);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%rax
je     1230 <main+0x90>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x8(%rbp),%rdi
call   10b0 <wait@plt>
mov    -0x8(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1201 <main+0x61>
mov    -0x8(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     122b <main+0x8b>
mov    -0x8(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1263 <main+0xc3>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
movl   $0x2a,(%rax)
mov    $0x1f4,%edi
call   10a0 <sleep@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1205 <main+0x75>
lea    0xebf(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   10a0 <wait@plt>
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
je     11da <main+0x4a>
mov    0x4(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
lea    0xe2f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%edi
call   1060 <WIFSIGNALED@plt>
test   %rax,%rax
je     1201 <main+0x71>
mov    0x4(%rsp),%edi
call   1070 <WTERMSIG@plt>
lea    0xe30(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe34(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"parent waiting for child ...\0A\00"
@.str.1 = [40 x i8] c"child ended normally, exit status = %d\0A\00"
@.str.2 = [31 x i8] c"child terminated by signal %d\0A\00"
@.str.3 = [28 x i8] c"child running -- PID is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i64 (...) @fork()
%4 = icmp ne i64 %3, 0
br i1 %4, label %5, label %24
5: ; preds = %0
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @wait(i32* %2)
%8 = load i32, i32* %2
%9 = call i64 @WIFEXITED(i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %5
%12 = load i32, i32* %2
%13 = call i32 @WEXITSTATUS(i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %11, %5
%16 = load i32, i32* %2
%17 = call i64 @WIFSIGNALED(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* %2
%21 = call i32 @WTERMSIG(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %19, %15
br label %29
24: ; preds = %0
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %25)
store i32 42, i32* null
%27 = call i32 @sleep(i32 500)
%28 = call i32 @exit(i32 3)
unreachable
29: ; preds = %23
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@.str.1 = [40 x i8] c"child ended normally, exit status = %d\0A\00"
@.str.2 = [31 x i8] c"child terminated by signal %d\0A\00"
@.str.3 = [28 x i8] c"child running -- PID is %d\0A\00"
@str = [29 x i8] c"parent waiting for child ...\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %23, label %5
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%7 = call i32 @wait(i32* %1)
%8 = load i32, i32* %1
%9 = call i64 @WIFEXITED(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %5
%12 = load i32, i32* %1
%13 = call i32 @WEXITSTATUS(i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %11, %5
%16 = load i32, i32* %1
%17 = call i64 @WIFSIGNALED(i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %15
%20 = load i32, i32* %1
%21 = call i32 @WTERMSIG(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %26
23: ; preds = %0
%24 = tail call i32 (...) @getpid()
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %24)
unreachable
26: ; preds = %15, %19
ret i32 0
}