// File: /workspace/github_c_makefile_star2_O2/sharpyfox[P]citrusleaf-client/src/cf_service.c
// Function: cf_process_daemonize
void
cf_process_daemonize(const char *redirect_file, int *fd_ignore_list, int list_size)
{
    int FD, j;
    char cfile[128];
    pid_t p;
 
    /* Fork ourselves, then let the parent expire */
    if (-1 == (p = fork())){
    	cf_error("Couldn't fork: %s", strerror(errno));
//      cf_crash(CF_MISC, CF_GLOBAL, CF_CRITICAL, "couldn't fork: %s", cf_strerror(errno));
        exit(-1);
    }
    if (0 != p)
        exit(0);

    /* Get a new session */
    if (-1 == setsid()){
    	cf_error("Couldn't set session: %s", strerror(errno));
        exit(-2);
//      cf_crash(CF_MISC, CF_GLOBAL, CF_CRITICAL, "couldn't set session: %s", cf_strerror(errno));
    }

    /* Drop all the file descriptors except the ones in fd_ignore_list*/
    for (int i = getdtablesize(); i > 2; i--) {
	for (j = 0; j < list_size; j++) {
	    if (fd_ignore_list[j] == i) {
		break;
	    }
	}
	if(j ==  list_size) {
	    close(i);
	}
    }


    /* Open a temporary file for console message redirection */
    if (redirect_file == NULL){ 
        snprintf(cfile, 128, "/tmp/aerospike-console.%d", getpid());
    }else{
        snprintf(cfile, 128, redirect_file);
    }
    if (-1 == (FD = open(cfile, O_WRONLY|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR))){
        cf_error("Couldn't open console redirection file: %s", strerror(errno));
//        cf_crash(CF_MISC, CF_GLOBAL, CF_CRITICAL, "couldn't open console redirection file: %s", cf_strerror(errno));
        exit(-3);
    }
    if (-1 == chmod(cfile, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))){
    	cf_error("Couldn't set mode on console redirection file: %s", strerror(errno));
//        cf_crash(CF_MISC, CF_GLOBAL, CF_CRITICAL, "couldn't set mode on console redirection file: %s", cf_strerror(errno));
        exit(-4);
    }

    /* Redirect stdout, stderr, and stdin to the console file */
    for (int i = 0; i < 3; i++) {
        if (-1 == dup2(FD, i)){
        	cf_error("Couldn't duplicate FD: %s", strerror(errno));
//            cf_crash(CF_MISC, CF_GLOBAL, CF_CRITICAL, "couldn't duplicate FD: %s", cf_strerror(errno));
            exit(-5);
        }
    }

     return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x80, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
callq 0x6c30
testl %eax, %eax
jne 0x10592
callq 0x64a0
cmpl $-0x1, %eax
je 0x105d4
callq 0x6c40
xorl %r12d, %r12d
testl %ebp, %ebp
movl %eax, %r15d
cmovgl %ebp, %r12d
cmpl $0x3, %r15d
jl 0x104c6
xorl %eax, %eax
cmpq %rax, %r12
je 0x104b2
cmpl %r15d, (%r14,%rax,4)
je 0x104b5
incq %rax
jmp 0x104a2
movl %r12d, %eax
cmpl %ebp, %eax
jne 0x104c1
movl %r15d, %edi
callq 0x6460
decl %r15d
jmp 0x1049a
testq %rbx, %rbx
je 0x104df
movq %rsp, %rdi
movl $0x80, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x6350
jmp 0x104fc
callq 0x61e0
movl %eax, %ecx
leaq 0x3fb3(%rip), %rdx      # 0x144a0
movq %rsp, %rdi
movl $0x80, %esi
xorl %eax, %eax
callq 0x6350
movq %rsp, %rdi
movl $0x441, %esi            # imm = 0x441
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x68e0
cmpl $-0x1, %eax
je 0x1060d
movq %rsp, %rdi
movl $0x1a4, %esi            # imm = 0x1A4
movl %eax, %ebx
callq 0x68c0
cmpl $-0x1, %eax
je 0x10646
xorl %ebp, %ebp
cmpl $0x3, %ebp
je 0x10582
movl %ebx, %edi
movl %ebp, %esi
callq 0x62f0
incl %ebp
cmpl $-0x1, %eax
jne 0x10533
movq 0x9a79(%rip), %rax      # 0x19fc8
cmpl $0x0, (%rax)
js 0x1057a
movq 0x9a45(%rip), %rax      # 0x19fa0
movq (%rax), %rbx
callq 0x60a0
movl (%rax), %edi
callq 0x6bb0
movq %rax, %rdx
leaq 0x3fa3(%rip), %rsi      # 0x14517
xorl %edi, %edi
xorl %eax, %eax
callq *%rbx
pushq $-0x5
popq %rdi
callq 0x6ac0
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpl $-0x1, %eax
jne 0x105cd
movq 0x9a2a(%rip), %rax      # 0x19fc8
cmpl $0x0, (%rax)
js 0x105c9
movq 0x99f6(%rip), %rax      # 0x19fa0
movq (%rax), %rbx
callq 0x60a0
movl (%rax), %edi
callq 0x6bb0
movq %rax, %rdx
leaq 0x3eb2(%rip), %rsi      # 0x14475
xorl %edi, %edi
xorl %eax, %eax
callq *%rbx
pushq $-0x1
jmp 0x1057c
xorl %edi, %edi
callq 0x6ac0
movq 0x99ed(%rip), %rax      # 0x19fc8
cmpl $0x0, (%rax)
js 0x10606
movq 0x99b9(%rip), %rax      # 0x19fa0
movq (%rax), %rbx
callq 0x60a0
movl (%rax), %edi
callq 0x6bb0
movq %rax, %rdx
leaq 0x3e87(%rip), %rsi      # 0x14487
xorl %edi, %edi
xorl %eax, %eax
callq *%rbx
pushq $-0x2
jmp 0x1057c
movq 0x99b4(%rip), %rax      # 0x19fc8
cmpl $0x0, (%rax)
js 0x1063f
movq 0x9980(%rip), %rax      # 0x19fa0
movq (%rax), %rbx
callq 0x60a0
movl (%rax), %edi
callq 0x6bb0
movq %rax, %rdx
leaq 0x3e81(%rip), %rsi      # 0x144ba
xorl %edi, %edi
xorl %eax, %eax
callq *%rbx
pushq $-0x3
jmp 0x1057c
movq 0x997b(%rip), %rax      # 0x19fc8
cmpl $0x0, (%rax)
js 0x10678
movq 0x9947(%rip), %rax      # 0x19fa0
movq (%rax), %rbx
callq 0x60a0
movl (%rax), %edi
callq 0x6bb0
movq %rax, %rdx
leaq 0x3e73(%rip), %rsi      # 0x144e5
xorl %edi, %edi
xorl %eax, %eax
callq *%rbx
pushq $-0x4
jmp 0x1057c
nop