#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*/ STDIN_FILENO;
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
scalar_t__ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ system(char *);
int main(int argc, char **argv) {
  pid_t pid;
  pid_t sid;
  char ch;
  if ((pid = fork()) < 0) {
    err_sys("fork error");
  } else if (pid == 0) {
    if ((sid = setsid()) < 0) {
      err_sys("setsid error");
    }
    if (read(STDIN_FILENO, &ch, 1) != 1) {
      printf("child has no controlling terminal\n");
    } else {
      printf("child is controlling terminal\n");
    }
    printf("sid is %d, pid is %d\n", sid, getpid());
    system("ps f");
    exit(0);
  }
  sleep(5);
  system("ps f");
  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)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11cc <main+0x3c>
lea    0xe3e(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1274 <main+0xe4>
cmpq   $0x0,-0x18(%rbp)
jne    126f <main+0xdf>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    11f8 <main+0x68>
lea    0xe18(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0x1,%eax
je     122b <main+0x9b>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1239 <main+0xa9>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe0d(%rip),%rdi
call   1050 <system@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    1274 <main+0xe4>
mov    $0x5,%edi
call   10a0 <sleep@plt>
lea    0xdeb(%rip),%rdi
call   1050 <system@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1235 <main+0x95>
jne    1241 <main+0xa1>
xor    %eax,%eax
call   1080 <setsid@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11d6 <main+0x36>
lea    0xe3a(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
cmp    $0x1,%eax
lea    0xe3b(%rip),%rax
lea    0xe52(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe02(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe05(%rip),%rdi
call   1060 <system@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xdc4(%rip),%rdi
call   1040 <err_sys@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
lea    0xddc(%rip),%rdi
call   1060 <system@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [13 x i8] c"setsid error\00"
@STDIN_FILENO = global i32 0
@.str.2 = [35 x i8] c"child has no controlling terminal\0A\00"
@.str.3 = [31 x i8] c"child is controlling terminal\0A\00"
@.str.4 = [22 x i8] c"sid is %d, pid is %d\0A\00"
@.str.5 = [5 x i8] c"ps f\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %36
13: ; preds = %2
%14 = load i64, i64* %6
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %35
16: ; preds = %13
%17 = call i64 (...) @setsid()
store i64 %17, i64* %7
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* @STDIN_FILENO
%23 = call i32 @read(i32 %22, i8* %8, i32 1)
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %29
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %25
%30 = load i64, i64* %7
%31 = call i32 (...) @getpid()
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64 %30, i32 %31)
%33 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %13
br label %36
36: ; preds = %35, %11
%37 = call i32 @sleep(i32 5)
%38 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [13 x i8] c"setsid error\00"
@STDIN_FILENO = global i32 0
@.str.4 = [22 x i8] c"sid is %d, pid is %d\0A\00"
@.str.5 = [5 x i8] c"ps f\00"
@str = [30 x i8] c"child is controlling terminal\00"
@str.6 = [34 x i8] c"child has no controlling terminal\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %25
8: ; preds = %2
%9 = icmp eq i64 %4, 0
br i1 %9, label %10, label %25
10: ; preds = %8
%11 = tail call i64 (...) @setsid()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = load i32, i32* @STDIN_FILENO
%17 = call i32 @read(i32 %16, i8* %3, i32 1)
%18 = icmp eq i32 %17, 1
%19 = select i1 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([34 x i8], [34 x i8]* @str.6, i64 0, i64 0)
%20 = tail call i32 @puts(i8* dereferenceable(1) %19)
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64 %11, i32 %21)
%23 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%24 = tail call i32 @exit(i32 0)
unreachable
25: ; preds = %8, %6
%26 = tail call i32 @sleep(i32 5)
%27 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}