// File: /workspace/github_c_makefile_star2_O1/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 0x3f91(%rip), %rax      # 0x6188
movl $0x0, 0x401c(%rax,%rbx,4)
leal 0x1(%rbx), %edx
leaq 0x124c(%rip), %rsi      # 0x3458
leaq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x11e0
leaq 0x1240(%rip), %rsi      # 0x3462
movq %r14, %rdi
callq 0x11a0
leaq 0x1233(%rip), %rsi      # 0x3464
leaq 0x1239(%rip), %rdx      # 0x3471
movq %rax, (%rsp)
movq %rax, %rdi
movl $0x5e, %ecx
callq 0x2ce6
leaq 0x3f08(%rip), %rdi      # 0x6158
callq 0x1250
movl 0x3f34(%rip), %r12d     # 0x6190
cmpl 0x3f31(%rip), %r12d     # 0x6194
jge 0x25c0
leaq 0x3ee8(%rip), %rbp      # 0x6158
movq 0x3d81(%rip), %r14      # 0x5ff8
leal 0x1(%r12), %r13d
movl %r13d, 0x3f0d(%rip)     # 0x6190
movq %rbp, %rdi
callq 0x1140
movq (%r14), %rdi
movq 0x3f03(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
leaq 0x11d8(%rip), %rsi      # 0x3478
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
movq 0x3ee5(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rdi
movl $0x4e36, %esi           # imm = 0x4E36
movl $0xc8, %edx
callq 0x1fc0
movl %eax, %r15d
movq (%r14), %rdi
movq 0x3ec5(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
cmpl $-0x1, %r15d
je 0x2585
movq 0x3ea0(%rip), %rax      # 0x6188
movslq %r12d, %rbp
movq (%rax,%rbp,8), %r8
movl 0x3e9e(%rip), %r10d     # 0x6194
leaq 0x1206(%rip), %rsi      # 0x3503
movl %ebx, %edx
movl %r13d, %r9d
xorl %eax, %eax
pushq %rcx
pushq %r10
callq 0x1130
addq $0x10, %rsp
movq 0x3e71(%rip), %rax      # 0x6188
movq (%rax,%rbp,8), %r14
movq %r14, %rdi
callq 0x10b0
movl %r15d, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x194c
movl $0x1000, %edx           # imm = 0x1000
movl %r15d, %edi
leaq 0x80(%rsp), %rsi
movl $0x5, %ecx
callq 0x1abb
testl %eax, %eax
js 0x235b
movq 0x3ca2(%rip), %r14      # 0x5ff8
jmp 0x2476
movq %r12, 0x8(%rsp)
movl $0x3, %r12d
movq 0x3c8b(%rip), %r14      # 0x5ff8
leaq 0x3e14(%rip), %rax      # 0x6188
incl 0x401c(%rax,%rbx,4)
movq (%r14), %rdi
movq 0x3e13(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
leaq 0x11a3(%rip), %rsi      # 0x3533
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
movq (%r14), %rdi
movq 0x3df2(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
decl %r12d
je 0x2621
leaq 0x125b(%rip), %rsi      # 0x3615
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
movq 0x3dcb(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rdi
movl $0x4e36, %esi           # imm = 0x4E36
movl $0xc8, %edx
callq 0x1fc0
movl %eax, %r15d
movq (%r14), %rdi
movq 0x3dab(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
cmpl $-0x1, %r15d
je 0x2682
movq 0x3d86(%rip), %rax      # 0x6188
movq (%rax,%rbp,8), %r8
movl 0x3d87(%rip), %r10d     # 0x6194
leaq 0x1223(%rip), %rsi      # 0x3637
movl %ebx, %edx
movl %r13d, %r9d
xorl %eax, %eax
pushq %rcx
pushq %r10
callq 0x1130
addq $0x10, %rsp
movq 0x3d5a(%rip), %rax      # 0x6188
movq (%rax,%rbp,8), %r14
movq %r14, %rdi
callq 0x10b0
movl %r15d, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x194c
movl $0x1000, %edx           # imm = 0x1000
movl %r15d, %edi
leaq 0x80(%rsp), %rsi
movl $0x5, %ecx
callq 0x1abb
testl %eax, %eax
movq 0x3b8d(%rip), %r14      # 0x5ff8
js 0x236d
movq 0x8(%rsp), %r12
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, 0x80(%rsp,%rax)
movq (%r14), %rdi
movq 0x3d0b(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
leaq 0x11ce(%rip), %rsi      # 0x3666
movl %ebx, %edx
leaq 0x80(%rsp), %r8
xorl %eax, %eax
callq 0x1130
movl 0x80(%rsp), %eax
movl $0x656e6f64, %ecx       # imm = 0x656E6F64
xorl %ecx, %eax
movzwl 0x84(%rsp), %ecx
xorl $0x2e, %ecx
orl %eax, %ecx
jne 0x24e5
movq 0x3cbb(%rip), %rax      # 0x6188
movq (%rax,%rbp,8), %rdx
movq (%rsp), %rdi
leaq 0xd36(%rip), %rsi       # 0x3212
xorl %eax, %eax
callq 0x1130
jmp 0x2558
movq 0x80(%rsp), %rax
movabsq $0x2e64656873617263, %rcx # imm = 0x2E64656873617263
xorq %rcx, %rax
movzbl 0x88(%rsp), %ecx
orq %rax, %rcx
movq (%r14), %rdi
movq 0x3c89(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
movq 0x3c6e(%rip), %rax      # 0x6188
movq (%rax,%rbp,8), %r8
jne 0x2529
leaq 0x1176(%rip), %rsi      # 0x369d
jmp 0x2530
leaq 0x11ca(%rip), %rsi      # 0x36fa
movl %ebx, %edx
xorl %eax, %eax
callq 0x1130
movslq 0xc064(%rip), %rax      # 0xe5a4
leal 0x1(%rax), %ecx
movl %ecx, 0xc05b(%rip)      # 0xe5a4
leaq 0x3c38(%rip), %rcx      # 0x6188
movl %r12d, 0x441c(%rcx,%rax,4)
leaq 0x3bf9(%rip), %rbp      # 0x6158
movl %r15d, %edi
callq 0x21a9
movq %rbp, %rdi
callq 0x1250
movl 0x3c1a(%rip), %r12d     # 0x6190
cmpl 0x3c17(%rip), %r12d     # 0x6194
jl 0x2277
jmp 0x25c0
leaq 0xf0f(%rip), %rsi       # 0x349b
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
leaq 0x3bb9(%rip), %rdi      # 0x6158
callq 0x1250
movslq 0x7bf5(%rip), %rax      # 0xa1a0
leal 0x1(%rax), %ecx
movl %ecx, 0x7bec(%rip)      # 0xa1a0
leaq 0x3bcd(%rip), %rcx      # 0x6188
movl %r12d, 0x18(%rcx,%rax,4)
leaq 0x3b91(%rip), %rdi      # 0x6158
callq 0x1140
movq 0x3a25(%rip), %rax      # 0x5ff8
movq (%rax), %r14
movq 0x3bbb(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %r15
callq 0x271f
subsd 0x3b92(%rip), %xmm0     # 0x6180
divsd 0xe52(%rip), %xmm0      # 0x3448
leaq 0xfd3(%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 0xf4c(%rip), %rsi       # 0x3574
movl %ebx, %edx
movq %rcx, %r8
movl $0x3, %r9d
xorl %eax, %eax
callq 0x1130
movq %r14, %r15
leaq 0x3b14(%rip), %r14      # 0x6158
movq %r14, %rdi
callq 0x1250
movslq 0x7b4d(%rip), %rax      # 0xa1a0
leal 0x1(%rax), %ecx
movl %ecx, 0x7b44(%rip)      # 0xa1a0
leaq 0x3b25(%rip), %rcx      # 0x6188
movq 0x8(%rsp), %rdx
movl %edx, 0x18(%rcx,%rax,4)
movq %r14, %rdi
callq 0x1140
movq (%rsp), %rdi
callq 0x1090
movq (%r15), %r14
jmp 0x26df
leaq 0xe12(%rip), %rsi       # 0x349b
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
leaq 0x3abc(%rip), %r14      # 0x6158
movq %r14, %rdi
callq 0x1250
movslq 0x7af5(%rip), %rax      # 0xa1a0
leal 0x1(%rax), %ecx
movl %ecx, 0x7aec(%rip)      # 0xa1a0
leaq 0x3acd(%rip), %rcx      # 0x6188
movq 0x8(%rsp), %rdx
movl %edx, 0x18(%rcx,%rax,4)
movq %r14, %rdi
callq 0x1140
movq (%rsp), %rdi
callq 0x1090
movq 0x391c(%rip), %rax      # 0x5ff8
movq (%rax), %r14
movq 0x3ab2(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %r15
callq 0x271f
subsd 0x3a89(%rip), %xmm0     # 0x6180
divsd 0xd49(%rip), %xmm0      # 0x3448
leaq 0xeca(%rip), %rsi       # 0x35d0
leaq 0x10(%rsp), %r8
movq %r14, %rdi
movl %ebx, %edx
movq %r15, %rcx
movb $0x1, %al
callq 0x1130
jmp 0x261a