#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 getsid(int);
int /*<<< orphan*/ printf(char *, long);
int setsid();
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  pid_t sid, nsid;
  switch (pid = fork()) {
  case -1:
    err("fork() failed");
  case 0:
    if ((sid = getsid(pid)) == -1)
      err("getsid() failed");
    printf("Child process, Sessione iniziale - SID = %ld\n", (long)sid);
    if ((nsid = setsid()) == -1)
      err("setsid() failed");
    if ((sid = getsid(pid)) == -1)
      err("getsid() failed");
    printf("  Child process, Cambio sessione - SID = %ld\n", (long)sid);
    exit(EXIT_SUCCESS);
  default:
    waitpid(pid, NULL, 0);
    if ((sid = setsid()) == -1)
      err("setsid() failed, parent is process group leader");
  }
  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,-0x20(%rbp)
sub    $0xffffffff,%eax
je     11b3 <main+0x43>
jmp    11a3 <main+0x33>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     11bf <main+0x4f>
jmp    124c <main+0xdc>
lea    0xe46(%rip),%rdi
call   1060 <err@plt>
mov    -0x14(%rbp),%edi
call   1040 <getsid@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11df <main+0x6f>
lea    0xe34(%rip),%rdi
call   1060 <err@plt>
movslq -0x18(%rbp),%rsi
lea    0xe34(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    120e <main+0x9e>
lea    0xe43(%rip),%rdi
call   1060 <err@plt>
mov    -0x14(%rbp),%edi
call   1040 <getsid@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    122e <main+0xbe>
lea    0xde5(%rip),%rdi
call   1060 <err@plt>
movslq -0x18(%rbp),%rsi
lea    0xe23(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1279 <main+0x109>
lea    0xe16(%rip),%rdi
call   1060 <err@plt>
jmp    127e <main+0x10e>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11c4 <main+0x54>
cmp    $0xffffffff,%ebx
je     11b8 <main+0x48>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11a8 <main+0x38>
lea    0xee7(%rip),%rdi
call   1060 <err@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe41(%rip),%rdi
call   1060 <err@plt>
mov    %ebx,%edi
call   1040 <getsid@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11de <main+0x6e>
lea    0xe35(%rip),%rdi
call   1060 <err@plt>
movslq %ebp,%rsi
lea    0xe36(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1205 <main+0x95>
lea    0xe4c(%rip),%rdi
call   1060 <err@plt>
mov    %ebx,%edi
call   1040 <getsid@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    121f <main+0xaf>
lea    0xdf4(%rip),%rdi
call   1060 <err@plt>
movslq %ebx,%rsi
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"fork() failed\00"
@.str.1 = [16 x i8] c"getsid() failed\00"
@.str.2 = [46 x i8] c"Child process, Sessione iniziale - SID = %ld\0A\00"
@.str.3 = [16 x i8] c"setsid() failed\00"
@.str.4 = [46 x i8] c" Child process, Cambio sessione - SID = %ld\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [48 x i8] c"setsid() failed, parent is process group leader\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
switch i32 %9, label %38 [
i32 -1, label %10
i32 0, label %12
]
10: ; preds = %2
%11 = call i32 @err(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %2, %10
%13 = load i32, i32* %6
%14 = call i32 @getsid(i32 %13)
store i32 %14, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i64 %20)
%22 = call i32 (...) @setsid()
store i32 %22, i32* %8
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = load i32, i32* %6
%28 = call i32 @getsid(i32 %27)
store i32 %28, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i64 %34)
%36 = load i32, i32* @EXIT_SUCCESS
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %2
%39 = load i32, i32* %6
%40 = call i32 @waitpid(i32 %39, i32* null, i32 0)
%41 = call i32 (...) @setsid()
store i32 %41, i32* %7
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @err(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
br label %45
45: ; preds = %43, %38
br label %46
46: ; preds = %45
%47 = load i32, i32* @EXIT_SUCCESS
ret i32 %47
}

LLVM IR (-O2):
@.str = [14 x i8] c"fork() failed\00"
@.str.1 = [16 x i8] c"getsid() failed\00"
@.str.2 = [46 x i8] c"Child process, Sessione iniziale - SID = %ld\0A\00"
@.str.3 = [16 x i8] c"setsid() failed\00"
@.str.4 = [46 x i8] c" Child process, Cambio sessione - SID = %ld\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [48 x i8] c"setsid() failed, parent is process group leader\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %28 [
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 @getsid(i32 %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = sext i32 %7 to i64
%13 = tail call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i64 %12)
%14 = tail call i32 (...) @setsid()
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = tail call i32 @getsid(i32 %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
%24 = sext i32 %19 to i64
%25 = tail call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i64 %24)
%26 = load i32, i32* @EXIT_SUCCESS
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %2
%29 = tail call i32 @waitpid(i32 %3, i32* null, i32 0)
%30 = tail call i32 (...) @setsid()
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = tail call i32 @err(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %28, %32
%35 = load i32, i32* @EXIT_SUCCESS
ret i32 %35
}