#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(int argc, char const *argv[]) {
  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");
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%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    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x1c(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jg     12b8 <main+0x148>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11e9 <main+0x79>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    124d <main+0xdd>
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    0xe1d(%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,-0x28(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x28(%rbp),%esi
mov    %eax,%edx
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
movl   $0x1,-0x20(%rbp)
cmpl   $0xa,-0x20(%rbp)
jg     129b <main+0x12b>
mov    0x2d83(%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    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1254 <main+0xe4>
jmp    12a0 <main+0x130>
jmp    12a5 <main+0x135>
jmp    12aa <main+0x13a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b1 <main+0x41>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

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    0xeef(%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    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xcec(%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    0xccf(%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    0xce8(%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 = [35 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(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @getpid()
%10 = call i32 (...) @getppid()
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
store i32 1, i32* %7
br label %12
12: ; preds = %49, %2
%13 = load i32, i32* %7
%14 = icmp sle i32 %13, 3
br i1 %14, label %15, label %52
15: ; preds = %12
%16 = call i64 (...) @fork()
store i64 %16, i64* %6
%17 = load i64, i64* %6
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = load i64, i64* %6
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = call i32 (...) @getpid()
%27 = call i32 (...) @getppid()
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 %27)
%29 = call i32 @sleep(i32 1)
%30 = call i32 (...) @getpid()
%31 = call i32 (...) @getppid()
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %30, i32 %31)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %22
store i32 1, i32* %8
br label %35
35: ; preds = %43, %34
%36 = load i32, i32* %8
%37 = icmp sle i32 %36, 10
br i1 %37, label %38, label %46
38: ; preds = %35
%39 = load i32, i32* @WNOHANG
%40 = call i32 @waitpid(i32 -1, i32* null, i32 %39)
%41 = call i32 @sleep(i32 1)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %38
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8
br label %35
46: ; preds = %35
br label %47
47: ; preds = %46
br label %48
48: ; preds = %47
br label %49
49: ; preds = %48
%50 = load i32, i32* %7
%51 = add nsw i32 %50, 1
store i32 %51, i32* %7
br label %12
52: ; preds = %12
ret i32 0
}

LLVM IR (-O2):
@.str = [35 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(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (...) @getppid()
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4)
br label %6
6: ; preds = %2, %15
%7 = phi i32 [ 1, %2 ], [ %56, %15 ]
%8 = tail call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %6
%14 = icmp eq i64 %8, 0
br i1 %14, label %58, label %15
15: ; preds = %13
%16 = load i32, i32* @WNOHANG
%17 = tail call i32 @waitpid(i32 -1, i32* null, i32 %16)
%18 = tail call i32 @sleep(i32 1)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%20 = load i32, i32* @WNOHANG
%21 = tail call i32 @waitpid(i32 -1, i32* null, i32 %20)
%22 = tail call i32 @sleep(i32 1)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%24 = load i32, i32* @WNOHANG
%25 = tail call i32 @waitpid(i32 -1, i32* null, i32 %24)
%26 = tail call i32 @sleep(i32 1)
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%28 = load i32, i32* @WNOHANG
%29 = tail call i32 @waitpid(i32 -1, i32* null, i32 %28)
%30 = tail call i32 @sleep(i32 1)
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%32 = load i32, i32* @WNOHANG
%33 = tail call i32 @waitpid(i32 -1, i32* null, i32 %32)
%34 = tail call i32 @sleep(i32 1)
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%36 = load i32, i32* @WNOHANG
%37 = tail call i32 @waitpid(i32 -1, i32* null, i32 %36)
%38 = tail call i32 @sleep(i32 1)
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%40 = load i32, i32* @WNOHANG
%41 = tail call i32 @waitpid(i32 -1, i32* null, i32 %40)
%42 = tail call i32 @sleep(i32 1)
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%44 = load i32, i32* @WNOHANG
%45 = tail call i32 @waitpid(i32 -1, i32* null, i32 %44)
%46 = tail call i32 @sleep(i32 1)
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%48 = load i32, i32* @WNOHANG
%49 = tail call i32 @waitpid(i32 -1, i32* null, i32 %48)
%50 = tail call i32 @sleep(i32 1)
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%52 = load i32, i32* @WNOHANG
%53 = tail call i32 @waitpid(i32 -1, i32* null, i32 %52)
%54 = tail call i32 @sleep(i32 1)
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%56 = add nuw nsw i32 %7, 1
%57 = icmp eq i32 %56, 4
br i1 %57, label %67, label %6
58: ; preds = %13
%59 = tail call i32 (...) @getpid()
%60 = tail call i32 (...) @getppid()
%61 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0), i32 %59, i32 %60)
%62 = tail call i32 @sleep(i32 1)
%63 = tail call i32 (...) @getpid()
%64 = tail call i32 (...) @getppid()
%65 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %63, i32 %64)
%66 = tail call i32 @exit(i32 0)
unreachable
67: ; preds = %15
ret i32 0
}