// File: qian-jiahong[P]uvncrep017-ws/repeater.cpp
// Function: cleanUpAfterRepeaterProcExit(int, int)
static void cleanUpAfterRepeaterProcExit(int exitCode, pid_t pid) {
    long code;
    int index;
    int serverInd;
    int viewerInd;
    
    debug(LEVEL_3, "cleanUpAfterRepeaterProcExit(): exitCode=%d, pid=%d\n", exitCode, pid);
    index = findRepeaterProcList(pid);
    if (index != UNKNOWN_REPINFO_IND) {
        code = repeaterProcs[index] -> code;
        serverInd = findServerList(code);
        viewerInd = findViewerList(code);
        
        if ((serverInd != UNKNOWN_REPINFO_IND) && (viewerInd != UNKNOWN_REPINFO_IND)) {
            //Remove repeaterproc from list
            removeRepeaterProcList(pid);
            
            debug(LEVEL_3, "cleanUpAfterRepeaterProcExit(): code=%ld, serverInd=%d, viewerInd=%d\n", 
                code, serverInd, viewerInd);
        
            //Send VIEWER_SERVER_SESSION_END to event interface
            if (useEventInterface) {
                repeaterEvent event;
                sessionEvent sessEv;
                            
                //VIEWER_SERVER_SESSION_END
                event.eventNum = VIEWER_SERVER_SESSION_END;
                event.timeStamp = time(NULL);
                event.repeaterProcessId = getpid();
                
                sessEv.serverTableIndex = serverInd;
                sessEv.viewerTableIndex = viewerInd;
                sessEv.code = code;
                sessEv.connMode = (code < 0) ? CONN_MODE1 : CONN_MODE2;
                sessEv.serverIp = servers[serverInd] -> peerIp;
                sessEv.viewerIp = viewers[viewerInd] -> peerIp;
                memcpy(event.extraInfo, &sessEv, sizeof(sessionEvent));
                if (false == sendRepeaterEvent(event)) {
                    debug(LEVEL_1, "cleanUpAfterRepeaterProcExit(): Warning, event fifo is full\n");
                }
            }
        
            switch(exitCode) {
                case 1:
                    //Error in select(), fall through
                case 2:
                    //Server has disconnected, fall through
                case 3:                
                    //Viewer has disconnected, fall through
                case 4:                
                    //Error when reading from viewer, fall through
                case 5:
                    //Error when reading from server
                    debug(LEVEL_1, "cleanUpAfterRepeaterProcExit(): closing connection (server=%d, viewer=%d)\n", 
                        servers[serverInd] -> socket, viewers[viewerInd] -> socket);
                    removeServerList(code);
                    removeViewerList(code);
                    break;

                default:
                    break;
            }
        }
        else {
            debug(LEVEL_2, "cleanUpAfterRepeaterProcExit(): illegal viewerInd = %d or serverInd =%d\n", 
                viewerInd, serverInd);
        }        
    }
    else {
        debug(LEVEL_2, "cleanUpAfterRepeaterProcExit(): proc not found\n");
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movl -0x4(%rbp), %edx
movl -0x8(%rbp), %ecx
movl $0x3, %edi
leaq 0x52af(%rip), %rsi      # 0xbe72
movb $0x0, %al
callq 0x2480
movl -0x8(%rbp), %edi
callq 0x6e60
movl %eax, -0x14(%rbp)
cmpl $0xf423f, -0x14(%rbp)   # imm = 0xF423F
je 0x6e35
movslq -0x14(%rbp), %rcx
leaq 0xaf83(%rip), %rax      # 0x11b70
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6220
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x62c0
movl %eax, -0x1c(%rbp)
cmpl $0xf423f, -0x18(%rbp)   # imm = 0xF423F
je 0x6e1a
cmpl $0xf423f, -0x1c(%rbp)   # imm = 0xF423F
je 0x6e1a
movl -0x8(%rbp), %edi
callq 0x6f00
movq -0x10(%rbp), %rdx
movl -0x18(%rbp), %ecx
movl -0x1c(%rbp), %r8d
movl $0x3, %edi
leaq 0x525e(%rip), %rsi      # 0xbea7
movb $0x0, %al
callq 0x2480
leaq 0x1169d(%rip), %rax     # 0x182f4
testb $0x1, (%rax)
je 0x6dc1
movl $0x5, -0x98(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x21e0
movq %rax, -0x90(%rbp)
callq 0x22f0
movl %eax, -0x88(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xd0(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0xcc(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x10(%rbp), %rdx
movl $0x2, %eax
movl $0x1, %ecx
cmpq $0x0, %rdx
cmovll %ecx, %eax
movl %eax, -0xc0(%rbp)
movslq -0x18(%rbp), %rcx
leaq 0xcde8(%rip), %rax      # 0x13ab0
movq (%rax,%rcx,8), %rax
movq 0x18(%rax), %rcx
movq %rcx, -0xbc(%rbp)
movq 0x20(%rax), %rax
movq %rax, -0xb4(%rbp)
movslq -0x1c(%rbp), %rcx
leaq 0xed03(%rip), %rax      # 0x159f0
movq (%rax,%rcx,8), %rax
movq 0x18(%rax), %rcx
movq %rcx, -0xac(%rbp)
movq 0x20(%rax), %rax
movq %rax, -0xa4(%rbp)
leaq -0x98(%rbp), %rdi
addq $0x14, %rdi
leaq -0xd0(%rbp), %rsi
movl $0x38, %edx
callq 0x21b0
leaq -0x148(%rbp), %rdi
leaq -0x98(%rbp), %rsi
movl $0x78, %edx
callq 0x21b0
movq -0xd8(%rbp), %rcx
movq %rsp, %rax
movq %rax, -0x150(%rbp)
movq %rcx, 0x70(%rax)
movups -0xe8(%rbp), %xmm0
movups %xmm0, 0x60(%rax)
movups -0xf8(%rbp), %xmm0
movups %xmm0, 0x50(%rax)
movups -0x108(%rbp), %xmm0
movups %xmm0, 0x40(%rax)
movups -0x148(%rbp), %xmm0
movups -0x138(%rbp), %xmm1
movups -0x128(%rbp), %xmm2
movups -0x118(%rbp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x9130
andb $0x1, %al
movzbl %al, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x6dbf
movl $0x1, %edi
leaq 0x5135(%rip), %rsi      # 0xbeed
movb $0x0, %al
callq 0x2480
jmp 0x6dc1
movl -0x4(%rbp), %eax
decl %eax
subl $0x4, %eax
ja 0x6e16
jmp 0x6dcd
movslq -0x18(%rbp), %rcx
leaq 0xccd8(%rip), %rax      # 0x13ab0
movq (%rax,%rcx,8), %rax
movl (%rax), %edx
movslq -0x1c(%rbp), %rcx
leaq 0xec07(%rip), %rax      # 0x159f0
movq (%rax,%rcx,8), %rax
movl (%rax), %ecx
movl $0x1, %edi
leaq 0x512f(%rip), %rsi      # 0xbf2a
movb $0x0, %al
callq 0x2480
movq -0x10(%rbp), %rdi
callq 0x6af0
movq -0x10(%rbp), %rdi
callq 0x6a40
jmp 0x6e18
jmp 0x6e18
jmp 0x6e33
movl -0x1c(%rbp), %edx
movl -0x18(%rbp), %ecx
movl $0x2, %edi
leaq 0x5149(%rip), %rsi      # 0xbf75
movb $0x0, %al
callq 0x2480
jmp 0x6e48
movl $0x2, %edi
leaq 0x517d(%rip), %rsi      # 0xbfbe
movb $0x0, %al
callq 0x2480
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
nopw %cs:(%rax,%rax)