// File: /workspace/github_cpp_makefile_star2_O1/moyix[P]pandalog_taint_parser/label_tcn.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 compute max TCN...\n");
    std::map<int,uint32_t> label_to_tcn;
    for (auto kvp : ptr_to_label) {
        for (auto l : kvp.second) {
            label_to_tcn[l] = std::max(label_to_tcn[l], ptr_to_tcn[kvp.first]);
        }
    }

    FILE *f = fopen("label_tcn.txt", "w");
    for (auto kvp : label_to_tcn)
        fprintf(f, "%d %u\n", kvp.first, kvp.second);
    fclose(f);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x6230
movl %eax, %ebp
movq 0x8(%rbx), %rdi
leaq 0x78(%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 0x65a0
movq %rax, 0x1cf5b(%rip)     # 0x245e0
movq %rax, 0x1cf64(%rip)     # 0x245f0
movl (%rax), %esi
movq 0x8(%rax), %rdx
movl 0x10(%rax), %ecx
leaq 0x11a4f(%rip), %rdi     # 0x190eb
xorl %eax, %eax
callq 0x6030
movq 0x1cf36(%rip), %r14     # 0x245e0
movq 0x1cf3f(%rip), %rax     # 0x245f0
movq 0x8(%rax), %rbx
leaq (%r14,%rbx), %rax
movq %rax, 0x1cf38(%rip)     # 0x245f8
movslq (%r14,%rbx), %r15
movl %r15d, 0x1cfed(%rip)    # 0x246b8
movq %r15, %rdi
shlq $0x4, %rdi
callq 0x6400
movq %rax, 0x1cf0a(%rip)     # 0x245e8
testq %r15, %r15
jle 0x7729
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 0x7716
movq (%rcx,%rdi), %r9
subq %r8, %r9
movq %r9, (%rax)
addq $0x10, %rax
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0x7701
movl $0x54, %edi
callq 0x61e0
movq %rax, %rbx
movslq %ebx, %r14
leaq (,%r14,8), %r15
movq %r15, %rdi
callq 0x6400
movq %rax, 0x18(%rsp)
movq %r15, %rdi
callq 0x6400
testl %r14d, %r14d
jle 0x77c5
movq %rax, %r15
leal -0x1(%rbx), %eax
cltq
movq %rax, 0x8(%rsp)
movl %ebx, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %r12d, %r12d
movq %r15, %r13
movq 0x18(%rsp), %rbp
movl 0x1cf35(%rip), %ecx     # 0x246b8
movl %ecx, %eax
cltd
idivl %ebx
movl %r12d, %edx
imull %eax, %edx
movl %edx, (%r15,%r12,8)
subl %edx, %ecx
cmpq 0x8(%rsp), %r12
cmovll %eax, %ecx
movl %ecx, 0x4(%r15,%r12,8)
movq %rbp, %rdi
xorl %esi, %esi
leaq -0x67f(%rip), %rdx      # 0x712e
movq %r13, %rcx
callq 0x65c0
incq %r12
addq $0x8, %rbp
addq $0x8, %r13
cmpq %r12, %r14
jne 0x777d
leaq -0x22b(%rip), %rdx      # 0x75a1
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x65c0
testl %ebx, %ebx
movq 0x18(%rsp), %r15
jle 0x77ff
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
xorl %esi, %esi
callq 0x62e0
incq %r14
cmpq %r14, %rbx
jne 0x77ec
movq 0x40(%rsp), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x62e0
leaq 0x119db(%rip), %rdi     # 0x191ef
callq 0x64e0
leaq 0x50(%rsp), %rax
movl $0x0, (%rax)
movq %rbx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rbx, 0x20(%rax)
movq 0x1ce35(%rip), %rcx     # 0x24670
leaq 0x1ce1e(%rip), %rax     # 0x24660
cmpq %rax, %rcx
je 0x78f8
leaq 0x48(%rsp), %r15
leaq 0x14(%rsp), %r12
movq 0x20(%rcx), %rax
movq %rax, 0x20(%rsp)
movq %rcx, 0x8(%rsp)
leaq 0x28(%rcx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x7c14
movq 0x28(%rsp), %rbx
movq 0x30(%rsp), %r13
cmpq %r13, %rbx
je 0x78c4
movl (%rbx), %eax
movl %eax, 0x14(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x7a78
movq %rax, %r14
leaq 0x1cd65(%rip), %rdi     # 0x24600
leaq 0x20(%rsp), %rsi
callq 0x7adc
movl (%r14), %r14d
movl (%rax), %ebp
movq %r15, %rdi
movq %r12, %rsi
callq 0x7a78
cmpl %ebp, %r14d
cmoval %r14d, %ebp
movl %ebp, (%rax)
addq $0x4, %rbx
jmp 0x787b
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x78db
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x6370
movq 0x8(%rsp), %rdi
callq 0x6200
movq %rax, %rcx
leaq 0x1cd71(%rip), %rax     # 0x24660
cmpq %rax, %rcx
jne 0x7855
leaq 0x1181e(%rip), %rdi     # 0x1911d
leaq 0x11825(%rip), %rsi     # 0x1912b
callq 0x63a0
movq %rax, %rbx
movq 0x60(%rsp), %r14
leaq 0x50(%rsp), %r12
cmpq %r12, %r14
je 0x7949
leaq 0x11809(%rip), %r15     # 0x1912d
movl 0x20(%r14), %edx
movl 0x24(%r14), %ecx
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x6460
movq %r14, %rdi
callq 0x6200
movq %rax, %r14
cmpq %r12, %rax
jne 0x7924
movq %rbx, %rdi
callq 0x6350
leaq 0x48(%rsp), %rdi
callq 0x7cb0
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x798e
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x798e
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x6370
leaq 0x48(%rsp), %rdi
callq 0x7cb0
movq %rbx, %rdi
callq 0x6590