#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 */
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int fork();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main() {
  int pid;
  if ((pid = fork()) < 0)
    printf("fork error\n");
  else if (pid == 0) {
    if ((pid = fork()) < 0)
      printf("fork error\n");
    else if (pid > 0)
      _exit(0);
    sleep(2);
    printf("second child,parent pid:%d\n", getppid());
    _exit(0);
  }
  _exit(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>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1185 <main+0x35>
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f8 <main+0xa8>
cmpl   $0x0,-0x8(%rbp)
jne    11f3 <main+0xa3>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11b5 <main+0x65>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11cb <main+0x7b>
cmpl   $0x0,-0x8(%rbp)
jle    11c6 <main+0x76>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    11cb <main+0x7b>
mov    $0x2,%edi
call   1060 <sleep@plt>
mov    $0x0,%al
call   1050 <getppid@plt>
mov    %eax,%esi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    11f8 <main+0xa8>
xor    %edi,%edi
call   1030 <_exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jns    117f <main+0x1f>
lea    0xea9(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
jne    11bd <main+0x5d>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jns    119a <main+0x3a>
lea    0xe89(%rip),%rdi
call   1040 <puts@plt>
jmp    119c <main+0x3c>
jne    11bd <main+0x5d>
mov    $0x2,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe4c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"fork error\0A\00"
@.str.1 = [28 x i8] c"second child,parent pid:%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 slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %27
7: ; preds = %0
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %26
10: ; preds = %7
%11 = call i32 (...) @fork()
store i32 %11, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %21
15: ; preds = %10
%16 = load i32, i32* %2
%17 = icmp sgt i32 %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 i32 (...) @getppid()
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = call i32 @_exit(i32 0)
unreachable
26: ; preds = %7
br label %27
27: ; preds = %26, %5
%28 = call i32 @_exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [28 x i8] c"second child,parent pid:%d\0A\00"
@str.2 = [11 x i8] c"fork error\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.2, i64 0, i64 0))
br label %21
5: ; preds = %0
%6 = icmp eq i32 %1, 0
br i1 %6, label %7, label %21
7: ; preds = %5
%8 = tail call i32 (...) @fork()
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.2, i64 0, i64 0))
br label %16
12: ; preds = %7
%13 = icmp eq i32 %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 i32 (...) @getppid()
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = tail call i32 @_exit(i32 0)
unreachable
21: ; preds = %5, %3
%22 = tail call i32 @_exit(i32 0)
unreachable
}