// File: /workspace/github_cpp_makefile_star2_O2/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 %r14d, %r14d
xorl %esi, %esi
xorl %eax, %eax
callq 0x6230
movl %eax, %ebp
movq 0x8(%rbx), %rdi
leaq 0x88(%rsp), %rbx
movq %rbx, %rsi
callq 0x62d0
movq 0x30(%rbx), %rsi
pushq $0x1
popq %rdx
pushq $0x2
popq %rcx
xorl %edi, %edi
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x65b0
movq %rax, 0x1b931(%rip)     # 0x225f0
movq %rax, 0x1b93a(%rip)     # 0x22600
movl (%rax), %esi
movq 0x8(%rax), %rdx
movl 0x10(%rax), %ecx
leaq 0xe409(%rip), %rdi      # 0x150df
xorl %eax, %eax
callq 0x6030
movq 0x1b90c(%rip), %rax     # 0x225f0
movq 0x1b915(%rip), %rcx     # 0x22600
movq 0x8(%rcx), %rbx
leaq (%rax,%rbx), %rcx
movq %rcx, 0x1b90e(%rip)     # 0x22608
leaq 0xc(%rax,%rbx), %r15
movl -0xc(%r15), %r12d
movl %r12d, 0x1b9ba(%rip)    # 0x226c4
movslq %r12d, %r13
movq %r13, %rdi
shlq $0x4, %rdi
callq 0x6400
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
movq %rax, 0x1b8cc(%rip)     # 0x225f8
shlq $0x4, %rcx
shlq $0x4, %r12
addq $-0x10, %r12
cmpq %r14, %rcx
je 0x6d62
movq (%r15), %rdx
movq %rdx, (%rax,%r14)
movq %rbx, %rsi
cmpq %r14, %r12
je 0x6d50
movq 0x18(%r15), %rsi
subq %rdx, %rsi
movq %rsi, 0x8(%rax,%r14)
addq $0x10, %r14
addq $0x18, %r15
jmp 0x6d38
pushq $0x54
popq %rdi
callq 0x61e0
movq %rax, %r13
movq %rax, %r14
shlq $0x20, %r14
movq %r14, %r15
sarq $0x1d, %r15
movq %r15, %rdi
callq 0x6400
movq %rax, %rbp
movq %r15, %rdi
callq 0x6400
movabsq $-0x100000000, %rbx     # imm = 0xFFFFFFFF00000000
addq %r14, %rbx
sarq $0x20, %rbx
xorl %r14d, %r14d
testl %r13d, %r13d
movl $0x0, %r12d
movq %r13, (%rsp)
cmovgl %r13d, %r12d
movq %rax, %r15
movq %rax, %r13
movq %rbp, 0x10(%rsp)
cmpq %r14, %r12
je 0x6e07
movl 0x1b8fb(%rip), %ecx     # 0x226c4
movl %ecx, %eax
cltd
idivl (%rsp)
movl %r14d, %edx
imull %eax, %edx
movl %edx, (%r15,%r14,8)
subl %edx, %ecx
cmpq %rbx, %r14
cmovll %eax, %ecx
movl %ecx, 0x4(%r15,%r14,8)
movq %rbp, %rdi
xorl %esi, %esi
leaq -0x563(%rip), %rdx      # 0x688f
movq %r13, %rcx
callq 0x65d0
incq %r14
addq $0x8, %rbp
addq $0x8, %r13
jmp 0x6dbe
leaq -0x21b(%rip), %rdx      # 0x6bf3
xorl %ebx, %ebx
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x65d0
movq 0x10(%rsp), %r14
cmpq %rbx, %r12
je 0x6e38
movq (%r14,%rbx,8), %rdi
xorl %esi, %esi
callq 0x62c0
incq %rbx
jmp 0x6e23
movq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x62c0
leaq 0xe39a(%rip), %rdi      # 0x151e5
callq 0x64f0
leaq 0x60(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq 0x1b80f(%rip), %rcx     # 0x22680
leaq 0x20(%rsp), %r15
leaq 0x1b793(%rip), %rbp     # 0x22610
leaq 0x1b7ec(%rip), %rax     # 0x22670
cmpq %rax, %rcx
je 0x6f13
movq %rcx, (%rsp)
leaq 0x20(%rcx), %rsi
movq %r15, %rdi
callq 0x70b4
movq 0x28(%rsp), %r12
movq 0x30(%rsp), %r13
cmpq %r13, %r12
je 0x6ef8
movl (%r12), %eax
movl %eax, 0xc(%rsp)
leaq 0x58(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x70c8
movq %rax, %rbx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x7116
movq 0x18(%rax), %r14
movq %rbp, %rdi
movq %r15, %rsi
callq 0x7116
addq $0x8, %rax
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x8268
addq $0x4, %r12
jmp 0x6ea7
leaq 0x28(%rsp), %rdi
callq 0x73ec
movq (%rsp), %rdi
callq 0x6200
movq %rax, %rcx
jmp 0x6e7d
leaq 0xe1f7(%rip), %rdi      # 0x15111
leaq 0xe1fe(%rip), %rsi      # 0x1511f
callq 0x6390
movq %rax, %rbx
movq 0x70(%rsp), %r15
leaq 0x30(%rsp), %r13
pushq $0xa
popq %rbp
leaq 0xe1e7(%rip), %r14      # 0x15124
leaq 0x60(%rsp), %rax
cmpq %rax, %r15
je 0x6fb4
leaq 0x20(%r15), %rsi
leaq 0x20(%rsp), %rdi
callq 0x7166
movl 0x20(%rsp), %edx
movq %rbx, %rdi
leaq 0xe1be(%rip), %rsi      # 0x15121
xorl %eax, %eax
callq 0x6470
movq 0x40(%rsp), %r12
cmpq %r13, %r12
je 0x6f93
movq 0x20(%r12), %rdx
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x6470
movq %r12, %rdi
callq 0x6370
movq %rax, %r12
jmp 0x6f6f
movl %ebp, %edi
movq %rbx, %rsi
callq 0x63d0
leaq 0x28(%rsp), %rdi
callq 0x71e6
movq %r15, %rdi
callq 0x6200
movq %rax, %r15
jmp 0x6f3d
movq %rbx, %rdi
callq 0x6330
leaq 0x58(%rsp), %rdi
callq 0x7678
xorl %eax, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6fdc
movq %rax, %rbx
jmp 0x6fee
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x73ec
leaq 0x58(%rsp), %rdi
callq 0x7678
movq %rbx, %rdi
callq 0x65a0