// File: /workspace/github_cpp_makefile_star2_O3/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, 0x1d05d(%rip)     # 0x245e0
movq %rax, 0x1d066(%rip)     # 0x245f0
movl (%rax), %esi
movq 0x8(%rax), %rdx
movl 0x10(%rax), %ecx
leaq 0x11b51(%rip), %rdi     # 0x190eb
xorl %eax, %eax
callq 0x6030
movq 0x1d038(%rip), %r14     # 0x245e0
movq 0x1d041(%rip), %rax     # 0x245f0
movq 0x8(%rax), %rbx
leaq (%r14,%rbx), %rax
movq %rax, 0x1d03a(%rip)     # 0x245f8
movslq (%r14,%rbx), %r15
movl %r15d, 0x1d0ef(%rip)    # 0x246b8
movq %r15, %rdi
shlq $0x4, %rdi
callq 0x6400
movq %rax, 0x1d00c(%rip)     # 0x245e8
testq %r15, %r15
jle 0x7627
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 0x7614
movq (%rcx,%rdi), %r9
subq %r8, %r9
movq %r9, (%rax)
addq $0x10, %rax
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0x75ff
movl $0x54, %edi
callq 0x61e0
movq %rax, 0x8(%rsp)
movslq %eax, %rbx
leaq (,%rbx,8), %r15
movq %r15, %rdi
callq 0x6400
movq %rax, %rbp
movq %r15, %rdi
callq 0x6400
testl %ebx, %ebx
jle 0x770c
movq %rax, %r15
movq 0x8(%rsp), %rbx
leal -0x1(%rbx), %eax
movslq %eax, %r14
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorl %r12d, %r12d
movq %r15, %r13
movq %rbp, 0x40(%rsp)
movl 0x1d037(%rip), %ecx     # 0x246b8
movl %ecx, %eax
cltd
idivl 0x8(%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 -0x69a(%rip), %rdx      # 0x7011
movq %r13, %rcx
callq 0x65c0
incq %r12
addq $0x8, %rbp
addq $0x8, %r13
cmpq %r12, %rbx
jne 0x767b
leaq -0x22b(%rip), %rdx      # 0x749f
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x65c0
cmpl $0x0, 0x8(%rsp)
movq 0x40(%rsp), %r14
jle 0x7721
movq 0x8(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x8(%rsp)
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
xorl %esi, %esi
callq 0x62e0
incq %rbx
cmpq %rbx, 0x8(%rsp)
jne 0x76f5
jmp 0x7721
leaq -0x274(%rip), %rdx      # 0x749f
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x65c0
movq 0x18(%rsp), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x62e0
leaq 0x11ab9(%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 0x1cf13(%rip), %rcx     # 0x24670
leaq 0x1cefc(%rip), %rax     # 0x24660
cmpq %rax, %rcx
je 0x781e
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 0x7b3a
movq 0x28(%rsp), %r14
movq 0x30(%rsp), %r13
cmpq %r13, %r14
je 0x77ec
movl (%r14), %eax
movl %eax, 0x14(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x799e
movq %rax, %rbx
leaq 0x1ce42(%rip), %rdi     # 0x24600
leaq 0x20(%rsp), %rsi
callq 0x7a04
movl (%rbx), %ebx
movl (%rax), %ebp
movq %r15, %rdi
movq %r12, %rsi
callq 0x799e
cmpl %ebp, %ebx
cmoval %ebx, %ebp
movl %ebp, (%rax)
addq $0x4, %r14
cmpq %r13, %r14
jne 0x77a2
movq 0x28(%rsp), %r14
testq %r14, %r14
je 0x7801
movq 0x38(%rsp), %rsi
subq %r14, %rsi
movq %r14, %rdi
callq 0x6370
movq 0x8(%rsp), %rdi
callq 0x6200
movq %rax, %rcx
leaq 0x1ce4b(%rip), %rax     # 0x24660
cmpq %rax, %rcx
jne 0x7777
leaq 0x118f8(%rip), %rdi     # 0x1911d
leaq 0x118ff(%rip), %rsi     # 0x1912b
callq 0x63a0
movq %rax, %rbx
movq 0x60(%rsp), %r14
leaq 0x50(%rsp), %r12
cmpq %r12, %r14
je 0x786f
leaq 0x118e3(%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 0x784a
movq %rbx, %rdi
callq 0x6350
leaq 0x48(%rsp), %rdi
callq 0x7bd6
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 0x78b4
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x78b4
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x6370
leaq 0x48(%rsp), %rdi
callq 0x7bd6
movq %rbx, %rdi
callq 0x6590