#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_SUCCESS;
int WEXITSTATUS(int);
int /*<<< orphan*/ err(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ prIds(char *);
int /*<<< orphan*/ printf(char *, scalar_t__, int);
int setsid();
scalar_t__ wait(int *);
int main(void) {
  prIds("parent");
  pid_t pid;
  int status;
  if ((pid = fork()) < 0) {
    err("cannot fork");
  } else if (pid > 0) {
    if (wait(&status) != pid) {
      err("wait error");
    }
    printf("wait %d: %d\n", pid, WEXITSTATUS(status));
    exit(EXIT_SUCCESS);
  } else {
    prIds("child before set session");
    if (setsid() == -1) {
      err("session error");
    }
    prIds("child after set session");
    exit(EXIT_SUCCESS);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe71(%rip),%rdi
call   1050 <prIds@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jge    11ba <main+0x3a>
lea    0xe57(%rip),%rdi
call   1060 <err@plt>
jmp    1256 <main+0xd6>
cmpq   $0x0,-0x8(%rbp)
jle    1214 <main+0x94>
lea    -0xc(%rbp),%rdi
call   1090 <wait@plt>
cmp    -0x8(%rbp),%rax
je     11e4 <main+0x64>
lea    0xe34(%rip),%rdi
call   1060 <err@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x18(%rbp),%rsi
mov    %eax,%edx
lea    0xe1d(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe10(%rip),%rdi
call   1050 <prIds@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
jne    123c <main+0xbc>
lea    0xe0d(%rip),%rdi
call   1060 <err@plt>
lea    0xe0f(%rip),%rdi
call   1050 <prIds@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe74(%rip),%rdi
call   1050 <prIds@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    11b1 <main+0x31>
lea    0xe63(%rip),%rdi
call   1060 <err@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
jne    11e5 <main+0x65>
lea    0xe71(%rip),%rdi
call   1050 <prIds@plt>
xor    %eax,%eax
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11d7 <main+0x57>
lea    0xe72(%rip),%rdi
call   1060 <err@plt>
lea    0xe74(%rip),%rdi
call   1050 <prIds@plt>
jmp    121d <main+0x9d>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
cmp    %rbx,%rax
je     1203 <main+0x83>
lea    0xe15(%rip),%rdi
call   1060 <err@plt>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe0b(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <printf@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"parent\00"
@.str.1 = [12 x i8] c"cannot fork\00"
@.str.2 = [11 x i8] c"wait error\00"
@.str.3 = [13 x i8] c"wait %d: %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [25 x i8] c"child before set session\00"
@.str.5 = [14 x i8] c"session error\00"
@.str.6 = [24 x i8] c"child after set session\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i32
%3 = call i32 @prIds(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%4 = call i64 (...) @fork()
store i64 %4, i64* %1
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @err(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %34
8: ; preds = %0
%9 = load i64, i64* %1
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %8
%12 = call i64 @wait(i32* %2)
%13 = load i64, i64* %1
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @err(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = load i64, i64* %1
%19 = load i32, i32* %2
%20 = call i32 @WEXITSTATUS(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i64 %18, i32 %20)
%22 = load i32, i32* @EXIT_SUCCESS
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %8
%25 = call i32 @prIds(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 (...) @setsid()
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @err(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = call i32 @prIds(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%32 = load i32, i32* @EXIT_SUCCESS
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %6
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"parent\00"
@.str.1 = [12 x i8] c"cannot fork\00"
@.str.2 = [11 x i8] c"wait error\00"
@.str.3 = [13 x i8] c"wait %d: %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [25 x i8] c"child before set session\00"
@.str.5 = [14 x i8] c"session error\00"
@.str.6 = [24 x i8] c"child after set session\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 @prIds(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%3 = bitcast i32* %1 to i8*
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @err(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
8: ; preds = %0
%9 = icmp eq i64 %4, 0
br i1 %9, label %21, label %10
10: ; preds = %8
%11 = call i64 @wait(i32* %1)
%12 = icmp eq i64 %11, %4
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = call i32 @err(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = load i32, i32* %1
%17 = call i32 @WEXITSTATUS(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i64 %4, i32 %17)
%19 = load i32, i32* @EXIT_SUCCESS
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %8
%22 = tail call i32 @prIds(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%23 = tail call i32 (...) @setsid()
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 @err(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
%28 = tail call i32 @prIds(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%29 = load i32, i32* @EXIT_SUCCESS
%30 = tail call i32 @exit(i32 %29)
unreachable
}