#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(int);
scalar_t__ fork();
int getpgrp();
int getpid();
int /*<<< orphan*/ getppid();
int getsid(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, ...);
scalar_t__ setpgid(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  printf("pgid=%u, pid=%u\n", getpgrp(), getpid());
  pid_t pid;
  if ((pid = fork()) < 0) {
    perror("fork error");
    exit(10);
  } else if (pid == 0) {
    printf("child pgid=%u, pid=%u\n", getpgrp(), getpid());
    if (setpgid(0, getppid()) < 0) {
      perror("setpid error");
      exit(10);
    }
    printf("child pgid=%u, pid=%u\n", getpgrp(), getpid());
    if (setsid() < 0) {
      perror("child setsid error");
      exit(10);
    }
    printf("child after setsid sid=%u, pgid=%u, pid=%u\n", getsid(0), getpgrp(),
           getpid());
  } else {
    printf("parent pgid=%u, pid=%u\n", getpgrp(), getpid());
    wait(NULL);
    if (setsid() < 0) {
      perror("setsid error");
      exit(10);
    }
    printf("parent after setsid, pgid=%u, pid=%u\n", getpgrp(), getpid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <getpgrp@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1215 <main+0x65>
lea    0xdfa(%rip),%rdi
call   1080 <perror@plt>
mov    $0xa,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12f5 <main+0x145>
mov    $0x0,%al
call   1070 <getpgrp@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <getppid@plt>
mov    %eax,%esi
xor    %edi,%edi
call   1090 <setpgid@plt>
cmp    $0x0,%rax
jge    1274 <main+0xc4>
lea    0xdbd(%rip),%rdi
call   1080 <perror@plt>
mov    $0xa,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1070 <getpgrp@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    12bf <main+0x10f>
lea    0xd7f(%rip),%rdi
call   1080 <perror@plt>
mov    $0xa,%edi
call   10b0 <exit@plt>
xor    %edi,%edi
call   1050 <getsid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1070 <getpgrp@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    %eax,%ecx
lea    0xd59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    136d <main+0x1bd>
mov    $0x0,%al
call   1070 <getpgrp@plt>
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x30(%rbp),%esi
mov    %eax,%edx
lea    0xd5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    1349 <main+0x199>
lea    0xcfb(%rip),%rdi
call   1080 <perror@plt>
mov    $0xa,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1070 <getpgrp@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x34(%rbp),%esi
mov    %eax,%edx
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1372 <main+0x1c2>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <getpgrp@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xed1(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12d8 <main+0x128>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <getpgrp@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
test   %rbx,%rbx
je     124d <main+0x9d>
lea    0xe6a(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     12e1 <main+0x131>
xor    %eax,%eax
call   1070 <getpgrp@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe49(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12cf <main+0x11f>
lea    0xdb7(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   10a0 <getppid@plt>
xor    %edi,%edi
mov    %eax,%esi
call   1090 <setpgid@plt>
test   %rax,%rax
js     12ea <main+0x13a>
xor    %eax,%eax
call   1070 <getpgrp@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd80(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     12f3 <main+0x143>
xor    %edi,%edi
call   1050 <getsid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getpgrp@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd80(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd21(%rip),%rdi
jmp    12fa <main+0x14a>
lea    0xd4d(%rip),%rdi
jmp    12fa <main+0x14a>
lea    0xd31(%rip),%rdi
jmp    12fa <main+0x14a>
lea    0xd35(%rip),%rdi
call   1080 <perror@plt>
mov    $0xa,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"pgid=%u, pid=%u\0A\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [23 x i8] c"child pgid=%u, pid=%u\0A\00"
@.str.3 = [13 x i8] c"setpid error\00"
@.str.4 = [19 x i8] c"child setsid error\00"
@.str.5 = [44 x i8] c"child after setsid sid=%u, pgid=%u, pid=%u\0A\00"
@.str.6 = [24 x i8] c"parent pgid=%u, pid=%u\0A\00"
@.str.7 = [13 x i8] c"setsid error\00"
@.str.8 = [38 x i8] c"parent after setsid, pgid=%u, pid=%u\0A\00"
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 i32 (...) @getpgrp()
%8 = call i32 (...) @getpid()
%9 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = call i64 (...) @fork()
store i64 %10, i64* %6
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 10)
unreachable
15: ; preds = %2
%16 = load i64, i64* %6
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %42
18: ; preds = %15
%19 = call i32 (...) @getpgrp()
%20 = call i32 (...) @getpid()
%21 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20)
%22 = call i32 (...) @getppid()
%23 = call i64 @setpgid(i32 0, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 10)
unreachable
28: ; preds = %18
%29 = call i32 (...) @getpgrp()
%30 = call i32 (...) @getpid()
%31 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %30)
%32 = call i64 (...) @setsid()
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @exit(i32 10)
unreachable
37: ; preds = %28
%38 = call i32 @getsid(i32 0)
%39 = call i32 (...) @getpgrp()
%40 = call i32 (...) @getpid()
%41 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i32 %38, i32 %39, i32 %40)
br label %56
42: ; preds = %15
%43 = call i32 (...) @getpgrp()
%44 = call i32 (...) @getpid()
%45 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %43, i32 %44)
%46 = call i32 @wait(i32* null)
%47 = call i64 (...) @setsid()
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%51 = call i32 @exit(i32 10)
unreachable
52: ; preds = %42
%53 = call i32 (...) @getpgrp()
%54 = call i32 (...) @getpid()
%55 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.8, i64 0, i64 0), i32 %53, i32 %54)
br label %56
56: ; preds = %52, %37
br label %57
57: ; preds = %56
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"pgid=%u, pid=%u\0A\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [23 x i8] c"child pgid=%u, pid=%u\0A\00"
@.str.3 = [13 x i8] c"setpid error\00"
@.str.4 = [19 x i8] c"child setsid error\00"
@.str.5 = [44 x i8] c"child after setsid sid=%u, pgid=%u, pid=%u\0A\00"
@.str.6 = [24 x i8] c"parent pgid=%u, pid=%u\0A\00"
@.str.7 = [13 x i8] c"setsid error\00"
@.str.8 = [38 x i8] c"parent after setsid, pgid=%u, pid=%u\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpgrp()
%4 = tail call i32 (...) @getpid()
%5 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4)
%6 = tail call i64 (...) @fork()
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 10)
unreachable
11: ; preds = %2
%12 = icmp eq i64 %6, 0
%13 = tail call i32 (...) @getpgrp()
%14 = tail call i32 (...) @getpid()
br i1 %12, label %15, label %37
15: ; preds = %11
%16 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %14)
%17 = tail call i32 (...) @getppid()
%18 = tail call i64 @setpgid(i32 0, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 @exit(i32 10)
unreachable
23: ; preds = %15
%24 = tail call i32 (...) @getpgrp()
%25 = tail call i32 (...) @getpid()
%26 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %25)
%27 = tail call i64 (...) @setsid()
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @exit(i32 10)
unreachable
32: ; preds = %23
%33 = tail call i32 @getsid(i32 0)
%34 = tail call i32 (...) @getpgrp()
%35 = tail call i32 (...) @getpid()
%36 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i32 %33, i32 %34, i32 %35)
br label %49
37: ; preds = %11
%38 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %13, i32 %14)
%39 = tail call i32 @wait(i32* null)
%40 = tail call i64 (...) @setsid()
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%44 = tail call i32 @exit(i32 10)
unreachable
45: ; preds = %37
%46 = tail call i32 (...) @getpgrp()
%47 = tail call i32 (...) @getpid()
%48 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.8, i64 0, i64 0), i32 %46, i32 %47)
br label %49
49: ; preds = %32, %45
ret i32 0
}