#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 stat {
  int /*<<< orphan*/ member_0;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char const *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int stat(char const *, struct stat *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void setup_daemon(const char *logdir) {
  pid_t pID = fork();
  if (pID < 0) {
    printf("Failed to fork.\n");
    exit(EXIT_FAILURE);
  } else if (pID > 0) {
    exit(EXIT_SUCCESS);
  }
  if (pID == 0) {
    if (setsid() < 0)
      exit(EXIT_FAILURE);
  }
  signal(SIGCHLD, SIG_IGN);
  signal(SIGHUP, SIG_IGN);
  pID = fork();
  if (pID < 0) {
    exit(EXIT_FAILURE);
  }
  if (pID > 0) {
    exit(EXIT_SUCCESS);
  }
  umask(0);
  struct stat st = {0};
  if (stat(logdir, &st) == -1) {
    syslog(LOG_ERR, "Log directory %s does not exits.", logdir);
    exit(EXIT_FAILURE);
  } else {
    chdir(logdir);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  openlog("/var/log/htsend.log", LOG_PID, LOG_DAEMON);
}


x86 assembly (clang, -O0):
<setup_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11fc <setup_daemon+0x3c>
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    1215 <setup_daemon+0x55>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    121a <setup_daemon+0x5a>
cmpq   $0x0,-0x10(%rbp)
jne    1249 <setup_daemon+0x89>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    1244 <setup_daemon+0x84>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    1249 <setup_daemon+0x89>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    129b <setup_daemon+0xdb>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    12b4 <setup_daemon+0xf4>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
xor    %edi,%edi
call   1090 <umask@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0xffffffff,%eax
jne    1308 <setup_daemon+0x148>
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd1c(%rip),%rsi
call   10b0 <syslog@plt>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1030 <chdir@plt>
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2c7c(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2c76(%rip),%rax
mov    (%rax),%esi
mov    0x2c8d(%rip),%rax
mov    (%rax),%edx
lea    0xcde(%rip),%rdi
call   10c0 <openlog@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_daemon>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     1299 <setup_daemon+0xe9>
jne    128b <setup_daemon+0xdb>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     12bf <setup_daemon+0x10f>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    0x2ddb(%rip),%rbx
mov    (%rbx),%esi
call   1070 <signal@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1070 <signal@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12bf <setup_daemon+0x10f>
jne    128b <setup_daemon+0xdb>
xor    %edi,%edi
call   1080 <umask@plt>
movl   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %r14,%rdi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
je     12a7 <setup_daemon+0xf7>
mov    %r14,%rdi
call   1040 <chdir@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
lea    0xda2(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    10b0 <openlog@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xd95(%rip),%rdi
call   1030 <puts@plt>
jmp    12bf <setup_daemon+0x10f>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
lea    0xd49(%rip),%rsi
mov    %r14,%rdx
call   10a0 <syslog@plt>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [17 x i8] c"Failed to fork.\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [33 x i8] c"Log directory %s does not exits.\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [20 x i8] c"/var/log/htsend.log\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
define void @setup_daemon(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca %struct.stat
store i8* %0, i8** %2
%5 = call i64 (...) @fork()
store i64 %5, i64* %3
%6 = load i64, i64* %3
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = load i64, i64* %3
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @EXIT_SUCCESS
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %12
br label %19
19: ; preds = %18
%20 = load i64, i64* %3
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = call i64 (...) @setsid()
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %22
br label %29
29: ; preds = %28, %19
%30 = load i32, i32* @SIGCHLD
%31 = load i32, i32* @SIG_IGN
%32 = call i32 @signal(i32 %30, i32 %31)
%33 = load i32, i32* @SIGHUP
%34 = load i32, i32* @SIG_IGN
%35 = call i32 @signal(i32 %33, i32 %34)
%36 = call i64 (...) @fork()
store i64 %36, i64* %3
%37 = load i64, i64* %3
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %29
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %29
%43 = load i64, i64* %3
%44 = icmp sgt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %42
%49 = call i32 @umask(i32 0)
%50 = bitcast %struct.stat* %4 to i8*
%51 = load i8*, i8** %2
%52 = call i32 @stat(i8* %51, %struct.stat* %4)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %60
54: ; preds = %48
%55 = load i32, i32* @LOG_ERR
%56 = load i8*, i8** %2
%57 = call i32 @syslog(i32 %55, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %56)
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %48
%61 = load i8*, i8** %2
%62 = call i32 @chdir(i8* %61)
br label %63
63: ; preds = %60
%64 = load i32, i32* @STDIN_FILENO
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* @STDOUT_FILENO
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* @STDERR_FILENO
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* @LOG_PID
%71 = load i32, i32* @LOG_DAEMON
%72 = call i32 @openlog(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %70, i32 %71)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [33 x i8] c"Log directory %s does not exits.\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [20 x i8] c"/var/log/htsend.log\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
@str = [16 x i8] c"Failed to fork.\00"
define void @setup_daemon(i8* %0) {
%2 = alloca %struct.stat
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %1
%10 = icmp eq i64 %3, 0
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = load i32, i32* @EXIT_SUCCESS
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %9
%15 = tail call i64 (...) @setsid()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %14
%21 = load i32, i32* @SIGCHLD
%22 = load i32, i32* @SIG_IGN
%23 = tail call i32 @signal(i32 %21, i32 %22)
%24 = load i32, i32* @SIGHUP
%25 = load i32, i32* @SIG_IGN
%26 = tail call i32 @signal(i32 %24, i32 %25)
%27 = tail call i64 (...) @fork()
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %20
%33 = icmp eq i64 %27, 0
br i1 %33, label %37, label %34
34: ; preds = %32
%35 = load i32, i32* @EXIT_SUCCESS
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %32
%38 = tail call i32 @umask(i32 0)
%39 = bitcast %struct.stat* %2 to i8*
%40 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
store i32 0, i32* %40
%41 = call i32 @stat(i8* %0, %struct.stat* %2)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = load i32, i32* @LOG_ERR
%45 = tail call i32 @syslog(i32 %44, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%46 = load i32, i32* @EXIT_FAILURE
%47 = tail call i32 @exit(i32 %46)
unreachable
48: ; preds = %37
%49 = tail call i32 @chdir(i8* %0)
%50 = load i32, i32* @STDIN_FILENO
%51 = tail call i32 @close(i32 %50)
%52 = load i32, i32* @STDOUT_FILENO
%53 = tail call i32 @close(i32 %52)
%54 = load i32, i32* @STDERR_FILENO
%55 = tail call i32 @close(i32 %54)
%56 = load i32, i32* @LOG_PID
%57 = load i32, i32* @LOG_DAEMON
%58 = tail call i32 @openlog(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %56, i32 %57)
ret void
}