// File: /workspace/github_cpp_makefile_star2_O3/moyix[P]pandalog_taint_parser/label_pcs.cpp
// Function: main
int main(int argc, char **argv) {
    int fd = open(argv[1], O_RDONLY);
    struct stat st;
    stat(argv[1], &st);
    plog = (uint8_t *) mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    //printf("Mapped at %p\n", plog);
    hdr = (PlHeader *)plog;
    printf("PandaLog v%u with directory at %" PRId64 " chunk size %x\n",
        hdr->version, hdr->dir_pos, hdr->chunk_size);
    dir = (PlDir *)(plog + hdr->dir_pos);
    //printf("Directory entries in pandalog: %u\n", dir->num_entries);
    total_chunks = dir->num_entries;
    chunks = (PlChunk *) malloc(sizeof(PlChunk) * total_chunks);
    //printf("Dumping directory:\n");
    for (int i = 0; i < total_chunks; i++) {
        chunks[i].start = dir->entries[i].start;
        if (i+1 != total_chunks)
            chunks[i].size = dir->entries[i+1].start - chunks[i].start;
        else
            chunks[i].size = hdr->dir_pos - chunks[i].start;
        //printf("  %zu - %zu\n", chunks[i].start, chunks[i].start+chunks[i].size);
    }

    int ncpu = sysconf(_SC_NPROCESSORS_ONLN);
    pthread_t *threads = (pthread_t *) malloc(sizeof(pthread_t)*ncpu);
    arg *thread_args = (arg *) malloc(sizeof(arg)*ncpu); 
    for (int i = 0; i < ncpu; i++) {
        thread_args[i].start = (total_chunks / ncpu) * i;
        thread_args[i].size = (i < ncpu - 1) ? 
            (total_chunks / ncpu) : (total_chunks - thread_args[i].start);
        //printf("Creating thread to process Chunks %u - %u\n", thread_args[i].start, thread_args[i].start+thread_args[i].size);
        pthread_create(&threads[i], NULL, process_chunks, &thread_args[i]);
    }
    pthread_t progress_thread;
    pthread_create(&progress_thread, NULL, progress_func, NULL);
    for (int i = 0; i < ncpu; i++) {
        pthread_join(threads[i], NULL);
    }
    pthread_join(progress_thread, NULL);

    printf("Final pass to gather PCs...\n");
    std::map<int,std::set<uint64_t>> label_to_pc;
    for (auto kvp : ptr_to_label) {
        for (auto l : kvp.second) {
            label_to_pc[l].insert(ptr_to_pc[kvp.first].begin(), ptr_to_pc[kvp.first].end());
        }
    }

    FILE *f = fopen("label_pcs.txt", "w");
    for (auto kvp : label_to_pc) {
        fprintf(f, "%d", kvp.first);
        for (auto pc : kvp.second)
            fprintf(f, " %#lx", pc);
        fprintf(f, "\n");
    }
    fclose(f);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x6230
movl %eax, %ebp
movq 0x8(%rbx), %rdi
leaq 0x88(%rsp), %rbx
movq %rbx, %rsi
callq 0x62f0
movq 0x30(%rbx), %rsi
xorl %edi, %edi
movl $0x1, %edx
movl $0x2, %ecx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x65c0
movq %rax, 0x1d088(%rip)     # 0x245f0
movq %rax, 0x1d091(%rip)     # 0x24600
movl (%rax), %esi
movq 0x8(%rax), %rdx
movl 0x10(%rax), %ecx
leaq 0x11b78(%rip), %rdi     # 0x190f7
xorl %eax, %eax
callq 0x6030
movq 0x1d063(%rip), %r14     # 0x245f0
movq 0x1d06c(%rip), %rax     # 0x24600
movq 0x8(%rax), %rbx
leaq (%r14,%rbx), %rax
movq %rax, 0x1d065(%rip)     # 0x24608
movslq (%r14,%rbx), %r15
movl %r15d, 0x1d116(%rip)    # 0x246c4
movq %r15, %rdi
shlq $0x4, %rdi
callq 0x6420
movq %rax, 0x1d037(%rip)     # 0x245f8
testq %r15, %r15
jle 0x760c
addq $0x8, %rax
leaq (%rbx,%r14), %rcx
addq $0x24, %rcx
shlq $0x3, %r15
leaq (%r15,%r15,2), %rdx
leaq (%r15,%r15,2), %rsi
addq $-0x18, %rsi
xorl %edi, %edi
movq -0x18(%rcx,%rdi), %r8
movq %r8, -0x8(%rax)
movq %rbx, %r9
cmpq %rdi, %rsi
je 0x75f9
movq (%rcx,%rdi), %r9
subq %r8, %r9
movq %r9, (%rax)
addq $0x10, %rax
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0x75e4
movl $0x54, %edi
callq 0x61e0
movq %rax, (%rsp)
movslq %eax, %rbx
leaq (,%rbx,8), %r15
movq %r15, %rdi
callq 0x6420
movq %rax, %rbp
movq %r15, %rdi
callq 0x6420
testl %ebx, %ebx
jle 0x76ea
movq %rax, %r15
movq (%rsp), %rbx
leal -0x1(%rbx), %eax
movslq %eax, %r14
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorl %r12d, %r12d
movq %r15, %r13
movq %rbp, 0x18(%rsp)
movl 0x1d060(%rip), %ecx     # 0x246c4
movl %ecx, %eax
cltd
idivl (%rsp)
movl %r12d, %edx
imull %eax, %edx
movl %edx, (%r15,%r12,8)
subl %edx, %ecx
cmpq %r14, %r12
cmovll %eax, %ecx
movl %ecx, 0x4(%r15,%r12,8)
movq %rbp, %rdi
xorl %esi, %esi
leaq -0x65c(%rip), %rdx      # 0x7031
movq %r13, %rcx
callq 0x65e0
incq %r12
addq $0x8, %rbp
addq $0x8, %r13
cmpq %r12, %rbx
jne 0x765e
leaq -0x225(%rip), %rdx      # 0x7487
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x65e0
cmpl $0x0, (%rsp)
movq 0x18(%rsp), %r14
jle 0x76ff
movq (%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, (%rsp)
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
xorl %esi, %esi
callq 0x62e0
incq %rbx
cmpq %rbx, (%rsp)
jne 0x76d4
jmp 0x76ff
leaq -0x26a(%rip), %rdx      # 0x7487
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x65e0
movq 0x10(%rsp), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x62e0
leaq 0x11ae9(%rip), %rdi     # 0x191fd
callq 0x6500
leaq 0x60(%rsp), %rax
movl $0x0, (%rax)
movq %rbx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rbx, 0x20(%rax)
movq 0x1cf45(%rip), %rcx     # 0x24680
leaq 0x1cf2e(%rip), %rax     # 0x24670
cmpq %rax, %rcx
je 0x7805
leaq 0x1cebe(%rip), %r13     # 0x24610
leaq 0x20(%rsp), %rbp
movq 0x20(%rcx), %rax
movq %rax, 0x20(%rsp)
movq %rcx, (%rsp)
leaq 0x28(%rcx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x7be2
movq 0x28(%rsp), %r14
movq 0x30(%rsp), %r12
cmpq %r12, %r14
je 0x77d4
movl (%r14), %eax
movl %eax, 0xc(%rsp)
leaq 0x58(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x79ee
movq %rax, %rbx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x7a54
movq 0x18(%rax), %r15
movq %r13, %rdi
movq %rbp, %rsi
callq 0x7a54
addq $0x8, %rax
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x88f6
addq $0x4, %r14
cmpq %r12, %r14
jne 0x7781
movq 0x28(%rsp), %r14
testq %r14, %r14
je 0x77e9
movq 0x38(%rsp), %rsi
subq %r14, %rsi
movq %r14, %rdi
callq 0x6370
movq (%rsp), %rdi
callq 0x6200
movq %rax, %rcx
leaq 0x1ce74(%rip), %rax     # 0x24670
cmpq %rax, %rcx
jne 0x7757
leaq 0x1191d(%rip), %rdi     # 0x19129
leaq 0x11924(%rip), %rsi     # 0x19137
callq 0x63b0
movq %rax, %rbx
movq 0x70(%rsp), %r14
leaq 0x60(%rsp), %rax
cmpq %rax, %r14
je 0x78bd
leaq 0x28(%rsp), %r15
leaq 0x30(%rsp), %r12
leaq 0x118fd(%rip), %r13     # 0x1913c
movl 0x20(%r14), %eax
movl %eax, 0x20(%rsp)
leaq 0x28(%r14), %rsi
movq %r15, %rdi
callq 0x7c7e
movl 0x20(%rsp), %edx
movq %rbx, %rdi
leaq 0x118d8(%rip), %rsi     # 0x19139
xorl %eax, %eax
callq 0x6480
movq 0x40(%rsp), %rbp
cmpq %r12, %rbp
je 0x7893
movq 0x20(%rbp), %rdx
movq %rbx, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x6480
movq %rbp, %rdi
callq 0x6390
movq %rax, %rbp
cmpq %r12, %rax
jne 0x7872
movl $0xa, %edi
movq %rbx, %rsi
callq 0x63f0
movq %r15, %rdi
callq 0x7b1c
movq %r14, %rdi
callq 0x6200
movq %rax, %r14
leaq 0x60(%rsp), %rax
cmpq %rax, %r14
jne 0x783f
movq %rbx, %rdi
callq 0x6350
leaq 0x58(%rsp), %rdi
callq 0x7e16
xorl %eax, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x78e5
movq %rax, %rbx
jmp 0x7904
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x7904
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x6370
leaq 0x58(%rsp), %rdi
callq 0x7e16
movq %rbx, %rdi
callq 0x65b0