#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 getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ usleep(int);
int main() {
  int i = 2;
  int j = 3;
  int t = 0;
  int r = 0;
  printf("frist: pid = %d, ppid = %d\n", getpid(), getppid());
  for (; t < i; ++t) {
    r = fork();
    if (r < 0)
      exit(0);
    else if (r)
      exit(0);
  }
  printf("pid = %d, ppid = %d, after i\n", getpid(), getppid());
  for (; t < j; ++t) {
    r = fork();
    if (r < 0)
      exit(0);
    else if (!r)
      exit(0);
  }
  printf("pid = %d, ppid = %d, after j\n", getpid(), getppid());
  usleep(1000);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x2,-0x8(%rbp)
movl   $0x3,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11fd <main+0x9d>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d4 <main+0x74>
xor    %edi,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
je     11e5 <main+0x85>
xor    %edi,%edi
call   1060 <exit@plt>
jmp    11ea <main+0x8a>
jmp    11ef <main+0x8f>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11ad <main+0x4d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe02(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    126f <main+0x10f>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1246 <main+0xe6>
xor    %edi,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1257 <main+0xf7>
xor    %edi,%edi
call   1060 <exit@plt>
jmp    125c <main+0xfc>
jmp    1261 <main+0x101>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    121f <main+0xbf>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xdae(%rip),%rdi
call   1040 <printf@plt>
mov    $0x3e8,%edi
call   1080 <usleep@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe88(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
js     11f6 <main+0x96>
jne    11f6 <main+0x96>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
js     11f6 <main+0x96>
jne    11f6 <main+0x96>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe6a(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
js     11f6 <main+0x96>
je     11f6 <main+0x96>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe5b(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    $0x3e8,%edi
call   1080 <usleep@plt>
xor    %eax,%eax
pop    %rbx
ret
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"frist: pid = %d, ppid = %d\0A\00"
@.str.1 = [30 x i8] c"pid = %d, ppid = %d, after i\0A\00"
@.str.2 = [30 x i8] c"pid = %d, ppid = %d, after j\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 2, i32* %2
store i32 3, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
%6 = call i32 (...) @getpid()
%7 = call i32 (...) @getppid()
%8 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
br label %9
9: ; preds = %26, %0
%10 = load i32, i32* %4
%11 = load i32, i32* %2
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %29
13: ; preds = %9
%14 = call i32 (...) @fork()
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %13
%20 = load i32, i32* %5
%21 = icmp ne i32 %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
br label %26
26: ; preds = %25
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %9
29: ; preds = %9
%30 = call i32 (...) @getpid()
%31 = call i32 (...) @getppid()
%32 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 %31)
br label %33
33: ; preds = %50, %29
%34 = load i32, i32* %4
%35 = load i32, i32* %3
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %53
37: ; preds = %33
%38 = call i32 (...) @fork()
store i32 %38, i32* %5
%39 = load i32, i32* %5
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @exit(i32 0)
unreachable
43: ; preds = %37
%44 = load i32, i32* %5
%45 = icmp ne i32 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %43
br label %49
49: ; preds = %48
br label %50
50: ; preds = %49
%51 = load i32, i32* %4
%52 = add nsw i32 %51, 1
store i32 %52, i32* %4
br label %33
53: ; preds = %33
%54 = call i32 (...) @getpid()
%55 = call i32 (...) @getppid()
%56 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %54, i32 %55)
%57 = call i32 @usleep(i32 1000)
ret i32 0
}

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