#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 */
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
int main() {
  pid_t child = fork();
  if (child == -1)
    printf("Child forking failed\n");
  else if (child == 0) {
    printf("In the child\n");
    printf("Exiting the child\n");
  } else {
    sleep(10);
    int child_status;
    child = wait(&child_status);
    printf("Child %d exited with code %d\n", child, child_status);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1176 <main+0x36>
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d0 <main+0x90>
cmpl   $0x0,-0x8(%rbp)
jne    11a1 <main+0x61>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11cb <main+0x8b>
mov    $0xa,%edi
call   1040 <sleep@plt>
lea    -0xc(%rbp),%rdi
call   1050 <wait@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d0 <main+0x90>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     116f <main+0x1f>
cmp    $0xffffffff,%eax
jne    1189 <main+0x39>
lea    0xed5(%rip),%rdi
call   1030 <puts@plt>
jmp    11b1 <main+0x61>
lea    0xea8(%rip),%rdi
call   1030 <puts@plt>
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
jmp    11b1 <main+0x61>
mov    $0xa,%edi
call   1050 <sleep@plt>
lea    0x4(%rsp),%rdi
call   1060 <wait@plt>
mov    0x4(%rsp),%edx
lea    0xe58(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [22 x i8] c"Child forking failed\0A\00"
@.str.1 = [14 x i8] c"In the child\0A\00"
@.str.2 = [19 x i8] c"Exiting the child\0A\00"
@.str.3 = [30 x i8] c"Child %d exited with code %d\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 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %22
9: ; preds = %0
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %21
15: ; preds = %9
%16 = call i32 @sleep(i32 10)
%17 = call i32 @wait(i32* %3)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = load i32, i32* %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %18, i32 %19)
br label %21
21: ; preds = %15, %12
br label %22
22: ; preds = %21, %7
ret i32 0
}

LLVM IR (-O2):
@.str.3 = [30 x i8] c"Child %d exited with code %d\0A\00"
@str = [13 x i8] c"In the child\00"
@str.4 = [18 x i8] c"Exiting the child\00"
@str.5 = [21 x i8] c"Child forking failed\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @fork()
switch i32 %2, label %8 [
i32 -1, label %3
i32 0, label %5
]
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.5, i64 0, i64 0))
br label %14
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0))
br label %14
8: ; preds = %0
%9 = tail call i32 @sleep(i32 10)
%10 = bitcast i32* %1 to i8*
%11 = call i32 @wait(i32* %1)
%12 = load i32, i32* %1
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %11, i32 %12)
br label %14
14: ; preds = %5, %8, %3
ret i32 0
}