#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
scalar_t__ getppid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t pid;
  printf("this is parent, pid = %d\n", getpid());
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error");
  } else if (pid == 0) {
    if ((pid = fork()) < 0)
      fprintf(stderr, "fork error");
    else if (pid > 0) {
      printf("this is first child(%d) exiting\n", getpid());
      exit(0);
    }
    sleep(2);
    printf("second child(%ld), parent pid = %ld\n", (long)getpid(),
           (long)getppid());
    exit(0);
  }
  printf("this is parent(%d) waiting for child %d\n", getpid(), pid);
  if (waitpid(pid, NULL, 0) != pid)
    fprintf(stderr, "waitpid error");
  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   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11da <main+0x5a>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1050 <fprintf@plt>
jmp    127b <main+0xfb>
cmpl   $0x0,-0x14(%rbp)
jne    1276 <main+0xf6>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1211 <main+0x91>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
call   1050 <fprintf@plt>
jmp    123e <main+0xbe>
cmpl   $0x0,-0x14(%rbp)
jle    1239 <main+0xb9>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    123e <main+0xbe>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
cltq
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    %rax,%rdx
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    127b <main+0xfb>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    -0x14(%rbp),%eax
je     12c1 <main+0x141>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe71(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11bc <main+0x3c>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11e0 <main+0x60>
jne    11e0 <main+0x60>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jns    1224 <main+0xa4>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1226 <main+0xa6>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe7d(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %ebx,%eax
je     121d <main+0x9d>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jne    125a <main+0xda>
mov    $0x2,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xdff(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdbd(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"this is parent, pid = %d\0A\00"
@stderr = global i32 0
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [33 x i8] c"this is first child(%d) exiting\0A\00"
@.str.3 = [37 x i8] c"second child(%ld), parent pid = %ld\0A\00"
@.str.4 = [41 x i8] c"this is parent(%d) waiting for child %d\0A\00"
@.str.5 = [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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @getpid()
%8 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %40
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %39
17: ; preds = %14
%18 = call i32 (...) @fork()
store i32 %18, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %31
23: ; preds = %17
%24 = load i32, i32* %6
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = call i32 (...) @getpid()
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %23
br label %31
31: ; preds = %30, %20
%32 = call i32 @sleep(i32 2)
%33 = call i32 (...) @getpid()
%34 = sext i32 %33 to i64
%35 = trunc i64 %34 to i32
%36 = call i64 (...) @getppid()
%37 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %35, i64 %36)
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %14
br label %40
40: ; preds = %39, %11
%41 = call i32 (...) @getpid()
%42 = load i32, i32* %6
%43 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %41, i32 %42)
%44 = load i32, i32* %6
%45 = call i32 @waitpid(i32 %44, i32* null, i32 0)
%46 = load i32, i32* %6
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %48, %40
%52 = call i32 @exit(i32 0)
unreachable
}

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