// File: /workspace/github_c_makefile_star2_O3/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 $0xc38, %rsp            # imm = 0xC38
movq %rsi, %r12
movl %edi, %ebx
leaq -0xab(%rip), %rsi       # 0x274b
movl $0xb, %edi
callq 0x1120
cmpl $0x3, %ebx
jne 0x2d42
leaq 0x430(%rsp), %r14
leaq 0x30(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10e0
cvtsi2sdq (%r14), %xmm0
cvtsi2sdq 0x8(%r14), %xmm1
mulsd 0xc1c(%rip), %xmm1      # 0x3450
addsd %xmm0, %xmm1
movsd %xmm1, 0x3940(%rip)     # 0x6180
movl $0x8000, %edi           # imm = 0x8000
callq 0x1160
leaq 0x1028(%rip), %r15      # 0x3879
leaq 0xc19(%rip), %r13       # 0x3471
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
movl $0xf1, %ecx
callq 0x2d4c
movq %r12, 0x8(%rsp)
movq 0x10(%r12), %rdi
leaq 0xbee(%rip), %rsi       # 0x346f
movq %rdi, (%rsp)
callq 0x11a0
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
movl $0xf6, %ecx
callq 0x2d72
movq $0x1000, (%r14)         # imm = 0x1000
movl $0x1000, %edi           # imm = 0x1000
callq 0x1160
movq %rax, (%rbx)
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
movl $0xfb, %ecx
callq 0x2d4c
leaq 0x30(%rsp), %rdi
leaq 0x430(%rsp), %rsi
movq %r12, %rdx
callq 0x11d0
xorl %r13d, %r13d
movl $0x0, %ebp
cmpl $-0x1, %eax
je 0x29c2
xorl %ebp, %ebp
leaq 0xb7b(%rip), %rbx       # 0x3471
leaq 0x30(%rsp), %r15
leaq 0x430(%rsp), %r14
cmpl $0x1001, %eax           # imm = 0x1001
jge 0x2d13
movq 0x30(%rsp), %rdi
cmpl $0x1001, %ebp           # imm = 0x1001
jge 0x2d1d
movzbl (%rdi), %ecx
cmpq $0x23, %rcx
ja 0x293f
movabsq $0x800002400, %rdx      # imm = 0x800002400
btq %rcx, %rdx
jae 0x293f
callq 0x1040
jmp 0x2979
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
sarq $0x20, %rax
movzbl (%rdi,%rax), %ecx
cmpl $0xd, %ecx
je 0x2962
cmpl $0xa, %ecx
jne 0x296b
movb $0x0, (%rdi,%rax)
movq 0x30(%rsp), %rdi
movslq %ebp, %rax
movq 0x10(%rsp), %rcx
movq %rdi, (%rcx,%rax,8)
incl %ebp
movq $0x1000, 0x430(%rsp) # imm = 0x1000
movl $0x1000, %edi           # imm = 0x1000
callq 0x1160
movq %rax, 0x30(%rsp)
movq %rax, %rdi
leaq 0xedb(%rip), %rsi       # 0x3879
movq %rbx, %rdx
movl $0xfb, %ecx
callq 0x2d4c
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x11d0
cmpl $-0x1, %eax
jne 0x2903
leaq 0x30(%rsp), %rax
movq (%rax), %rdi
callq 0x1040
movq %r12, %rdi
callq 0x1090
movl $0x800, %edi            # imm = 0x800
callq 0x1160
leaq 0xe91(%rip), %r14       # 0x3879
leaq 0xa82(%rip), %r15       # 0x3471
movq %rax, (%rsp)
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl $0x11d, %ecx            # imm = 0x11D
callq 0x2d4c
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0xa59(%rip), %rsi       # 0x346f
movq %rdi, 0x8(%rsp)
callq 0x11a0
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl $0x120, %ecx            # imm = 0x120
callq 0x2d72
leaq 0x430(%rsp), %rbx
movq $0x100, (%rbx)          # imm = 0x100
movl $0x100, %edi            # imm = 0x100
callq 0x1160
leaq 0x30(%rsp), %rcx
movq %rax, (%rcx)
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl $0x126, %ecx            # imm = 0x126
callq 0x2d4c
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x11d0
cmpl $-0x1, %eax
je 0x2b5a
xorl %r13d, %r13d
leaq 0x9e4(%rip), %r15       # 0x3471
leaq 0x30(%rsp), %r14
leaq 0x430(%rsp), %rbx
cmpl $0x101, %eax            # imm = 0x101
jge 0x2d26
movq 0x30(%rsp), %rdi
cmpl $0x101, %r13d           # imm = 0x101
jge 0x2d30
movzbl (%rdi), %ecx
cmpq $0x23, %rcx
ja 0x2ad7
movabsq $0x800002400, %rdx      # imm = 0x800002400
btq %rcx, %rdx
jae 0x2ad7
callq 0x1040
jmp 0x2b11
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
sarq $0x20, %rax
movzbl (%rdi,%rax), %ecx
cmpl $0xd, %ecx
je 0x2afa
cmpl $0xa, %ecx
jne 0x2b03
movb $0x0, (%rdi,%rax)
movq 0x30(%rsp), %rdi
movslq %r13d, %rax
movq (%rsp), %rcx
movq %rdi, (%rcx,%rax,8)
incl %r13d
movq $0x100, 0x430(%rsp) # imm = 0x100
movl $0x100, %edi            # imm = 0x100
callq 0x1160
movq %rax, 0x30(%rsp)
movq %rax, %rdi
leaq 0xd43(%rip), %rsi       # 0x3879
movq %r15, %rdx
movl $0x126, %ecx            # imm = 0x126
callq 0x2d4c
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x11d0
cmpl $-0x1, %eax
jne 0x2a9a
leaq 0x30(%rsp), %rbx
movq (%rbx), %rdi
callq 0x1040
movq %r12, %rdi
callq 0x1090
leaq 0x430(%rsp), %r14
xorl %r15d, %r15d
movl $0x800, %edx            # imm = 0x800
movq %r14, %rdi
xorl %esi, %esi
callq 0x10f0
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10f0
movq 0x10(%rsp), %rax
movq %rax, 0x35e4(%rip)      # 0x6188
movl %ebp, 0x35ea(%rip)      # 0x6194
movl %r15d, 0x35df(%rip)     # 0x6190
movq (%rsp), %rax
movq %rax, 0x35dc(%rip)      # 0x6198
movl %r15d, 0x75dd(%rip)     # 0xa1a0
testl %r13d, %r13d
jle 0x2c96
movl %r13d, %ebp
xorl %r15d, %r15d
leaq -0xa05(%rip), %r12      # 0x21d4
movl %r15d, (%rbx)
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x1190
testl %eax, %eax
jne 0x2d3a
incq %r15
addq $0x8, %r14
addq $0x4, %rbx
cmpq %r15, %rbp
jne 0x2bd9
testl %r13d, %r13d
jle 0x2c88
leaq 0x3578(%rip), %r13      # 0x6188
movq 0x33e1(%rip), %rbx      # 0x5ff8
leaq 0xc5b(%rip), %r14       # 0x3879
leaq 0xd3f(%rip), %r15       # 0x3964
xorl %r12d, %r12d
movq 0x430(%rsp,%r12,8), %rdi
xorl %esi, %esi
callq 0x1220
testl %eax, %eax
jne 0x2c52
movl 0x401c(%r13,%r12,4), %r8d
testl %r8d, %r8d
jg 0x2c67
incq %r12
cmpq %r12, %rbp
jne 0x2c28
jmp 0x2c88
movq (%rbx), %rdi
movq %r15, %rsi
movq %r14, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1130
jmp 0x2c3b
movq (%rbx), %rdi
movq 0x3527(%rip), %rax      # 0x6198
movq (%rax,%r12,8), %rcx
leaq 0xd18(%rip), %rsi       # 0x3994
movq %r14, %rdx
xorl %eax, %eax
callq 0x1130
jmp 0x2c48
cmpl $0x0, 0x7511(%rip)      # 0xa1a0
jle 0x2c96
callq 0x1553
cmpl $0x0, 0xb907(%rip)      # 0xe5a4
jle 0x2ca4
callq 0x161f
movq 0x334d(%rip), %rax      # 0x5ff8
movq (%rax), %rbx
leaq 0x20(%rsp), %r14
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x10e0
cvtsi2sdq (%r14), %xmm1
cvtsi2sdq 0x8(%r14), %xmm0
mulsd 0x77d(%rip), %xmm0      # 0x3450
addsd %xmm1, %xmm0
subsd 0x34a1(%rip), %xmm0     # 0x6180
divsd 0x761(%rip), %xmm0      # 0x3448
leaq 0xe48(%rip), %rsi       # 0x3b36
leaq 0xb84(%rip), %rdx       # 0x3879
movq %rbx, %rdi
movb $0x1, %al
callq 0x1130
xorl %eax, %eax
addq $0xc38, %rsp            # imm = 0xC38
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%rsp), %rdi
callq 0x1478
movq (%rsp), %rsi
callq 0x143f
leaq 0x30(%rsp), %rdi
callq 0x14ed
movq 0x8(%rsp), %rsi
callq 0x14b4
movl %r15d, %edi
callq 0x1529
movq %r12, %rdi
callq 0x1411
nop