// File: /workspace/github_cpp_makefile_star2_O2/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 %r14d, %r14d
xorl %esi, %esi
xorl %eax, %eax
callq 0x6230
movl %eax, %ebp
movq 0x8(%rbx), %rdi
leaq 0x78(%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 0x6590
movq %rax, 0x1a936(%rip)     # 0x215e0
movq %rax, 0x1a93f(%rip)     # 0x215f0
movl (%rax), %esi
movq 0x8(%rax), %rdx
movl 0x10(%rax), %ecx
leaq 0xe42a(%rip), %rdi      # 0x150eb
xorl %eax, %eax
callq 0x6030
movq 0x1a911(%rip), %rax     # 0x215e0
movq 0x1a91a(%rip), %rcx     # 0x215f0
movq 0x8(%rcx), %rbx
leaq (%rax,%rbx), %rcx
movq %rcx, 0x1a913(%rip)     # 0x215f8
leaq 0xc(%rax,%rbx), %r15
movl -0xc(%r15), %r12d
movl %r12d, 0x1a9c3(%rip)    # 0x216b8
movslq %r12d, %r13
movq %r13, %rdi
shlq $0x4, %rdi
callq 0x63e0
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
movq %rax, 0x1a8d1(%rip)     # 0x215e8
shlq $0x4, %rcx
shlq $0x4, %r12
addq $-0x10, %r12
cmpq %r14, %rcx
je 0x6d4d
movq (%r15), %rdx
movq %rdx, (%rax,%r14)
movq %rbx, %rsi
cmpq %r14, %r12
je 0x6d3b
movq 0x18(%r15), %rsi
subq %rdx, %rsi
movq %rsi, 0x8(%rax,%r14)
addq $0x10, %r14
addq $0x18, %r15
jmp 0x6d23
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 0x63e0
movq %rax, %rbp
movq %r15, %rdi
callq 0x63e0
movabsq $-0x100000000, %rbx     # imm = 0xFFFFFFFF00000000
addq %r14, %rbx
sarq $0x20, %rbx
xorl %r14d, %r14d
testl %r13d, %r13d
movl $0x0, %r12d
movq %r13, 0x8(%rsp)
cmovgl %r13d, %r12d
movq %rax, %r15
movq %rax, %r13
movq %rbp, 0x18(%rsp)
cmpq %r14, %r12
je 0x6df4
movl 0x1a903(%rip), %ecx     # 0x216b8
movl %ecx, %eax
cltd
idivl 0x8(%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 -0x570(%rip), %rdx      # 0x686f
movq %r13, %rcx
callq 0x65b0
incq %r14
addq $0x8, %rbp
addq $0x8, %r13
jmp 0x6daa
leaq -0x220(%rip), %rdx      # 0x6bdb
xorl %ebx, %ebx
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x65b0
movq 0x18(%rsp), %r14
cmpq %rbx, %r12
je 0x6e25
movq (%r14,%rbx,8), %rdi
xorl %esi, %esi
callq 0x62c0
incq %rbx
jmp 0x6e10
movq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x62c0
leaq 0xe3b7(%rip), %rdi      # 0x151ef
callq 0x64d0
leaq 0x50(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq 0x1a812(%rip), %rcx     # 0x21670
leaq 0x48(%rsp), %r12
leaq 0x14(%rsp), %r13
leaq 0x1a7f1(%rip), %rax     # 0x21660
cmpq %rax, %rcx
je 0x6efc
movq %rcx, 0x8(%rsp)
leaq 0x20(%rcx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x7048
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %rbp
cmpq %rbp, %r14
je 0x6ee0
movl (%r14), %eax
movl %eax, 0x14(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x705c
movq %rax, %rbx
leaq 0x1a74a(%rip), %rdi     # 0x21600
leaq 0x28(%rsp), %rsi
callq 0x70aa
movl (%rbx), %ebx
movl (%rax), %r15d
movq %r12, %rdi
movq %r13, %rsi
callq 0x705c
cmpl %r15d, %ebx
cmoval %ebx, %r15d
movl %r15d, (%rax)
addq $0x4, %r14
jmp 0x6e95
leaq 0x30(%rsp), %rdi
callq 0x730a
movq 0x8(%rsp), %rdi
callq 0x6200
movq %rax, %rcx
jmp 0x6e68
leaq 0xe21a(%rip), %rdi      # 0x1511d
leaq 0xe221(%rip), %rsi      # 0x1512b
callq 0x6380
movq %rax, %rbx
movq 0x60(%rsp), %r15
leaq 0xe20f(%rip), %r14      # 0x1512d
leaq 0x50(%rsp), %r12
cmpq %r12, %r15
je 0x6f4a
movl 0x20(%r15), %edx
movl 0x24(%r15), %ecx
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x6450
movq %r15, %rdi
callq 0x6200
movq %rax, %r15
jmp 0x6f23
movq %rbx, %rdi
callq 0x6330
leaq 0x48(%rsp), %rdi
callq 0x73a0
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x6f82
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x730a
leaq 0x48(%rsp), %rdi
callq 0x73a0
movq %r14, %rdi
callq 0x6580