#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fork();
int getpgid(int);
int getpid();
int getppid();
int /*<<< orphan*/ handle_error(int, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  printf("started parent\n");
  printf("starting child\n");
  int pid = fork();
  if (pid < 0) {
    handle_error(pid, "could not fork child from parent\n");
    exit(1);
  } else if (pid > 0) {
    printf("in parent child-pid=%d\n", pid);
    sleep(10);
    printf("finishing parent\n");
    exit(0);
  } else {
    printf("in child pid=%d pgid=%d ppid=%d\n", getpid(), getpgid(getpid()),
           getppid());
    printf("starting daemon (grand child)");
    int pid = fork();
    if (pid < 0) {
      handle_error(pid, "could not fork grand child from child\n");
      exit(1);
    } else if (pid > 0) {
      printf("in child grand-child-pid=%d\n", pid);
      printf("finishing child\n");
      exit(0);
    } else {
      printf("in grand child pid=%d pgid=%d ppid=%d\n", getpid(),
             getpgid(getpid()), getppid());
      while (getppid() != 1) {
        printf("waiting for init to become ppid\n");
        sleep(1);
      }
      printf("daemonized");
      int i;
      for (i = 0; i < 100; i++) {
        printf("in daemon: %3d**2 = %5d\n", i, i * i);
        sleep(1);
      }
      printf("done with daemon terminating\n");
      exit(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)
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11df <main+0x5f>
mov    -0x14(%rbp),%edi
lea    0xe50(%rip),%rsi
call   10a0 <handle_error@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jle    1219 <main+0x99>
mov    -0x14(%rbp),%esi
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1050 <getpgid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    %eax,%ecx
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    128c <main+0x10c>
mov    -0x18(%rbp),%edi
lea    0xe2e(%rip),%rsi
call   10a0 <handle_error@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jle    12bc <main+0x13c>
mov    -0x18(%rbp),%esi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1050 <getpgid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    %eax,%ecx
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
cmp    $0x1,%eax
je     1321 <main+0x1a1>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    12f4 <main+0x174>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jge    1370 <main+0x1f0>
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
imul   -0x1c(%rbp),%edx
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1336 <main+0x1b6>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xf6e(%rip),%rdi
call   1030 <puts@plt>
lea    0xf71(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jns    11ce <main+0x3e>
lea    0xe43(%rip),%rsi
mov    %eax,%edi
call   10b0 <handle_error@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jne    1226 <main+0x96>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1060 <getpgid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe43(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe50(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jns    1253 <main+0xc3>
lea    0xe55(%rip),%rsi
jmp    11bd <main+0x2d>
lea    0xdf5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
lea    0xf2c(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jne    130f <main+0x17f>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1060 <getpgid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe3d(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <getppid@plt>
cmp    $0x1,%eax
je     12be <main+0x12e>
lea    0xea3(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1070 <getppid@plt>
cmp    $0x1,%eax
jne    12a0 <main+0x110>
lea    0xe1f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe1c(%rip),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edx
imul   %ebp,%edx
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
add    $0x1,%ebp
cmp    $0x64,%ebp
jne    12e0 <main+0x150>
lea    0xe1c(%rip),%rdi
jmp    1247 <main+0xb7>
lea    0xd8a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe3d(%rip),%rdi
jmp    1247 <main+0xb7>

LLVM IR (-O0):
@.str = [16 x i8] c"started parent\0A\00"
@.str.1 = [16 x i8] c"starting child\0A\00"
@.str.2 = [34 x i8] c"could not fork child from parent\0A\00"
@.str.3 = [24 x i8] c"in parent child-pid=%d\0A\00"
@.str.4 = [18 x i8] c"finishing parent\0A\00"
@.str.5 = [33 x i8] c"in child pid=%d pgid=%d ppid=%d\0A\00"
@.str.6 = [30 x i8] c"starting daemon (grand child)\00"
@.str.7 = [39 x i8] c"could not fork grand child from child\0A\00"
@.str.8 = [29 x i8] c"in child grand-child-pid=%d\0A\00"
@.str.9 = [17 x i8] c"finishing child\0A\00"
@.str.10 = [39 x i8] c"in grand child pid=%d pgid=%d ppid=%d\0A\00"
@.str.11 = [33 x i8] c"waiting for init to become ppid\0A\00"
@.str.12 = [11 x i8] c"daemonized\00"
@.str.13 = [25 x i8] c"in daemon: %3d**2 = %5d\0A\00"
@.str.14 = [30 x i8] c"done with daemon terminating\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 (...) @fork()
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* %6
%16 = call i32 @handle_error(i32 %15, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %6
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i32, i32* %6
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = call i32 @sleep(i32 10)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %18
%28 = call i32 (...) @getpid()
%29 = call i32 (...) @getpid()
%30 = call i32 @getpgid(i32 %29)
%31 = call i32 (...) @getppid()
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %28, i32 %30, i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%34 = call i32 (...) @fork()
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %27
%38 = load i32, i32* %7
%39 = call i32 @handle_error(i32 %38, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %27
%42 = load i32, i32* %7
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i32, i32* %7
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0))
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %41
%50 = call i32 (...) @getpid()
%51 = call i32 (...) @getpid()
%52 = call i32 @getpgid(i32 %51)
%53 = call i32 (...) @getppid()
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.10, i64 0, i64 0), i32 %50, i32 %52, i32 %53)
br label %55
55: ; preds = %58, %49
%56 = call i32 (...) @getppid()
%57 = icmp ne i32 %56, 1
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.11, i64 0, i64 0))
%60 = call i32 @sleep(i32 1)
br label %55
61: ; preds = %55
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i64 0, i64 0))
store i32 0, i32* %8
br label %63
63: ; preds = %73, %61
%64 = load i32, i32* %8
%65 = icmp slt i32 %64, 100
br i1 %65, label %66, label %76
66: ; preds = %63
%67 = load i32, i32* %8
%68 = load i32, i32* %8
%69 = load i32, i32* %8
%70 = mul nsw i32 %68, %69
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.13, i64 0, i64 0), i32 %67, i32 %70)
%72 = call i32 @sleep(i32 1)
br label %73
73: ; preds = %66
%74 = load i32, i32* %8
%75 = add nsw i32 %74, 1
store i32 %75, i32* %8
br label %63
76: ; preds = %63
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.14, i64 0, i64 0))
%78 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.2 = [34 x i8] c"could not fork child from parent\0A\00"
@.str.3 = [24 x i8] c"in parent child-pid=%d\0A\00"
@.str.5 = [33 x i8] c"in child pid=%d pgid=%d ppid=%d\0A\00"
@.str.6 = [30 x i8] c"starting daemon (grand child)\00"
@.str.7 = [39 x i8] c"could not fork grand child from child\0A\00"
@.str.8 = [29 x i8] c"in child grand-child-pid=%d\0A\00"
@.str.10 = [39 x i8] c"in grand child pid=%d pgid=%d ppid=%d\0A\00"
@.str.12 = [11 x i8] c"daemonized\00"
@.str.13 = [25 x i8] c"in daemon: %3d**2 = %5d\0A\00"
@str = [15 x i8] c"started parent\00"
@str.15 = [15 x i8] c"starting child\00"
@str.16 = [29 x i8] c"done with daemon terminating\00"
@str.17 = [32 x i8] c"waiting for init to become ppid\00"
@str.18 = [16 x i8] c"finishing child\00"
@str.19 = [17 x i8] c"finishing parent\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.15, i64 0, i64 0))
%5 = tail call i32 (...) @fork()
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @handle_error(i32 %5, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = icmp eq i32 %5, 0
br i1 %11, label %17, label %12
12: ; preds = %10
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %5)
%14 = tail call i32 @sleep(i32 10)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.19, i64 0, i64 0))
%16 = tail call i32 @exit(i32 0)
unreachable
17: ; preds = %10
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (...) @getpid()
%20 = tail call i32 @getpgid(i32 %19)
%21 = tail call i32 (...) @getppid()
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %18, i32 %20, i32 %21)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%24 = tail call i32 (...) @fork()
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = tail call i32 @handle_error(i32 %24, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %17
%30 = icmp eq i32 %24, 0
br i1 %30, label %35, label %31
31: ; preds = %29
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 %24)
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.18, i64 0, i64 0))
%34 = tail call i32 @exit(i32 0)
unreachable
35: ; preds = %29
%36 = tail call i32 (...) @getpid()
%37 = tail call i32 (...) @getpid()
%38 = tail call i32 @getpgid(i32 %37)
%39 = tail call i32 (...) @getppid()
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.10, i64 0, i64 0), i32 %36, i32 %38, i32 %39)
%41 = tail call i32 (...) @getppid()
%42 = icmp eq i32 %41, 1
br i1 %42, label %48, label %43
43: ; preds = %35, %43
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.17, i64 0, i64 0))
%45 = tail call i32 @sleep(i32 1)
%46 = tail call i32 (...) @getppid()
%47 = icmp eq i32 %46, 1
br i1 %47, label %48, label %43
48: ; preds = %43, %35
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i64 0, i64 0))
br label %50
50: ; preds = %48, %50
%51 = phi i32 [ 0, %48 ], [ %55, %50 ]
%52 = mul nuw nsw i32 %51, %51
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.13, i64 0, i64 0), i32 %51, i32 %52)
%54 = tail call i32 @sleep(i32 1)
%55 = add nuw nsw i32 %51, 1
%56 = icmp eq i32 %55, 100
br i1 %56, label %57, label %50
57: ; preds = %50
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.16, i64 0, i64 0))
%59 = tail call i32 @exit(i32 0)
unreachable
}