#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*/ WNOHANG;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
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 <= 3; 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());
      sleep(1);
      printf("I'm process %d. My parent is %d. I finished my work\n", getpid(),
             getppid());
      exit(0);
    } else {
      for (j = 1; j <= 10; j++) {
        waitpid(-1, NULL, WNOHANG);
        sleep(1);
        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,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jg     12b1 <main+0x141>
mov    $0x0,%al
call   1090 <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    1246 <main+0xd6>
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    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
movl   $0x1,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jg     1294 <main+0x124>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <waitpid@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    124d <main+0xdd>
jmp    1299 <main+0x129>
jmp    129e <main+0x12e>
jmp    12a3 <main+0x133>
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   %r14
push   %rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe65(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%ebp
mov    0x2e36(%rip),%r14
lea    0xeee(%rip),%rbx
nopl   0x0(%rax)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1330 <main+0x1b0>
je     1348 <main+0x1c8>
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1060 <waitpid@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1060 <waitpid@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1060 <waitpid@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1060 <waitpid@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1060 <waitpid@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1060 <waitpid@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1060 <waitpid@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1060 <waitpid@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1060 <waitpid@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    (%r14),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1060 <waitpid@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
add    $0xffffffff,%ebp
jne    11c0 <main+0x40>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xceb(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xcce(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xce7(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%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"
@WNOHANG = global i32 0
@.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
store i32 0, i32* %1
%5 = call i32 (...) @getpid()
%6 = call i32 (...) @getppid()
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
store i32 1, i32* %3
br label %8
8: ; preds = %45, %0
%9 = load i32, i32* %3
%10 = icmp sle i32 %9, 3
br i1 %10, label %11, label %48
11: ; preds = %8
%12 = call i64 (...) @fork()
store i64 %12, i64* %2
%13 = load i64, i64* %2
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = load i64, i64* %2
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %18
%22 = call i32 (...) @getpid()
%23 = call i32 (...) @getppid()
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %23)
%25 = call i32 @sleep(i32 1)
%26 = call i32 (...) @getpid()
%27 = call i32 (...) @getppid()
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %27)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %18
store i32 1, i32* %4
br label %31
31: ; preds = %39, %30
%32 = load i32, i32* %4
%33 = icmp sle i32 %32, 10
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = load i32, i32* @WNOHANG
%36 = call i32 @waitpid(i32 -1, i32* null, i32 %35)
%37 = call i32 @sleep(i32 1)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %34
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %31
42: ; preds = %31
br label %43
43: ; preds = %42
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44
%46 = load i32, i32* %3
%47 = add nsw i32 %46, 1
store i32 %47, i32* %3
br label %8
48: ; preds = %8
%49 = 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"
@WNOHANG = global i32 0
@str = [19 x i8] c"father working ...\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (...) @getppid()
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
br label %4
4: ; preds = %0, %13
%5 = phi i32 [ 1, %0 ], [ %54, %13 ]
%6 = tail call i64 (...) @fork()
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %4
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %4
%12 = icmp eq i64 %6, 0
br i1 %12, label %56, label %13
13: ; preds = %11
%14 = load i32, i32* @WNOHANG
%15 = tail call i32 @waitpid(i32 -1, i32* null, i32 %14)
%16 = tail call i32 @sleep(i32 1)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%18 = load i32, i32* @WNOHANG
%19 = tail call i32 @waitpid(i32 -1, i32* null, i32 %18)
%20 = tail call i32 @sleep(i32 1)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%22 = load i32, i32* @WNOHANG
%23 = tail call i32 @waitpid(i32 -1, i32* null, i32 %22)
%24 = tail call i32 @sleep(i32 1)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%26 = load i32, i32* @WNOHANG
%27 = tail call i32 @waitpid(i32 -1, i32* null, i32 %26)
%28 = tail call i32 @sleep(i32 1)
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%30 = load i32, i32* @WNOHANG
%31 = tail call i32 @waitpid(i32 -1, i32* null, i32 %30)
%32 = tail call i32 @sleep(i32 1)
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%34 = load i32, i32* @WNOHANG
%35 = tail call i32 @waitpid(i32 -1, i32* null, i32 %34)
%36 = tail call i32 @sleep(i32 1)
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%38 = load i32, i32* @WNOHANG
%39 = tail call i32 @waitpid(i32 -1, i32* null, i32 %38)
%40 = tail call i32 @sleep(i32 1)
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%42 = load i32, i32* @WNOHANG
%43 = tail call i32 @waitpid(i32 -1, i32* null, i32 %42)
%44 = tail call i32 @sleep(i32 1)
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%46 = load i32, i32* @WNOHANG
%47 = tail call i32 @waitpid(i32 -1, i32* null, i32 %46)
%48 = tail call i32 @sleep(i32 1)
%49 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%50 = load i32, i32* @WNOHANG
%51 = tail call i32 @waitpid(i32 -1, i32* null, i32 %50)
%52 = tail call i32 @sleep(i32 1)
%53 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%54 = add nuw nsw i32 %5, 1
%55 = icmp eq i32 %54, 4
br i1 %55, label %65, label %4
56: ; preds = %11
%57 = tail call i32 (...) @getpid()
%58 = tail call i32 (...) @getppid()
%59 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0), i32 %57, i32 %58)
%60 = tail call i32 @sleep(i32 1)
%61 = tail call i32 (...) @getpid()
%62 = tail call i32 (...) @getppid()
%63 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %61, i32 %62)
%64 = tail call i32 @exit(i32 0)
unreachable
65: ; preds = %13
%66 = tail call i32 @exit(i32 0)
unreachable
}