#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ get_info(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ tcgetpgrp(int);
scalar_t__ tcgetsid(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  int fd;
  get_info("CALLING PROCESS", 0);
  fd = open("/dev/tty", O_RDWR);
  printf("PGID leader: %ld\n", (long)tcgetpgrp(fd));
  printf("PGID foreground: %ld\n", (long)tcgetsid(0));
  switch (pid = fork()) {
  case -1:
    fprintf(stderr, "Err.(%s) fork() failed\n", strerror(errno));
    exit(EXIT_FAILURE);
  case 0:
    setsid();
    get_info("CHILD PROCESS", pid);
    exit(EXIT_SUCCESS);
  default:
    waitpid(pid, NULL, 0);
  }
  return (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)
lea    0xe33(%rip),%rdi
xor    %esi,%esi
mov    %esi,-0x20(%rbp)
call   1040 <get_info@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    0xe29(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <tcgetpgrp@plt>
mov    %rax,%rsi
lea    0xe18(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
call   1030 <tcgetsid@plt>
mov    %rax,%rsi
lea    0xe13(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%eax
call   10d0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     124b <main+0x9b>
jmp    123b <main+0x8b>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     1285 <main+0xd5>
jmp    12a9 <main+0xf9>
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xdcf(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    -0x14(%rbp),%esi
lea    0xdc3(%rip),%rdi
call   1040 <get_info@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe48(%rip),%rdi
xor    %esi,%esi
call   1040 <get_info@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    0xe41(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%edi
call   1080 <tcgetpgrp@plt>
lea    0xe37(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
xor    %edi,%edi
call   1030 <tcgetsid@plt>
lea    0xe33(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     1226 <main+0x76>
cmp    $0xffffffff,%eax
je     1249 <main+0x99>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
xor    %eax,%eax
call   1060 <setsid@plt>
lea    0xe25(%rip),%rdi
xor    %esi,%esi
call   1040 <get_info@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%ebx
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdda(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"CALLING PROCESS\00"
@.str.1 = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@.str.2 = [18 x i8] c"PGID leader: %ld\0A\00"
@.str.3 = [22 x i8] c"PGID foreground: %ld\0A\00"
@stderr = global i32 0
@.str.4 = [24 x i8] c"Err.(%s) fork() failed\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.5 = [14 x i8] c"CHILD PROCESS\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 @get_info(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 0)
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = call i64 @tcgetpgrp(i32 %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 %12)
%14 = call i64 @tcgetsid(i32 0)
%15 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %14)
%16 = call i32 (...) @fork()
store i32 %16, i32* %6
switch i32 %16, label %30 [
i32 -1, label %17
i32 0, label %24
]
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = call i32 (...) @setsid()
%26 = load i32, i32* %6
%27 = call i32 @get_info(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = load i32, i32* @EXIT_SUCCESS
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %2
%31 = load i32, i32* %6
%32 = call i32 @waitpid(i32 %31, i32* null, i32 0)
br label %33
33: ; preds = %30
%34 = load i32, i32* @EXIT_SUCCESS
ret i32 %34
}

LLVM IR (-O2):
@.str = [16 x i8] c"CALLING PROCESS\00"
@.str.1 = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@.str.2 = [18 x i8] c"PGID leader: %ld\0A\00"
@.str.3 = [22 x i8] c"PGID foreground: %ld\0A\00"
@stderr = global i32 0
@.str.4 = [24 x i8] c"Err.(%s) fork() failed\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.5 = [14 x i8] c"CHILD PROCESS\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @get_info(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = tail call i64 @tcgetpgrp(i32 %5)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 %6)
%8 = tail call i64 @tcgetsid(i32 0)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %8)
%10 = tail call i32 (...) @fork()
switch i32 %10, label %23 [
i32 -1, label %11
i32 0, label %18
]
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = tail call i32 (...) @setsid()
%20 = tail call i32 @get_info(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 0)
%21 = load i32, i32* @EXIT_SUCCESS
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = tail call i32 @waitpid(i32 %10, i32* null, i32 0)
%25 = load i32, i32* @EXIT_SUCCESS
ret i32 %25
}