#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 */
scalar_t__ fork();
int /*<<< orphan*/ logPid(int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ wait(int *);
int main() {
  int idx = 1;
  pid_t pid = 0;
  int stat = 0;
  logPid(idx);
  pid = fork();
  if (pid > 0) {
    wait(&stat);
    puts("all fork is ended");
    return 0;
  }
  logPid(idx);
  ++idx;
  logPid(idx);
  pid = fork();
  if (pid > 0) {
    wait(&stat);
    return 0;
  }
  logPid(idx);
  ++idx;
  logPid(idx);
  pid = fork();
  if (pid > 0) {
    wait(&stat);
    return 0;
  }
  logPid(idx);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <logPid@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    11a4 <main+0x64>
lea    -0x14(%rbp),%rdi
call   1050 <wait@plt>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    123b <main+0xfb>
mov    -0x8(%rbp),%edi
call   1040 <logPid@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <logPid@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    11e8 <main+0xa8>
lea    -0x14(%rbp),%rdi
call   1050 <wait@plt>
movl   $0x0,-0x4(%rbp)
jmp    123b <main+0xfb>
mov    -0x8(%rbp),%edi
call   1040 <logPid@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <logPid@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    122c <main+0xec>
lea    -0x14(%rbp),%rdi
call   1050 <wait@plt>
movl   $0x0,-0x4(%rbp)
jmp    123b <main+0xfb>
mov    -0x8(%rbp),%edi
call   1040 <logPid@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
movl   $0x0,0x4(%rsp)
mov    $0x1,%edi
call   1040 <logPid@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jle    1177 <main+0x37>
lea    0x4(%rsp),%rdi
call   1050 <wait@plt>
lea    0xe90(%rip),%rdi
call   1030 <puts@plt>
jmp    11c1 <main+0x81>
mov    $0x1,%edi
call   1040 <logPid@plt>
mov    $0x2,%edi
call   1040 <logPid@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jg     11b7 <main+0x77>
mov    $0x2,%edi
call   1040 <logPid@plt>
mov    $0x3,%edi
call   1040 <logPid@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jle    11c5 <main+0x85>
lea    0x4(%rsp),%rdi
call   1050 <wait@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    $0x3,%edi
call   1040 <logPid@plt>
jmp    11c1 <main+0x81>

LLVM IR (-O0):
@.str = [18 x i8] c"all fork is ended\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %2
store i64 0, i64* %3
store i32 0, i32* %4
%5 = load i32, i32* %2
%6 = call i32 @logPid(i32 %5)
%7 = call i64 (...) @fork()
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @wait(i32* %4)
%12 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %40
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32 @logPid(i32 %14)
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = call i32 @logPid(i32 %18)
%20 = call i64 (...) @fork()
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = call i32 @wait(i32* %4)
store i32 0, i32* %1
br label %40
25: ; preds = %13
%26 = load i32, i32* %2
%27 = call i32 @logPid(i32 %26)
%28 = load i32, i32* %2
%29 = add nsw i32 %28, 1
store i32 %29, i32* %2
%30 = load i32, i32* %2
%31 = call i32 @logPid(i32 %30)
%32 = call i64 (...) @fork()
store i64 %32, i64* %3
%33 = load i64, i64* %3
%34 = icmp sgt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %25
%36 = call i32 @wait(i32* %4)
store i32 0, i32* %1
br label %40
37: ; preds = %25
%38 = load i32, i32* %2
%39 = call i32 @logPid(i32 %38)
store i32 0, i32* %1
br label %40
40: ; preds = %37, %35, %23, %10
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str = [18 x i8] c"all fork is ended\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i32 @logPid(i32 1)
%4 = tail call i64 (...) @fork()
%5 = icmp sgt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @wait(i32* %1)
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %25
9: ; preds = %0
%10 = tail call i32 @logPid(i32 1)
%11 = tail call i32 @logPid(i32 2)
%12 = tail call i64 (...) @fork()
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @wait(i32* %1)
br label %25
16: ; preds = %9
%17 = tail call i32 @logPid(i32 2)
%18 = tail call i32 @logPid(i32 3)
%19 = tail call i64 (...) @fork()
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @wait(i32* %1)
br label %25
23: ; preds = %16
%24 = tail call i32 @logPid(i32 3)
br label %25
25: ; preds = %23, %21, %14, %6
ret i32 0
}