#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 int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ alarm(int);
int execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ getchar();
scalar_t__ getpgrp();
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ printf(char *, ...);
int setpgid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  pid_t child_pid;
  char *params[] = {"sleep", "5", NULL};
  printf(">> Parent process (PID=%ld PPID=%ld PGID=%ld)\n", (long)getpid(),
         (long)getppid(), (long)getpgrp());
  printf(">> [Parent] creating child. Press Return when an attempt to change "
         "the child process group is to be made\n\n");
  switch (child_pid = fork()) {
  case -1:
    pexit("fork");
    break;
  case 0:
    printf("\n>> Child (PID=%ld PPID=%ld PGID=%ld) going to sleep\n",
           (long)getpid(), (long)getppid(), (long)getpgrp());
    sleep(5);
    printf(">> Child performing an exec\n");
    if (execvp("/usr/bin/sleep", params) == -1)
      pexit("execv");
    _exit(EXIT_FAILURE);
  default:
    break;
  }
  alarm(20);
  printf("Press Return to try to change the child process group");
  getchar();
  printf(">> [Parent] Attempting to change child process group\n");
  if (setpgid(child_pid, child_pid) == -1) {
    printf(">> Parent failed. Error message below\n");
    pexit("setpgid");
  } else {
    printf(">> Child process group successfully changed\n");
    printf(">> Waiting for child to terminate\n");
    wait(NULL);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2c0a(%rip),%rax
mov    %rax,-0x10(%rbp)
movaps 0x2bef(%rip),%xmm0
movaps %xmm0,-0x20(%rbp)
xor    %eax,%eax
mov    %al,-0x25(%rbp)
call   1040 <getpid@plt>
mov    %rax,%rcx
mov    -0x25(%rbp),%al
mov    %rcx,-0x38(%rbp)
call   10b0 <getppid@plt>
mov    %rax,%rcx
mov    -0x25(%rbp),%al
mov    %rcx,-0x30(%rbp)
call   1080 <getpgrp@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
mov    -0x25(%rbp),%al
lea    0xdc0(%rip),%rdi
call   1050 <printf@plt>
mov    -0x25(%rbp),%al
lea    0xde0(%rip),%rdi
call   1050 <printf@plt>
mov    -0x25(%rbp),%al
call   1100 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
sub    $0xffffffff,%eax
je     1284 <main+0xa4>
jmp    1274 <main+0x94>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     1295 <main+0xb5>
jmp    1316 <main+0x136>
lea    0xe11(%rip),%rdi
call   10d0 <pexit@plt>
jmp    131b <main+0x13b>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   10b0 <getppid@plt>
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1080 <getpgrp@plt>
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x5,%edi
call   10e0 <sleep@plt>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x20(%rbp),%rsi
lea    0xe06(%rip),%rdi
call   1090 <execvp@plt>
cmp    $0xffffffff,%eax
jne    1308 <main+0x128>
lea    0xe00(%rip),%rdi
call   10d0 <pexit@plt>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    131b <main+0x13b>
mov    $0x14,%edi
call   1060 <alarm@plt>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
call   10a0 <setpgid@plt>
cmp    $0xffffffff,%eax
jne    137b <main+0x19b>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe2b(%rip),%rdi
call   10d0 <pexit@plt>
jmp    13a0 <main+0x1c0>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10f0 <wait@plt>
mov    0x2c29(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2c02(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0x2be6(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,%r14
xor    %eax,%eax
call   10c0 <getppid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <getpgrp@plt>
lea    0xdd2(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe78(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
je     12c4 <main+0xd4>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    126a <main+0x7a>
lea    0xdcc(%rip),%rdi
call   10e0 <pexit@plt>
mov    $0x14,%edi
call   1070 <alarm@plt>
lea    0xe06(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
lea    0xeb5(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    %ebx,%esi
call   10b0 <setpgid@plt>
cmp    $0xffffffff,%eax
jne    133a <main+0x14a>
lea    0xf1a(%rip),%rdi
call   1040 <puts@plt>
lea    0xdfd(%rip),%rdi
call   10e0 <pexit@plt>
jmp    1359 <main+0x169>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,%r14
xor    %eax,%eax
call   10c0 <getppid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <getpgrp@plt>
lea    0xd50(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x5,%edi
call   10f0 <sleep@plt>
lea    0xe22(%rip),%rdi
call   1040 <puts@plt>
lea    0xd59(%rip),%rdi
mov    %rsp,%rsi
call   10a0 <execvp@plt>
cmp    $0xffffffff,%eax
jne    132c <main+0x13c>
lea    0xd54(%rip),%rdi
call   10e0 <pexit@plt>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
lea    0xe39(%rip),%rdi
call   1040 <puts@plt>
lea    0xe59(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1100 <wait@plt>
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"sleep\00"
@.str.1 = [2 x i8] c"5\00"
@__const.main.params = [3 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [47 x i8] c">> Parent process (PID=%ld PPID=%ld PGID=%ld)\0A\00"
@.str.3 = [107 x i8] c">> [Parent] creating child. Press Return when an attempt to change the child process group is to be made\0A\0A\00"
@.str.4 = [5 x i8] c"fork\00"
@.str.5 = [54 x i8] c"\0A>> Child (PID=%ld PPID=%ld PGID=%ld) going to sleep\0A\00"
@.str.6 = [29 x i8] c">> Child performing an exec\0A\00"
@.str.7 = [15 x i8] c"/usr/bin/sleep\00"
@.str.8 = [6 x i8] c"execv\00"
@EXIT_FAILURE = global i32 0
@.str.9 = [54 x i8] c"Press Return to try to change the child process group\00"
@.str.10 = [54 x i8] c">> [Parent] Attempting to change child process group\0A\00"
@.str.11 = [39 x i8] c">> Parent failed. Error message below\0A\00"
@.str.12 = [8 x i8] c"setpgid\00"
@.str.13 = [45 x i8] c">> Child process group successfully changed\0A\00"
@.str.14 = [35 x i8] c">> Waiting for child to terminate\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [3 x i8*]
store i32 0, i32* %1
%4 = bitcast [3 x i8*]* %3 to i8*
%5 = call i64 (...) @getpid()
%6 = call i64 (...) @getppid()
%7 = call i64 (...) @getpgrp()
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i64 %5, i64 %6, i64 %7)
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([107 x i8], [107 x i8]* @.str.3, i64 0, i64 0))
%10 = call i32 (...) @fork()
store i32 %10, i32* %2
switch i32 %10, label %28 [
i32 -1, label %11
i32 0, label %13
]
11: ; preds = %0
%12 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %29
13: ; preds = %0
%14 = call i64 (...) @getpid()
%15 = call i64 (...) @getppid()
%16 = call i64 (...) @getpgrp()
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), i64 %14, i64 %15, i64 %16)
%18 = call i32 @sleep(i32 5)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%20 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%21 = call i32 @execvp(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8** %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
br label %25
25: ; preds = %23, %13
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @_exit(i32 %26)
unreachable
28: ; preds = %0
br label %29
29: ; preds = %28, %11
%30 = call i32 @alarm(i32 20)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.9, i64 0, i64 0))
%32 = call i32 (...) @getchar()
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.10, i64 0, i64 0))
%34 = load i32, i32* %2
%35 = load i32, i32* %2
%36 = call i32 @setpgid(i32 %34, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %29
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.11, i64 0, i64 0))
%40 = call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.12, i64 0, i64 0))
br label %45
41: ; preds = %29
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.13, i64 0, i64 0))
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.14, i64 0, i64 0))
%44 = call i32 @wait(i32* null)
br label %45
45: ; preds = %41, %38
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"sleep\00"
@.str.1 = [2 x i8] c"5\00"
@__const.main.params = [3 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [47 x i8] c">> Parent process (PID=%ld PPID=%ld PGID=%ld)\0A\00"
@.str.4 = [5 x i8] c"fork\00"
@.str.5 = [54 x i8] c"\0A>> Child (PID=%ld PPID=%ld PGID=%ld) going to sleep\0A\00"
@.str.7 = [15 x i8] c"/usr/bin/sleep\00"
@.str.8 = [6 x i8] c"execv\00"
@EXIT_FAILURE = global i32 0
@.str.9 = [54 x i8] c"Press Return to try to change the child process group\00"
@.str.12 = [8 x i8] c"setpgid\00"
@EXIT_SUCCESS = global i32 0
@str = [106 x i8] c">> [Parent] creating child. Press Return when an attempt to change the child process group is to be made\0A\00"
@str.15 = [28 x i8] c">> Child performing an exec\00"
@str.16 = [53 x i8] c">> [Parent] Attempting to change child process group\00"
@str.17 = [44 x i8] c">> Child process group successfully changed\00"
@str.18 = [34 x i8] c">> Waiting for child to terminate\00"
@str.19 = [38 x i8] c">> Parent failed. Error message below\00"
define i32 @main() {
%1 = alloca [3 x i8*]
%2 = bitcast [3 x i8*]* %1 to i8*
%3 = tail call i64 (...) @getpid()
%4 = tail call i64 (...) @getppid()
%5 = tail call i64 (...) @getpgrp()
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i64 %3, i64 %4, i64 %5)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([106 x i8], [106 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 (...) @fork()
switch i32 %8, label %26 [
i32 -1, label %9
i32 0, label %11
]
9: ; preds = %0
%10 = tail call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %26
11: ; preds = %0
%12 = tail call i64 (...) @getpid()
%13 = tail call i64 (...) @getppid()
%14 = tail call i64 (...) @getpgrp()
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), i64 %12, i64 %13, i64 %14)
%16 = tail call i32 @sleep(i32 5)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.15, i64 0, i64 0))
%18 = getelementptr inbounds [3 x i8*], [3 x i8*]* %1, i64 0, i64 0
%19 = call i32 @execvp(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8** %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
br label %23
23: ; preds = %21, %11
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @_exit(i32 %24)
unreachable
26: ; preds = %0, %9
%27 = tail call i32 @alarm(i32 20)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.9, i64 0, i64 0))
%29 = tail call i32 (...) @getchar()
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.16, i64 0, i64 0))
%31 = tail call i32 @setpgid(i32 %8, i32 %8)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.19, i64 0, i64 0))
%35 = tail call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.12, i64 0, i64 0))
br label %40
36: ; preds = %26
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.17, i64 0, i64 0))
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.18, i64 0, i64 0))
%39 = tail call i32 @wait(i32* null)
br label %40
40: ; preds = %36, %33
%41 = load i32, i32* @EXIT_SUCCESS
%42 = tail call i32 @exit(i32 %41)
unreachable
}