#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(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int getpid();
int getppid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int main(void) {
  pid_t p;
  fprintf(stderr, "[%d] Parent process.\n", getpid());
  p = fork();
  if (p == 0) {
    fprintf(stderr, "[%d] Child process. My parent is %d.\n", getpid(),
            getppid());
    sleep(10);
    fprintf(stderr, "[%d] I will terminate...\n", getpid());
    exit(0);
  } else {
    fprintf(stderr, "[%d] Waiting for the termination of my child %d.\n",
            getpid(), p);
    waitpid(p, NULL, 0);
    fprintf(stderr, "[%d] My child %d has exited.\n", getpid(), p);
    fprintf(stderr, "[%d] I will terminate...\n", getpid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
lea    0xe62(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1225 <main+0xb5>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
lea    0xe0e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xd95(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e62(%rip),%r15
mov    (%r15),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe71(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%rbx
mov    (%r15),%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
test   %rbx,%rbx
je     121f <main+0xaf>
lea    0xe9a(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    (%r15),%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xea0(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r15),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe35(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xde9(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
mov    (%r15),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xde6(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"[%d] Parent process.\0A\00"
@.str.1 = [38 x i8] c"[%d] Child process. My parent is %d.\0A\00"
@.str.2 = [26 x i8] c"[%d] I will terminate...\0A\00"
@.str.3 = [50 x i8] c"[%d] Waiting for the termination of my child %d.\0A\00"
@.str.4 = [30 x i8] c"[%d] My child %d has exited.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = load i32, i32* @stderr
%4 = call i32 (...) @getpid()
%5 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %3, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i64 (...) @fork()
store i64 %6, i64* %2
%7 = load i64, i64* %2
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 (...) @getpid()
%12 = call i32 (...) @getppid()
%13 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %12)
%14 = call i32 @sleep(i32 10)
%15 = load i32, i32* @stderr
%16 = call i32 (...) @getpid()
%17 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %0
%20 = load i32, i32* @stderr
%21 = call i32 (...) @getpid()
%22 = load i64, i64* %2
%23 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %21, i64 %22)
%24 = load i64, i64* %2
%25 = call i32 @waitpid(i64 %24, i32* null, i32 0)
%26 = load i32, i32* @stderr
%27 = call i32 (...) @getpid()
%28 = load i64, i64* %2
%29 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %27, i64 %28)
%30 = load i32, i32* @stderr
%31 = call i32 (...) @getpid()
%32 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %19
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"[%d] Parent process.\0A\00"
@.str.1 = [38 x i8] c"[%d] Child process. My parent is %d.\0A\00"
@.str.2 = [26 x i8] c"[%d] I will terminate...\0A\00"
@.str.3 = [50 x i8] c"[%d] Waiting for the termination of my child %d.\0A\00"
@.str.4 = [30 x i8] c"[%d] My child %d has exited.\0A\00"
define i32 @main() {
%1 = load i32, i32* @stderr
%2 = tail call i32 (...) @getpid()
%3 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
%6 = load i32, i32* @stderr
%7 = tail call i32 (...) @getpid()
br i1 %5, label %8, label %16
8: ; preds = %0
%9 = tail call i32 (...) @getppid()
%10 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %9)
%11 = tail call i32 @sleep(i32 10)
%12 = load i32, i32* @stderr
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %0
%17 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %7, i64 %4)
%18 = tail call i32 @waitpid(i64 %4, i32* null, i32 0)
%19 = load i32, i32* @stderr
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %20, i64 %4)
%22 = load i32, i32* @stderr
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %23)
ret i32 0
}