#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_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpgrp();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setpgid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    perror("fork() ");
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    printf("This is child: pid %u, parent pid %u and group id %u.\n", getpid(),
           getppid(), getpgrp());
    printf("Set a new group.\n");
    setpgid(0, 0);
    printf("This is child: pid %u, parent pid %u and group id %u.\n", getpid(),
           getppid(), getpgrp());
  } else {
    printf("This is parent: pid %u\n", getpid());
    sleep(3);
  }
  exit(EXIT_SUCCESS);
}


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)
jge    11d6 <main+0x46>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    125f <main+0xcf>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1050 <getpgrp@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    %eax,%ecx
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1070 <setpgid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <getpgrp@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1280 <main+0xf0>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
jmp    1285 <main+0xf5>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
jns    11ca <main+0x2a>
lea    0xe49(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
test   %rbx,%rbx
jne    1242 <main+0xa2>
xor    %eax,%eax
call   1090 <getppid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getpgrp@plt>
lea    0xe16(%rip),%r14
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1080 <setpgid@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getpgrp@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    125c <main+0xbc>
lea    0xdf6(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"fork() \00"
@EXIT_FAILURE = global i32 0
@.str.1 = [55 x i8] c"This is child: pid %u, parent pid %u and group id %u.\0A\00"
@.str.2 = [18 x i8] c"Set a new group.\0A\00"
@.str.3 = [24 x i8] c"This is parent: pid %u\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = load i64, i64* %6
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i64, i64* %6
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 (...) @getppid()
%20 = call i32 (...) @getpgrp()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19, i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @setpgid(i32 0, i32 0)
%24 = call i32 (...) @getpid()
%25 = call i32 (...) @getppid()
%26 = call i32 (...) @getpgrp()
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %25, i32 %26)
br label %32
28: ; preds = %14
%29 = call i32 (...) @getpid()
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = call i32 @sleep(i32 3)
br label %32
32: ; preds = %28, %17
br label %33
33: ; preds = %32
%34 = load i32, i32* @EXIT_SUCCESS
%35 = call i32 @exit(i32 %34)
unreachable
}

LLVM IR (-O2):
@.str = [8 x i8] c"fork() \00"
@EXIT_FAILURE = global i32 0
@.str.1 = [55 x i8] c"This is child: pid %u, parent pid %u and group id %u.\0A\00"
@.str.3 = [24 x i8] c"This is parent: pid %u\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [17 x i8] c"Set a new group.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = icmp eq i64 %3, 0
%11 = tail call i32 (...) @getpid()
br i1 %10, label %12, label %22
12: ; preds = %9
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 (...) @getpgrp()
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %13, i32 %14)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @setpgid(i32 0, i32 0)
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (...) @getppid()
%20 = tail call i32 (...) @getpgrp()
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19, i32 %20)
br label %25
22: ; preds = %9
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%24 = tail call i32 @sleep(i32 3)
br label %25
25: ; preds = %12, %22
%26 = load i32, i32* @EXIT_SUCCESS
%27 = tail call i32 @exit(i32 %26)
unreachable
}