// File: /workspace/github_c_makefile_star2_O2/hasindu2008[P]f5p/f5pl.c
// Function: main
int main(int argc, char* argv[]) {
    signal(SIGSEGV, sig_handler);

    //argument check check
    if (argc != 3) {
        ERROR("Not enough arguments. Usage %s <ip_list> <file_list>\n",
              argv[0]);
        exit(EXIT_FAILURE);
    }

    realtime0=realtime();

    //read the list of tar files
    char** file_list = (char**)malloc(sizeof(char*) * (MAX_FILES));
    MALLOC_CHK(file_list);
    int32_t file_list_cnt = 0;
    int32_t i = 0;
    char* file_list_name = argv[2];
    FILE* file_list_fp = fopen(file_list_name, "r");
    NULL_CHK(file_list_fp);
    while (1) {
        size_t line_size = MAX_PATH_SIZE;
        char* line =
            malloc(sizeof(char) * (line_size)); //filepath+newline+nullcharacter
        MALLOC_CHK(line);
        int32_t readlinebytes = getline(&line, &line_size, file_list_fp);
        if (readlinebytes == -1) { //file has ended
            free(line);
            break;
        } else if (readlinebytes > MAX_PATH_SIZE) {
            free(line);
            ERROR("The file path %s is longer hard coded limit %d\n", line,
                  MAX_PATH_SIZE);
            exit(EXIT_FAILURE);
        }
        if (file_list_cnt > MAX_FILES) {
            free(line);
            ERROR("The number of entries in %s exceeded the hard coded limit "
                  "%d\n",
                  file_list_name, MAX_FILES);
            exit(EXIT_FAILURE);
        }
        if (line[0] == '#' || line[0] == '\n' ||
            line[0] == '\r') { //comments and emprty lines
            free(line);
            continue;
        }
        if (line[readlinebytes - 1] == '\n' ||
            line[readlinebytes - 1] == '\r') {
            line[readlinebytes - 1] = '\0';
        }
        file_list[file_list_cnt] = line;
        file_list_cnt++;
    }
    fclose(file_list_fp);

    //read the list of nodes
    char** ip_list = (char**)malloc(sizeof(char*) * (MAX_IPS));
    MALLOC_CHK(ip_list);
    char* ip_list_name = argv[1];
    FILE* ip_list_fp = fopen(ip_list_name, "r");
    NULL_CHK(ip_list_fp);
    int32_t ip_cnt = 0;
    while (1) {
        size_t line_size = MAX_IP_LEN;
        char* line =
            malloc(sizeof(char) * (line_size)); //ip+newline+nullcharacter
        MALLOC_CHK(line);
        int32_t readlinebytes = getline(&line, &line_size, ip_list_fp);
        if (readlinebytes == -1) { //file has ended
            free(line);
            break;
        } else if (readlinebytes > MAX_IP_LEN) {
            free(line);
            ERROR("The IP length %s is longer hard coded limit %d\n", line,
                  MAX_IP_LEN);
            exit(EXIT_FAILURE);
        }
        if (ip_cnt > MAX_IPS) {
            free(line);
            ERROR("The number of entries in %s exceeded the hard coded limit "
                  "%d\n",
                  ip_list_name, MAX_IPS);
            exit(EXIT_FAILURE);
        }
        if (line[0] == '#' || line[0] == '\n' ||
            line[0] == '\r') { //comments and emprty lines
            free(line);
            continue;
        }
        if (line[readlinebytes - 1] == '\n' ||
            line[readlinebytes - 1] == '\r') {
            line[readlinebytes - 1] = '\0';
        }
        ip_list[ip_cnt] = line;
        ip_cnt++;
    }
    fclose(ip_list_fp);

    //data structures for threads
    pthread_t node_thread[MAX_IPS] = {0};
    int32_t thread_id[MAX_IPS] = {0};

    core.file_list = file_list;
    core.file_list_cnt = file_list_cnt;
    core.file_list_idx = 0;
    core.ip_list = ip_list;
    core.failed_cnt = 0;

    //create threads for each node
    for (i = 0; i < ip_cnt; i++) {
        thread_id[i] = i;
        int ret = pthread_create(&node_thread[i], NULL, node_handler,
                                 (void*)(&thread_id[i]));
        if (ret != 0) {
            ERROR("Error joining thread %d", i);
            exit(EXIT_FAILURE);
        }
    }

    //joining client side threads
    for (i = 0; i < ip_cnt; i++) {
        int ret = pthread_join(node_thread[i], NULL);
        if (ret != 0) {
            ERROR("Error joining thread %d", i);
            //exit(EXIT_FAILURE);
        }
        if (core.num_hangs[i] > 0) {
            INFO("Node %s disconnected/hanged %d times", core.ip_list[i],
                 core.num_hangs[i]);
        }
    }

    //failed files due to device hangs
    if (core.failed_cnt > 0) {
        FILE* failed_report = fopen("failed.cfg", "w");
        NULL_CHK(failed_report);
        ERROR("List of failures due to consecutively device hangs in %s","failed.cfg");
        fprintf(
            failed_report,
            "# Files that failed due to devices that consecutively hanged.\n");
        for (i = 0; i < core.failed_cnt; i++) {
            int id = core.failed[i];
            //ERROR("%s was skipped due to a device retire", core.file_list[id]);
            fprintf(failed_report, "%s\n", core.file_list[id]);
        }
        fclose(failed_report);
    }

    //failed files due to sefaults or other non 0 exit status (see logs)
    if (core.failed_other_cnt > 0) {
        FILE* failed_report = fopen("failed_other.cfg", "w");
        NULL_CHK(failed_report);
        ERROR("List of failures with non 0 exit stats in %s","failed_other.cfg");
        fprintf(failed_report,
                "# Files that failed with a software crash or exited with non "
                "0 status. Please inspect the device log for more info.\n");
        for (i = 0; i < core.failed_other_cnt; i++) {
            int id = core.failed_other[i];
            //WARNING("%s was skipped due to a software crash or a non 0 exit status. Please see the log for more info.", core.file_list[id]);
            fprintf(failed_report, "%s\n", core.file_list[id]);
        }
        fclose(failed_report);
    }

    INFO("Everything done. Elapsed time: %.3fh",(realtime() - realtime0)/3600);
    //todo : free iplist and filelist

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc28, %rsp            # imm = 0xC28
movq %rsi, %r14
movl %edi, %ebx
leaq -0xa1(%rip), %rsi       # 0x2208
pushq $0xb
popq %rdi
callq 0x1120
cmpl $0x3, %ebx
jne 0x28ec
callq 0x21d4
movsd %xmm0, 0x3ec1(%rip)     # 0x6188
movl $0x8000, %edi           # imm = 0x8000
callq 0x1160
leaq 0x15a1(%rip), %r15      # 0x3879
leaq 0x1192(%rip), %r13      # 0x3471
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
movl $0xf1, %ecx
callq 0x2910
movq %r14, 0x10(%rsp)
movq 0x10(%r14), %rdi
leaq 0x1168(%rip), %rsi      # 0x346f
movq %rdi, 0x8(%rsp)
callq 0x11b0
movq %rax, %rbp
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
movl $0xf6, %ecx
callq 0x29be
xorl %r12d, %r12d
leaq 0x20(%rsp), %rbx
leaq 0x420(%rsp), %r14
movq $0x1000, 0x420(%rsp) # imm = 0x1000
movl $0x1000, %edi           # imm = 0x1000
callq 0x1160
movq %rax, 0x20(%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
movl $0xfb, %ecx
callq 0x2910
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x11e0
cmpl $-0x1, %eax
je 0x23f5
cmpl $0x1001, %eax           # imm = 0x1001
jge 0x2839
movq 0x20(%rsp), %rdi
cmpl $0x1001, %r12d          # imm = 0x1001
jge 0x2862
movzbl (%rdi), %ecx
cmpq $0x23, %rcx
ja 0x23b5
movabsq $0x800002400, %rdx      # imm = 0x800002400
btq %rcx, %rdx
jae 0x23b5
callq 0x1040
jmp 0x2337
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
sarq $0x20, %rax
movzbl (%rdi,%rax), %ecx
cmpl $0xd, %ecx
je 0x23d8
cmpl $0xa, %ecx
jne 0x23e1
movb $0x0, (%rdi,%rax)
movq 0x20(%rsp), %rdi
movslq %r12d, %rax
movq 0x18(%rsp), %rcx
movq %rdi, (%rcx,%rax,8)
incl %r12d
jmp 0x2337
leaq 0x20(%rsp), %r14
movq (%r14), %rdi
callq 0x1040
movq %rbp, %rdi
callq 0x1090
movl $0x800, %edi            # imm = 0x800
callq 0x1160
leaq 0x145e(%rip), %r15      # 0x3879
leaq 0x104f(%rip), %rbp      # 0x3471
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movl $0x11d, %ecx            # imm = 0x11D
callq 0x2910
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x1025(%rip), %rsi      # 0x346f
movq %rdi, 0x10(%rsp)
callq 0x11b0
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
movq %rbp, %r13
movq %rbp, %rdx
movl $0x120, %ecx            # imm = 0x120
callq 0x29be
xorl %ebp, %ebp
leaq 0x420(%rsp), %r15
movq $0x100, 0x420(%rsp) # imm = 0x100
movl $0x100, %edi            # imm = 0x100
callq 0x1160
movq %rax, 0x20(%rsp)
movq %rax, %rdi
leaq 0x13dd(%rip), %rsi      # 0x3879
movq %r13, %rdx
movl $0x126, %ecx            # imm = 0x126
callq 0x2910
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x11e0
cmpl $-0x1, %eax
je 0x253a
cmpl $0x101, %eax            # imm = 0x101
jge 0x288c
movq 0x20(%rsp), %rdi
cmpl $0x101, %ebp            # imm = 0x101
jge 0x28b5
movzbl (%rdi), %ecx
cmpq $0x23, %rcx
ja 0x24fb
movabsq $0x800002400, %rdx      # imm = 0x800002400
btq %rcx, %rdx
jae 0x24fb
callq 0x1040
jmp 0x2477
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
sarq $0x20, %rax
movzbl (%rdi,%rax), %ecx
cmpl $0xd, %ecx
je 0x251e
cmpl $0xa, %ecx
jne 0x2527
movb $0x0, (%rdi,%rax)
movq 0x20(%rsp), %rdi
movslq %ebp, %rax
movq 0x8(%rsp), %rcx
movq %rdi, (%rcx,%rax,8)
incl %ebp
jmp 0x2477
movq 0x20(%rsp), %rdi
callq 0x1040
movq %rbx, %rdi
callq 0x1090
xorl %ebx, %ebx
movl $0x800, %edx            # imm = 0x800
movq %r15, %rdi
xorl %esi, %esi
callq 0x10f0
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
xorl %esi, %esi
callq 0x10f0
movq 0x18(%rsp), %rax
movq %rax, 0x3c18(%rip)      # 0x6190
movl %r12d, 0x3c1d(%rip)     # 0x619c
andl $0x0, 0x3c12(%rip)      # 0x6198
movq 0x8(%rsp), %rax
movq %rax, 0x3c0e(%rip)      # 0x61a0
andl $0x0, 0x7c0f(%rip)      # 0xa1a8
testl %ebp, %ebp
cmovlel %ebx, %ebp
leaq -0x849(%rip), %r12      # 0x1d5c
cmpq %rbx, %rbp
je 0x25f4
movl %ebx, (%r14)
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
callq 0x11a0
incq %rbx
addq $0x8, %r15
addq $0x4, %r14
testl %eax, %eax
je 0x25a5
movq 0x3a25(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
decl %ebx
leaq 0x1385(%rip), %rsi      # 0x3964
leaq 0x1293(%rip), %rdx      # 0x3879
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1130
jmp 0x28e4
leaq 0x3b95(%rip), %r13      # 0x6190
leaq 0x1392(%rip), %rbx      # 0x3994
leaq 0x1270(%rip), %r14      # 0x3879
leaq 0x1354(%rip), %r15      # 0x3964
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0x267d
movq 0x420(%rsp,%r12,8), %rdi
xorl %esi, %esi
callq 0x1230
testl %eax, %eax
jne 0x263d
movl 0x401c(%r13,%r12,4), %r8d
testl %r8d, %r8d
jg 0x2659
incq %r12
jmp 0x2613
movq 0x39b4(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq %r15, %rsi
movq %r14, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1130
jmp 0x262b
movq 0x3998(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq 0x3b36(%rip), %rax      # 0x61a0
movq (%rax,%r12,8), %rcx
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1130
jmp 0x2638
cmpl $0x0, 0x7b24(%rip)      # 0xa1a8
jle 0x2734
leaq 0x133f(%rip), %r14      # 0x39d0
leaq 0xdca(%rip), %rsi       # 0x3462
movq %r14, %rdi
callq 0x11b0
movq %rax, %rbx
leaq 0x11cf(%rip), %r15      # 0x3879
leaq 0xdc0(%rip), %rdx       # 0x3471
movq %rax, %rdi
movq %r15, %rsi
movl $0x16b, %ecx            # imm = 0x16B
callq 0x29be
movq 0x3930(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1309(%rip), %rsi      # 0x39db
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x1130
leaq 0x1346(%rip), %rdi      # 0x3a2c
pushq $0x3e
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x1220
leaq 0xb17(%rip), %r14       # 0x3212
xorl %r15d, %r15d
movslq 0x7aa3(%rip), %rax      # 0xa1a8
cmpq %rax, %r15
jge 0x272c
movslq 0x18(%r13,%r15,4), %rax
movq 0x3a7a(%rip), %rcx      # 0x6190
movq (%rcx,%rax,8), %rdx
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1130
incq %r15
jmp 0x26fe
movq %rbx, %rdi
callq 0x1090
cmpl $0x0, 0xbe71(%rip)      # 0xe5ac
jle 0x27ee
leaq 0x1323(%rip), %r14      # 0x3a6b
leaq 0xd13(%rip), %rsi       # 0x3462
movq %r14, %rdi
callq 0x11b0
movq %rax, %rbx
leaq 0x1118(%rip), %r15      # 0x3879
leaq 0xd09(%rip), %rdx       # 0x3471
movq %rax, %rdi
movq %r15, %rsi
movl $0x17b, %ecx            # imm = 0x17B
callq 0x29be
movq 0x3879(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x12f3(%rip), %rsi      # 0x3a7c
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x1130
leaq 0x1324(%rip), %rdi      # 0x3ac1
pushq $0x74
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x1220
leaq 0xa60(%rip), %r14       # 0x3212
xorl %r15d, %r15d
movslq 0xbdf0(%rip), %rax      # 0xe5ac
cmpq %rax, %r15
jge 0x27e6
movslq 0x441c(%r13,%r15,4), %rax
movq 0x39c0(%rip), %rcx      # 0x6190
movq (%rcx,%rax,8), %rdx
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1130
incq %r15
jmp 0x27b5
movq %rbx, %rdi
callq 0x1090
movq 0x3803(%rip), %rax      # 0x5ff8
movq (%rax), %rbx
callq 0x21d4
subsd 0x3983(%rip), %xmm0     # 0x6188
divsd 0xc3b(%rip), %xmm0      # 0x3448
leaq 0x1322(%rip), %rsi      # 0x3b36
leaq 0x105e(%rip), %rdx      # 0x3879
movq %rbx, %rdi
movb $0x1, %al
callq 0x1130
xorl %eax, %eax
addq $0xc28, %rsp            # imm = 0xC28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %rdi
callq 0x1040
movq 0x37ae(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq 0x20(%rsp), %rcx
leaq 0x1025(%rip), %rsi      # 0x387e
leaq 0x1019(%rip), %rdx      # 0x3879
jmp 0x2884
callq 0x1040
movq 0x378a(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x104e(%rip), %rsi      # 0x38c6
leaq 0xffa(%rip), %rdx       # 0x3879
movq 0x8(%rsp), %rcx
movl $0x1000, %r8d           # imm = 0x1000
jmp 0x28dd
movq 0x20(%rsp), %rdi
callq 0x1040
movq 0x375b(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq 0x20(%rsp), %rcx
leaq 0x1070(%rip), %rsi      # 0x391c
leaq 0xfc6(%rip), %rdx       # 0x3879
jmp 0x28d7
callq 0x1040
movq 0x3737(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0xffb(%rip), %rsi       # 0x38c6
leaq 0xfa7(%rip), %rdx       # 0x3879
movq 0x10(%rsp), %rcx
movl $0x100, %r8d            # imm = 0x100
xorl %eax, %eax
callq 0x1130
pushq $0x1
popq %rdi
callq 0x1200
movq 0x3705(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq (%r14), %rcx
leaq 0xf2b(%rip), %rsi       # 0x382b
leaq 0xf72(%rip), %rdx       # 0x3879
xorl %eax, %eax
callq 0x1130
jmp 0x28e4