// File: /workspace/github_c_makefile_star2_O1/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 $0xc48, %rsp            # imm = 0xC48
movq %rsi, %r14
movl %edi, %ebx
leaq -0xab(%rip), %rsi       # 0x2753
movl $0xb, %edi
callq 0x1120
cmpl $0x3, %ebx
jne 0x2cb6
leaq 0x440(%rsp), %rbx
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
callq 0x10e0
cvtsi2sdq (%rbx), %xmm0
movq %rbx, %r15
cvtsi2sdq 0x8(%rbx), %xmm1
mulsd 0xc14(%rip), %xmm1      # 0x3450
addsd %xmm0, %xmm1
movsd %xmm1, 0x3938(%rip)     # 0x6180
movl $0x8000, %edi           # imm = 0x8000
callq 0x1160
leaq 0x1020(%rip), %r13      # 0x3879
leaq 0xc11(%rip), %rbp       # 0x3471
movq %rax, 0x20(%rsp)
movq %rax, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movl $0xf1, %ecx
callq 0x2cc0
movq %r14, 0x10(%rsp)
movq 0x10(%r14), %rdi
leaq 0xbe7(%rip), %rsi       # 0x346f
movq %rdi, 0x8(%rsp)
callq 0x11a0
movq %rax, %r14
movq %rax, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movl $0xf6, %ecx
callq 0x2ce6
xorl %r12d, %r12d
movq $0x1000, 0x440(%rsp) # imm = 0x1000
movl $0x1000, %edi           # imm = 0x1000
callq 0x1160
movq %rax, 0x40(%rsp)
movq %rax, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movl $0xfb, %ecx
callq 0x2cc0
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x11d0
movq %rax, %rbx
cmpl $-0x1, %ebx
je 0x2968
cmpl $0x1001, %ebx           # imm = 0x1001
jge 0x2c90
movq 0x40(%rsp), %rdi
cmpl $0x1001, %r12d          # imm = 0x1001
jge 0x2c86
movzbl (%rdi), %eax
cmpq $0x23, %rax
ja 0x2928
movabsq $0x800002400, %rcx      # imm = 0x800002400
btq %rax, %rcx
jb 0x296d
movq %rbx, %rax
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
sarq $0x20, %rax
movzbl (%rdi,%rax), %ecx
cmpl $0xd, %ecx
je 0x294e
cmpl $0xa, %ecx
jne 0x2952
movb $0x0, (%rdi,%rax)
movq 0x40(%rsp), %rax
movslq %r12d, %rcx
movq 0x20(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
incl %r12d
jmp 0x2972
movq 0x40(%rsp), %rdi
callq 0x1040
cmpl $-0x1, %ebx
jne 0x28ab
movl %r12d, 0x1c(%rsp)
movq %r14, %rdi
callq 0x1090
movl $0x800, %edi            # imm = 0x800
callq 0x1160
leaq 0xee0(%rip), %rbx       # 0x3879
leaq 0xad1(%rip), %r14       # 0x3471
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl $0x11d, %ecx            # imm = 0x11D
callq 0x2cc0
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0xaa7(%rip), %rsi       # 0x346f
movq %rdi, 0x10(%rsp)
callq 0x11a0
movq %rax, %rbp
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %r12
movq %r14, %rdx
movl $0x120, %ecx            # imm = 0x120
callq 0x2ce6
xorl %r13d, %r13d
leaq 0x40(%rsp), %r14
leaq 0x440(%rsp), %rbx
movq $0x100, 0x440(%rsp) # imm = 0x100
movl $0x100, %edi            # imm = 0x100
callq 0x1160
movq %rax, 0x40(%rsp)
movq %rax, %rdi
leaq 0xe59(%rip), %rsi       # 0x3879
movq %r12, %rdx
movl $0x126, %ecx            # imm = 0x126
callq 0x2cc0
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x11d0
movq %rax, %r15
cmpl $-0x1, %r15d
je 0x2abc
cmpl $0x101, %r15d           # imm = 0x101
jge 0x2ca4
movq 0x40(%rsp), %rdi
cmpl $0x101, %r13d           # imm = 0x101
jge 0x2c9a
movzbl (%rdi), %eax
cmpq $0x23, %rax
ja 0x2a7c
movabsq $0x800002400, %rcx      # imm = 0x800002400
btq %rax, %rcx
jb 0x2ac1
movq %r15, %rax
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
sarq $0x20, %rax
movzbl (%rdi,%rax), %ecx
cmpl $0xd, %ecx
je 0x2aa2
cmpl $0xa, %ecx
jne 0x2aa6
movb $0x0, (%rdi,%rax)
movq 0x40(%rsp), %rax
movslq %r13d, %rcx
movq 0x8(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
incl %r13d
jmp 0x2ac6
movq 0x40(%rsp), %rdi
callq 0x1040
cmpl $-0x1, %r15d
jne 0x29fb
movq %rbp, %rdi
callq 0x1090
leaq 0x440(%rsp), %rbx
xorl %ebp, %ebp
movl $0x800, %edx            # imm = 0x800
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10f0
leaq 0x40(%rsp), %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
xorl %esi, %esi
callq 0x10f0
movq 0x20(%rsp), %rax
movq %rax, 0x3677(%rip)      # 0x6188
movl 0x1c(%rsp), %eax
movl %eax, 0x3679(%rip)      # 0x6194
movl %ebp, 0x366f(%rip)      # 0x6190
movq 0x8(%rsp), %rax
movq %rax, 0x366b(%rip)      # 0x6198
movl %ebp, 0x766d(%rip)      # 0xa1a0
testl %r13d, %r13d
jle 0x2b70
movl %r13d, %ebp
xorl %r15d, %r15d
leaq -0x969(%rip), %r12      # 0x21dc
movl %r15d, (%r14)
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
callq 0x1190
testl %eax, %eax
jne 0x2cae
incq %r15
addq $0x8, %rbx
addq $0x4, %r14
cmpq %r15, %rbp
jne 0x2b45
testl %r13d, %r13d
jle 0x2bfb
movl %r13d, %r13d
leaq 0x3605(%rip), %rbp      # 0x6188
movq 0x346e(%rip), %rbx      # 0x5ff8
leaq 0xce8(%rip), %r14       # 0x3879
leaq 0xdcc(%rip), %r15       # 0x3964
xorl %r12d, %r12d
movq 0x440(%rsp,%r12,8), %rdi
xorl %esi, %esi
callq 0x1220
testl %eax, %eax
jne 0x2bc5
movl 0x401c(%rbp,%r12,4), %r8d
testl %r8d, %r8d
jg 0x2bda
incq %r12
cmpq %r12, %r13
jne 0x2b9b
jmp 0x2bfb
movq (%rbx), %rdi
movq %r15, %rsi
movq %r14, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1130
jmp 0x2bae
movq (%rbx), %rdi
movq 0x35b4(%rip), %rax      # 0x6198
movq (%rax,%r12,8), %rcx
leaq 0xda5(%rip), %rsi       # 0x3994
movq %r14, %rdx
xorl %eax, %eax
callq 0x1130
jmp 0x2bbb
cmpl $0x0, 0x759e(%rip)      # 0xa1a0
jle 0x2c09
callq 0x157f
cmpl $0x0, 0xb994(%rip)      # 0xe5a4
jle 0x2c17
callq 0x164b
movq 0x33da(%rip), %rax      # 0x5ff8
movq (%rax), %rbx
leaq 0x30(%rsp), %r14
leaq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x10e0
cvtsi2sdq (%r14), %xmm1
cvtsi2sdq 0x8(%r14), %xmm0
mulsd 0x80a(%rip), %xmm0      # 0x3450
addsd %xmm1, %xmm0
subsd 0x352e(%rip), %xmm0     # 0x6180
divsd 0x7ee(%rip), %xmm0      # 0x3448
leaq 0xed5(%rip), %rsi       # 0x3b36
leaq 0xc11(%rip), %rdx       # 0x3879
movq %rbx, %rdi
movb $0x1, %al
callq 0x1130
xorl %eax, %eax
addq $0xc48, %rsp            # imm = 0xC48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rsi
callq 0x146a
leaq 0x40(%rsp), %rdi
callq 0x14a3
movq 0x10(%rsp), %rsi
callq 0x14df
leaq 0x40(%rsp), %rdi
callq 0x1518
movq %r15, %rdi
callq 0x1554
movq %r14, %rdi
callq 0x143c
nop