// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xcb0, %rsp            # imm = 0xCB0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0xb, %edi
leaq -0xe5(%rip), %rsi       # 0x2850
callq 0x1150
cmpl $0x3, -0x8(%rbp)
je 0x2970
movq 0x46b1(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
leaq 0x1ed3(%rip), %rsi      # 0x482b
leaq 0x1f1a(%rip), %rdx      # 0x4879
movb $0x0, %al
callq 0x1160
movl $0x1, %edi
callq 0x1220
callq 0x2810
movsd %xmm0, 0x4813(%rip)     # 0x7190
movl $0x8000, %edi           # imm = 0x8000
callq 0x1190
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x1ee3(%rip), %rsi      # 0x4879
leaq 0x1ad4(%rip), %rdx      # 0x4471
movl $0xf1, %ecx
callq 0x3140
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
leaq 0x1aa3(%rip), %rsi      # 0x446f
callq 0x11d0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x1e99(%rip), %rsi      # 0x4879
leaq 0x1a8a(%rip), %rdx      # 0x4471
movl $0xf6, %ecx
callq 0x3250
movq $0x1000, -0x38(%rbp)    # imm = 0x1000
movq -0x38(%rbp), %rdi
shlq $0x0, %rdi
callq 0x1190
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x1e64(%rip), %rsi      # 0x4879
leaq 0x1a55(%rip), %rdx      # 0x4471
movl $0xfb, %ecx
callq 0x3140
movq -0x30(%rbp), %rdx
leaq -0x40(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1200
movl %eax, -0x44(%rbp)
cmpl $-0x1, -0x44(%rbp)
jne 0x2a4e
movq -0x40(%rbp), %rdi
callq 0x1040
jmp 0x2b67
cmpl $0x1000, -0x44(%rbp)    # imm = 0x1000
jle 0x2a93
movq -0x40(%rbp), %rdi
callq 0x1040
movq 0x4591(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x40(%rbp), %rcx
leaq 0x1e09(%rip), %rsi      # 0x487e
leaq 0x1dfd(%rip), %rdx      # 0x4879
movl $0x1000, %r8d           # imm = 0x1000
movb $0x0, %al
callq 0x1160
movl $0x1, %edi
callq 0x1220
jmp 0x2a95
cmpl $0x1000, -0x1c(%rbp)    # imm = 0x1000
jle 0x2ada
movq -0x40(%rbp), %rdi
callq 0x1040
movq 0x454a(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x28(%rbp), %rcx
leaq 0x1e0a(%rip), %rsi      # 0x48c6
leaq 0x1db6(%rip), %rdx      # 0x4879
movl $0x1000, %r8d           # imm = 0x1000
movb $0x0, %al
callq 0x1160
movl $0x1, %edi
callq 0x1220
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
je 0x2afe
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0x2afe
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0x2b0c
movq -0x40(%rbp), %rdi
callq 0x1040
jmp 0x29f1
movq -0x40(%rbp), %rax
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
je 0x2b38
movq -0x40(%rbp), %rax
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0x2b49
movq -0x40(%rbp), %rax
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x29f1
movq -0x30(%rbp), %rdi
callq 0x1090
movl $0x800, %edi            # imm = 0x800
callq 0x1190
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
leaq 0x1cf0(%rip), %rsi      # 0x4879
leaq 0x18e1(%rip), %rdx      # 0x4471
movl $0x11d, %ecx            # imm = 0x11D
callq 0x3140
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
leaq 0x18be(%rip), %rsi      # 0x446f
callq 0x11d0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
leaq 0x1cb4(%rip), %rsi      # 0x4879
leaq 0x18a5(%rip), %rdx      # 0x4471
movl $0x120, %ecx            # imm = 0x120
callq 0x3250
movl $0x0, -0x64(%rbp)
movq $0x100, -0x70(%rbp)     # imm = 0x100
movq -0x70(%rbp), %rdi
shlq $0x0, %rdi
callq 0x1190
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
leaq 0x1c78(%rip), %rsi      # 0x4879
leaq 0x1869(%rip), %rdx      # 0x4471
movl $0x126, %ecx            # imm = 0x126
callq 0x3140
movq -0x60(%rbp), %rdx
leaq -0x78(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1200
movl %eax, -0x7c(%rbp)
cmpl $-0x1, -0x7c(%rbp)
jne 0x2c3a
movq -0x78(%rbp), %rdi
callq 0x1040
jmp 0x2d53
cmpl $0x100, -0x7c(%rbp)     # imm = 0x100
jle 0x2c7f
movq -0x78(%rbp), %rdi
callq 0x1040
movq 0x43a5(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x78(%rbp), %rcx
leaq 0x1cbb(%rip), %rsi      # 0x491c
leaq 0x1c11(%rip), %rdx      # 0x4879
movl $0x100, %r8d            # imm = 0x100
movb $0x0, %al
callq 0x1160
movl $0x1, %edi
callq 0x1220
jmp 0x2c81
cmpl $0x100, -0x64(%rbp)     # imm = 0x100
jle 0x2cc6
movq -0x78(%rbp), %rdi
callq 0x1040
movq 0x435e(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x58(%rbp), %rcx
leaq 0x1c1e(%rip), %rsi      # 0x48c6
leaq 0x1bca(%rip), %rdx      # 0x4879
movl $0x100, %r8d            # imm = 0x100
movb $0x0, %al
callq 0x1160
movl $0x1, %edi
callq 0x1220
movq -0x78(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
je 0x2cea
movq -0x78(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0x2cea
movq -0x78(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0x2cf8
movq -0x78(%rbp), %rdi
callq 0x1040
jmp 0x2bdd
movq -0x78(%rbp), %rax
movl -0x7c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
je 0x2d24
movq -0x78(%rbp), %rax
movl -0x7c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0x2d35
movq -0x78(%rbp), %rax
movl -0x7c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movq -0x78(%rbp), %rdx
movq -0x50(%rbp), %rax
movslq -0x64(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x2bdd
movq -0x60(%rbp), %rdi
callq 0x1090
leaq -0x880(%rbp), %rdi
xorl %esi, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x1110
leaq -0xc80(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x1110
movq -0x18(%rbp), %rax
movq %rax, 0x440b(%rip)      # 0x7198
movl -0x1c(%rbp), %eax
movl %eax, 0x440e(%rip)      # 0x71a4
movl $0x0, 0x4400(%rip)      # 0x71a0
movq -0x50(%rbp), %rax
movq %rax, 0x43fd(%rip)      # 0x71a8
movl $0x0, 0x83fb(%rip)      # 0xb1b0
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x64(%rbp), %eax
jge 0x2e55
movl -0x20(%rbp), %ecx
movslq -0x20(%rbp), %rax
movl %ecx, -0xc80(%rbp,%rax,4)
movslq -0x20(%rbp), %rax
leaq -0x880(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
movslq -0x20(%rbp), %rax
leaq -0xc80(%rbp), %rcx
shlq $0x2, %rax
addq %rax, %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0xed5(%rip), %rdx      # 0x1f30
callq 0x11c0
movl %eax, -0xc84(%rbp)
cmpl $0x0, -0xc84(%rbp)
je 0x2e45
movq 0x41d8(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movl -0x20(%rbp), %ecx
leaq 0x1b37(%rip), %rsi      # 0x4964
leaq 0x1a45(%rip), %rdx      # 0x4879
movb $0x0, %al
callq 0x1160
movl $0x1, %edi
callq 0x1220
jmp 0x2e47
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2dbc
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x64(%rbp), %eax
jge 0x2f14
movslq -0x20(%rbp), %rax
movq -0x880(%rbp,%rax,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1240
movl %eax, -0xc88(%rbp)
cmpl $0x0, -0xc88(%rbp)
je 0x2eae
movq 0x4165(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movl -0x20(%rbp), %ecx
leaq 0x1ac4(%rip), %rsi      # 0x4964
leaq 0x19d2(%rip), %rdx      # 0x4879
movb $0x0, %al
callq 0x1160
movslq -0x20(%rbp), %rcx
leaq 0x42df(%rip), %rax      # 0x7198
cmpl $0x0, 0x401c(%rax,%rcx,4)
jle 0x2f04
movq 0x412e(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq 0x42d4(%rip), %rax      # 0x71a8
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movslq -0x20(%rbp), %rdx
leaq 0x42b1(%rip), %rax      # 0x7198
movl 0x401c(%rax,%rdx,4), %r8d
leaq 0x1a9e(%rip), %rsi      # 0x4994
leaq 0x197c(%rip), %rdx      # 0x4879
movb $0x0, %al
callq 0x1160
jmp 0x2f06
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2e5c
cmpl $0x0, 0x8295(%rip)      # 0xb1b0
jle 0x2ffa
leaq 0x1aa8(%rip), %rdi      # 0x49d0
leaq 0x1533(%rip), %rsi      # 0x4462
callq 0x11d0
movq %rax, -0xc90(%rbp)
movq -0xc90(%rbp), %rdi
leaq 0x1930(%rip), %rsi      # 0x4879
leaq 0x1521(%rip), %rdx      # 0x4471
movl $0x16b, %ecx            # imm = 0x16B
callq 0x3250
movq 0x4097(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x1a70(%rip), %rsi      # 0x49db
leaq 0x1907(%rip), %rdx      # 0x4879
leaq 0x1a57(%rip), %rcx      # 0x49d0
movb $0x0, %al
callq 0x1160
movq -0xc90(%rbp), %rdi
leaq 0x1a9e(%rip), %rsi      # 0x4a2c
movb $0x0, %al
callq 0x1160
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl 0x820b(%rip), %eax      # 0xb1b0
jge 0x2fee
movslq -0x20(%rbp), %rcx
leaq 0x41e6(%rip), %rax      # 0x7198
movl 0x18(%rax,%rcx,4), %eax
movl %eax, -0xc94(%rbp)
movq -0xc90(%rbp), %rdi
movq 0x41ce(%rip), %rax      # 0x7198
movslq -0xc94(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x1236(%rip), %rsi      # 0x4212
movb $0x0, %al
callq 0x1160
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2f9c
movq -0xc90(%rbp), %rdi
callq 0x1090
cmpl $0x0, 0xc5b3(%rip)      # 0xf5b4
jle 0x30e3
leaq 0x1a5d(%rip), %rdi      # 0x4a6b
leaq 0x144d(%rip), %rsi      # 0x4462
callq 0x11d0
movq %rax, -0xca0(%rbp)
movq -0xca0(%rbp), %rdi
leaq 0x184a(%rip), %rsi      # 0x4879
leaq 0x143b(%rip), %rdx      # 0x4471
movl $0x17b, %ecx            # imm = 0x17B
callq 0x3250
movq 0x3fb1(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x1a2b(%rip), %rsi      # 0x4a7c
leaq 0x1821(%rip), %rdx      # 0x4879
leaq 0x1a0c(%rip), %rcx      # 0x4a6b
movb $0x0, %al
callq 0x1160
movq -0xca0(%rbp), %rdi
leaq 0x1a4d(%rip), %rsi      # 0x4ac1
movb $0x0, %al
callq 0x1160
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl 0xc529(%rip), %eax      # 0xf5b4
jge 0x30d7
movslq -0x20(%rbp), %rcx
leaq 0x4100(%rip), %rax      # 0x7198
movl 0x441c(%rax,%rcx,4), %eax
movl %eax, -0xca4(%rbp)
movq -0xca0(%rbp), %rdi
movq 0x40e5(%rip), %rax      # 0x7198
movslq -0xca4(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x114d(%rip), %rsi      # 0x4212
movb $0x0, %al
callq 0x1160
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x3082
movq -0xca0(%rbp), %rdi
callq 0x1090
movq 0x3f0e(%rip), %rax      # 0x6ff8
movq (%rax), %rax
movq %rax, -0xcb0(%rbp)
callq 0x2810
movq -0xcb0(%rbp), %rdi
subsd 0x4088(%rip), %xmm0     # 0x7190
movsd 0x1338(%rip), %xmm1     # 0x4448
divsd %xmm1, %xmm0
leaq 0x1a1b(%rip), %rsi      # 0x4b36
leaq 0x1757(%rip), %rdx      # 0x4879
movb $0x1, %al
callq 0x1160
xorl %eax, %eax
addq $0xcb0, %rsp            # imm = 0xCB0
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop