// File: /workspace/github_cpp_makefile_star2_O1/mrGoncharuk[P]foldermonitor/src/main.cpp
// Function: daemonize()
int	daemonize()
{
	pid_t pid, sid;

	pid = fork();
	if(pid > 0)
	{
		exit(EXIT_SUCCESS);
	}
	else if(pid < 0)
	{
		exit(EXIT_FAILURE);
	}
	umask(0);

	openlog("daemon-foldermonitor", LOG_NOWAIT | LOG_PID, LOG_MAIL);
	syslog(LOG_NOTICE, "Staring daemon-foldermonitor...");

	sid = setsid();
	if(sid < 0)
	{
		syslog(LOG_ERR, "Could not generate session ID for child process");
		exit(EXIT_FAILURE);
	}

	if((chdir("/")) < 0)
	{
		syslog(LOG_ERR, "Could not change working directory to /");
		exit(EXIT_FAILURE);
	}


	const char *pidfile = "/var/run/daemon-foldermonitor.pid";
	int pidFilehandle = open(pidfile, O_RDWR | O_CREAT, 0666);
	if (pidFilehandle == -1 )
	{
		syslog(LOG_INFO, "Could not open PID lock file %s, exiting", pidfile);
		exit(EXIT_FAILURE);
	}
	int fr = flock(pidFilehandle, LOCK_EX | LOCK_NB);
	if (fr < 0)
	{
		syslog(LOG_INFO, "Daemon already running. Terminating current daemon with pid <%d>.", getpid());
		exit(EXIT_FAILURE);
	}
	std::string strpid = std::to_string(getpid());
	write(pidFilehandle, strpid.c_str(), strpid.length());
	
	close(STDIN_FILENO);
	close(STDOUT_FILENO);
	close(STDERR_FILENO);
	return pidFilehandle;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
callq 0x2080
testl %eax, %eax
jg 0x2677
js 0x26d2
xorl %edi, %edi
callq 0x22f0
leaq 0x1ae9(%rip), %rdi      # 0x4004
movl $0x11, %esi
movl $0x10, %edx
callq 0x2190
leaq 0x1ae8(%rip), %rsi      # 0x4019
movl $0x5, %edi
xorl %eax, %eax
callq 0x2050
callq 0x21f0
testl %eax, %eax
js 0x267e
leaq 0x1b3e(%rip), %rdi      # 0x408f
callq 0x2290
testl %eax, %eax
js 0x2687
leaq 0x1b2c(%rip), %rdi      # 0x4091
movl $0x42, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x2160
cmpl $-0x1, %eax
je 0x269c
movl %eax, %ebx
movl %eax, %edi
movl $0x6, %esi
callq 0x2340
testl %eax, %eax
js 0x26b8
callq 0x2270
movl %eax, %r14d
movl %eax, %ebp
negl %ebp
cmovsl %eax, %ebp
movl $0x1, %r15d
cmpl $0xa, %ebp
jb 0x25f8
movl $0x4, %r15d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %ebp, %ecx
cmpl $0x63, %ecx
jbe 0x25ef
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x25f5
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x25f8
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r15d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x25bc
addl $-0x3, %r15d
jmp 0x25f8
addl $-0x2, %r15d
jmp 0x25f8
decl %r15d
shrl $0x1f, %r14d
leal (%r15,%r14), %esi
leaq 0x18(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x8(%rsp), %r12
movq %r12, %rdi
movl $0x2d, %edx
callq 0x2200
addq (%r12), %r14
movq %r14, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x2a02
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movl %ebx, %edi
callq 0x20c0
xorl %edi, %edi
callq 0x2360
movl $0x1, %edi
callq 0x2360
movl $0x2, %edi
callq 0x2360
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x2666
callq 0x21c0
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
callq 0x2210
leaq 0x19b4(%rip), %rsi      # 0x4039
jmp 0x268e
leaq 0x19db(%rip), %rsi      # 0x4069
movl $0x3, %edi
xorl %eax, %eax
callq 0x2050
jmp 0x26d2
leaq 0x1a10(%rip), %rsi      # 0x40b3
leaq 0x19e7(%rip), %rdx      # 0x4091
movl $0x6, %edi
xorl %eax, %eax
callq 0x2050
jmp 0x26d2
callq 0x2270
leaq 0x1a18(%rip), %rsi      # 0x40dc
movl $0x6, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x2050
movl $0x1, %edi
callq 0x2210
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x26ee
callq 0x21c0
movq %rbx, %rdi
callq 0x22e0