#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 */
scalar_t__ fork();
int getpgid(int /*<<< orphan*/);
int getpgrp();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int, int, int);
int main() {
  printf("parent %d %d %d %d\n", getpid(), getppid(), getpgrp(), getpgid(0));
  for (int i = 0; i < 3; ++i) {
    if (fork() == 0) {
      printf("child %d %d %d %d\n", getpid(), getppid(), getpgrp(), getpgid(0));
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getpgrp@plt>
mov    %eax,-0xc(%rbp)
xor    %edi,%edi
call   1060 <getpgid@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    %eax,%r8d
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    121e <main+0xbe>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jne    120b <main+0xab>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <getpgrp@plt>
mov    %eax,-0x18(%rbp)
xor    %edi,%edi
call   1060 <getpgid@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    %eax,%r8d
lea    0xe0e(%rip),%rdi
call   1040 <printf@plt>
jmp    1210 <main+0xb0>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11b3 <main+0x53>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1070 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getpgrp@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1060 <getpgid@plt>
lea    0xe72(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    11e2 <main+0x82>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1070 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getpgrp@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1060 <getpgid@plt>
lea    0xe41(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    1227 <main+0xc7>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1070 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getpgrp@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1060 <getpgid@plt>
lea    0xdfc(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    126c <main+0x10c>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1070 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getpgrp@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1060 <getpgid@plt>
lea    0xdb7(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"parent %d %d %d %d\0A\00"
@.str.1 = [19 x i8] c"child %d %d %d %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (...) @getpid()
%4 = call i32 (...) @getppid()
%5 = call i32 (...) @getpgrp()
%6 = call i32 @getpgid(i32 0)
%7 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4, i32 %5, i32 %6)
store i32 0, i32* %2
br label %8
8: ; preds = %21, %0
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %24
11: ; preds = %8
%12 = call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = call i32 (...) @getpid()
%16 = call i32 (...) @getppid()
%17 = call i32 (...) @getpgrp()
%18 = call i32 @getpgid(i32 0)
%19 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %16, i32 %17, i32 %18)
br label %20
20: ; preds = %14, %11
br label %21
21: ; preds = %20
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
br label %8
24: ; preds = %8
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [20 x i8] c"parent %d %d %d %d\0A\00"
@.str.1 = [19 x i8] c"child %d %d %d %d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (...) @getppid()
%3 = tail call i32 (...) @getpgrp()
%4 = tail call i32 @getpgid(i32 0)
%5 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2, i32 %3, i32 %4)
%6 = tail call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 (...) @getppid()
%11 = tail call i32 (...) @getpgrp()
%12 = tail call i32 @getpgid(i32 0)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %10, i32 %11, i32 %12)
br label %14
14: ; preds = %0, %8
%15 = tail call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (...) @getppid()
%20 = tail call i32 (...) @getpgrp()
%21 = tail call i32 @getpgid(i32 0)
%22 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19, i32 %20, i32 %21)
br label %23
23: ; preds = %17, %14
%24 = tail call i64 (...) @fork()
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = tail call i32 (...) @getpid()
%28 = tail call i32 (...) @getppid()
%29 = tail call i32 (...) @getpgrp()
%30 = tail call i32 @getpgid(i32 0)
%31 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %27, i32 %28, i32 %29, i32 %30)
br label %32
32: ; preds = %26, %23
ret i32 0
}