#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 pid_t;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ err(char *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ get_process_info(int);
int getpgrp();
int /*<<< orphan*/ setpgid(int, int);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  pid_t pgid;
  switch (pid = fork()) {
  case -1:
    err("fork() failed");
  case 0:
    if ((pgid = getpgrp()) == -1)
      err("getpgrp() failed");
    get_process_info(pgid);
    if ((pid = fork()) < 0)
      err("fork() failed");
    else if (pid == 0) {
      if (setpgid(pid, pgid) < 0)
        err("setpgid() failed");
      get_process_info(pgid);
    } else {
      waitpid(pid, NULL, 0);
      exit(EXIT_SUCCESS);
    }
    exit(EXIT_SUCCESS);
  default:
    waitpid(pid, NULL, 0);
  }
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     11b3 <main+0x43>
jmp    11a3 <main+0x33>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     11bf <main+0x4f>
jmp    1270 <main+0x100>
lea    0xe46(%rip),%rdi
call   1030 <err@plt>
mov    $0x0,%al
call   1050 <getpgrp@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11de <main+0x6e>
lea    0xe35(%rip),%rdi
call   1030 <err@plt>
mov    -0x18(%rbp),%edi
call   1060 <get_process_info@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    120a <main+0x9a>
lea    0xe00(%rip),%rdi
call   1030 <err@plt>
jmp    1262 <main+0xf2>
cmpl   $0x0,-0x14(%rbp)
jne    1241 <main+0xd1>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1070 <setpgid@plt>
cmp    $0x0,%eax
jge    1234 <main+0xc4>
lea    0xdf0(%rip),%rdi
call   1030 <err@plt>
mov    -0x18(%rbp),%edi
call   1060 <get_process_info@plt>
jmp    125d <main+0xed>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1262 <main+0xf2>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11a3 <main+0x33>
cmp    $0xffffffff,%eax
je     1197 <main+0x27>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
lea    0xe62(%rip),%rdi
call   1030 <err@plt>
xor    %eax,%eax
call   1050 <getpgrp@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11bd <main+0x4d>
lea    0xe56(%rip),%rdi
call   1030 <err@plt>
mov    %ebx,%edi
call   1060 <get_process_info@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jns    11dd <main+0x6d>
lea    0xe2a(%rip),%rdi
call   1030 <err@plt>
jmp    120c <main+0x9c>
jne    1201 <main+0x91>
xor    %edi,%edi
mov    %ebx,%esi
call   1070 <setpgid@plt>
test   %eax,%eax
jns    11f8 <main+0x88>
lea    0xe2c(%rip),%rdi
call   1030 <err@plt>
mov    %ebx,%edi
call   1060 <get_process_info@plt>
jmp    120c <main+0x9c>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"fork() failed\00"
@.str.1 = [17 x i8] c"getpgrp() failed\00"
@.str.2 = [17 x i8] c"setpgid() failed\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @fork()
store i32 %8, i32* %6
switch i32 %8, label %45 [
i32 -1, label %9
i32 0, label %11
]
9: ; preds = %2
%10 = call i32 @err(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %2, %9
%12 = call i32 (...) @getpgrp()
store i32 %12, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @err(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = load i32, i32* %7
%18 = call i32 @get_process_info(i32 %17)
%19 = call i32 (...) @fork()
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @err(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %42
23: ; preds = %16
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %23
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = call i32 @setpgid(i32 %27, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @err(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = load i32, i32* %7
%35 = call i32 @get_process_info(i32 %34)
br label %41
36: ; preds = %23
%37 = load i32, i32* %6
%38 = call i32 @waitpid(i32 %37, i32* null, i32 0)
%39 = load i32, i32* @EXIT_SUCCESS
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %33
br label %42
42: ; preds = %41, %21
%43 = load i32, i32* @EXIT_SUCCESS
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %2
%46 = load i32, i32* %6
%47 = call i32 @waitpid(i32 %46, i32* null, i32 0)
br label %48
48: ; preds = %45
%49 = load i32, i32* @EXIT_SUCCESS
ret i32 %49
}

LLVM IR (-O2):
@.str = [14 x i8] c"fork() failed\00"
@.str.1 = [17 x i8] c"getpgrp() failed\00"
@.str.2 = [17 x i8] c"setpgid() failed\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %33 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %2
%5 = tail call i32 @err(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %2, %4
%7 = tail call i32 (...) @getpgrp()
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @err(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = tail call i32 @get_process_info(i32 %7)
%13 = tail call i32 (...) @fork()
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @err(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %30
17: ; preds = %11
%18 = icmp eq i32 %13, 0
br i1 %18, label %19, label %26
19: ; preds = %17
%20 = tail call i32 @setpgid(i32 0, i32 %7)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 @err(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
%25 = tail call i32 @get_process_info(i32 %7)
br label %30
26: ; preds = %17
%27 = tail call i32 @waitpid(i32 %13, i32* null, i32 0)
%28 = load i32, i32* @EXIT_SUCCESS
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %24, %15
%31 = load i32, i32* @EXIT_SUCCESS
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %2
%34 = tail call i32 @waitpid(i32 %3, i32* null, i32 0)
%35 = load i32, i32* @EXIT_SUCCESS
ret i32 %35
}