#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*/ O_RDWR;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int chdir(char *);
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int fileno(int /*<<< orphan*/);
int fork();
int open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
int mkdaemon(int fchdir, int fclose) {
  int fdlimit = sysconf(_SC_OPEN_MAX);
  int fd = 0;
  int i;
  fdlimit = fileno(stderr);
  switch (fork()) {
  case 0:
    break;
  case -1:
    return -1;
  default:
    _exit(0);
  }
  if (setsid() < 0)
    return -1;
  switch (fork()) {
  case 0:
    break;
  case -1:
    return -1;
  default:
    _exit(0);
  }
  if (fchdir)
    i = chdir("/");
  if (fclose) {
    for (fd = 0; fd < fdlimit; fd++)
      close(fd);
    fd = open("/dev/null", O_RDWR);
    i = dup(0);
    i = dup(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<mkdaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     11f6 <mkdaemon+0x66>
jmp    11e1 <mkdaemon+0x51>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
jne    1202 <mkdaemon+0x72>
jmp    11f1 <mkdaemon+0x61>
jmp    1209 <mkdaemon+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <mkdaemon+0x157>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    1226 <mkdaemon+0x96>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <mkdaemon+0x157>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
sub    $0xffffffff,%eax
je     1255 <mkdaemon+0xc5>
jmp    1240 <mkdaemon+0xb0>
mov    -0x20(%rbp),%eax
test   %eax,%eax
jne    1261 <mkdaemon+0xd1>
jmp    1250 <mkdaemon+0xc0>
jmp    1268 <mkdaemon+0xd8>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <mkdaemon+0x157>
xor    %edi,%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x8(%rbp)
je     1281 <mkdaemon+0xf1>
lea    0xd87(%rip),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     12e0 <mkdaemon+0x150>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    12b4 <mkdaemon+0x124>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1292 <mkdaemon+0x102>
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
lea    0xd3e(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
xor    %edi,%edi
call   1050 <dup@plt>
mov    %eax,-0x18(%rbp)
xor    %edi,%edi
call   1050 <dup@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdaemon>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     124e <mkdaemon+0xbe>
test   %ebp,%ebp
jne    125b <mkdaemon+0xcb>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     1245 <mkdaemon+0xb5>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     124e <mkdaemon+0xbe>
test   %ebp,%ebp
jne    125b <mkdaemon+0xcb>
test   %r15d,%r15d
je     1204 <mkdaemon+0x74>
lea    0xe01(%rip),%rdi
call   1040 <chdir@plt>
test   %r14d,%r14d
je     124c <mkdaemon+0xbc>
test   %ebx,%ebx
jle    121e <mkdaemon+0x8e>
xor    %ebp,%ebp
nop
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1210 <mkdaemon+0x80>
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
lea    0xdd4(%rip),%rdi
call   1090 <open@plt>
xor    %ebp,%ebp
xor    %edi,%edi
call   1050 <dup@plt>
xor    %edi,%edi
call   1050 <dup@plt>
jmp    124e <mkdaemon+0xbe>
mov    $0xffffffff,%ebp
jmp    124e <mkdaemon+0xbe>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@_SC_OPEN_MAX = global i32 0
@stderr = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @mkdaemon(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* @_SC_OPEN_MAX
%10 = call i32 @sysconf(i32 %9)
store i32 %10, i32* %6
store i32 0, i32* %7
%11 = load i32, i32* @stderr
%12 = call i32 @fileno(i32 %11)
store i32 %12, i32* %6
%13 = call i32 (...) @fork()
switch i32 %13, label %16 [
i32 0, label %14
i32 -1, label %15
]
14: ; preds = %2
br label %18
15: ; preds = %2
store i32 -1, i32* %3
br label %53
16: ; preds = %2
%17 = call i32 @_exit(i32 0)
unreachable
18: ; preds = %14
%19 = call i64 (...) @setsid()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 -1, i32* %3
br label %53
22: ; preds = %18
%23 = call i32 (...) @fork()
switch i32 %23, label %26 [
i32 0, label %24
i32 -1, label %25
]
24: ; preds = %22
br label %28
25: ; preds = %22
store i32 -1, i32* %3
br label %53
26: ; preds = %22
%27 = call i32 @_exit(i32 0)
unreachable
28: ; preds = %24
%29 = load i32, i32* %4
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %32, i32* %8
br label %33
33: ; preds = %31, %28
%34 = load i32, i32* %5
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %52
36: ; preds = %33
store i32 0, i32* %7
br label %37
37: ; preds = %44, %36
%38 = load i32, i32* %7
%39 = load i32, i32* %6
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %47
41: ; preds = %37
%42 = load i32, i32* %7
%43 = call i32 @close(i32 %42)
br label %44
44: ; preds = %41
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
store i32 %46, i32* %7
br label %37
47: ; preds = %37
%48 = load i32, i32* @O_RDWR
%49 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %48)
store i32 %49, i32* %7
%50 = call i32 @dup(i32 0)
store i32 %50, i32* %8
%51 = call i32 @dup(i32 0)
store i32 %51, i32* %8
br label %52
52: ; preds = %47, %33
store i32 0, i32* %3
br label %53
53: ; preds = %52, %25, %21, %15
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@_SC_OPEN_MAX = global i32 0
@stderr = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @mkdaemon(i32 %0, i32 %1) {
%3 = load i32, i32* @_SC_OPEN_MAX
%4 = tail call i32 @sysconf(i32 %3)
%5 = load i32, i32* @stderr
%6 = tail call i32 @fileno(i32 %5)
%7 = tail call i32 (...) @fork()
switch i32 %7, label %8 [
i32 0, label %10
i32 -1, label %35
]
8: ; preds = %2
%9 = tail call i32 @_exit(i32 0)
unreachable
10: ; preds = %2
%11 = tail call i64 (...) @setsid()
%12 = icmp slt i64 %11, 0
br i1 %12, label %35, label %13
13: ; preds = %10
%14 = tail call i32 (...) @fork()
switch i32 %14, label %15 [
i32 0, label %17
i32 -1, label %35
]
15: ; preds = %13
%16 = tail call i32 @_exit(i32 0)
unreachable
17: ; preds = %13
%18 = icmp eq i32 %0, 0
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %17
%22 = icmp eq i32 %1, 0
br i1 %22, label %35, label %23
23: ; preds = %21
%24 = icmp sgt i32 %6, 0
br i1 %24, label %25, label %30
25: ; preds = %23, %25
%26 = phi i32 [ %28, %25 ], [ 0, %23 ]
%27 = tail call i32 @close(i32 %26)
%28 = add nuw nsw i32 %26, 1
%29 = icmp eq i32 %28, %6
br i1 %29, label %30, label %25
30: ; preds = %25, %23
%31 = load i32, i32* @O_RDWR
%32 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = tail call i32 @dup(i32 0)
%34 = tail call i32 @dup(i32 0)
br label %35
35: ; preds = %21, %30, %13, %10, %2
%36 = phi i32 [ %7, %2 ], [ -1, %10 ], [ %14, %13 ], [ 0, %30 ], [ 0, %21 ]
ret i32 %36
}