#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 /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int getpgrp();
int getpid();
int getsid(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print();
int /*<<< orphan*/ printf(char *, ...);
int setpgid(int, int);
int setsid();
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: session grpid\n");
    return -1;
  }
  if (setsid() == -1)
    perror("setsid");
  int ret = 0;
  pid_t pid = getpid();
  pid_t grp = getpgrp();
  pid_t sid = getsid(0);
  printf("parent %u: %u, %u\n", sid, pid, grp);
  pid_t cid = fork();
  if (cid < 0)
    err_sys("fork");
  else if (cid == 0) {
    if (setsid() == -1)
      err_sys("setsid");
    pid = getpid();
    grp = getpgrp();
    sid = getsid(0);
    printf("session %u: %u, %u\n", sid, pid, grp);
  } else
    exit(0);
  cid = fork();
  if (cid < 0)
    err_sys("fork");
  else if (cid == 0) {
    pid_t pid = getpid();
    pid_t grp = getpgrp();
    pid_t sid = getsid(0);
    printf("child %u: %u, %u\n", sid, pid, grp);
    ret = setpgid(0, 0);
    if (ret == -1)
      err_sys("setpgid child");
    execlp(argv[1], argv[1], NULL);
    exit(-1);
  }
  ret = setpgid(cid, cid);
  if (ret == -1)
    err_sys("setpgid 1");
  ret = setpgid(0, cid);
  if (ret == -1)
    err_sys("setpgid 2");
  print();
  return 0;
}


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)
cmpl   $0x2,-0x8(%rbp)
jge    11fa <main+0x3a>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e6 <main+0x226>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1216 <main+0x56>
lea    0xe05(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1090 <getpgrp@plt>
mov    %eax,-0x1c(%rbp)
xor    %edi,%edi
call   1060 <getsid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1277 <main+0xb7>
lea    0xdc3(%rip),%rdi
call   1030 <err_sys@plt>
jmp    12e3 <main+0x123>
cmpl   $0x0,-0x24(%rbp)
jne    12d7 <main+0x117>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    129d <main+0xdd>
lea    0xd7e(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1090 <getpgrp@plt>
mov    %eax,-0x1c(%rbp)
xor    %edi,%edi
call   1060 <getsid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12de <main+0x11e>
xor    %edi,%edi
call   10c0 <exit@plt>
jmp    12e3 <main+0x123>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1308 <main+0x148>
lea    0xd32(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1391 <main+0x1d1>
cmpl   $0x0,-0x24(%rbp)
jne    138c <main+0x1cc>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1090 <getpgrp@plt>
mov    %eax,-0x2c(%rbp)
xor    %edi,%edi
call   1060 <getsid@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x2c(%rbp),%ecx
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %esi,%esi
mov    %esi,%edi
call   10b0 <setpgid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1369 <main+0x1a9>
lea    0xcf7(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10d0 <execlp@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
jmp    1391 <main+0x1d1>
mov    -0x24(%rbp),%edi
mov    -0x24(%rbp),%esi
call   10b0 <setpgid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    13b5 <main+0x1f5>
lea    0xcb9(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x24(%rbp),%esi
xor    %edi,%edi
call   10b0 <setpgid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    13d8 <main+0x218>
lea    0xca0(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   1080 <print@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jg     11ef <main+0x1f>
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    134a <main+0x17a>
mov    %rsi,%r14
xor    %eax,%eax
call   1080 <setsid@plt>
cmp    $0xffffffff,%eax
je     1351 <main+0x181>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10a0 <getpgrp@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1070 <getsid@plt>
lea    0xde5(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
js     1287 <main+0xb7>
jne    1362 <main+0x192>
xor    %eax,%eax
call   1080 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1258 <main+0x88>
lea    0xdad(%rip),%rdi
call   1040 <err_sys@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10a0 <getpgrp@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1070 <getsid@plt>
lea    0xda7(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1293 <main+0xc3>
lea    0xd8c(%rip),%rdi
call   1040 <err_sys@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1301 <main+0x131>
jne    130d <main+0x13d>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10a0 <getpgrp@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1070 <getsid@plt>
lea    0xd71(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
xor    %esi,%esi
call   10c0 <setpgid@plt>
cmp    $0xffffffff,%eax
jne    12e9 <main+0x119>
lea    0xd61(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x8(%r14),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10e0 <execlp@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xd12(%rip),%rdi
call   1040 <err_sys@plt>
mov    %ebp,%edi
mov    %ebp,%esi
call   10c0 <setpgid@plt>
cmp    $0xffffffff,%eax
jne    1327 <main+0x157>
lea    0xd31(%rip),%rdi
call   1040 <err_sys@plt>
xor    %ebx,%ebx
xor    %edi,%edi
mov    %ebp,%esi
call   10c0 <setpgid@plt>
cmp    $0xffffffff,%eax
jne    1343 <main+0x173>
lea    0xd1f(%rip),%rdi
call   1040 <err_sys@plt>
xor    %eax,%eax
call   1090 <print@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xca8(%rip),%rdi
call   10b0 <perror@plt>
jmp    1202 <main+0x32>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"Usage: session grpid\0A\00"
@.str.1 = [7 x i8] c"setsid\00"
@.str.2 = [19 x i8] c"parent %u: %u, %u\0A\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [20 x i8] c"session %u: %u, %u\0A\00"
@.str.5 = [18 x i8] c"child %u: %u, %u\0A\00"
@.str.6 = [14 x i8] c"setpgid child\00"
@.str.7 = [10 x i8] c"setpgid 1\00"
@.str.8 = [10 x i8] c"setpgid 2\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %104
18: ; preds = %2
%19 = call i32 (...) @setsid()
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
store i32 0, i32* %6
%24 = call i32 (...) @getpid()
store i32 %24, i32* %7
%25 = call i32 (...) @getpgrp()
store i32 %25, i32* %8
%26 = call i32 @getsid(i32 0)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = load i32, i32* %7
%29 = load i32, i32* %8
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 %28, i32 %29)
%31 = call i32 (...) @fork()
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %23
%35 = call i32 @err_sys(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %55
36: ; preds = %23
%37 = load i32, i32* %10
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %52
39: ; preds = %36
%40 = call i32 (...) @setsid()
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @err_sys(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
%45 = call i32 (...) @getpid()
store i32 %45, i32* %7
%46 = call i32 (...) @getpgrp()
store i32 %46, i32* %8
%47 = call i32 @getsid(i32 0)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = load i32, i32* %7
%50 = load i32, i32* %8
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %48, i32 %49, i32 %50)
br label %54
52: ; preds = %36
%53 = call i32 @exit(i32 0)
unreachable
54: ; preds = %44
br label %55
55: ; preds = %54, %34
%56 = call i32 (...) @fork()
store i32 %56, i32* %10
%57 = load i32, i32* %10
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @err_sys(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %87
61: ; preds = %55
%62 = load i32, i32* %10
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %86
64: ; preds = %61
%65 = call i32 (...) @getpid()
store i32 %65, i32* %11
%66 = call i32 (...) @getpgrp()
store i32 %66, i32* %12
%67 = call i32 @getsid(i32 0)
store i32 %67, i32* %13
%68 = load i32, i32* %13
%69 = load i32, i32* %11
%70 = load i32, i32* %12
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %68, i32 %69, i32 %70)
%72 = call i32 @setpgid(i32 0, i32 0)
store i32 %72, i32* %6
%73 = load i32, i32* %6
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %77
75: ; preds = %64
%76 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %77
77: ; preds = %75, %64
%78 = load i8**, i8*** %5
%79 = getelementptr inbounds i8*, i8** %78, i64 1
%80 = load i8*, i8** %79
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i64 1
%83 = load i8*, i8** %82
%84 = call i32 @execlp(i8* %80, i8* %83, i32* null)
%85 = call i32 @exit(i32 -1)
unreachable
86: ; preds = %61
br label %87
87: ; preds = %86, %59
%88 = load i32, i32* %10
%89 = load i32, i32* %10
%90 = call i32 @setpgid(i32 %88, i32 %89)
store i32 %90, i32* %6
%91 = load i32, i32* %6
%92 = icmp eq i32 %91, -1
br i1 %92, label %93, label %95
93: ; preds = %87
%94 = call i32 @err_sys(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
br label %95
95: ; preds = %93, %87
%96 = load i32, i32* %10
%97 = call i32 @setpgid(i32 0, i32 %96)
store i32 %97, i32* %6
%98 = load i32, i32* %6
%99 = icmp eq i32 %98, -1
br i1 %99, label %100, label %102
100: ; preds = %95
%101 = call i32 @err_sys(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
br label %102
102: ; preds = %100, %95
%103 = call i32 (...) @print()
store i32 0, i32* %3
br label %104
104: ; preds = %102, %16
%105 = load i32, i32* %3
ret i32 %105
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"setsid\00"
@.str.2 = [19 x i8] c"parent %u: %u, %u\0A\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [20 x i8] c"session %u: %u, %u\0A\00"
@.str.5 = [18 x i8] c"child %u: %u, %u\0A\00"
@.str.6 = [14 x i8] c"setpgid child\00"
@.str.7 = [10 x i8] c"setpgid 1\00"
@.str.8 = [10 x i8] c"setpgid 2\00"
@str = [21 x i8] c"Usage: session grpid\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %67
6: ; preds = %2
%7 = tail call i32 (...) @setsid()
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (...) @getpgrp()
%14 = tail call i32 @getsid(i32 0)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %12, i32 %13)
%16 = tail call i32 (...) @fork()
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = tail call i32 @err_sys(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %34
20: ; preds = %11
%21 = icmp eq i32 %16, 0
br i1 %21, label %22, label %32
22: ; preds = %20
%23 = tail call i32 (...) @setsid()
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @err_sys(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = tail call i32 (...) @getpid()
%29 = tail call i32 (...) @getpgrp()
%30 = tail call i32 @getsid(i32 0)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %30, i32 %28, i32 %29)
br label %34
32: ; preds = %20
%33 = tail call i32 @exit(i32 0)
unreachable
34: ; preds = %27, %18
%35 = tail call i32 (...) @fork()
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = tail call i32 @err_sys(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %55
39: ; preds = %34
%40 = icmp eq i32 %35, 0
br i1 %40, label %41, label %55
41: ; preds = %39
%42 = tail call i32 (...) @getpid()
%43 = tail call i32 (...) @getpgrp()
%44 = tail call i32 @getsid(i32 0)
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %44, i32 %42, i32 %43)
%46 = tail call i32 @setpgid(i32 0, i32 0)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = tail call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %50
50: ; preds = %48, %41
%51 = getelementptr inbounds i8*, i8** %1, i64 1
%52 = load i8*, i8** %51
%53 = tail call i32 @execlp(i8* %52, i8* %52, i32* null)
%54 = tail call i32 @exit(i32 -1)
unreachable
55: ; preds = %39, %37
%56 = tail call i32 @setpgid(i32 %35, i32 %35)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = tail call i32 @err_sys(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
%61 = tail call i32 @setpgid(i32 0, i32 %35)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = tail call i32 @err_sys(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
%66 = tail call i32 (...) @print()
br label %67
67: ; preds = %65, %4
%68 = phi i32 [ -1, %4 ], [ 0, %65 ]
ret i32 %68
}