#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ stdout;
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(void) {
  pid_t pid1, pid2;
  printf("original process, pid = %d\n", getpid());
  if ((pid1 = fork()) < 0) {
    err_sys("fork error");
  } else if (pid1 == 0) {
    printf("first child, pid = %d, parent = %d\n", getpid(), getppid());
    fflush(stdout);
    exit(EXIT_SUCCESS);
  }
  if ((pid2 = fork()) < 0) {
    err_sys("fork error");
  } else if (pid2 == 0) {
    printf("second child, pid = %d, parent = %d\n", getpid(), getppid());
    fflush(stdout);
    exit(EXIT_SUCCESS);
  }
  if (waitpid(pid1, NULL, 0) != pid1)
    err_sys("waitpid error");
  if (waitpid(pid2, NULL, 0) != pid2)
    err_sys("waitpid error");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11cc <main+0x4c>
lea    0xe5a(%rip),%rdi
call   1030 <err_sys@plt>
jmp    121c <main+0x9c>
cmpq   $0x0,-0x10(%rbp)
jne    1217 <main+0x97>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    121c <main+0x9c>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1242 <main+0xc2>
lea    0xde4(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1292 <main+0x112>
cmpq   $0x0,-0x18(%rbp)
jne    128d <main+0x10d>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1292 <main+0x112>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    -0x10(%rbp),%rax
je     12b7 <main+0x137>
lea    0xdbe(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    -0x18(%rbp),%rax
je     12dc <main+0x15c>
lea    0xd99(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe6e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11b8 <main+0x38>
lea    0xe6b(%rip),%rdi
call   1030 <err_sys@plt>
jmp    11d3 <main+0x53>
jne    11d3 <main+0x53>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xe56(%rip),%rdi
jmp    1209 <main+0x89>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%r14
test   %rax,%rax
jns    11f0 <main+0x70>
lea    0xe33(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1224 <main+0xa4>
jne    1224 <main+0xa4>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xe42(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    125e <main+0xde>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    %rbx,%rax
je     1241 <main+0xc1>
lea    0xe34(%rip),%rdi
call   1030 <err_sys@plt>
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    %r14,%rax
je     125e <main+0xde>
lea    0xe17(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"original process, pid = %d\0A\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [36 x i8] c"first child, pid = %d, parent = %d\0A\00"
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.3 = [37 x i8] c"second child, pid = %d, parent = %d\0A\00"
@.str.4 = [14 x i8] c"waitpid error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
store i32 0, i32* %1
%4 = call i32 (...) @getpid()
%5 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i64 (...) @fork()
store i64 %6, i64* %2
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %22
10: ; preds = %0
%11 = load i64, i64* %2
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %10
%14 = call i32 (...) @getpid()
%15 = call i32 (...) @getppid()
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %15)
%17 = load i32, i32* @stdout
%18 = call i32 @fflush(i32 %17)
%19 = load i32, i32* @EXIT_SUCCESS
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %10
br label %22
22: ; preds = %21, %8
%23 = call i64 (...) @fork()
store i64 %23, i64* %3
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %39
27: ; preds = %22
%28 = load i64, i64* %3
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = call i32 (...) @getpid()
%32 = call i32 (...) @getppid()
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %32)
%34 = load i32, i32* @stdout
%35 = call i32 @fflush(i32 %34)
%36 = load i32, i32* @EXIT_SUCCESS
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %27
br label %39
39: ; preds = %38, %25
%40 = load i64, i64* %2
%41 = call i64 @waitpid(i64 %40, i32* null, i32 0)
%42 = load i64, i64* %2
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %39
%47 = load i64, i64* %3
%48 = call i64 @waitpid(i64 %47, i32* null, i32 0)
%49 = load i64, i64* %3
%50 = icmp ne i64 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %51, %46
%54 = load i32, i32* @EXIT_SUCCESS
%55 = call i32 @exit(i32 %54)
unreachable
}

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