#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 /*<<< orphan*/ setpgid(int, int);
int main() {
  int pid = getpid();
  int ppid = getppid();
  int pgrp = getpgrp();
  int pgid = getpgid(0);
  printf("parent %d %d %d %d\n", pid, ppid, pgrp, pgid);
  if (fork() == 0) {
    int cpid = getpid();
    int cppid = getppid();
    int cpgrp = getpgrp();
    int cpgid = getpgid(0);
    printf("child before %d %d %d %d\n", cpid, cppid, cpgrp, cpgid);
    setpgid(cpid, pgid);
    cpid = getpid();
    cppid = getppid();
    cpgrp = getpgrp();
    cpgid = getpgid(0);
    printf("child after %d %d %d %d\n", cpid, cppid, cpgrp, cpgid);
  }
  if (fork() == 0) {
    int cpid = getpid();
    int cppid = getppid();
    int cpgrp = getpgrp();
    int cpgid = getpgid(0);
    printf("child2 before %d %d %d %d\n", cpid, cppid, cpgrp, cpgid);
    setpgid(cpgid, 1000);
    cpid = getpid();
    cppid = getppid();
    cpgrp = getpgrp();
    cpgid = getpgid(0);
    printf("child2 after %d %d %d %d\n", cpid, cppid, cpgrp, cpgid);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1050 <getpgrp@plt>
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   1060 <getpgid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0x14(%rbp),%r8d
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    125e <main+0xee>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <getpgrp@plt>
mov    %eax,-0x20(%rbp)
xor    %edi,%edi
call   1060 <getpgid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%r8d
lea    0xe07(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1070 <setpgid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <getpgrp@plt>
mov    %eax,-0x20(%rbp)
xor    %edi,%edi
call   1060 <getpgid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%r8d
lea    0xdd5(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    12fe <main+0x18e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1050 <getpgrp@plt>
mov    %eax,-0x30(%rbp)
xor    %edi,%edi
call   1060 <getpgid@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x34(%rbp),%r8d
lea    0xd9c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x34(%rbp),%edi
mov    $0x3e8,%esi
call   1070 <setpgid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1050 <getpgrp@plt>
mov    %eax,-0x30(%rbp)
xor    %edi,%edi
call   1060 <getpgid@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x34(%rbp),%r8d
lea    0xd69(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getpgrp@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1060 <getpgid@plt>
mov    %eax,%r14d
lea    0xe5c(%rip),%rdi
mov    %r15d,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    123b <main+0xcb>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1050 <getpgrp@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1060 <getpgid@plt>
lea    0xe2b(%rip),%rdi
mov    %ebx,%esi
mov    %r15d,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %r14d,%esi
call   1070 <setpgid@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getpgrp@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1060 <getpgid@plt>
lea    0xe02(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    12cd <main+0x15d>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1050 <getpgrp@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1060 <getpgid@plt>
mov    %eax,%ebp
lea    0xdcf(%rip),%rdi
mov    %r14d,%esi
mov    %r15d,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
call   1040 <printf@plt>
mov    %ebp,%edi
mov    $0x3e8,%esi
call   1070 <setpgid@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getpgrp@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1060 <getpgid@plt>
lea    0xda4(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [20 x i8] c"parent %d %d %d %d\0A\00"
@.str.1 = [26 x i8] c"child before %d %d %d %d\0A\00"
@.str.2 = [25 x i8] c"child after %d %d %d %d\0A\00"
@.str.3 = [27 x i8] c"child2 before %d %d %d %d\0A\00"
@.str.4 = [26 x i8] c"child2 after %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 %20
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 ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %10, i32 %11, i32 %12)
%14 = tail call i32 @setpgid(i32 %9, i32 %4)
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 (...) @getppid()
%17 = tail call i32 (...) @getpgrp()
%18 = tail call i32 @getpgid(i32 0)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %16, i32 %17, i32 %18)
br label %20
20: ; preds = %8, %0
%21 = tail call i64 (...) @fork()
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %20
%24 = tail call i32 (...) @getpid()
%25 = tail call i32 (...) @getppid()
%26 = tail call i32 (...) @getpgrp()
%27 = tail call i32 @getpgid(i32 0)
%28 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %24, i32 %25, i32 %26, i32 %27)
%29 = tail call i32 @setpgid(i32 %27, i32 1000)
%30 = tail call i32 (...) @getpid()
%31 = tail call i32 (...) @getppid()
%32 = tail call i32 (...) @getpgrp()
%33 = tail call i32 @getpgid(i32 0)
%34 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %30, i32 %31, i32 %32, i32 %33)
br label %35
35: ; preds = %23, %20
ret i32 0
}