#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(int);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ waitpid(int, int *, int);
int main(void) {
  pid_t pid;
  int i, j;
  printf("I'm process %d. My parent is %d.\n", getpid(), getppid());
  for (i = 1; i <= 4; i++) {
    pid = fork();
    if (pid < 0) {
      printf("fork error");
      exit(1);
    } else if (pid == 0) {
      printf("I'm process %d. My parent is %d. I'm going to work for 1 second "
             "...\n",
             getpid(), getppid());
      usleep(100000);
      printf("I'm process %d. My parent is %d. I finished my work\n", getpid(),
             getppid());
    } else
      for (j = 1; j <= 3; j++) {
        usleep(100000);
        int status;
        int options = 0;
        waitpid(-1, &status, options);
        printf("father working ...\n");
      }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jg     12b0 <main+0x140>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11e2 <main+0x72>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1244 <main+0xd4>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x186a0,%edi
call   1090 <usleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x28(%rbp),%esi
mov    %eax,%edx
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1298 <main+0x128>
movl   $0x1,-0x18(%rbp)
cmpl   $0x3,-0x18(%rbp)
jg     1293 <main+0x123>
mov    $0x186a0,%edi
call   1090 <usleep@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%edx
mov    $0xffffffff,%edi
lea    -0x1c(%rbp),%rsi
call   1050 <waitpid@plt>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    124b <main+0xdb>
jmp    1298 <main+0x128>
jmp    129d <main+0x12d>
jmp    12a2 <main+0x132>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11aa <main+0x3a>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe5e(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x4,%ebx
lea    0xe74(%rip),%r14
lea    0xeb2(%rip),%r15
lea    0x4(%rsp),%r12
lea    0xedb(%rip),%r13
jmp    121b <main+0x9b>
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <getppid@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x186a0,%edi
call   10a0 <usleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <getppid@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffff,%ebx
je     1291 <main+0x111>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1298 <main+0x118>
je     11d0 <main+0x50>
mov    $0x186a0,%edi
call   10a0 <usleep@plt>
mov    $0xffffffff,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %r13,%rdi
call   1030 <puts@plt>
mov    $0x186a0,%edi
call   10a0 <usleep@plt>
mov    $0xffffffff,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %r13,%rdi
call   1030 <puts@plt>
mov    $0x186a0,%edi
call   10a0 <usleep@plt>
mov    $0xffffffff,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %r13,%rdi
call   1030 <puts@plt>
add    $0xffffffff,%ebx
jne    121b <main+0x9b>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xd83(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"I'm process %d. My parent is %d.\0A\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [69 x i8] c"I'm process %d. My parent is %d. I'm going to work for 1 second ...\0A\00"
@.str.3 = [53 x i8] c"I'm process %d. My parent is %d. I finished my work\0A\00"
@.str.4 = [20 x i8] c"father working ...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (...) @getpid()
%8 = call i32 (...) @getppid()
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
store i32 1, i32* %3
br label %10
10: ; preds = %46, %0
%11 = load i32, i32* %3
%12 = icmp sle i32 %11, 4
br i1 %12, label %13, label %49
13: ; preds = %10
%14 = call i64 (...) @fork()
store i64 %14, i64* %2
%15 = load i64, i64* %2
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = load i64, i64* %2
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = call i32 (...) @getpid()
%25 = call i32 (...) @getppid()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %25)
%27 = call i32 @usleep(i32 100000)
%28 = call i32 (...) @getpid()
%29 = call i32 (...) @getppid()
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %29)
br label %44
31: ; preds = %20
store i32 1, i32* %4
br label %32
32: ; preds = %40, %31
%33 = load i32, i32* %4
%34 = icmp sle i32 %33, 3
br i1 %34, label %35, label %43
35: ; preds = %32
%36 = call i32 @usleep(i32 100000)
store i32 0, i32* %6
%37 = load i32, i32* %6
%38 = call i32 @waitpid(i32 -1, i32* %5, i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %35
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
br label %32
43: ; preds = %32
br label %44
44: ; preds = %43, %23
br label %45
45: ; preds = %44
br label %46
46: ; preds = %45
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 1
store i32 %48, i32* %3
br label %10
49: ; preds = %10
%50 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [34 x i8] c"I'm process %d. My parent is %d.\0A\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [69 x i8] c"I'm process %d. My parent is %d. I'm going to work for 1 second ...\0A\00"
@.str.3 = [53 x i8] c"I'm process %d. My parent is %d. I finished my work\0A\00"
@str = [19 x i8] c"father working ...\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @getpid()
%3 = tail call i32 (...) @getppid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %3)
%5 = bitcast i32* %1 to i8*
br label %6
6: ; preds = %0, %33
%7 = phi i32 [ 1, %0 ], [ %34, %33 ]
%8 = call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %6
%14 = icmp eq i64 %8, 0
br i1 %14, label %25, label %15
15: ; preds = %13
%16 = call i32 @usleep(i32 100000)
%17 = call i32 @waitpid(i32 -1, i32* %1, i32 0)
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%19 = call i32 @usleep(i32 100000)
%20 = call i32 @waitpid(i32 -1, i32* %1, i32 0)
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%22 = call i32 @usleep(i32 100000)
%23 = call i32 @waitpid(i32 -1, i32* %1, i32 0)
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %33
25: ; preds = %13
%26 = call i32 (...) @getpid()
%27 = call i32 (...) @getppid()
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 %27)
%29 = call i32 @usleep(i32 100000)
%30 = call i32 (...) @getpid()
%31 = call i32 (...) @getppid()
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %30, i32 %31)
br label %33
33: ; preds = %15, %25
%34 = add nuw nsw i32 %7, 1
%35 = icmp eq i32 %34, 5
br i1 %35, label %36, label %6
36: ; preds = %33
%37 = call i32 @exit(i32 0)
unreachable
}