// File: /workspace/github_c_makefile_star2_O3/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 0x3f99(%rip), %rax      # 0x6188
movl $0x0, 0x401c(%rax,%rbx,4)
leal 0x1(%rbx), %edx
leaq 0x1254(%rip), %rsi      # 0x3458
leaq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x11e0
leaq 0x1248(%rip), %rsi      # 0x3462
movq %r14, %rdi
callq 0x11a0
leaq 0x123b(%rip), %rsi      # 0x3464
leaq 0x1241(%rip), %rdx      # 0x3471
movq %rax, (%rsp)
movq %rax, %rdi
movl $0x5e, %ecx
callq 0x2d72
leaq 0x3f10(%rip), %rdi      # 0x6158
callq 0x1250
movl 0x3f3c(%rip), %r12d     # 0x6190
cmpl 0x3f39(%rip), %r12d     # 0x6194
jge 0x25b8
leaq 0x3ef0(%rip), %rbp      # 0x6158
movq 0x3d89(%rip), %r14      # 0x5ff8
leal 0x1(%r12), %r13d
movl %r13d, 0x3f15(%rip)     # 0x6190
movq %rbp, %rdi
callq 0x1140
movq (%r14), %rdi
movq 0x3f0b(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
leaq 0x11e0(%rip), %rsi      # 0x3478
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
movq 0x3eed(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rdi
movl $0x4e36, %esi           # imm = 0x4E36
movl $0xc8, %edx
callq 0x1fb7
movl %eax, %r15d
movq (%r14), %rdi
movq 0x3ecd(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
cmpl $-0x1, %r15d
je 0x257d
movq 0x3ea8(%rip), %rax      # 0x6188
movslq %r12d, %rbp
movq (%rax,%rbp,8), %r8
movl 0x3ea6(%rip), %r10d     # 0x6194
leaq 0x120e(%rip), %rsi      # 0x3503
movl %ebx, %edx
movl %r13d, %r9d
xorl %eax, %eax
pushq %rcx
pushq %r10
callq 0x1130
addq $0x10, %rsp
movq 0x3e79(%rip), %rax      # 0x6188
movq (%rax,%rbp,8), %r14
movq %r14, %rdi
callq 0x10b0
movl %r15d, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x191c
movl $0x1000, %edx           # imm = 0x1000
movl %r15d, %edi
leaq 0x80(%rsp), %rsi
movl $0x5, %ecx
callq 0x1a8e
testl %eax, %eax
js 0x2353
movq 0x3caa(%rip), %r14      # 0x5ff8
jmp 0x246e
movq %r12, 0x8(%rsp)
movl $0x3, %r12d
movq 0x3c93(%rip), %r14      # 0x5ff8
leaq 0x3e1c(%rip), %rax      # 0x6188
incl 0x401c(%rax,%rbx,4)
movq (%r14), %rdi
movq 0x3e1b(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
leaq 0x11ab(%rip), %rsi      # 0x3533
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
movq (%r14), %rdi
movq 0x3dfa(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
decl %r12d
je 0x2619
leaq 0x1263(%rip), %rsi      # 0x3615
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
movq 0x3dd3(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rdi
movl $0x4e36, %esi           # imm = 0x4E36
movl $0xc8, %edx
callq 0x1fb7
movl %eax, %r15d
movq (%r14), %rdi
movq 0x3db3(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
cmpl $-0x1, %r15d
je 0x267a
movq 0x3d8e(%rip), %rax      # 0x6188
movq (%rax,%rbp,8), %r8
movl 0x3d8f(%rip), %r10d     # 0x6194
leaq 0x122b(%rip), %rsi      # 0x3637
movl %ebx, %edx
movl %r13d, %r9d
xorl %eax, %eax
pushq %rcx
pushq %r10
callq 0x1130
addq $0x10, %rsp
movq 0x3d62(%rip), %rax      # 0x6188
movq (%rax,%rbp,8), %r14
movq %r14, %rdi
callq 0x10b0
movl %r15d, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x191c
movl $0x1000, %edx           # imm = 0x1000
movl %r15d, %edi
leaq 0x80(%rsp), %rsi
movl $0x5, %ecx
callq 0x1a8e
testl %eax, %eax
movq 0x3b95(%rip), %r14      # 0x5ff8
js 0x2365
movq 0x8(%rsp), %r12
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, 0x80(%rsp,%rax)
movq (%r14), %rdi
movq 0x3d13(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
leaq 0x11d6(%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 0x24dd
movq 0x3cc3(%rip), %rax      # 0x6188
movq (%rax,%rbp,8), %rdx
movq (%rsp), %rdi
leaq 0xd3e(%rip), %rsi       # 0x3212
xorl %eax, %eax
callq 0x1130
jmp 0x2550
movq 0x80(%rsp), %rax
movabsq $0x2e64656873617263, %rcx # imm = 0x2E64656873617263
xorq %rcx, %rax
movzbl 0x88(%rsp), %ecx
orq %rax, %rcx
movq (%r14), %rdi
movq 0x3c91(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %rcx
movq 0x3c76(%rip), %rax      # 0x6188
movq (%rax,%rbp,8), %r8
jne 0x2521
leaq 0x117e(%rip), %rsi      # 0x369d
jmp 0x2528
leaq 0x11d2(%rip), %rsi      # 0x36fa
movl %ebx, %edx
xorl %eax, %eax
callq 0x1130
movslq 0xc06c(%rip), %rax      # 0xe5a4
leal 0x1(%rax), %ecx
movl %ecx, 0xc063(%rip)      # 0xe5a4
leaq 0x3c40(%rip), %rcx      # 0x6188
movl %r12d, 0x441c(%rcx,%rax,4)
leaq 0x3c01(%rip), %rbp      # 0x6158
movl %r15d, %edi
callq 0x21a0
movq %rbp, %rdi
callq 0x1250
movl 0x3c22(%rip), %r12d     # 0x6190
cmpl 0x3c1f(%rip), %r12d     # 0x6194
jl 0x226f
jmp 0x25b8
leaq 0xf17(%rip), %rsi       # 0x349b
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
leaq 0x3bc1(%rip), %rdi      # 0x6158
callq 0x1250
movslq 0x7bfd(%rip), %rax      # 0xa1a0
leal 0x1(%rax), %ecx
movl %ecx, 0x7bf4(%rip)      # 0xa1a0
leaq 0x3bd5(%rip), %rcx      # 0x6188
movl %r12d, 0x18(%rcx,%rax,4)
leaq 0x3b99(%rip), %rdi      # 0x6158
callq 0x1140
movq 0x3a2d(%rip), %rax      # 0x5ff8
movq (%rax), %r14
movq 0x3bc3(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %r15
callq 0x2717
subsd 0x3b9a(%rip), %xmm0     # 0x6180
divsd 0xe5a(%rip), %xmm0      # 0x3448
leaq 0xfdb(%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 0xf54(%rip), %rsi       # 0x3574
movl %ebx, %edx
movq %rcx, %r8
movl $0x3, %r9d
xorl %eax, %eax
callq 0x1130
movq %r14, %r15
leaq 0x3b1c(%rip), %r14      # 0x6158
movq %r14, %rdi
callq 0x1250
movslq 0x7b55(%rip), %rax      # 0xa1a0
leal 0x1(%rax), %ecx
movl %ecx, 0x7b4c(%rip)      # 0xa1a0
leaq 0x3b2d(%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 0x26d7
leaq 0xe1a(%rip), %rsi       # 0x349b
movl %ebx, %edx
movq %rcx, %r8
xorl %eax, %eax
callq 0x1130
leaq 0x3ac4(%rip), %r14      # 0x6158
movq %r14, %rdi
callq 0x1250
movslq 0x7afd(%rip), %rax      # 0xa1a0
leal 0x1(%rax), %ecx
movl %ecx, 0x7af4(%rip)      # 0xa1a0
leaq 0x3ad5(%rip), %rcx      # 0x6188
movq 0x8(%rsp), %rdx
movl %edx, 0x18(%rcx,%rax,4)
movq %r14, %rdi
callq 0x1140
movq (%rsp), %rdi
callq 0x1090
movq 0x3924(%rip), %rax      # 0x5ff8
movq (%rax), %r14
movq 0x3aba(%rip), %rax      # 0x6198
movq (%rax,%rbx,8), %r15
callq 0x2717
subsd 0x3a91(%rip), %xmm0     # 0x6180
divsd 0xd51(%rip), %xmm0      # 0x3448
leaq 0xed2(%rip), %rsi       # 0x35d0
leaq 0x10(%rsp), %r8
movq %r14, %rdi
movl %ebx, %edx
movq %r15, %rcx
movb $0x1, %al
callq 0x1130
jmp 0x2612