#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*/ STDIN_FILENO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int setsid();
int main(int argc, char **argv) {
  pid_t mypid = getpid();
  printf("Parent PID: %i\n", mypid);
  pid_t child_pid = fork();
  if (child_pid > 0) {
    printf("Child PID: %i\n", child_pid);
    exit(0);
  }
  pid_t session_id = setsid();
  printf("Session ID (SID): %i\n", session_id);
  pid_t child2_pid = fork();
  if (child2_pid > 0) {
    printf("Child2 PID: %i\n", child2_pid);
  }
  char *buf;
  read(STDIN_FILENO, buf, 1);
}


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)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe76(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11b9 <main+0x59>
mov    -0x18(%rbp),%esi
lea    0xe63(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe52(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11f5 <main+0x95>
mov    -0x20(%rbp),%esi
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe91(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jg     11c6 <main+0x66>
xor    %eax,%eax
call   1050 <setsid@plt>
lea    0xe90(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jle    11af <main+0x4f>
lea    0xe8d(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%edx
call   1060 <read@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe43(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"Parent PID: %i\0A\00"
@.str.1 = [15 x i8] c"Child PID: %i\0A\00"
@.str.2 = [22 x i8] c"Session ID (SID): %i\0A\00"
@.str.3 = [16 x i8] c"Child2 PID: %i\0A\00"
@STDIN_FILENO = 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
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (...) @getpid()
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = call i32 (...) @fork()
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* %7
%19 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %2
%22 = call i32 (...) @setsid()
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = call i32 (...) @fork()
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = load i32, i32* %9
%30 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %28, %21
%32 = load i32, i32* @STDIN_FILENO
%33 = load i8*, i8** %10
%34 = call i32 @read(i32 %32, i8* %33, i32 1)
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [16 x i8] c"Parent PID: %i\0A\00"
@.str.1 = [15 x i8] c"Child PID: %i\0A\00"
@.str.2 = [22 x i8] c"Session ID (SID): %i\0A\00"
@.str.3 = [16 x i8] c"Child2 PID: %i\0A\00"
@STDIN_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 (...) @fork()
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = tail call i32 (...) @setsid()
%12 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = tail call i32 (...) @fork()
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %13)
br label %17
17: ; preds = %15, %10
%18 = load i32, i32* @STDIN_FILENO
%19 = tail call i32 @read(i32 %18, i8* undef, i32 1)
ret i32 0
}