#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 /*<<< 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 setsid();
int main(void) {
  pid_t pid;
  int i;
  pid = fork();
  if (pid == -1) {
    return -1;
  } else if (pid != 0) {
    exit(EXIT_SUCCESS);
  }
  if (setsid() == -1) {
    return -1;
  }
  if (chdir("/") == -1) {
    return -1;
  }
  for (i = 0; i < 1; i++) {
    close(i);
  }
  open("/dev/null/", O_RDWR);
  dup(0);
  dup(0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    119f <main+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <main+0xda>
cmpl   $0x0,-0x8(%rbp)
je     11b7 <main+0x47>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11bc <main+0x4c>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11d8 <main+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <main+0xda>
lea    0xe2a(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11f9 <main+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <main+0xda>
movl   $0x0,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jge    1220 <main+0xb0>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1200 <main+0x90>
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
lea    0xdd0(%rip),%rdi
call   1070 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11d1 <main+0x61>
test   %ebx,%ebx
jne    11d5 <main+0x65>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11d1 <main+0x61>
lea    0xe6e(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
je     11d1 <main+0x61>
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <close@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe42(%rip),%rdi
call   1070 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
mov    %ebx,%eax
pop    %rbx
ret
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [11 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 %8
7: ; preds = %0
store i32 -1, i32* %1
br label %38
8: ; preds = %0
%9 = load i32, i32* %2
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @EXIT_SUCCESS
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %8
br label %15
15: ; preds = %14
%16 = call i32 (...) @setsid()
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 -1, i32* %1
br label %38
19: ; preds = %15
%20 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 -1, i32* %1
br label %38
23: ; preds = %19
store i32 0, i32* %3
br label %24
24: ; preds = %30, %23
%25 = load i32, i32* %3
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i32, i32* %3
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %27
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 1
store i32 %32, i32* %3
br label %24
33: ; preds = %24
%34 = load i32, i32* @O_RDWR
%35 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = call i32 @dup(i32 0)
%37 = call i32 @dup(i32 0)
store i32 0, i32* %1
br label %38
38: ; preds = %33, %22, %18, %7
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [11 x i8] c"/dev/null/\00"
@O_RDWR = global i32 0
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %2 [
i32 -1, label %17
i32 0, label %5
]
2: ; preds = %0
%3 = load i32, i32* @EXIT_SUCCESS
%4 = tail call i32 @exit(i32 %3)
unreachable
5: ; preds = %0
%6 = tail call i32 (...) @setsid()
%7 = icmp eq i32 %6, -1
br i1 %7, label %17, label %8
8: ; preds = %5
%9 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, -1
br i1 %10, label %17, label %11
11: ; preds = %8
%12 = tail call i32 @close(i32 0)
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = tail call i32 @dup(i32 0)
%16 = tail call i32 @dup(i32 0)
br label %17
17: ; preds = %8, %5, %0, %11
%18 = phi i32 [ 0, %11 ], [ %1, %0 ], [ -1, %5 ], [ -1, %8 ]
ret i32 %18
}