// File: /workspace/github_c_makefile_star2_O3/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 0x6ba0
testl %eax, %eax
jne 0x10f83
callq 0x6470
cmpl $-0x1, %eax
je 0x10fcb
callq 0x6bb0
cmpl $0x3, %eax
jl 0x10eb5
movl %eax, %r15d
movl %ebp, %r12d
testl %ebp, %ebp
movl $0x0, %eax
jle 0x10e9c
xorl %eax, %eax
cmpl %r15d, (%r14,%rax,4)
je 0x10e9c
incq %rax
cmpq %rax, %r12
jne 0x10e8c
jmp 0x10ea0
cmpl %ebp, %eax
jne 0x10ea8
movl %r15d, %edi
callq 0x6430
leal -0x1(%r15), %eax
cmpl $0x3, %r15d
movl %eax, %r15d
jg 0x10e81
testq %rbx, %rbx
je 0x10ece
movq %rsp, %rdi
movl $0x80, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x6330
jmp 0x10eeb
callq 0x61e0
movl %eax, %ecx
leaq 0x45b4(%rip), %rdx      # 0x15490
movq %rsp, %rdi
movl $0x80, %esi
xorl %eax, %eax
callq 0x6330
movq %rsp, %rdi
movl $0x441, %esi            # imm = 0x441
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x6890
cmpl $-0x1, %eax
je 0x11007
movq %rsp, %rdi
movl $0x1a4, %esi            # imm = 0x1A4
movl %eax, %ebx
callq 0x6870
cmpl $-0x1, %eax
je 0x11043
xorl %ebp, %ebp
movl %ebx, %edi
movl %ebp, %esi
callq 0x62d0
cmpl $-0x1, %eax
je 0x10f47
incl %ebp
cmpl $0x3, %ebp
jne 0x10f22
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0xa07a(%rip), %rax      # 0x1afc8
cmpl $0x0, (%rax)
js 0x10f79
movq 0xa046(%rip), %rax      # 0x1afa0
movq (%rax), %rbx
callq 0x60a0
movl (%rax), %edi
callq 0x6b20
movq %rax, %rdx
leaq 0x4594(%rip), %rsi      # 0x15507
xorl %edi, %edi
xorl %eax, %eax
callq *%rbx
movl $0xfffffffb, %edi       # imm = 0xFFFFFFFB
callq 0x6a40
cmpl $-0x1, %eax
jne 0x10fc4
movq 0xa039(%rip), %rax      # 0x1afc8
cmpl $0x0, (%rax)
js 0x10fba
movq 0xa005(%rip), %rax      # 0x1afa0
movq (%rax), %rbx
callq 0x60a0
movl (%rax), %edi
callq 0x6b20
movq %rax, %rdx
leaq 0x44b1(%rip), %rsi      # 0x15465
xorl %edi, %edi
xorl %eax, %eax
callq *%rbx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x6a40
xorl %edi, %edi
callq 0x6a40
movq 0x9ff6(%rip), %rax      # 0x1afc8
cmpl $0x0, (%rax)
js 0x10ffd
movq 0x9fc2(%rip), %rax      # 0x1afa0
movq (%rax), %rbx
callq 0x60a0
movl (%rax), %edi
callq 0x6b20
movq %rax, %rdx
leaq 0x4480(%rip), %rsi      # 0x15477
xorl %edi, %edi
xorl %eax, %eax
callq *%rbx
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
callq 0x6a40
movq 0x9fba(%rip), %rax      # 0x1afc8
cmpl $0x0, (%rax)
js 0x11039
movq 0x9f86(%rip), %rax      # 0x1afa0
movq (%rax), %rbx
callq 0x60a0
movl (%rax), %edi
callq 0x6b20
movq %rax, %rdx
leaq 0x4477(%rip), %rsi      # 0x154aa
xorl %edi, %edi
xorl %eax, %eax
callq *%rbx
movl $0xfffffffd, %edi       # imm = 0xFFFFFFFD
callq 0x6a40
movq 0x9f7e(%rip), %rax      # 0x1afc8
cmpl $0x0, (%rax)
js 0x11075
movq 0x9f4a(%rip), %rax      # 0x1afa0
movq (%rax), %rbx
callq 0x60a0
movl (%rax), %edi
callq 0x6b20
movq %rax, %rdx
leaq 0x4466(%rip), %rsi      # 0x154d5
xorl %edi, %edi
xorl %eax, %eax
callq *%rbx
movl $0xfffffffc, %edi       # imm = 0xFFFFFFFC
callq 0x6a40
nop