#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
scalar_t__ STDERR_FILENO;
int STDIN_FILENO;
scalar_t__ STDOUT_FILENO;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, scalar_t__);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ sleep(int);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd, max_fd;
  switch (fork()) {
  case -1:
    exit(EXIT_FAILURE);
  case 0:
    break;
  default:
    exit(EXIT_SUCCESS);
  }
  if (setsid() < 0)
    exit(EXIT_FAILURE);
  switch (fork()) {
  case -1:
    exit(EXIT_FAILURE);
  case 0:
    break;
  default:
    exit(EXIT_SUCCESS);
  }
  umask(0);
  chdir("/");
  max_fd = sysconf(_SC_OPEN_MAX);
  for (fd = 0; fd < max_fd; fd++)
    close(fd);
  close(STDIN_FILENO);
  fd = open("/dev/null", O_RDWR);
  if (fd != STDIN_FILENO)
    exit(EXIT_FAILURE);
  if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO)
    exit(EXIT_FAILURE);
  if (dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO)
    exit(EXIT_FAILURE);
  for (;;)
    sleep(1);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     11e0 <main+0x40>
jmp    11d0 <main+0x30>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     11ee <main+0x4e>
jmp    11f3 <main+0x53>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1201 <main+0x61>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    1220 <main+0x80>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
sub    $0xffffffff,%eax
je     124a <main+0xaa>
jmp    123a <main+0x9a>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     1258 <main+0xb8>
jmp    125d <main+0xbd>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    126b <main+0xcb>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xd87(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12b8 <main+0x118>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1296 <main+0xf6>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%esi
lea    0xd2c(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2cc8(%rip),%rcx
cmp    (%rcx),%eax
je     12fe <main+0x15e>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
mov    0x2ce2(%rip),%rax
mov    (%rax),%rsi
call   1040 <dup2@plt>
mov    0x2cd3(%rip),%rcx
cmp    (%rcx),%rax
je     1334 <main+0x194>
mov    0x2c93(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
mov    0x2c84(%rip),%rax
mov    (%rax),%rsi
call   1040 <dup2@plt>
mov    0x2c75(%rip),%rcx
cmp    (%rcx),%rax
je     136a <main+0x1ca>
mov    0x2c5d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    136f <main+0x1cf>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    136f <main+0x1cf>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jne    127c <main+0xdc>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     1281 <main+0xe1>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jne    127c <main+0xdc>
xor    %ebx,%ebx
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xe1f(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
test   %eax,%eax
jle    120e <main+0x6e>
mov    %eax,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1200 <main+0x60>
mov    0x2d9b(%rip),%rbx
mov    (%rbx),%edi
call   1050 <close@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
lea    0xdd6(%rip),%rdi
call   1080 <open@plt>
cmp    (%rbx),%eax
jne    1281 <main+0xe1>
mov    0x2db4(%rip),%rbp
mov    0x0(%rbp),%rsi
mov    %eax,%edi
call   1040 <dup2@plt>
cmp    0x0(%rbp),%rax
jne    1281 <main+0xe1>
mov    (%rbx),%edi
mov    0x2d72(%rip),%rbx
mov    (%rbx),%rsi
call   1040 <dup2@plt>
cmp    (%rbx),%rax
jne    1281 <main+0xe1>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1270 <main+0xd0>
cmp    $0xffffffff,%eax
jne    128f <main+0xef>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@_SC_OPEN_MAX = global i32 0
@STDIN_FILENO = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDOUT_FILENO = global i64 0
@STDERR_FILENO = global i64 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @fork()
switch i32 %8, label %13 [
i32 -1, label %9
i32 0, label %12
]
9: ; preds = %2
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
br label %16
13: ; preds = %2
%14 = load i32, i32* @EXIT_SUCCESS
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %12
%17 = call i64 (...) @setsid()
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %16
%23 = call i32 (...) @fork()
switch i32 %23, label %28 [
i32 -1, label %24
i32 0, label %27
]
24: ; preds = %22
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %22
br label %31
28: ; preds = %22
%29 = load i32, i32* @EXIT_SUCCESS
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %27
%32 = call i32 @umask(i32 0)
%33 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%34 = load i32, i32* @_SC_OPEN_MAX
%35 = call i32 @sysconf(i32 %34)
store i32 %35, i32* %7
store i32 0, i32* %6
br label %36
36: ; preds = %43, %31
%37 = load i32, i32* %6
%38 = load i32, i32* %7
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %46
40: ; preds = %36
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %40
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %36
46: ; preds = %36
%47 = load i32, i32* @STDIN_FILENO
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* @O_RDWR
%50 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %49)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = load i32, i32* @STDIN_FILENO
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %57
54: ; preds = %46
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %46
%58 = load i32, i32* @STDIN_FILENO
%59 = load i64, i64* @STDOUT_FILENO
%60 = call i64 @dup2(i32 %58, i64 %59)
%61 = load i64, i64* @STDOUT_FILENO
%62 = icmp ne i64 %60, %61
br i1 %62, label %63, label %66
63: ; preds = %57
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %57
%67 = load i32, i32* @STDIN_FILENO
%68 = load i64, i64* @STDERR_FILENO
%69 = call i64 @dup2(i32 %67, i64 %68)
%70 = load i64, i64* @STDERR_FILENO
%71 = icmp ne i64 %69, %70
br i1 %71, label %72, label %75
72: ; preds = %66
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %66
br label %76
76: ; preds = %76, %75
%77 = call i32 @sleep(i32 1)
br label %76
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"/\00"
@_SC_OPEN_MAX = global i32 0
@STDIN_FILENO = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDOUT_FILENO = global i64 0
@STDERR_FILENO = global i64 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %7 [
i32 -1, label %4
i32 0, label %10
]
4: ; preds = %2
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %2
%8 = load i32, i32* @EXIT_SUCCESS
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = tail call i64 (...) @setsid()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %10
%17 = tail call i32 (...) @fork()
switch i32 %17, label %21 [
i32 -1, label %18
i32 0, label %24
]
18: ; preds = %16
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %16
%22 = load i32, i32* @EXIT_SUCCESS
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %16
%25 = tail call i32 @umask(i32 0)
%26 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @_SC_OPEN_MAX
%28 = tail call i32 @sysconf(i32 %27)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %24, %30
%31 = phi i32 [ %33, %30 ], [ 0, %24 ]
%32 = tail call i32 @close(i32 %31)
%33 = add nuw nsw i32 %31, 1
%34 = icmp eq i32 %33, %28
br i1 %34, label %35, label %30
35: ; preds = %30, %24
%36 = load i32, i32* @STDIN_FILENO
%37 = tail call i32 @close(i32 %36)
%38 = load i32, i32* @O_RDWR
%39 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32, i32* @STDIN_FILENO
%41 = icmp eq i32 %39, %40
br i1 %41, label %45, label %42
42: ; preds = %35
%43 = load i32, i32* @EXIT_FAILURE
%44 = tail call i32 @exit(i32 %43)
unreachable
45: ; preds = %35
%46 = load i64, i64* @STDOUT_FILENO
%47 = tail call i64 @dup2(i32 %39, i64 %46)
%48 = load i64, i64* @STDOUT_FILENO
%49 = icmp eq i64 %47, %48
br i1 %49, label %53, label %50
50: ; preds = %45
%51 = load i32, i32* @EXIT_FAILURE
%52 = tail call i32 @exit(i32 %51)
unreachable
53: ; preds = %45
%54 = load i32, i32* @STDIN_FILENO
%55 = load i64, i64* @STDERR_FILENO
%56 = tail call i64 @dup2(i32 %54, i64 %55)
%57 = load i64, i64* @STDERR_FILENO
%58 = icmp eq i64 %56, %57
br i1 %58, label %62, label %59
59: ; preds = %53
%60 = load i32, i32* @EXIT_FAILURE
%61 = tail call i32 @exit(i32 %60)
unreachable
62: ; preds = %53, %62
%63 = tail call i32 @sleep(i32 1)
br label %62
}