#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 */

/* Variables and functions */
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ sleep(int);
int main() {
  if (fork() == 0) {
    printf("child. pid=%d ppid=%d\n", getpid(), getppid());
    sleep(3);
    printf("child. pid=%d ppid=%d\n", getpid(), getppid());
  } else {
    printf("parent. pid=%d ppid=%d\n", getpid(), getppid());
    sleep(1);
    while (1)
      ;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
jne    11c3 <main+0x73>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0xc(%rbp),%esi
mov    %eax,%edx
lea    0xe73(%rip),%rdi
call   1040 <printf@plt>
mov    $0x3,%edi
call   1060 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x8(%rbp),%esi
mov    %eax,%edx
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
jmp    11f4 <main+0xa4>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe37(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11ef <main+0x9f>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getppid@plt>
test   %rbx,%rbx
je     1192 <main+0x42>
lea    0xe9e(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
nopl   0x0(%rax)
jmp    1190 <main+0x40>
lea    0xe67(%rip),%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    $0x3,%edi
call   1060 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getppid@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"child. pid=%d ppid=%d\0A\00"
@.str.1 = [24 x i8] c"parent. pid=%d ppid=%d\0A\00"
define i32 @main() {
%1 = alloca i32
store i32 0, i32* %1
%2 = call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %12
4: ; preds = %0
%5 = call i32 (...) @getpid()
%6 = call i32 (...) @getppid()
%7 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = call i32 @sleep(i32 3)
%9 = call i32 (...) @getpid()
%10 = call i32 (...) @getppid()
%11 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
br label %18
12: ; preds = %0
%13 = call i32 (...) @getpid()
%14 = call i32 (...) @getppid()
%15 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14)
%16 = call i32 @sleep(i32 1)
br label %17
17: ; preds = %12, %17
br label %17
18: ; preds = %4
ret i32 0
}

LLVM IR (-O2):
@.str = [23 x i8] c"child. pid=%d ppid=%d\0A\00"
@.str.1 = [24 x i8] c"parent. pid=%d ppid=%d\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (...) @getppid()
br i1 %2, label %5, label %11
5: ; preds = %0
%6 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4)
%7 = tail call i32 @sleep(i32 3)
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (...) @getppid()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %9)
ret i32 0
11: ; preds = %0
%12 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %3, i32 %4)
%13 = tail call i32 @sleep(i32 1)
br label %14
14: ; preds = %11, %14
br label %14
}