#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (pid == 0) {
    printf("child process sleep for 3 seconds...\n");
    sleep(3);
    printf("child process exit.\n");
  } else {
    printf("parent process waiting for child to exit...\n");
    wait(0);
    printf("parent process exit\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a4 <main+0x44>
lea    0xe6f(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11d5 <main+0x75>
lea    0xe50(%rip),%rdi
call   1030 <printf@plt>
mov    $0x3,%edi
call   1060 <sleep@plt>
lea    0xe60(%rip),%rdi
call   1030 <printf@plt>
jmp    11f4 <main+0x94>
lea    0xe64(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <wait@plt>
lea    0xe7e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     118d <main+0x2d>
cmp    $0xffffffff,%eax
je     11b3 <main+0x53>
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <wait@plt>
lea    0xea6(%rip),%rdi
jmp    11aa <main+0x4a>
lea    0xeb1(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   1060 <sleep@plt>
lea    0xec0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe46(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [38 x i8] c"child process sleep for 3 seconds...\0A\00"
@.str.2 = [21 x i8] c"child process exit.\0A\00"
@.str.3 = [45 x i8] c"parent process waiting for child to exit...\0A\00"
@.str.4 = [21 x i8] c"parent process exit\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @fork()
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @sleep(i32 3)
%20 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %25
21: ; preds = %14
%22 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @wait(i32 0)
%24 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %25
25: ; preds = %21, %17
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@str = [44 x i8] c"parent process waiting for child to exit...\00"
@str.5 = [20 x i8] c"parent process exit\00"
@str.6 = [37 x i8] c"child process sleep for 3 seconds...\00"
@str.7 = [20 x i8] c"child process exit.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %11 [
i32 -1, label %4
i32 0, label %8
]
4: ; preds = %2
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.6, i64 0, i64 0))
%10 = tail call i32 @sleep(i32 3)
br label %14
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @wait(i32 0)
br label %14
14: ; preds = %11, %8
%15 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0), %11 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0), %8 ]
%16 = tail call i32 @puts(i8* dereferenceable(1) %15)
ret i32 0
}