#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 /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, scalar_t__);
int /*<<< orphan*/ setpgid(int, int);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  pid_t pid;
  int cc;
  pid = fork();
  if (pid == 0) {
    printf("Child 1 %d %d\n", getpid(), getppid());
    while (1)
      ++cc;
    exit(0);
  }
  printf("Parent %d forked %d\n", getpid(), pid);
  sleep(10);
  pid = fork();
  if (pid == 0) {
    setpgid(getpid(), getpid());
    printf("Child 2 %d %d\n", getpid(), getppid());
    sleep(3600);
    exit(0);
  }
  printf("Parent %d forked %d\n", getpid(), pid);
  sleep(10);
  pid = fork();
  if (pid == 0) {
    pid = fork();
    if (pid == 0) {
      setsid();
      printf("Child 3 %d %d\n", getpid(), getppid());
      sleep(86400);
      exit(0);
    }
    if (pid > 0) {
      printf("Parent %d forked %d\n", getpid(), pid);
      exit(0);
    }
  }
  printf("Parent %d forked %d\n", getpid(), pid);
  while (1) {
    pid = wait(NULL);
    printf("Parent %d child %d gone\n", getpid(), pid);
  }
  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   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f0 <main+0x60>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x20(%rbp),%esi
movslq %eax,%rdx
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    %eax,-0x1c(%rbp)
jmp    11df <main+0x4f>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
lea    0xe0b(%rip),%rdi
call   1040 <printf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1278 <main+0xe8>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%esi
call   1060 <setpgid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x28(%rbp),%esi
movslq %eax,%rdx
lea    0xdc2(%rip),%rdi
call   1040 <printf@plt>
mov    $0xe10,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
lea    0xd83(%rip),%rdi
call   1040 <printf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1332 <main+0x1a2>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1302 <main+0x172>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x30(%rbp),%esi
movslq %eax,%rdx
lea    0xd47(%rip),%rdi
call   1040 <printf@plt>
mov    $0x15180,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    132d <main+0x19d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
lea    0xcee(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1332 <main+0x1a2>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
lea    0xcc9(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
lea    0xcd6(%rip),%rdi
call   1040 <printf@plt>
jmp    134b <main+0x1bb>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
test   %rbx,%rbx
je     1270 <main+0xe0>
lea    0xe59(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
test   %rbp,%rbp
je     1292 <main+0x102>
lea    0xe22(%rip),%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
call   1040 <printf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1229 <main+0x99>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     12ca <main+0x13a>
mov    %rax,%rbx
jg     1303 <main+0x173>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdd8(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
lea    0xdfa(%rip),%rbx
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
call   10a0 <wait@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %rbp,%rdx
call   1040 <printf@plt>
jmp    1250 <main+0xc0>
xor    %eax,%eax
call   1070 <getppid@plt>
movslq %eax,%rdx
lea    0xd7f(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
nopl   0x0(%rax,%rax,1)
jmp    1290 <main+0x100>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <setpgid@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
movslq %eax,%rdx
lea    0xd68(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    $0xe10,%edi
jmp    12f7 <main+0x167>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
movslq %eax,%rdx
lea    0xd48(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    $0x15180,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xcfe(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Child 1 %d %d\0A\00"
@.str.1 = [21 x i8] c"Parent %d forked %d\0A\00"
@.str.2 = [15 x i8] c"Child 2 %d %d\0A\00"
@.str.3 = [15 x i8] c"Child 3 %d %d\0A\00"
@.str.4 = [25 x i8] c"Parent %d child %d gone\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = call i32 (...) @getpid()
%13 = call i32 (...) @getppid()
%14 = sext i32 %13 to i64
%15 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12, i64 %14)
br label %16
16: ; preds = %11, %16
%17 = load i32, i32* %7
%18 = add nsw i32 %17, 1
store i32 %18, i32* %7
br label %16
19: ; preds = %2
%20 = call i32 (...) @getpid()
%21 = load i64, i64* %6
%22 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %20, i64 %21)
%23 = call i32 @sleep(i32 10)
%24 = call i64 (...) @fork()
store i64 %24, i64* %6
%25 = load i64, i64* %6
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %19
%28 = call i32 (...) @getpid()
%29 = call i32 (...) @getpid()
%30 = call i32 @setpgid(i32 %28, i32 %29)
%31 = call i32 (...) @getpid()
%32 = call i32 (...) @getppid()
%33 = sext i32 %32 to i64
%34 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %31, i64 %33)
%35 = call i32 @sleep(i32 3600)
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %19
%38 = call i32 (...) @getpid()
%39 = load i64, i64* %6
%40 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %38, i64 %39)
%41 = call i32 @sleep(i32 10)
%42 = call i64 (...) @fork()
store i64 %42, i64* %6
%43 = load i64, i64* %6
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %66
45: ; preds = %37
%46 = call i64 (...) @fork()
store i64 %46, i64* %6
%47 = load i64, i64* %6
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %45
%50 = call i32 (...) @setsid()
%51 = call i32 (...) @getpid()
%52 = call i32 (...) @getppid()
%53 = sext i32 %52 to i64
%54 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %51, i64 %53)
%55 = call i32 @sleep(i32 86400)
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %45
%58 = load i64, i64* %6
%59 = icmp sgt i64 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %57
%61 = call i32 (...) @getpid()
%62 = load i64, i64* %6
%63 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %61, i64 %62)
%64 = call i32 @exit(i32 0)
unreachable
65: ; preds = %57
br label %66
66: ; preds = %65, %37
%67 = call i32 (...) @getpid()
%68 = load i64, i64* %6
%69 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %67, i64 %68)
br label %70
70: ; preds = %66, %70
%71 = call i64 @wait(i32* null)
store i64 %71, i64* %6
%72 = call i32 (...) @getpid()
%73 = load i64, i64* %6
%74 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %72, i64 %73)
br label %70
}

LLVM IR (-O2):
@.str = [15 x i8] c"Child 1 %d %d\0A\00"
@.str.1 = [21 x i8] c"Parent %d forked %d\0A\00"
@.str.2 = [15 x i8] c"Child 2 %d %d\0A\00"
@.str.3 = [15 x i8] c"Child 3 %d %d\0A\00"
@.str.4 = [25 x i8] c"Parent %d child %d gone\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
%5 = tail call i32 (...) @getpid()
br i1 %4, label %6, label %11
6: ; preds = %2
%7 = tail call i32 (...) @getppid()
%8 = sext i32 %7 to i64
%9 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %5, i64 %8)
br label %10
10: ; preds = %10, %6
br label %10
11: ; preds = %2
%12 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %5, i64 %3)
%13 = tail call i32 @sleep(i32 10)
%14 = tail call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
%16 = tail call i32 (...) @getpid()
br i1 %15, label %17, label %26
17: ; preds = %11
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 @setpgid(i32 %16, i32 %18)
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 (...) @getppid()
%22 = sext i32 %21 to i64
%23 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %20, i64 %22)
%24 = tail call i32 @sleep(i32 3600)
%25 = tail call i32 @exit(i32 0)
unreachable
26: ; preds = %11
%27 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %16, i64 %14)
%28 = tail call i32 @sleep(i32 10)
%29 = tail call i64 (...) @fork()
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %48
31: ; preds = %26
%32 = tail call i64 (...) @fork()
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = tail call i32 (...) @setsid()
%36 = tail call i32 (...) @getpid()
%37 = tail call i32 (...) @getppid()
%38 = sext i32 %37 to i64
%39 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %36, i64 %38)
%40 = tail call i32 @sleep(i32 86400)
%41 = tail call i32 @exit(i32 0)
unreachable
42: ; preds = %31
%43 = icmp sgt i64 %32, 0
br i1 %43, label %44, label %48
44: ; preds = %42
%45 = tail call i32 (...) @getpid()
%46 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %45, i64 %32)
%47 = tail call i32 @exit(i32 0)
unreachable
48: ; preds = %42, %26
%49 = phi i64 [ %32, %42 ], [ %29, %26 ]
%50 = tail call i32 (...) @getpid()
%51 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %50, i64 %49)
br label %52
52: ; preds = %52, %48
%53 = tail call i64 @wait(i32* null)
%54 = tail call i32 (...) @getpid()
%55 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %54, i64 %53)
br label %52
}