// File: /workspace/github_c_makefile_star2_O2/pencode-lab[P]Shoit/shoit_misc.c
// Function: shoit_misc_daemon
void shoit_misc_daemon(char *work_dir)
{
    pid_t pid;

    /* Fork off the parent process */
    pid = fork();

    /* An error occurred */
    if (pid < 0){
        perror("fork");
        exit(1);
    }

    /* Success: Let the parent terminate */
    if (pid > 0)
        exit(0);

    /* On success: The child process becomes session leader */
    if (setsid() < 0){
        perror("setsid");
        exit(1);                                                                                                                     
    }                  
    /* Catch, ignore and handle signals */                                                                                           
    //TODO: Implement a working signal handler */                                                                                    
    signal(SIGCHLD, SIG_IGN);                                                                                                        
    signal(SIGHUP, SIG_IGN);                                                                                                         
                                                                                                                                     
    /* Fork off for the second time*/                                                                                                
    pid = fork();                                                                                                                    
                                                                                                                                     
    /* An error occurred */                                                                                                          
    if (pid < 0){                                                                                                                     
        perror("fork");                                                                                                              
        exit(1);                                                                                                                     
    }                                                                                                                                
                                                                                                                                     
    /* Success: Let the parent terminate */                                                                                          
    if (pid > 0)                                                                                                                     
        exit(0);                                                                                                                     
    /* Set new file permissions */                                                                                                   
    umask(0);                                                                                                                        
                                                                                                                                     
    /* Change the working directory to the root directory */                                                                         
    /* or another appropriated directory */                                                                                          
    if(chdir(work_dir) ){                                                                                                            
        perror("chdir");                                                                                                             
        exit(1);                                                                                                                     
    }                                                                                                                                
                                                                                                                                     
    /* Close all open file descriptors */                                                                                            
    int x;                                                                                                                           
    for (x = sysconf(_SC_OPEN_MAX); x>=0; x--)                                                                                       
    {                                                                                                                                
        close (x);                                                                                                                   
    }                                                                                                                                
                                                                                                                                     
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
callq 0x2480
testl %eax, %eax
js 0x362c
jne 0x3635
callq 0x21d0
testl %eax, %eax
js 0x363c
pushq $0x11
popq %rdi
pushq $0x1
popq %r14
movq %r14, %rsi
callq 0x2230
pushq $0x1
popq %rdi
movq %r14, %rsi
callq 0x2230
callq 0x2480
testl %eax, %eax
js 0x362c
jne 0x3635
xorl %edi, %edi
callq 0x2260
movq %rbx, %rdi
callq 0x20e0
testl %eax, %eax
jne 0x3645
pushq $0x4
popq %rdi
callq 0x23a0
movq %rax, %rbx
testl %ebx, %ebx
js 0x3624
movl %ebx, %edi
callq 0x21b0
decl %ebx
jmp 0x3615
addq $0x8, %rsp
popq %rbx
popq %r14
retq
leaq 0x2ed2(%rip), %rdi      # 0x6505
jmp 0x364c
xorl %edi, %edi
callq 0x23f0
leaq 0x2ec7(%rip), %rdi      # 0x650a
jmp 0x364c
leaq 0x2ec5(%rip), %rdi      # 0x6511
callq 0x2390
pushq $0x1
popq %rdi
callq 0x23f0
nopl (%rax)