// File: /workspace/github_c_makefile_star2_O2/hasindu2008[P]f5p/f5pl.c
// Function: node_handler
void* node_handler(void* arg) {
    //thread index
    int32_t tid = *((int32_t*)arg);

    //buffer for socket communication
    char buffer[MAX_PATH_SIZE];

    //reset the hang counter
    core.num_hangs[tid] = 0;

    //open report file
    char report_fname[100];
    sprintf(report_fname, "dev%d.cfg", tid+1);
    FILE* report = fopen(report_fname, "w");
    NULL_CHK(report);

    while (1) {
        pthread_mutex_lock(&file_list_mutex);
        int32_t fidx = core.file_list_idx;

        //check if there are files remaining to be processed
        if (fidx < core.file_list_cnt) {
            core.file_list_idx++;
            pthread_mutex_unlock(&file_list_mutex);
        } else {
            pthread_mutex_unlock(&file_list_mutex);
            break;
        }

        fprintf(stderr, "[t%d(%s)::INFO] Connecting to %s.\n", tid,
                core.ip_list[tid], core.ip_list[tid]);
        int socketfd = TCP_client_connect_try(core.ip_list[tid], PORT,CONNECT_TIME_OUT);
        if(socketfd==-1){
            fprintf(stderr,
                        "[t%d(%s)::WARNING]\033[1;33m Connection initiation to device %s failed. Giving up hope on the device.\033[0m\n",
                        tid, core.ip_list[tid], core.ip_list[tid]);  
            pthread_mutex_lock(&file_list_mutex); //todo : this can be a different lock for efficiency
            int32_t failed_cnt = core.failed_cnt;
            core.failed_cnt++;
            core.failed[failed_cnt] = fidx;
            pthread_mutex_unlock(&file_list_mutex);  
            break;         
        }

        fprintf(stderr, "[t%d(%s)::INFO] Assigning %s (%d of %d) to %s.\n", tid,
                core.ip_list[tid], core.file_list[fidx], fidx+1 , core.file_list_cnt, core.ip_list[tid]);
        send_full_msg(socketfd, core.file_list[fidx],
                      strlen(core.file_list[fidx]));

        //receive the ''done' message (will block until done)
        int received = recv_full_msg_try(socketfd, buffer, MAX_PATH_SIZE, RECEIVE_TIME_OUT);

        //handle incase the socket has broken
        int32_t count = 0;
        while (received < 0) {
            count++;
            core.num_hangs[tid]++;
            fprintf(stderr,
                        "[t%d(%s)::WARNING]\033[1;33m Device %s has hung/disconnected. \033[0m\n",
                        tid, core.ip_list[tid], core.ip_list[tid]);            
            if (count >= MAX_CONSECUTIVE_FAILURES) { //if the device failed so many times
                fprintf(stderr,
                        "[t%d(%s)::ERROR]\033[1;31m Device %s failed %d times "
                        "consecutively. Retiring the device. \033[0m\n",
                        tid, core.ip_list[tid], core.ip_list[tid], count);
                pthread_mutex_lock(&file_list_mutex); //todo : this can be a different lock for efficiency
                int32_t failed_cnt = core.failed_cnt;
                core.failed_cnt++;
                core.failed[failed_cnt] = fidx;
                pthread_mutex_unlock(&file_list_mutex);
                fclose(report);
                fprintf(stderr,
                "[t%d(%s)::INFO] \033[1;34m Processed list: %s Elapsed time: %.3fh \033[0m\n",tid,core.ip_list[tid],report_fname,(realtime() - realtime0)/3600);
                pthread_exit(0);
            }
            fprintf(stderr, "[t%d(%s)::INFO] Connecting to %s\n", tid,
                    core.ip_list[tid], core.ip_list[tid]);
            socketfd = TCP_client_connect_try(core.ip_list[tid], PORT,CONNECT_TIME_OUT);
            if(socketfd==-1){ //complete device hang detection
                fprintf(stderr,
                            "[t%d(%s)::WARNING]\033[1;33m Connection initiation to device %s failed. Giving up hope on the device.\033[0m\n",
                            tid, core.ip_list[tid], core.ip_list[tid]);  
                pthread_mutex_lock(&file_list_mutex); //todo : this can be a different lock for efficiency
                int32_t failed_cnt = core.failed_cnt;
                core.failed_cnt++;
                core.failed[failed_cnt] = fidx;
                pthread_mutex_unlock(&file_list_mutex);  
                fclose(report);
                 fprintf(stderr,
                "[t%d(%s)::INFO] \033[1;34m Processed list: %s Elapsed time: %.3fh \033[0m\n",tid,core.ip_list[tid],report_fname,(realtime() - realtime0)/3600);
                pthread_exit(0);        
            }            
            fprintf(stderr, "[t%d(%s)::INFO] Assigning %s (%d of %d) to %s\n", tid,
                    core.ip_list[tid], core.file_list[fidx],  fidx+1 , core.file_list_cnt, core.ip_list[tid]);
            send_full_msg(socketfd, core.file_list[fidx],
                          strlen(core.file_list[fidx]));
            received = recv_full_msg_try(socketfd, buffer, MAX_PATH_SIZE, 5);
        }

        //print the message
        buffer[received] = '\0'; //null character before printing the string
        fprintf(stderr, "[t%d(%s)::INFO] Received message '%s'.\n", tid,
                core.ip_list[tid], buffer);

        if (strcmp(buffer, "done.") == 0) {
            //write to report
            fprintf(report, "%s\n", core.file_list[fidx]);
        } else if (strcmp(buffer, "crashed.") == 0) {
            fprintf(stderr,"[t%d(%s)::WARNING]\033[1;33m %s terminated due to a signal. Please inspect the device log.\033[0m\n",tid,core.ip_list[tid],
                    core.file_list[fidx]);
            int32_t failed_cnt = core.failed_other_cnt;
            core.failed_other_cnt++;
            core.failed_other[failed_cnt] = fidx;
        } else {
            fprintf(stderr,
                "[t%d(%s)::WARNING] \033[1;33m%s exited with a non 0 exit status. Please inspect the device log.\033[0m\n",tid,core.ip_list[tid],
                core.file_list[fidx]);
            int32_t failed_cnt = core.failed_other_cnt;
            core.failed_other_cnt++;
            core.failed_other[failed_cnt] = fidx;
        }

        //close the connection
        TCP_client_disconnect(socketfd);
    }
    fprintf(stderr,
                "[t%d(%s)::INFO] \033[1;34m Processed list: %s Elapsed time: %.3fh \033[0m\n",tid,core.ip_list[tid],report_fname,(realtime() - realtime0)/3600);

    fclose(report);
    pthread_exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1088, %rsp           # imm = 0x1088
movslq (%rdi), %rbx
leaq 0x4419(%rip), %rax      # 0x6190
andl $0x0, 0x401c(%rax,%rbx,4)
leal 0x1(%rbx), %edx
leaq 0x16cf(%rip), %rsi      # 0x3458
leaq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x11f0
leaq 0x16c3(%rip), %rsi      # 0x3462
movq %r14, %rdi
callq 0x11b0
leaq 0x16b6(%rip), %rsi      # 0x3464
leaq 0x16bc(%rip), %rdx      # 0x3471
pushq $0x5e
popq %rcx
movq %rax, (%rsp)
movq %rax, %rdi
callq 0x29be
leaq 0x4395(%rip), %r14      # 0x6160
movq 0x4226(%rip), %r15      # 0x5ff8
movq %r14, %rdi
callq 0x1260
movl 0x43b7(%rip), %r13d     # 0x6198
cmpl 0x43b4(%rip), %r13d     # 0x619c
jge 0x20ca
leal 0x1(%r13), %ebp
movl %ebp, 0x43a0(%rip)      # 0x6198
movq %r14, %rdi
callq 0x1140
movq (%r15), %rdi
movq 0x4396(%rip), %rax      # 0x61a0
movq (%rax,%rbx,8), %rcx
leaq 0x1663(%rip), %rsi      # 0x3478
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
movq 0x4378(%rip), %rax      # 0x61a0
movq (%rax,%rbx,8), %rdi
movl $0x4e36, %esi           # imm = 0x4E36
movl $0xc8, %edx
callq 0x1b32
movl %eax, %r12d
movq (%r15), %rdi
movq 0x4358(%rip), %rax      # 0x61a0
movq (%rax,%rbx,8), %rcx
cmpl $-0x1, %r12d
je 0x208f
movq %r13, 0x8(%rsp)
movl %ebp, %r9d
movslq %r13d, %rbp
movq 0x4328(%rip), %rax      # 0x6190
movq (%rax,%rbp,8), %r8
movl 0x4329(%rip), %r10d     # 0x619c
leaq 0x1689(%rip), %rsi      # 0x3503
movl %ebx, %edx
movl %r9d, %r14d
xorl %eax, %eax
pushq %rcx
pushq %r10
callq 0x1130
addq $0x10, %rsp
pushq $0x2
popq %r15
movq 0x42f8(%rip), %rax      # 0x6190
movq (%rax,%rbp,8), %r13
movq %r13, %rdi
callq 0x10b0
movl %r12d, %edi
movq %r13, %rsi
movq %rax, %rdx
callq 0x137c
movl $0x1000, %edx           # imm = 0x1000
movl %r12d, %edi
leaq 0x80(%rsp), %r13
movq %r13, %rsi
pushq $0x5
popq %rcx
callq 0x1559
testl %eax, %eax
jns 0x1f9f
leaq 0x42b4(%rip), %rax      # 0x6190
incl 0x401c(%rax,%rbx,4)
movq 0x410e(%rip), %r13      # 0x5ff8
movq (%r13), %rdi
movq 0x42ab(%rip), %rax      # 0x61a0
movq (%rax,%rbx,8), %rcx
leaq 0x1633(%rip), %rsi      # 0x3533
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
movq (%r13), %rdi
movq 0x4289(%rip), %rax      # 0x61a0
movq (%rax,%rbx,8), %rcx
subl $0x1, %r15d
jb 0x2124
leaq 0x16e9(%rip), %rsi      # 0x3615
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
movq 0x4261(%rip), %rax      # 0x61a0
movq (%rax,%rbx,8), %rdi
movl $0x4e36, %esi           # imm = 0x4E36
movl $0xc8, %edx
callq 0x1b32
movl %eax, %r12d
movq (%r13), %rdi
movq 0x4240(%rip), %rax      # 0x61a0
movq (%rax,%rbx,8), %rcx
cmpl $-0x1, %r12d
je 0x213d
movq 0x421b(%rip), %rax      # 0x6190
movq (%rax,%rbp,8), %r8
movl 0x421c(%rip), %r10d     # 0x619c
leaq 0x16b0(%rip), %rsi      # 0x3637
movl %ebx, %edx
movl %r14d, %r9d
xorl %eax, %eax
pushq %rcx
pushq %r10
callq 0x1130
addq $0x10, %rsp
jmp 0x1e91
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, 0x80(%rsp,%rax)
movq 0x4045(%rip), %r15      # 0x5ff8
movq (%r15), %rdi
movq 0x41e3(%rip), %rax      # 0x61a0
movq (%rax,%rbx,8), %rcx
leaq 0x169e(%rip), %rsi      # 0x3666
movl %ebx, %edx
movq %r13, %r8
xorl %eax, %eax
callq 0x1130
movq %r13, %rdi
leaq 0x16b0(%rip), %rsi      # 0x368e
pushq $0x6
popq %rdx
callq 0x1180
testl %eax, %eax
jne 0x2010
movq 0x419f(%rip), %rax      # 0x6190
movq (%rax,%rbp,8), %rdx
movq (%rsp), %rdi
leaq 0x1212(%rip), %rsi      # 0x3212
xorl %eax, %eax
callq 0x1130
leaq 0x4152(%rip), %r14      # 0x6160
jmp 0x2082
movq %r13, %rdi
leaq 0x167a(%rip), %rsi      # 0x3694
pushq $0x9
popq %rdx
callq 0x1180
movq (%r15), %rdi
movq 0x4174(%rip), %rcx      # 0x61a0
movq (%rcx,%rbx,8), %rcx
movq 0x4159(%rip), %rdx      # 0x6190
movq (%rdx,%rbp,8), %r8
testl %eax, %eax
leaq 0x411c(%rip), %r14      # 0x6160
jne 0x204f
leaq 0x1650(%rip), %rsi      # 0x369d
jmp 0x2056
leaq 0x16a4(%rip), %rsi      # 0x36fa
movl %ebx, %edx
xorl %eax, %eax
callq 0x1130
movslq 0xc546(%rip), %rax      # 0xe5ac
leal 0x1(%rax), %ecx
movl %ecx, 0xc53d(%rip)      # 0xe5ac
leaq 0x411a(%rip), %rcx      # 0x6190
movq 0x8(%rsp), %rdx
movl %edx, 0x441c(%rcx,%rax,4)
movl %r12d, %edi
callq 0x1d2b
jmp 0x1dd2
leaq 0x1405(%rip), %rsi      # 0x349b
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
leaq 0x40b7(%rip), %rdi      # 0x6160
callq 0x1260
movslq 0x80f3(%rip), %rax      # 0xa1a8
leal 0x1(%rax), %ecx
movl %ecx, 0x80ea(%rip)      # 0xa1a8
leaq 0x40cb(%rip), %rcx      # 0x6190
movl %r13d, 0x18(%rcx,%rax,4)
leaq 0x408f(%rip), %rdi      # 0x6160
callq 0x1140
movq (%r15), %r14
movq 0x40c0(%rip), %rax      # 0x61a0
movq (%rax,%rbx,8), %r15
callq 0x21d4
subsd 0x4097(%rip), %xmm0     # 0x6188
divsd 0x134f(%rip), %xmm0     # 0x3448
leaq 0x14d0(%rip), %rsi      # 0x35d0
leaq 0x10(%rsp), %r8
movq %r14, %rdi
movl %ebx, %edx
movq %r15, %rcx
movb $0x1, %al
callq 0x1130
movq (%rsp), %rdi
callq 0x1090
xorl %edi, %edi
callq 0x1150
leaq 0x1449(%rip), %rsi      # 0x3574
pushq $0x3
popq %r9
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
jmp 0x2150
leaq 0x1357(%rip), %rsi      # 0x349b
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
leaq 0x4009(%rip), %r14      # 0x6160
movq %r14, %rdi
callq 0x1260
movslq 0x8042(%rip), %rax      # 0xa1a8
leal 0x1(%rax), %ecx
movl %ecx, 0x8039(%rip)      # 0xa1a8
leaq 0x401a(%rip), %rcx      # 0x6190
movq 0x8(%rsp), %rdx
movl %edx, 0x18(%rcx,%rax,4)
movq %r14, %rdi
callq 0x1140
movq (%rsp), %rdi
callq 0x1090
movq (%r13), %r14
movq 0x4005(%rip), %rax      # 0x61a0
movq (%rax,%rbx,8), %r15
callq 0x21d4
subsd 0x3fdc(%rip), %xmm0     # 0x6188
divsd 0x1294(%rip), %xmm0     # 0x3448
leaq 0x1415(%rip), %rsi      # 0x35d0
leaq 0x10(%rsp), %r8
movq %r14, %rdi
movl %ebx, %edx
movq %r15, %rcx
movb $0x1, %al
callq 0x1130
jmp 0x211d