#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();
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(void) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    perror("fork");
  } else if (pid == 0) {
    if ((pid = fork()) < 0) {
      perror("fork");
    } else if (pid > 0) {
      exit(0);
    }
    sleep(2);
    printf("second child, parent pid = %ld\n", (long)getpid());
    exit(0);
  }
  if (waitpid(pid, NULL, 0) != pid) {
    perror("waitpid");
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11a5 <main+0x35>
lea    0xe65(%rip),%rdi
call   1060 <perror@plt>
jmp    1219 <main+0xa9>
cmpq   $0x0,-0x10(%rbp)
jne    1214 <main+0xa4>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11d6 <main+0x66>
lea    0xe34(%rip),%rdi
call   1060 <perror@plt>
jmp    11ed <main+0x7d>
cmpq   $0x0,-0x10(%rbp)
jle    11e8 <main+0x78>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    11ed <main+0x7d>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
lea    0xdfd(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1219 <main+0xa9>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    -0x10(%rbp),%rax
je     123e <main+0xce>
lea    0xdec(%rip),%rdi
call   1060 <perror@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11b0 <main+0x40>
lea    0xe79(%rip),%rdi
call   1060 <perror@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    %rbx,%rax
je     11a9 <main+0x39>
lea    0xe81(%rip),%rdi
call   1060 <perror@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jne    118c <main+0x1c>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jns    11f1 <main+0x81>
lea    0xe3b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe23(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jne    11a9 <main+0x39>
jmp    11ca <main+0x5a>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [32 x i8] c"second child, parent pid = %ld\0A\00"
@.str.2 = [8 x i8] c"waitpid\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i64 (...) @fork()
store i64 %3, i64* %2
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %27
7: ; preds = %0
%8 = load i64, i64* %2
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %26
10: ; preds = %7
%11 = call i64 (...) @fork()
store i64 %11, i64* %2
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %21
15: ; preds = %10
%16 = load i64, i64* %2
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %15
br label %21
21: ; preds = %20, %13
%22 = call i32 @sleep(i32 2)
%23 = call i64 (...) @getpid()
%24 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i64 %23)
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %7
br label %27
27: ; preds = %26, %5
%28 = load i64, i64* %2
%29 = call i64 @waitpid(i64 %28, i32* null, i32 0)
%30 = load i64, i64* %2
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = call i32 @exit(i32 0)
unreachable
}

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