#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 */
scalar_t__ fork();
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ printf(char *, int, ...);
int main(int argc, char **argv) {
  pid_t child;
  int main = (int)getppid();
  int parent = (int)getpid();
  printf("Main process id is %d.\n\n", main);
  child = fork();
  if (child != 0) {
    printf("Hello from a parent: %d with a child %d.\n", parent, child);
  } else {
    printf("Hi, I'm a kiddo.  My id is %d.  My parent is %d.\n", (int)getpid(),
           parent);
  }
}


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   1050 <getppid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ab <main+0x6b>
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11c5 <main+0x85>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x20(%rbp),%edx
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <getppid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r14
lea    0xea1(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     118a <main+0x4a>
lea    0xe9e(%rip),%rdi
mov    %r14d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11a4 <main+0x64>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xeab(%rip),%rdi
mov    %eax,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Main process id is %d.\0A\0A\00"
@.str.1 = [42 x i8] c"Hello from a parent: %d with a child %d.\0A\00"
@.str.2 = [50 x i8] c"Hi, I'm a kiddo. My id is %d. My parent is %d.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @getppid()
%10 = trunc i64 %9 to i32
store i32 %10, i32* %7
%11 = call i64 (...) @getpid()
%12 = trunc i64 %11 to i32
store i32 %12, i32* %8
%13 = load i32, i32* %7
%14 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i64 (...) @fork()
store i64 %15, i64* %6
%16 = load i64, i64* %6
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load i32, i32* %8
%20 = load i64, i64* %6
%21 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %19, i64 %20)
br label %27
22: ; preds = %2
%23 = call i64 (...) @getpid()
%24 = trunc i64 %23 to i32
%25 = load i32, i32* %8
%26 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %25)
br label %27
27: ; preds = %22, %18
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@.str = [25 x i8] c"Main process id is %d.\0A\0A\00"
@.str.1 = [42 x i8] c"Hello from a parent: %d with a child %d.\0A\00"
@.str.2 = [50 x i8] c"Hi, I'm a kiddo. My id is %d. My parent is %d.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @getppid()
%4 = trunc i64 %3 to i32
%5 = tail call i64 (...) @getpid()
%6 = trunc i64 %5 to i32
%7 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %4)
%8 = tail call i64 (...) @fork()
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %6, i64 %8)
br label %16
12: ; preds = %2
%13 = tail call i64 (...) @getpid()
%14 = trunc i64 %13 to i32
%15 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %6)
br label %16
16: ; preds = %12, %10
ret i32 0
}