#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ printf(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int setpgid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
int main() {
  setbuf(stdout, NULL);
  printf("[Parent] creating a child\n");
  switch (fork()) {
  case -1:
    pexit("fork");
    break;
  case 0:
    printf("[Child] created\n");
    if (setpgid(0, 0) == -1)
      pexit("setpgid");
    printf("[Child] Created its own process group\n");
    printf("[Child] Waiting for parent to complete\n");
    sleep(3);
    char buf[8];
    if (read(STDIN_FILENO, buf, 8) == -1) {
      printf("[Child] read(2) failed. Check error below.\n");
      pexit("read");
    }
    break;
  default:
    printf("[Parent] finishing in order to make orphaned process group\n");
    exit(EXIT_SUCCESS);
    break;
  }
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,-0x14(%rbp)
mov    %eax,%esi
call   1030 <setbuf@plt>
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11db <main+0x5b>
jmp    11cb <main+0x4b>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11ec <main+0x6c>
jmp    1275 <main+0xf5>
lea    0xe39(%rip),%rdi
call   1080 <pexit@plt>
jmp    128f <main+0x10f>
lea    0xe2d(%rip),%rdi
call   1040 <printf@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1060 <setpgid@plt>
cmp    $0xffffffff,%eax
jne    1216 <main+0x96>
lea    0xe20(%rip),%rdi
call   1080 <pexit@plt>
lea    0xe1c(%rip),%rdi
call   1040 <printf@plt>
lea    0xe37(%rip),%rdi
call   1040 <printf@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    1270 <main+0xf0>
lea    0xe29(%rip),%rdi
call   1040 <printf@plt>
lea    0xe49(%rip),%rdi
call   1080 <pexit@plt>
jmp    128f <main+0x10f>
lea    0xe3d(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <setbuf@plt>
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     122d <main+0xad>
test   %eax,%eax
jne    1247 <main+0xc7>
lea    0xe70(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1060 <setpgid@plt>
cmp    $0xffffffff,%eax
jne    11db <main+0x5b>
lea    0xe2f(%rip),%rdi
call   1080 <pexit@plt>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    1239 <main+0xb9>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
lea    0xde2(%rip),%rdi
jmp    1234 <main+0xb4>
lea    0xdcc(%rip),%rdi
call   1080 <pexit@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [27 x i8] c"[Parent] creating a child\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [17 x i8] c"[Child] created\0A\00"
@.str.3 = [8 x i8] c"setpgid\00"
@.str.4 = [39 x i8] c"[Child] Created its own process group\0A\00"
@.str.5 = [40 x i8] c"[Child] Waiting for parent to complete\0A\00"
@STDIN_FILENO = global i32 0
@.str.6 = [44 x i8] c"[Child] read(2) failed. Check error below.\0A\00"
@.str.7 = [5 x i8] c"read\00"
@.str.8 = [60 x i8] c"[Parent] finishing in order to make orphaned process group\0A\00"
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [8 x i8]
store i32 0, i32* %1
%3 = load i32, i32* @stdout
%4 = call i32 @setbuf(i32 %3, i32* null)
%5 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (...) @fork()
switch i32 %6, label %27 [
i32 -1, label %7
i32 0, label %9
]
7: ; preds = %0
%8 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %31
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @setpgid(i32 0, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%17 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
%18 = call i32 @sleep(i32 3)
%19 = load i32, i32* @STDIN_FILENO
%20 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 8)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0))
%25 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
br label %26
26: ; preds = %23, %15
br label %31
27: ; preds = %0
%28 = call i32 @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.8, i64 0, i64 0))
%29 = load i32, i32* @EXIT_SUCCESS
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %26, %7
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
}

LLVM IR (-O2):
@stdout = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@.str.3 = [8 x i8] c"setpgid\00"
@STDIN_FILENO = global i32 0
@.str.7 = [5 x i8] c"read\00"
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
@str = [26 x i8] c"[Parent] creating a child\00"
@str.9 = [16 x i8] c"[Child] created\00"
@str.10 = [38 x i8] c"[Child] Created its own process group\00"
@str.11 = [39 x i8] c"[Child] Waiting for parent to complete\00"
@str.12 = [43 x i8] c"[Child] read(2) failed. Check error below.\00"
@str.13 = [59 x i8] c"[Parent] finishing in order to make orphaned process group\00"
define i32 @main() {
%1 = alloca [8 x i8]
%2 = load i32, i32* @stdout
%3 = tail call i32 @setbuf(i32 %2, i32* null)
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 (...) @fork()
switch i32 %5, label %22 [
i32 -1, label %26
i32 0, label %6
]
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.9, i64 0, i64 0))
%8 = tail call i32 @setpgid(i32 0, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.10, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.11, i64 0, i64 0))
%15 = tail call i32 @sleep(i32 3)
%16 = load i32, i32* @STDIN_FILENO
%17 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 8)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %29
20: ; preds = %12
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.12, i64 0, i64 0))
br label %26
22: ; preds = %0
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str.13, i64 0, i64 0))
%24 = load i32, i32* @EXIT_SUCCESS
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %0, %20
%27 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), %20 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %0 ]
%28 = tail call i32 @pexit(i8* %27)
br label %29
29: ; preds = %26, %12
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
}