// File: AGWA[P]logmailer/logmailer.cpp
// Function: daemonize(char const*)
static void daemonize (const char* pid_file)
{
	// Open the PID file (open before forking so we can report errors)
	std::ofstream	pid_out;
	if (pid_file) {
		pid_out.open(pid_file, std::ofstream::out | std::ofstream::trunc);
		if (!pid_out) {
			throw System_error("open", pid_file, errno);
		}
	}

	// Fork
	pid_t		pid = fork();
	if (pid == -1) {
		throw System_error("fork", "", errno);
	}
	if (pid != 0) {
		// Exit parent
		_exit(0);
	}
	setsid();

	// Write the PID file now that we've forked
	if (pid_out) {
		pid_out << getpid() << '\n';
		pid_out.close();
	}

	// dup FDs to /dev/null
	close(0);
	close(1);
	close(2);
	open("/dev/null", O_RDONLY);
	open("/dev/null", O_WRONLY);
	open("/dev/null", O_WRONLY);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x310, %rsp            # imm = 0x310
movq %rdi, -0x8(%rbp)
leaq -0x208(%rbp), %rdi
callq 0x34d0
cmpq $0x0, -0x8(%rbp)
je 0x476b
movq -0x8(%rbp), %rax
movq %rax, -0x2c0(%rbp)
movl $0x10, %edi
movl $0x20, %esi
callq 0x6210
movl %eax, -0x2b8(%rbp)
jmp 0x45b8
movl -0x2b8(%rbp), %edx
movq -0x2c0(%rbp), %rsi
leaq -0x208(%rbp), %rdi
callq 0x3040
jmp 0x45d3
movq -0x208(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x208(%rbp,%rax), %rdi
callq 0x3130
movb %al, -0x2c1(%rbp)
jmp 0x45f3
movb -0x2c1(%rbp), %al
testb $0x1, %al
jne 0x4602
jmp 0x4769
movb $0x1, -0x262(%rbp)
movl $0x48, %edi
callq 0x3160
movq %rax, -0x2d8(%rbp)
leaq -0x239(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
callq 0x3510
movq -0x2d0(%rbp), %rdx
leaq 0x2c0a(%rip), %rsi      # 0x7245
leaq -0x238(%rbp), %rdi
callq 0x5dd0
jmp 0x4649
movq -0x8(%rbp), %rax
movq %rax, -0x2e8(%rbp)
leaq -0x261(%rbp), %rdi
movq %rdi, -0x2e0(%rbp)
callq 0x3510
movq -0x2e8(%rbp), %rsi
movq -0x2e0(%rbp), %rdx
leaq -0x260(%rbp), %rdi
callq 0x5dd0
jmp 0x4683
callq 0x3030
movq -0x2d8(%rbp), %rdi
movl (%rax), %ecx
leaq -0x238(%rbp), %rsi
leaq -0x260(%rbp), %rdx
callq 0x5e80
jmp 0x46a6
movq -0x2d8(%rbp), %rdi
movb $0x0, -0x262(%rbp)
leaq 0x46cd(%rip), %rsi      # 0x8d88
leaq 0x182e(%rip), %rdx      # 0x5ef0
callq 0x34e0
jmp 0x49d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x49bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x4741
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x4729
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x260(%rbp), %rdi
callq 0x3588
leaq -0x261(%rbp), %rdi
callq 0x32f0
leaq -0x238(%rbp), %rdi
callq 0x3588
leaq -0x239(%rbp), %rdi
callq 0x32f0
testb $0x1, -0x262(%rbp)
jne 0x4758
jmp 0x4764
movq -0x2d8(%rbp), %rdi
callq 0x31c0
jmp 0x49bd
jmp 0x476b
callq 0x30b0
movl %eax, -0x268(%rbp)
cmpl $-0x1, -0x268(%rbp)
jne 0x48c8
movb $0x1, -0x2b2(%rbp)
movl $0x48, %edi
callq 0x3160
movq %rax, -0x2f8(%rbp)
leaq -0x289(%rbp), %rdi
movq %rdi, -0x2f0(%rbp)
callq 0x3510
movq -0x2f0(%rbp), %rdx
leaq 0x2a5d(%rip), %rsi      # 0x7219
leaq -0x288(%rbp), %rdi
callq 0x5dd0
jmp 0x47ca
leaq -0x2b1(%rbp), %rdi
movq %rdi, -0x300(%rbp)
callq 0x3510
movq -0x300(%rbp), %rdx
leaq 0x2a66(%rip), %rsi      # 0x7251
leaq -0x2b0(%rbp), %rdi
callq 0x5dd0
jmp 0x47f9
callq 0x3030
movq -0x2f8(%rbp), %rdi
movl (%rax), %ecx
leaq -0x288(%rbp), %rsi
leaq -0x2b0(%rbp), %rdx
callq 0x5e80
jmp 0x481c
movq -0x2f8(%rbp), %rdi
movb $0x0, -0x2b2(%rbp)
leaq 0x4557(%rip), %rsi      # 0x8d88
leaq 0x16b8(%rip), %rdx      # 0x5ef0
callq 0x34e0
jmp 0x49d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x48a0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x4888
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x2b0(%rbp), %rdi
callq 0x3588
leaq -0x2b1(%rbp), %rdi
callq 0x32f0
leaq -0x288(%rbp), %rdi
callq 0x3588
leaq -0x289(%rbp), %rdi
callq 0x32f0
testb $0x1, -0x2b2(%rbp)
jne 0x48b7
jmp 0x48c3
movq -0x2f8(%rbp), %rdi
callq 0x31c0
jmp 0x49bd
cmpl $0x0, -0x268(%rbp)
je 0x48da
xorl %edi, %edi
callq 0x34a0
jmp 0x48da
callq 0x3320
movq -0x208(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x208(%rbp,%rax), %rdi
callq 0x33c0
movb %al, -0x301(%rbp)
jmp 0x48ff
movb -0x301(%rbp), %al
testb $0x1, %al
jne 0x490b
jmp 0x494a
callq 0x3450
movl %eax, %esi
leaq -0x208(%rbp), %rdi
callq 0x34f0
movq %rax, -0x310(%rbp)
jmp 0x4927
movq -0x310(%rbp), %rdi
movl $0xa, %esi
callq 0x3350
jmp 0x493a
leaq -0x208(%rbp), %rdi
callq 0x3410
jmp 0x4948
jmp 0x494a
xorl %edi, %edi
callq 0x3560
jmp 0x4953
movl $0x1, %edi
callq 0x3560
jmp 0x495f
movl $0x2, %edi
callq 0x3560
jmp 0x496b
leaq 0x28ac(%rip), %rdi      # 0x721e
xorl %esi, %esi
movb %sil, %al
callq 0x31d0
jmp 0x497e
leaq 0x2899(%rip), %rdi      # 0x721e
xorl %eax, %eax
movl $0x1, %esi
callq 0x31d0
jmp 0x4993
leaq 0x2884(%rip), %rdi      # 0x721e
xorl %eax, %eax
movl $0x1, %esi
callq 0x31d0
jmp 0x49a8
leaq -0x208(%rbp), %rdi
callq 0x3460
addq $0x310, %rsp            # imm = 0x310
popq %rbp
retq
leaq -0x208(%rbp), %rdi
callq 0x3460
movq -0x210(%rbp), %rdi
callq 0x3500
nopw %cs:(%rax,%rax)