#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 */
struct sigaction {
  scalar_t__ sa_flags;
  int /*<<< orphan*/ sa_mask;
  int /*<<< orphan*/ sa_handler;
};
struct rlimit {
  int rlim_max;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ RLIMIT_NOFILE;
int RLIM_INFINITY;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int chdir(char *);
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
scalar_t__ sigaction(int /*<<< orphan*/, struct sigaction *,
                     int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemon(int nochdir, int noclose) {
  int i;
  pid_t pid;
  struct rlimit rl;
  struct sigaction sa;
  int ret;
  umask(0);
  if (noclose == 0) {
    if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
      return -1;
  }
  if ((pid = fork()) < 0)
    return -1;
  else if (pid != 0)
    exit(0);
  setsid();
  sa.sa_handler = SIG_IGN;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  if (sigaction(SIGHUP, &sa, NULL) < 0)
    return -1;
  if ((pid = fork()) < 0)
    return -1;
  else if (pid != 0)
    exit(0);
  if (nochdir == 0) {
    ret = chdir("/");
    (void)ret;
  }
  if (noclose == 0) {
    if (rl.rlim_max == RLIM_INFINITY)
      rl.rlim_max = 1024;
    for (i = 0; i < rl.rlim_max; i++)
      close(i);
    open("/dev/null", O_RDWR);
    ret = dup(0);
    ret = dup(0);
    (void)ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xor    %edi,%edi
call   1090 <umask@plt>
cmpl   $0x0,-0xc(%rbp)
jne    11fc <daemon+0x4c>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   10c0 <getrlimit@plt>
cmp    $0x0,%rax
jge    11f7 <daemon+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <daemon+0x1a5>
jmp    11fc <daemon+0x4c>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    121d <daemon+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <daemon+0x1a5>
cmpq   $0x0,-0x18(%rbp)
je     122f <daemon+0x7f>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1234 <daemon+0x84>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
call   1080 <sigemptyset@plt>
movq   $0x0,-0x30(%rbp)
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    1288 <daemon+0xd8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <daemon+0x1a5>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    12a9 <daemon+0xf9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <daemon+0x1a5>
cmpq   $0x0,-0x18(%rbp)
je     12bb <daemon+0x10b>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    12c0 <daemon+0x110>
cmpl   $0x0,-0x8(%rbp)
jne    12d9 <daemon+0x129>
lea    0xd2f(%rip),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    134e <daemon+0x19e>
mov    -0x20(%rbp),%eax
mov    0x2cd3(%rip),%rcx
cmp    (%rcx),%eax
jne    12fc <daemon+0x14c>
movl   $0x400,-0x20(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1325 <daemon+0x175>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1303 <daemon+0x153>
mov    0x2cb4(%rip),%rax
mov    (%rax),%esi
lea    0xccd(%rip),%rdi
call   10a0 <open@plt>
xor    %edi,%edi
call   1050 <dup@plt>
mov    %eax,-0x34(%rbp)
xor    %edi,%edi
call   1050 <dup@plt>
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
mov    %edi,%r14d
xor    %edi,%edi
call   1090 <umask@plt>
test   %ebx,%ebx
jne    11e4 <daemon+0x34>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   10c0 <getrlimit@plt>
test   %rax,%rax
js     1282 <daemon+0xd2>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     12c5 <daemon+0x115>
jne    12d0 <daemon+0x120>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x18(%rsp),%rdi
call   1080 <sigemptyset@plt>
movq   $0x0,0x10(%rsp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
test   %rax,%rax
js     12c5 <daemon+0x115>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12c5 <daemon+0x115>
jne    12d0 <daemon+0x120>
test   %r14d,%r14d
jne    1263 <daemon+0xb3>
lea    0xda2(%rip),%rdi
call   1040 <chdir@plt>
xor    %ebp,%ebp
test   %ebx,%ebx
jne    12c5 <daemon+0x115>
mov    0x8(%rsp),%eax
mov    0x2d4c(%rip),%rcx
cmp    (%rcx),%eax
jne    1289 <daemon+0xd9>
movl   $0x400,0x8(%rsp)
jmp    128d <daemon+0xdd>
mov    $0xffffffff,%ebp
jmp    12c5 <daemon+0x115>
test   %eax,%eax
jle    12a0 <daemon+0xf0>
xor    %ebx,%ebx
nop
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    0x8(%rsp),%ebx
jl     1290 <daemon+0xe0>
mov    0x2d39(%rip),%rax
mov    (%rax),%esi
lea    0xd52(%rip),%rdi
call   10a0 <open@plt>
xor    %ebp,%ebp
xor    %edi,%edi
call   1050 <dup@plt>
xor    %edi,%edi
call   1050 <dup@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32 }
%struct.sigaction = type { i64, i32, i32 }
@RLIMIT_NOFILE = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str = [2 x i8] c"/\00"
@RLIM_INFINITY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @daemon(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.rlimit
%9 = alloca %struct.sigaction
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = call i32 @umask(i32 0)
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @RLIMIT_NOFILE
%16 = call i64 @getrlimit(i32 %15, %struct.rlimit* %8)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 -1, i32* %3
br label %86
19: ; preds = %14
br label %20
20: ; preds = %19, %2
%21 = call i64 (...) @fork()
store i64 %21, i64* %7
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 -1, i32* %3
br label %86
24: ; preds = %20
%25 = load i64, i64* %7
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %24
br label %30
30: ; preds = %29
%31 = call i32 (...) @setsid()
%32 = load i32, i32* @SIG_IGN
%33 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 2
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 1
%35 = call i32 @sigemptyset(i32* %34)
%36 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 0
store i64 0, i64* %36
%37 = load i32, i32* @SIGHUP
%38 = call i64 @sigaction(i32 %37, %struct.sigaction* %9, i32* null)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %30
store i32 -1, i32* %3
br label %86
41: ; preds = %30
%42 = call i64 (...) @fork()
store i64 %42, i64* %7
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %41
store i32 -1, i32* %3
br label %86
45: ; preds = %41
%46 = load i64, i64* %7
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @exit(i32 0)
unreachable
50: ; preds = %45
br label %51
51: ; preds = %50
%52 = load i32, i32* %4
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %55, i32* %10
%56 = load i32, i32* %10
br label %57
57: ; preds = %54, %51
%58 = load i32, i32* %5
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %85
60: ; preds = %57
%61 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
%62 = load i32, i32* %61
%63 = load i32, i32* @RLIM_INFINITY
%64 = icmp eq i32 %62, %63
br i1 %64, label %65, label %67
65: ; preds = %60
%66 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
store i32 1024, i32* %66
br label %67
67: ; preds = %65, %60
store i32 0, i32* %6
br label %68
68: ; preds = %76, %67
%69 = load i32, i32* %6
%70 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
%71 = load i32, i32* %70
%72 = icmp slt i32 %69, %71
br i1 %72, label %73, label %79
73: ; preds = %68
%74 = load i32, i32* %6
%75 = call i32 @close(i32 %74)
br label %76
76: ; preds = %73
%77 = load i32, i32* %6
%78 = add nsw i32 %77, 1
store i32 %78, i32* %6
br label %68
79: ; preds = %68
%80 = load i32, i32* @O_RDWR
%81 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %80)
%82 = call i32 @dup(i32 0)
store i32 %82, i32* %10
%83 = call i32 @dup(i32 0)
store i32 %83, i32* %10
%84 = load i32, i32* %10
br label %85
85: ; preds = %79, %57
store i32 0, i32* %3
br label %86
86: ; preds = %85, %44, %40, %23, %18
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
%struct.sigaction = type { i64, i32, i32 }
@RLIMIT_NOFILE = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str = [2 x i8] c"/\00"
@RLIM_INFINITY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @daemon(i32 %0, i32 %1) {
%3 = alloca %struct.rlimit
%4 = alloca %struct.sigaction
%5 = bitcast %struct.rlimit* %3 to i8*
%6 = bitcast %struct.sigaction* %4 to i8*
%7 = tail call i32 @umask(i32 0)
%8 = icmp eq i32 %1, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @RLIMIT_NOFILE
%11 = call i64 @getrlimit(i32 %10, %struct.rlimit* %3)
%12 = icmp slt i64 %11, 0
br i1 %12, label %62, label %13
13: ; preds = %9, %2
%14 = call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %62, label %16
16: ; preds = %13
%17 = icmp eq i64 %14, 0
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %16
%21 = call i32 (...) @setsid()
%22 = load i32, i32* @SIG_IGN
%23 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
%25 = call i32 @sigemptyset(i32* %24)
%26 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store i64 0, i64* %26
%27 = load i32, i32* @SIGHUP
%28 = call i64 @sigaction(i32 %27, %struct.sigaction* %4, i32* null)
%29 = icmp slt i64 %28, 0
br i1 %29, label %62, label %30
30: ; preds = %20
%31 = call i64 (...) @fork()
%32 = icmp slt i64 %31, 0
br i1 %32, label %62, label %33
33: ; preds = %30
%34 = icmp eq i64 %31, 0
br i1 %34, label %37, label %35
35: ; preds = %33
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %33
%38 = icmp eq i32 %0, 0
br i1 %38, label %39, label %41
39: ; preds = %37
%40 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %41
41: ; preds = %39, %37
br i1 %8, label %42, label %62
42: ; preds = %41
%43 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
%44 = load i32, i32* %43
%45 = load i32, i32* @RLIM_INFINITY
%46 = icmp eq i32 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %42
store i32 1024, i32* %43
br label %50
48: ; preds = %42
%49 = icmp sgt i32 %44, 0
br i1 %49, label %50, label %57
50: ; preds = %47, %48
br label %51
51: ; preds = %50, %51
%52 = phi i32 [ %54, %51 ], [ 0, %50 ]
%53 = call i32 @close(i32 %52)
%54 = add nuw nsw i32 %52, 1
%55 = load i32, i32* %43
%56 = icmp slt i32 %54, %55
br i1 %56, label %51, label %57
57: ; preds = %51, %48
%58 = load i32, i32* @O_RDWR
%59 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %58)
%60 = call i32 @dup(i32 0)
%61 = call i32 @dup(i32 0)
br label %62
62: ; preds = %41, %57, %30, %20, %13, %9
%63 = phi i32 [ -1, %9 ], [ -1, %13 ], [ -1, %20 ], [ -1, %30 ], [ 0, %57 ], [ 0, %41 ]
ret i32 %63
}