#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*/ err_sys(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getppid();
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) {
    err_sys("error on fork");
  } else if (pid == 0) {
    if ((pid = fork()) < 0) {
      err_sys("error on fork");
    } else if (pid > 0) {
      exit(0);
    }
    sleep(2);
    printf("second child, parent pid = %d\n", getppid());
    exit(0);
  }
  if (waitpid(pid, NULL, 0) != pid) {
    err_sys("waitpid error");
  }
  return 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   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11ac <main+0x3c>
lea    0xe5e(%rip),%rdi
call   1030 <err_sys@plt>
jmp    121f <main+0xaf>
cmpq   $0x0,-0x18(%rbp)
jne    121a <main+0xaa>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11dd <main+0x6d>
lea    0xe2d(%rip),%rdi
call   1030 <err_sys@plt>
jmp    11f4 <main+0x84>
cmpq   $0x0,-0x18(%rbp)
jle    11ef <main+0x7f>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    11f4 <main+0x84>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,%esi
lea    0xe00(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    121f <main+0xaf>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    -0x18(%rbp),%rax
je     1244 <main+0xd4>
lea    0xdee(%rip),%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
js     119c <main+0x2c>
jne    11a8 <main+0x38>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jns    11c9 <main+0x59>
lea    0xe6b(%rip),%rdi
call   1030 <err_sys@plt>
jmp    11cb <main+0x5b>
lea    0xe5d(%rip),%rdi
call   1030 <err_sys@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    %rbx,%rax
je     11c5 <main+0x55>
lea    0xe6d(%rip),%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
pop    %rbx
ret
jne    11ea <main+0x7a>
mov    $0x2,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe2b(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"error on fork\00"
@.str.1 = [31 x i8] c"second child, parent pid = %d\0A\00"
@.str.2 = [14 x i8] c"waitpid error\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 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %31
11: ; preds = %2
%12 = load i64, i64* %6
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %30
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 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %25
19: ; preds = %14
%20 = load i64, i64* %6
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %19
br label %25
25: ; preds = %24, %17
%26 = call i32 @sleep(i32 2)
%27 = call i32 (...) @getppid()
%28 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %11
br label %31
31: ; preds = %30, %9
%32 = load i64, i64* %6
%33 = call i64 @waitpid(i64 %32, i32* null, i32 0)
%34 = load i64, i64* %6
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %31
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"error on fork\00"
@.str.1 = [31 x i8] c"second child, parent pid = %d\0A\00"
@.str.2 = [14 x i8] c"waitpid error\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 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %23
7: ; preds = %2
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %23
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 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %18
14: ; preds = %9
%15 = icmp eq i64 %10, 0
br i1 %15, label %18, label %16
16: ; preds = %14
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %14, %12
%19 = tail call i32 @sleep(i32 2)
%20 = tail call i32 (...) @getppid()
%21 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = tail call i32 @exit(i32 0)
unreachable
23: ; preds = %7, %5
%24 = tail call i64 @waitpid(i64 %3, i32* null, i32 0)
%25 = icmp eq i64 %24, %3
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = tail call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
ret i32 0
}