#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int getpid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int main() {
  pid_t pid1;
  if ((pid1 = fork()) == 0) {
    printf("child[%d]\n", getpid());
    if ((pid1 = fork()) == 0) {
      printf("child[%d]\n", getpid());
      sleep(20);
      exit(0);
    }
    exit(0);
  }
  waitpid(pid1, 0, 0);
  printf("parent[%d]\n", getpid());
  sleep(60);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jne    11d7 <main+0x77>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jne    11d0 <main+0x70>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe46(%rip),%rdi
call   1040 <printf@plt>
mov    $0x14,%edi
call   1070 <sleep@plt>
xor    %edi,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <waitpid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe18(%rip),%rdi
call   1040 <printf@plt>
mov    $0x3c,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     119a <main+0x3a>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe86(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0x3c,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe58(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11d9 <main+0x79>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe38(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0x14,%edi
call   1070 <sleep@plt>
xor    %edi,%edi
call   1060 <exit@plt>

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

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