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

/* Variables and functions */
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int *);
int main(void) {
  pid_t pid = fork();
  if (pid == -1)
    exit(-1);
  if (pid > 0) {
    printf("[parent]: I'm the parent, the pid of my child is %i\n"
           "I'll start waiting for it in 10 seconds.\n",
           pid);
    sleep(10);
    int status;
    wait(&status);
    if (WIFSIGNALED(status)) {
      printf("[parent]: My child has died from a signal: %i\n",
             WTERMSIG(status));
    } else if (WIFEXITED(status)) {
      printf("[parent]: My child has died from natural death\n");
    } else {
      printf("[parent]: I don't know what happened to my child\n");
    }
  } else {
    printf("[child]: I'm dying soon, try to kill me.\n");
    sleep(5);
    printf("[child]: Dying now!\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ad <main+0x2d>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jle    1247 <main+0xc7>
mov    -0x8(%rbp),%esi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
lea    -0xc(%rbp),%rdi
call   1090 <wait@plt>
mov    -0xc(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     120a <main+0x8a>
mov    -0xc(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1242 <main+0xc2>
mov    -0xc(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     122f <main+0xaf>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123d <main+0xbd>
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1242 <main+0xc2>
jmp    126d <main+0xed>
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     1234 <main+0xa4>
test   %eax,%eax
jle    11f2 <main+0x62>
lea    0xe54(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
lea    0x4(%rsp),%rdi
call   10a0 <wait@plt>
mov    0x4(%rsp),%edi
call   1050 <WIFSIGNALED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     1211 <main+0x81>
call   1060 <WTERMSIG@plt>
lea    0xe77(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1230 <main+0xa0>
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
lea    0xea7(%rip),%rdi
jmp    122b <main+0x9b>
call   1070 <WIFEXITED@plt>
test   %rax,%rax
je     1224 <main+0x94>
lea    0xed9(%rip),%rdi
jmp    122b <main+0x9b>
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [94 x i8] c"[parent]: I'm the parent, the pid of my child is %i\0AI'll start waiting for it in 10 seconds.\0A\00"
@.str.1 = [47 x i8] c"[parent]: My child has died from a signal: %i\0A\00"
@.str.2 = [48 x i8] c"[parent]: My child has died from natural death\0A\00"
@.str.3 = [50 x i8] c"[parent]: I don't know what happened to my child\0A\00"
@.str.4 = [42 x i8] c"[child]: I'm dying soon, try to kill me.\0A\00"
@.str.5 = [21 x i8] c"[child]: Dying now!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = load i32, i32* %2
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %34
12: ; preds = %9
%13 = load i32, i32* %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @sleep(i32 10)
%16 = call i32 @wait(i32* %3)
%17 = load i32, i32* %3
%18 = call i64 @WIFSIGNALED(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %12
%21 = load i32, i32* %3
%22 = call i32 @WTERMSIG(i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %33
24: ; preds = %12
%25 = load i32, i32* %3
%26 = call i64 @WIFEXITED(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
br label %32
30: ; preds = %24
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %28
br label %33
33: ; preds = %32, %20
br label %38
34: ; preds = %9
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @sleep(i32 5)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %38
38: ; preds = %34, %33
ret i32 0
}

LLVM IR (-O2):
@.str = [94 x i8] c"[parent]: I'm the parent, the pid of my child is %i\0AI'll start waiting for it in 10 seconds.\0A\00"
@.str.1 = [47 x i8] c"[parent]: My child has died from a signal: %i\0A\00"
@str = [41 x i8] c"[child]: I'm dying soon, try to kill me.\00"
@str.6 = [20 x i8] c"[child]: Dying now!\00"
@str.7 = [49 x i8] c"[parent]: I don't know what happened to my child\00"
@str.8 = [47 x i8] c"[parent]: My child has died from natural death\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @fork()
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @exit(i32 -1)
unreachable
6: ; preds = %0
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %28
8: ; preds = %6
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([94 x i8], [94 x i8]* @.str, i64 0, i64 0), i32 %2)
%10 = tail call i32 @sleep(i32 10)
%11 = bitcast i32* %1 to i8*
%12 = call i32 @wait(i32* %1)
%13 = load i32, i32* %1
%14 = call i64 @WIFSIGNALED(i32 %13)
%15 = icmp eq i64 %14, 0
%16 = load i32, i32* %1
br i1 %15, label %20, label %17
17: ; preds = %8
%18 = call i32 @WTERMSIG(i32 %16)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %27
20: ; preds = %8
%21 = call i64 @WIFEXITED(i32 %16)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.8, i64 0, i64 0))
br label %27
25: ; preds = %20
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.7, i64 0, i64 0))
br label %27
27: ; preds = %23, %25, %17
br label %32
28: ; preds = %6
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%30 = tail call i32 @sleep(i32 5)
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
br label %32
32: ; preds = %28, %27
ret i32 0
}