#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();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    perror("fork error\n");
  } else if (pid == 0) {
    if ((pid = fork()) < 0) {
      perror("fork error\n");
    } else if (pid > 0) {
      printf("first child, parent pid = %d\n", getppid());
      exit(0);
    }
    sleep(2);
    printf("second child, parent pid = %d\n", getppid());
    exit(0);
  }
  if (waitpid(pid, NULL, 0) != pid) {
    perror("wait error\n");
  }
  printf("main exit, pid = %d\n", getpid());
  exit(0);
}


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   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11bc <main+0x3c>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
jmp    1244 <main+0xc4>
cmpq   $0x0,-0x18(%rbp)
jne    123f <main+0xbf>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11ed <main+0x6d>
lea    0xe1d(%rip),%rdi
call   1060 <perror@plt>
jmp    1219 <main+0x99>
cmpq   $0x0,-0x18(%rbp)
jle    1214 <main+0x94>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,%esi
lea    0xe04(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1219 <main+0x99>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,%esi
lea    0xdf7(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1244 <main+0xc4>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    -0x18(%rbp),%rax
je     1269 <main+0xe9>
lea    0xde5(%rip),%rdi
call   1060 <perror@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xddc(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11c9 <main+0x49>
lea    0xe69(%rip),%rdi
call   1060 <perror@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    %rbx,%rax
je     11b9 <main+0x39>
lea    0xe95(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8e(%rip),%rdi
jmp    11fb <main+0x7b>
jne    119c <main+0x1c>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    1209 <main+0x89>
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe2f(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
je     11e3 <main+0x63>
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xdf3(%rip),%rdi
jmp    11fb <main+0x7b>

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

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