// File: /workspace/github_cpp_makefile_star2_O1/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, 0x1cf79(%rip)     # 0x245f0
movq %rax, 0x1cf82(%rip)     # 0x24600
movl (%rax), %esi
movq 0x8(%rax), %rdx
movl 0x10(%rax), %ecx
leaq 0x11a69(%rip), %rdi     # 0x190f7
xorl %eax, %eax
callq 0x6030
movq 0x1cf54(%rip), %r14     # 0x245f0
movq 0x1cf5d(%rip), %rax     # 0x24600
movq 0x8(%rax), %rbx
leaq (%r14,%rbx), %rax
movq %rax, 0x1cf56(%rip)     # 0x24608
movslq (%r14,%rbx), %r15
movl %r15d, 0x1d007(%rip)    # 0x246c4
movq %r15, %rdi
shlq $0x4, %rdi
callq 0x6420
movq %rax, 0x1cf28(%rip)     # 0x245f8
testq %r15, %r15
jle 0x771b
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 0x7708
movq (%rcx,%rdi), %r9
subq %r8, %r9
movq %r9, (%rax)
addq $0x10, %rax
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0x76f3
movl $0x54, %edi
callq 0x61e0
movq %rax, %rbx
movslq %ebx, %r14
leaq (,%r14,8), %r15
movq %r15, %rdi
callq 0x6420
movq %rax, 0x10(%rsp)
movq %r15, %rdi
callq 0x6420
testl %r14d, %r14d
jle 0x77b5
movq %rax, %r15
leal -0x1(%rbx), %eax
cltq
movq %rax, (%rsp)
movl %ebx, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %r12d, %r12d
movq %r15, %r13
movq 0x10(%rsp), %rbp
movl 0x1cf50(%rip), %ecx     # 0x246c4
movl %ecx, %eax
cltd
idivl %ebx
movl %r12d, %edx
imull %eax, %edx
movl %edx, (%r15,%r12,8)
subl %edx, %ecx
cmpq (%rsp), %r12
cmovll %eax, %ecx
movl %ecx, 0x4(%r15,%r12,8)
movq %rbp, %rdi
xorl %esi, %esi
leaq -0x64f(%rip), %rdx      # 0x714e
movq %r13, %rcx
callq 0x65e0
incq %r12
addq $0x8, %rbp
addq $0x8, %r13
cmpq %r12, %r14
jne 0x776e
leaq -0x226(%rip), %rdx      # 0x7596
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x65e0
testl %ebx, %ebx
movq 0x10(%rsp), %r15
jle 0x77ef
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
xorl %esi, %esi
callq 0x62e0
incq %r14
cmpq %r14, %rbx
jne 0x77dc
movq 0x50(%rsp), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x62e0
leaq 0x119f9(%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 0x1ce55(%rip), %rcx     # 0x24680
leaq 0x1ce3e(%rip), %rax     # 0x24670
cmpq %rax, %rcx
je 0x78ef
leaq 0x1cdce(%rip), %r13     # 0x24610
leaq 0x18(%rsp), %rbp
movq 0x20(%rcx), %rax
movq %rax, 0x18(%rsp)
movq %rcx, (%rsp)
leaq 0x28(%rcx), %rsi
leaq 0x20(%rsp), %rdi
callq 0x7ccc
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %r12
cmpq %r12, %r15
je 0x78bc
movl (%r15), %eax
movl %eax, 0xc(%rsp)
leaq 0x58(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x7ad8
movq %rax, %r14
movq %r13, %rdi
movq %rbp, %rsi
callq 0x7b3c
movq 0x18(%rax), %rbx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x7b3c
addq $0x8, %rax
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x89ee
addq $0x4, %r15
jmp 0x786c
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x78d3
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x6370
movq (%rsp), %rdi
callq 0x6200
movq %rax, %rcx
leaq 0x1cd8a(%rip), %rax     # 0x24670
cmpq %rax, %rcx
jne 0x7847
leaq 0x11833(%rip), %rdi     # 0x19129
leaq 0x1183a(%rip), %rsi     # 0x19137
callq 0x63b0
movq %rax, %rbx
movq 0x70(%rsp), %r14
leaq 0x60(%rsp), %rax
cmpq %rax, %r14
je 0x79a7
leaq 0x20(%rsp), %r15
leaq 0x28(%rsp), %r12
leaq 0x11813(%rip), %r13     # 0x1913c
movl 0x20(%r14), %eax
movl %eax, 0x18(%rsp)
leaq 0x28(%r14), %rsi
movq %r15, %rdi
callq 0x7d68
movl 0x18(%rsp), %edx
movq %rbx, %rdi
leaq 0x117ee(%rip), %rsi     # 0x19139
xorl %eax, %eax
callq 0x6480
movq 0x38(%rsp), %rbp
cmpq %r12, %rbp
je 0x797d
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 0x795c
movl $0xa, %edi
movq %rbx, %rsi
callq 0x63f0
movq %r15, %rdi
callq 0x7c06
movq %r14, %rdi
callq 0x6200
movq %rax, %r14
leaq 0x60(%rsp), %rax
cmpq %rax, %r14
jne 0x7929
movq %rbx, %rdi
callq 0x6350
leaq 0x58(%rsp), %rdi
callq 0x7f00
xorl %eax, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x79cf
movq %rax, %rbx
jmp 0x79ee
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x79ee
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x6370
leaq 0x58(%rsp), %rdi
callq 0x7f00
movq %rbx, %rdi
callq 0x65b0