#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fsstatd_handle_signals();
scalar_t__ setsid();
int /*<<< orphan*/ store_pid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void fsstatd_daemonize(void) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    syslog(LOG_ERR, "fork error: %s\n", strerror(errno));
    exit(1);
  }
  if (pid > 0)
    exit(0);
  umask(0);
  store_pid();
  fsstatd_handle_signals();
  if (setsid() < 0) {
    syslog(LOG_ERR, "setsid error: %s\n", strerror(errno));
    exit(1);
  }
  if (chdir("/") < 0) {
    syslog(LOG_ERR, "chdir error: %s\n", strerror(errno));
    exit(1);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
}


x86 assembly (clang, -O0):
<fsstatd_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11f3 <fsstatd_daemonize+0x53>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0xc(%rbp),%edi
mov    %eax,%edx
lea    0xe1c(%rip),%rsi
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    1205 <fsstatd_daemonize+0x65>
xor    %edi,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    $0x0,%al
call   1070 <store_pid@plt>
mov    $0x0,%al
call   1040 <fsstatd_handle_signals@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    1260 <fsstatd_daemonize+0xc0>
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%edx
lea    0xdbf(%rip),%rsi
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdbb(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    12ab <fsstatd_daemonize+0x10b>
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
lea    0xd88(%rip),%rsi
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsstatd_daemonize>:
push   %rbx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     120c <fsstatd_daemonize+0x6c>
jne    122c <fsstatd_daemonize+0x8c>
xor    %edi,%edi
call   1080 <umask@plt>
xor    %eax,%eax
call   1070 <store_pid@plt>
xor    %eax,%eax
call   1040 <fsstatd_handle_signals@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     1233 <fsstatd_daemonize+0x93>
lea    0xe4b(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     1253 <fsstatd_daemonize+0xb3>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
pop    %rbx
jmp    1050 <close@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%ebx
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdd6(%rip),%rsi
jmp    1271 <fsstatd_daemonize+0xd1>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdbf(%rip),%rsi
jmp    1271 <fsstatd_daemonize+0xd1>
mov    0x2d5e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdb3(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [16 x i8] c"fork error: %s\0A\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"setsid error: %s\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [17 x i8] c"chdir error: %s\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @fsstatd_daemonize() {
%1 = alloca i64
%2 = call i64 (...) @fork()
store i64 %2, i64* %1
%3 = load i64, i64* %1
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %0
%6 = load i32, i32* @LOG_ERR
%7 = load i32, i32* @errno
%8 = call i32 @strerror(i32 %7)
%9 = call i32 @syslog(i32 %6, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i64, i64* %1
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %11
%17 = call i32 @umask(i32 0)
%18 = call i32 (...) @store_pid()
%19 = call i32 (...) @fsstatd_handle_signals()
%20 = call i64 (...) @setsid()
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %16
%23 = load i32, i32* @LOG_ERR
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 @syslog(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
%29 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %28
%32 = load i32, i32* @LOG_ERR
%33 = load i32, i32* @errno
%34 = call i32 @strerror(i32 %33)
%35 = call i32 @syslog(i32 %32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %28
%38 = load i32, i32* @STDIN_FILENO
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* @STDOUT_FILENO
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* @STDERR_FILENO
%43 = call i32 @close(i32 %42)
ret void
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [16 x i8] c"fork error: %s\0A\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"setsid error: %s\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [17 x i8] c"chdir error: %s\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @fsstatd_daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %9
3: ; preds = %0
%4 = load i32, i32* @LOG_ERR
%5 = load i32, i32* @errno
%6 = tail call i32 @strerror(i32 %5)
%7 = tail call i32 @syslog(i32 %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = icmp eq i64 %1, 0
br i1 %10, label %13, label %11
11: ; preds = %9
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %9
%14 = tail call i32 @umask(i32 0)
%15 = tail call i32 (...) @store_pid()
%16 = tail call i32 (...) @fsstatd_handle_signals()
%17 = tail call i64 (...) @setsid()
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %13
%20 = load i32, i32* @LOG_ERR
%21 = load i32, i32* @errno
%22 = tail call i32 @strerror(i32 %21)
%23 = tail call i32 @syslog(i32 %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %13
%26 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i32, i32* @LOG_ERR
%30 = load i32, i32* @errno
%31 = tail call i32 @strerror(i32 %30)
%32 = tail call i32 @syslog(i32 %29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %25
%35 = load i32, i32* @STDIN_FILENO
%36 = tail call i32 @close(i32 %35)
%37 = load i32, i32* @STDOUT_FILENO
%38 = tail call i32 @close(i32 %37)
%39 = load i32, i32* @STDERR_FILENO
%40 = tail call i32 @close(i32 %39)
ret void
}