#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*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int errno;
int fork();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int setsid();
int sigaction(int /*<<< orphan*/, struct sigaction *, struct sigaction *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int ss_daemon(int nochdir, int redirect_fd) {
  struct sigaction osa, sa;
  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 (osa_ok != -1)
    sigaction(SIGHUP, &osa, NULL);
  if (newgrp == -1) {
    errno = oerrno;
    return (-1);
  }
  if (!nochdir)
    (void)chdir("/");
  if (redirect_fd != -1) {
    (void)dup2(redirect_fd, STDOUT_FILENO);
    (void)dup2(redirect_fd, STDERR_FILENO);
    if (redirect_fd > 2)
      (void)close(redirect_fd);
    redirect_fd = open("/dev/null", O_RDWR, 0);
    if (redirect_fd != -1) {
      (void)dup2(redirect_fd, STDIN_FILENO);
      if (redirect_fd > 2)
        (void)close(redirect_fd);
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<ss_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x24(%rbp),%rdi
call   1090 <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   1040 <sigaction@plt>
mov    %eax,-0x3c(%rbp)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x40(%rbp)
sub    $0xffffffff,%eax
je     11fe <ss_daemon+0x6e>
jmp    11ee <ss_daemon+0x5e>
mov    -0x40(%rbp),%eax
test   %eax,%eax
je     120a <ss_daemon+0x7a>
jmp    120f <ss_daemon+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    131a <ss_daemon+0x18a>
jmp    1216 <ss_daemon+0x86>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
je     124c <ss_daemon+0xbc>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
cmpl   $0xffffffff,-0x34(%rbp)
jne    126e <ss_daemon+0xde>
mov    -0x38(%rbp),%ecx
mov    0x2d60(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    131a <ss_daemon+0x18a>
cmpl   $0x0,-0x8(%rbp)
jne    1284 <ss_daemon+0xf4>
lea    0xd81(%rip),%rdi
call   1050 <chdir@plt>
cmpl   $0xffffffff,-0xc(%rbp)
je     1313 <ss_daemon+0x183>
mov    -0xc(%rbp),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
cmpl   $0x2,-0xc(%rbp)
jle    12c2 <ss_daemon+0x132>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
lea    0xd30(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
je     130e <ss_daemon+0x17e>
mov    -0xc(%rbp),%edi
mov    0x2cc0(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
cmpl   $0x2,-0xc(%rbp)
jle    1309 <ss_daemon+0x179>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
jmp    130e <ss_daemon+0x17e>
jmp    1313 <ss_daemon+0x183>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ss_daemon>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r15d
mov    %edi,%r14d
lea    0x14(%rsp),%rdi
call   1090 <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   1040 <sigaction@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     12d4 <ss_daemon+0x144>
test   %ebp,%ebp
jne    12e5 <ss_daemon+0x155>
xor    %eax,%eax
call   1080 <setsid@plt>
mov    %eax,%ebp
mov    0x2db6(%rip),%rax
mov    (%rax),%r13d
cmp    $0xffffffff,%ebx
je     1228 <ss_daemon+0x98>
mov    (%r12),%edi
lea    0x18(%rsp),%rsi
xor    %edx,%edx
mov    %rax,%rbx
call   1040 <sigaction@plt>
mov    %rbx,%rax
cmp    $0xffffffff,%ebp
je     12b2 <ss_daemon+0x122>
test   %r14d,%r14d
je     12bc <ss_daemon+0x12c>
cmp    $0xffffffff,%r15d
je     12d2 <ss_daemon+0x142>
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1060 <dup2@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1060 <dup2@plt>
cmp    $0x3,%r15d
jl     1274 <ss_daemon+0xe4>
mov    %r15d,%edi
call   1070 <close@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
lea    0xd7e(%rip),%rdi
xor    %ebp,%ebp
xor    %edx,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12d4 <ss_daemon+0x144>
mov    %eax,%ebx
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <dup2@plt>
cmp    $0x3,%ebx
jl     12d4 <ss_daemon+0x144>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    12d4 <ss_daemon+0x144>
mov    %r13d,(%rax)
mov    $0xffffffff,%ebp
jmp    12d4 <ss_daemon+0x144>
lea    0xd3d(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%r15d
jne    1244 <ss_daemon+0xb4>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1030 <_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"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
define i32 @ss_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
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 2
%12 = call i32 @sigemptyset(i32* %11)
%13 = load i32, i32* @SIG_IGN
%14 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
store i64 0, i64* %15
%16 = load i32, i32* @SIGHUP
%17 = call i32 @sigaction(i32 %16, %struct.sigaction* %7, %struct.sigaction* %6)
store i32 %17, i32* %10
%18 = call i32 (...) @fork()
switch i32 %18, label %21 [
i32 -1, label %19
i32 0, label %20
]
19: ; preds = %2
store i32 -1, i32* %3
br label %73
20: ; preds = %2
br label %23
21: ; preds = %2
%22 = call i32 @_exit(i32 0)
unreachable
23: ; preds = %20
%24 = call i32 (...) @setsid()
store i32 %24, i32* %8
%25 = load i32, i32* @errno
store i32 %25, i32* %9
%26 = load i32, i32* %10
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* @SIGHUP
%30 = call i32 @sigaction(i32 %29, %struct.sigaction* %6, %struct.sigaction* null)
br label %31
31: ; preds = %28, %23
%32 = load i32, i32* %8
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = load i32, i32* %9
store i32 %35, i32* @errno
store i32 -1, i32* %3
br label %73
36: ; preds = %31
%37 = load i32, i32* %4
%38 = icmp ne i32 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = load i32, i32* %5
%43 = icmp ne i32 %42, -1
br i1 %43, label %44, label %72
44: ; preds = %41
%45 = load i32, i32* %5
%46 = load i32, i32* @STDOUT_FILENO
%47 = call i32 @dup2(i32 %45, i32 %46)
%48 = load i32, i32* %5
%49 = load i32, i32* @STDERR_FILENO
%50 = call i32 @dup2(i32 %48, i32 %49)
%51 = load i32, i32* %5
%52 = icmp sgt i32 %51, 2
br i1 %52, label %53, label %56
53: ; preds = %44
%54 = load i32, i32* %5
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %53, %44
%57 = load i32, i32* @O_RDWR
%58 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %57, i32 0)
store i32 %58, i32* %5
%59 = load i32, i32* %5
%60 = icmp ne i32 %59, -1
br i1 %60, label %61, label %71
61: ; preds = %56
%62 = load i32, i32* %5
%63 = load i32, i32* @STDIN_FILENO
%64 = call i32 @dup2(i32 %62, i32 %63)
%65 = load i32, i32* %5
%66 = icmp sgt i32 %65, 2
br i1 %66, label %67, label %70
67: ; preds = %61
%68 = load i32, i32* %5
%69 = call i32 @close(i32 %68)
br label %70
70: ; preds = %67, %61
br label %71
71: ; preds = %70, %56
br label %72
72: ; preds = %71, %41
store i32 0, i32* %3
br label %73
73: ; preds = %72, %34, %19
%74 = load i32, i32* %3
ret i32 %74
}

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"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
define i32 @ss_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 %51
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 %51
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, -1
br i1 %32, label %51, label %33
33: ; preds = %31
%34 = load i32, i32* @STDOUT_FILENO
%35 = call i32 @dup2(i32 %1, i32 %34)
%36 = load i32, i32* @STDERR_FILENO
%37 = call i32 @dup2(i32 %1, i32 %36)
%38 = icmp sgt i32 %1, 2
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @close(i32 %1)
br label %41
41: ; preds = %39, %33
%42 = load i32, i32* @O_RDWR
%43 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %42, i32 0)
%44 = icmp eq i32 %43, -1
br i1 %44, label %51, label %45
45: ; preds = %41
%46 = load i32, i32* @STDIN_FILENO
%47 = call i32 @dup2(i32 %43, i32 %46)
%48 = icmp sgt i32 %43, 2
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @close(i32 %43)
br label %51
51: ; preds = %31, %45, %49, %41, %2, %26
%52 = phi i32 [ -1, %26 ], [ %14, %2 ], [ 0, %41 ], [ 0, %49 ], [ 0, %45 ], [ 0, %31 ]
ret i32 %52
}