#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_handler;
  int /*<<< orphan*/ sa_mask;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _PATH_DEVNULL;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setsid();
int sigaction(int /*<<< orphan*/, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int ng_daemon(int nochdir, int noclose) {
  struct sigaction osa, sa;
  int fd;
  pid_t newgrp;
  int oerrno;
  int osa_ok;
  sigemptyset(&sa.sa_mask);
  sa.sa_handler = SIG_IGN;
  sa.sa_flags = 0;
  osa_ok = sigaction(SIGHUP, &sa, &osa);
  switch (fork()) {
  case -1:
    return -1;
  case 0:
    break;
  default:
    exit(0);
  }
  newgrp = setsid();
  oerrno = errno;
  if (-1 != osa_ok) {
    sigaction(SIGHUP, &osa, NULL);
  }
  if (-1 == newgrp) {
    errno = oerrno;
    return -1;
  }
  if (!nochdir) {
    (void)chdir("/");
  }
  if (!noclose) {
    fd = open(_PATH_DEVNULL, O_RDWR, 0);
    if (-1 != fd) {
      (void)dup2(fd, STDIN_FILENO);
      (void)dup2(fd, STDOUT_FILENO);
      (void)dup2(fd, STDERR_FILENO);
      if (fd > 2) {
        (void)close(fd);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<ng_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x24(%rbp),%rdi
call   1080 <sigemptyset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <sigaction@plt>
mov    %eax,-0x40(%rbp)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x44(%rbp)
sub    $0xffffffff,%eax
je     11fe <ng_daemon+0x6e>
jmp    11ee <ng_daemon+0x5e>
mov    -0x44(%rbp),%eax
test   %eax,%eax
je     120a <ng_daemon+0x7a>
jmp    120f <ng_daemon+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <ng_daemon+0x186>
jmp    1216 <ng_daemon+0x86>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    %eax,-0x38(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x40(%rbp),%eax
je     1250 <ng_daemon+0xc0>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
mov    $0xffffffff,%eax
cmp    -0x38(%rbp),%eax
jne    1276 <ng_daemon+0xe6>
mov    -0x3c(%rbp),%ecx
mov    0x2d58(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <ng_daemon+0x186>
cmpl   $0x0,-0x8(%rbp)
jne    128c <ng_daemon+0xfc>
lea    0xd79(%rip),%rdi
call   1040 <chdir@plt>
cmpl   $0x0,-0xc(%rbp)
jne    130f <ng_daemon+0x17f>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
mov    $0xffffffff,%eax
cmp    -0x34(%rbp),%eax
je     130a <ng_daemon+0x17a>
mov    -0x34(%rbp),%edi
mov    0x2cde(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x34(%rbp),%edi
mov    0x2d0d(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x34(%rbp),%edi
mov    0x2ccc(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmpl   $0x2,-0x34(%rbp)
jle    1305 <ng_daemon+0x175>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
jmp    130a <ng_daemon+0x17a>
jmp    130f <ng_daemon+0x17f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ng_daemon>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
lea    0x14(%rsp),%rdi
call   1080 <sigemptyset@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2e25(%rip),%r12
mov    (%r12),%edi
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
call   1030 <sigaction@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12b0 <ng_daemon+0x120>
mov    %r14d,0x4(%rsp)
test   %ebx,%ebx
jne    12c1 <ng_daemon+0x131>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    %eax,%ebx
mov    0x2db1(%rip),%r13
mov    0x0(%r13),%r14d
cmp    $0xffffffff,%ebp
je     1228 <ng_daemon+0x98>
mov    (%r12),%edi
lea    0x18(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
cmp    $0xffffffff,%ebx
je     12a7 <ng_daemon+0x117>
test   %r15d,%r15d
jne    123e <ng_daemon+0xae>
lea    0xdc7(%rip),%rdi
call   1040 <chdir@plt>
xor    %ebx,%ebx
cmpl   $0x0,0x4(%rsp)
jne    12b0 <ng_daemon+0x120>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12b0 <ng_daemon+0x120>
mov    %eax,%ebp
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
cmp    $0x3,%ebp
jl     12b0 <ng_daemon+0x120>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    12b0 <ng_daemon+0x120>
mov    %r14d,0x0(%r13)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
%struct.sigaction = type { i64, i32, i32 }
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@errno = global i32 0
@.str = [2 x i8] c"/\00"
@_PATH_DEVNULL = global i32 0
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @ng_daemon(i32 %0, i32 %1) {
%3 = alloca %struct.sigaction
%4 = alloca %struct.sigaction
%5 = bitcast %struct.sigaction* %3 to i8*
%6 = bitcast %struct.sigaction* %4 to i8*
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
%8 = call i32 @sigemptyset(i32* %7)
%9 = load i32, i32* @SIG_IGN
%10 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store i64 0, i64* %11
%12 = load i32, i32* @SIGHUP
%13 = call i32 @sigaction(i32 %12, %struct.sigaction* %4, %struct.sigaction* %3)
%14 = call i32 (...) @fork()
switch i32 %14, label %15 [
i32 -1, label %48
i32 0, label %17
]
15: ; preds = %2
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %2
%18 = call i32 (...) @setsid()
%19 = load i32, i32* @errno
%20 = icmp eq i32 %13, -1
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = load i32, i32* @SIGHUP
%23 = call i32 @sigaction(i32 %22, %struct.sigaction* %3, %struct.sigaction* null)
br label %24
24: ; preds = %21, %17
%25 = icmp eq i32 %18, -1
br i1 %25, label %26, label %27
26: ; preds = %24
store i32 %19, i32* @errno
br label %48
27: ; preds = %24
%28 = icmp eq i32 %0, 0
br i1 %28, label %29, label %31
29: ; preds = %27
%30 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %27
%32 = icmp eq i32 %1, 0
br i1 %32, label %33, label %48
33: ; preds = %31
%34 = load i32, i32* @_PATH_DEVNULL
%35 = load i32, i32* @O_RDWR
%36 = call i32 @open(i32 %34, i32 %35, i32 0)
%37 = icmp eq i32 %36, -1
br i1 %37, label %48, label %38
38: ; preds = %33
%39 = load i32, i32* @STDIN_FILENO
%40 = call i32 @dup2(i32 %36, i32 %39)
%41 = load i32, i32* @STDOUT_FILENO
%42 = call i32 @dup2(i32 %36, i32 %41)
%43 = load i32, i32* @STDERR_FILENO
%44 = call i32 @dup2(i32 %36, i32 %43)
%45 = icmp sgt i32 %36, 2
br i1 %45, label %46, label %48
46: ; preds = %38
%47 = call i32 @close(i32 %36)
br label %48
48: ; preds = %31, %38, %46, %33, %2, %26
%49 = phi i32 [ -1, %26 ], [ %14, %2 ], [ 0, %33 ], [ 0, %46 ], [ 0, %38 ], [ 0, %31 ]
ret i32 %49
}