// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movq %rdi, -0x8(%rbp)
callq 0x2480
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jge 0x40f0
leaq 0x54ef(%rip), %rdi      # 0x95d0
callq 0x2390
movl $0x1, %edi
callq 0x23f0
cmpl $0x0, -0xc(%rbp)
jle 0x40fd
xorl %edi, %edi
callq 0x23f0
callq 0x21e0
cmpl $0x0, %eax
jge 0x411d
leaq 0x54c7(%rip), %rdi      # 0x95d5
callq 0x2390
movl $0x1, %edi
callq 0x23f0
movl $0x11, %edi
movl $0x1, %esi
callq 0x2230
movl $0x1, %edi
movl $0x1, %esi
callq 0x2230
callq 0x2480
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jge 0x415f
leaq 0x5480(%rip), %rdi      # 0x95d0
callq 0x2390
movl $0x1, %edi
callq 0x23f0
cmpl $0x0, -0xc(%rbp)
jle 0x416c
xorl %edi, %edi
callq 0x23f0
xorl %edi, %edi
callq 0x2260
movq -0x8(%rbp), %rdi
callq 0x20e0
cmpl $0x0, %eax
je 0x4197
leaq 0x5454(%rip), %rdi      # 0x95dc
callq 0x2390
movl $0x1, %edi
callq 0x23f0
movl $0x4, %edi
callq 0x23a0
movl %eax, -0x10(%rbp)
cmpl $0x0, -0x10(%rbp)
jl 0x41bd
movl -0x10(%rbp), %edi
callq 0x21c0
movl -0x10(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x41a4
addq $0x10, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)