#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();
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ printf(char *, int);
int main() {
  pid_t child_pid;
  printf("The main program has the PID %d.\n", (int)getpid());
  child_pid = fork();
  if (child_pid != 0) {
    printf("This is the parent process with PID %d.\n", (int)getpid());
    printf("The child process has the PID %d.\n", child_pid);
  } else {
    printf("This is the child process with PID %d.\n", (int)getpid());
    printf("Its parent process is with PID %d.\n", (int)getppid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xea1(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11a1 <main+0x61>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe9a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xeb4(%rip),%rdi
call   1040 <printf@plt>
jmp    11cb <main+0x8b>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xebd(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getppid@plt>
mov    %eax,%esi
lea    0xed0(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xeb1(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
test   %ebx,%ebx
je     1183 <main+0x43>
lea    0xeb1(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xecc(%rip),%rdi
mov    %ebx,%esi
jmp    11a1 <main+0x61>
lea    0xee4(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xef7(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [34 x i8] c"The main program has the PID %d.\0A\00"
@.str.1 = [41 x i8] c"This is the parent process with PID %d.\0A\00"
@.str.2 = [35 x i8] c"The child process has the PID %d.\0A\00"
@.str.3 = [40 x i8] c"This is the child process with PID %d.\0A\00"
@.str.4 = [36 x i8] c"Its parent process is with PID %d.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i64 (...) @getpid()
%4 = trunc i64 %3 to i32
%5 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32 (...) @fork()
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = call i64 (...) @getpid()
%11 = trunc i64 %10 to i32
%12 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %2
%14 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %13)
br label %22
15: ; preds = %0
%16 = call i64 (...) @getpid()
%17 = trunc i64 %16 to i32
%18 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = call i64 (...) @getppid()
%20 = trunc i64 %19 to i32
%21 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %15, %9
ret i32 0
}

LLVM IR (-O2):
@.str = [34 x i8] c"The main program has the PID %d.\0A\00"
@.str.1 = [41 x i8] c"This is the parent process with PID %d.\0A\00"
@.str.2 = [35 x i8] c"The child process has the PID %d.\0A\00"
@.str.3 = [40 x i8] c"This is the child process with PID %d.\0A\00"
@.str.4 = [36 x i8] c"Its parent process is with PID %d.\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @getpid()
%2 = trunc i64 %1 to i32
%3 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = tail call i32 (...) @fork()
%5 = icmp eq i32 %4, 0
%6 = tail call i64 (...) @getpid()
%7 = trunc i64 %6 to i32
br i1 %5, label %11, label %8
8: ; preds = %0
%9 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %4)
br label %16
11: ; preds = %0
%12 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %7)
%13 = tail call i64 (...) @getppid()
%14 = trunc i64 %13 to i32
%15 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %11, %8
ret i32 0
}