#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 /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  pid_t pid;
  printf("\nThis is the parent, about to fork. pid=%d\n", getpid());
  fflush(stdout);
  pid = fork();
  if (pid > 0) {
    printf("\nThis is the parent, after the fork. Child pid=%d\n", pid);
    sleep(10);
    printf("\nThis is the parent waking up and exiting\n");
    exit(EXIT_SUCCESS);
  }
  if (pid == 0) {
    printf("\nThis is the child, after the fork. pid=%d\n", getpid());
    exit(EXIT_SUCCESS);
  }
  {
    perror("fork");
    exit(errno);
  };
}


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   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    11f9 <main+0x89>
mov    -0x18(%rbp),%rsi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1229 <main+0xb9>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe86(%rip),%rdi
call   1060 <perror@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe71(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jle    11db <main+0x5b>
lea    0xe73(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
lea    0xebc(%rip),%rdi
call   1030 <puts@plt>
jmp    11f4 <main+0x74>
jne    1202 <main+0x82>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe74(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe82(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [44 x i8] c"\0AThis is the parent, about to fork. pid=%d\0A\00"
@stdout = global i32 0
@.str.1 = [51 x i8] c"\0AThis is the parent, after the fork. Child pid=%d\0A\00"
@.str.2 = [43 x i8] c"\0AThis is the parent waking up and exiting\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [44 x i8] c"\0AThis is the child, after the fork. pid=%d\0A\00"
@.str.4 = [5 x i8] c"fork\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @getpid()
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @stdout
%10 = call i32 @fflush(i32 %9)
%11 = call i64 (...) @fork()
store i64 %11, i64* %6
%12 = load i64, i64* %6
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i64, i64* %6
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i64 %15)
%17 = call i32 @sleep(i32 10)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EXIT_SUCCESS
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i64, i64* %6
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = load i32, i32* @EXIT_SUCCESS
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%31 = load i32, i32* @errno
%32 = call i32 @exit(i32 %31)
unreachable
}

LLVM IR (-O2):
@.str = [44 x i8] c"\0AThis is the parent, about to fork. pid=%d\0A\00"
@stdout = global i32 0
@.str.1 = [51 x i8] c"\0AThis is the parent, after the fork. Child pid=%d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [44 x i8] c"\0AThis is the child, after the fork. pid=%d\0A\00"
@.str.4 = [5 x i8] c"fork\00"
@errno = global i32 0
@str = [42 x i8] c"\0AThis is the parent waking up and exiting\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @stdout
%6 = tail call i32 @fflush(i32 %5)
%7 = tail call i64 (...) @fork()
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i64 %7)
%11 = tail call i32 @sleep(i32 10)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_SUCCESS
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = icmp eq i64 %7, 0
br i1 %16, label %17, label %22
17: ; preds = %15
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @EXIT_SUCCESS
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %15
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%24 = load i32, i32* @errno
%25 = tail call i32 @exit(i32 %24)
unreachable
}