#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*/ EXIT_SUCCESS;
int NR_OPEN;
int /*<<< orphan*/ O_RDWR;
int chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int setsid();
int main(void) {
  pid_t pid;
  int i;
  pid = fork();
  if (pid == -1) {
    perror("fail fork");
    return -1;
  } else if (pid != 0) {
    printf("exit parent\n");
    exit(EXIT_SUCCESS);
  }
  if (setsid() == -1) {
    perror("fail setsid");
    return -1;
  }
  if (chdir("/") == -1) {
    perror("fail chdir");
    return -1;
  }
  printf("close files\n");
  for (i = 0; i < NR_OPEN; i++)
    close(i);
  printf("redirect 012\n");
  open("/dev/null", O_RDWR);
  dup(0);
  dup(0);
  while (1)
    ;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11cb <main+0x3b>
lea    0xe46(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <main+0x128>
cmpl   $0x0,-0x8(%rbp)
je     11ef <main+0x5f>
lea    0xe2e(%rip),%rdi
call   1040 <printf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    11f4 <main+0x64>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    121c <main+0x8c>
lea    0xe0c(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <main+0x128>
lea    0xe00(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1249 <main+0xb9>
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <main+0x128>
lea    0xde0(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2d6a(%rip),%rcx
cmp    (%rcx),%eax
jge    1284 <main+0xf4>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    125c <main+0xcc>
lea    0xdb2(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
lea    0xdab(%rip),%rdi
call   1080 <open@plt>
xor    %edi,%edi
call   1050 <dup@plt>
xor    %edi,%edi
call   1050 <dup@plt>
jmp    12b3 <main+0x123>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    1222 <main+0x92>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     1230 <main+0xa0>
lea    0xe5c(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
je     1239 <main+0xa9>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    0x2df9(%rip),%r14
cmpl   $0x0,(%r14)
jle    11ef <main+0x5f>
xor    %ebx,%ebx
nop
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    (%r14),%ebx
jl     11e0 <main+0x50>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    0xe18(%rip),%rdi
call   1080 <open@plt>
xor    %edi,%edi
call   1050 <dup@plt>
xor    %edi,%edi
call   1050 <dup@plt>
xchg   %ax,%ax
jmp    1220 <main+0x90>
cmp    $0xffffffff,%eax
jne    1252 <main+0xc2>
lea    0xdd2(%rip),%rdi
jmp    1240 <main+0xb0>
lea    0xdd3(%rip),%rdi
jmp    1240 <main+0xb0>
lea    0xdd8(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xded(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"fail fork\00"
@.str.1 = [13 x i8] c"exit parent\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [12 x i8] c"fail setsid\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [11 x i8] c"fail chdir\00"
@.str.5 = [13 x i8] c"close files\0A\00"
@NR_OPEN = global i32 0
@.str.6 = [14 x i8] c"redirect 012\0A\00"
@.str.7 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %46
9: ; preds = %0
%10 = load i32, i32* %2
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_SUCCESS
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %9
br label %17
17: ; preds = %16
%18 = call i32 (...) @setsid()
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %46
22: ; preds = %17
%23 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %46
27: ; preds = %22
%28 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %29
29: ; preds = %36, %27
%30 = load i32, i32* %3
%31 = load i32, i32* @NR_OPEN
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = load i32, i32* %3
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33
%37 = load i32, i32* %3
%38 = add nsw i32 %37, 1
store i32 %38, i32* %3
br label %29
39: ; preds = %29
%40 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%41 = load i32, i32* @O_RDWR
%42 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %41)
%43 = call i32 @dup(i32 0)
%44 = call i32 @dup(i32 0)
br label %45
45: ; preds = %39, %45
br label %45
46: ; preds = %25, %20, %7
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
@.str = [10 x i8] c"fail fork\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [12 x i8] c"fail setsid\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [11 x i8] c"fail chdir\00"
@NR_OPEN = global i32 0
@.str.7 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@str = [12 x i8] c"close files\00"
@str.8 = [13 x i8] c"redirect 012\00"
@str.9 = [12 x i8] c"exit parent\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %2 [
i32 -1, label %29
i32 0, label %6
]
2: ; preds = %0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.9, i64 0, i64 0))
%4 = load i32, i32* @EXIT_SUCCESS
%5 = tail call i32 @exit(i32 %4)
unreachable
6: ; preds = %0
%7 = tail call i32 (...) @setsid()
%8 = icmp eq i32 %7, -1
br i1 %8, label %29, label %9
9: ; preds = %6
%10 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%11 = icmp eq i32 %10, -1
br i1 %11, label %29, label %12
12: ; preds = %9
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%14 = load i32, i32* @NR_OPEN
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12, %16
%17 = phi i32 [ %19, %16 ], [ 0, %12 ]
%18 = tail call i32 @close(i32 %17)
%19 = add nuw nsw i32 %17, 1
%20 = load i32, i32* @NR_OPEN
%21 = icmp slt i32 %19, %20
br i1 %21, label %16, label %22
22: ; preds = %16, %12
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%24 = load i32, i32* @O_RDWR
%25 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %24)
%26 = tail call i32 @dup(i32 0)
%27 = tail call i32 @dup(i32 0)
br label %28
28: ; preds = %28, %22
br label %28
29: ; preds = %9, %6, %0
%30 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), %6 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), %9 ]
%31 = tail call i32 @perror(i8* %30)
ret i32 -1
}