// File: 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
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x6250
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x6310
movq -0x78(%rbp), %rsi
movl -0x14(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, %edx
movl $0x2, %ecx
movq %r9, %rdi
callq 0x6640
movq %rax, 0x3e4b9(%rip)     # 0x45640
movq 0x3e4b2(%rip), %rax     # 0x45640
movq %rax, 0x3e4bb(%rip)     # 0x45650
movq 0x3e4b4(%rip), %rax     # 0x45650
movl (%rax), %esi
movq 0x3e4ab(%rip), %rax     # 0x45650
movq 0x8(%rax), %rdx
movq 0x3e4a0(%rip), %rax     # 0x45650
movl 0x10(%rax), %ecx
leaq 0x2af45(%rip), %rdi     # 0x320ff
movb $0x0, %al
callq 0x6040
movq 0x3e478(%rip), %rax     # 0x45640
movq 0x3e481(%rip), %rcx     # 0x45650
addq 0x8(%rcx), %rax
movq %rax, 0x3e47e(%rip)     # 0x45658
movq 0x3e477(%rip), %rax     # 0x45658
movl (%rax), %eax
movl %eax, 0x3e52f(%rip)     # 0x45718
movslq 0x3e528(%rip), %rdi     # 0x45718
shlq $0x4, %rdi
callq 0x6460
movq %rax, 0x3e448(%rip)     # 0x45648
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl 0x3e502(%rip), %eax     # 0x45718
jge 0x7309
movq 0x3e435(%rip), %rax     # 0x45658
addq $0x4, %rax
movslq -0xac(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq 0x3e408(%rip), %rax     # 0x45648
movslq -0xac(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl -0xac(%rbp), %eax
addl $0x1, %eax
cmpl 0x3e4b8(%rip), %eax     # 0x45718
je 0x72b7
movq 0x3e3ef(%rip), %rax     # 0x45658
addq $0x4, %rax
movl -0xac(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq 0x3e3bd(%rip), %rax     # 0x45648
movslq -0xac(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
subq (%rax), %rcx
movq 0x3e3a5(%rip), %rax     # 0x45648
movslq -0xac(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x72f3
movq 0x3e392(%rip), %rax     # 0x45650
movq 0x8(%rax), %rcx
movq 0x3e37f(%rip), %rax     # 0x45648
movslq -0xac(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
subq (%rax), %rcx
movq 0x3e367(%rip), %rax     # 0x45648
movslq -0xac(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x72f5
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x720a
movl $0x54, %edi
callq 0x6200
movl %eax, -0xb0(%rbp)
movslq -0xb0(%rbp), %rdi
shlq $0x3, %rdi
callq 0x6460
movq %rax, -0xb8(%rbp)
movslq -0xb0(%rbp), %rdi
shlq $0x3, %rdi
callq 0x6460
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpl -0xb0(%rbp), %eax
jge 0x7435
movl 0x3e3af(%rip), %eax     # 0x45718
cltd
idivl -0xb0(%rbp)
movl %eax, %edx
imull -0xc4(%rbp), %edx
movq -0xc0(%rbp), %rax
movslq -0xc4(%rbp), %rcx
movl %edx, (%rax,%rcx,8)
movl -0xc4(%rbp), %eax
movl -0xb0(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x73b2
movl 0x3e375(%rip), %eax     # 0x45718
cltd
idivl -0xb0(%rbp)
movl %eax, -0x19c(%rbp)
jmp 0x73cf
movl 0x3e360(%rip), %eax     # 0x45718
movq -0xc0(%rbp), %rcx
movslq -0xc4(%rbp), %rdx
subl (%rcx,%rdx,8), %eax
movl %eax, -0x19c(%rbp)
movl -0x19c(%rbp), %edx
movq -0xc0(%rbp), %rax
movslq -0xc4(%rbp), %rcx
movl %edx, 0x4(%rax,%rcx,8)
movq -0xb8(%rbp), %rdi
movslq -0xc4(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0xc0(%rbp), %rcx
movslq -0xc4(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0xa8c(%rip), %rdx      # 0x6990
callq 0x6650
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x7351
leaq -0xd0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x437(%rip), %rdx      # 0x7010
movq %rcx, %rsi
callq 0x6650
movl $0x0, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
cmpl -0xb0(%rbp), %eax
jge 0x7493
movq -0xb8(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6300
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x7459
movq -0xd0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6300
leaq 0x2ac87(%rip), %rdi     # 0x32131
movb $0x0, %al
callq 0x6040
leaq -0x108(%rbp), %rdi
callq 0x7e40
leaq 0x3e1f4(%rip), %rax     # 0x456b8
movq %rax, -0x110(%rbp)
leaq 0x3e1e6(%rip), %rdi     # 0x456b8
callq 0x7e60
movq %rax, -0x118(%rbp)
leaq 0x3e1d3(%rip), %rdi     # 0x456b8
callq 0x7e90
movq %rax, -0x120(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x7ec0
testb $0x1, %al
jne 0x750d
jmp 0x7688
leaq -0x118(%rbp), %rdi
callq 0x7ef0
movq %rax, %rsi
leaq -0x140(%rbp), %rdi
callq 0x7f10
jmp 0x752a
leaq -0x140(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rdi
callq 0x7f50
movq %rax, -0x160(%rbp)
movq -0x158(%rbp), %rdi
callq 0x7f80
movq %rax, -0x168(%rbp)
leaq -0x160(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x7fb0
testb $0x1, %al
jne 0x757e
jmp 0x766b
leaq -0x160(%rbp), %rdi
callq 0x7ff0
movl (%rax), %eax
movl %eax, -0x16c(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x16c(%rbp), %rsi
callq 0x8010
movq %rax, -0x1a8(%rbp)
jmp 0x75ae
leaq 0x3e0ab(%rip), %rdi     # 0x45660
leaq -0x140(%rbp), %rsi
callq 0x80f0
movq %rax, -0x1b0(%rbp)
jmp 0x75ca
movq -0x1b0(%rbp), %rsi
movq -0x1a8(%rbp), %rdi
callq 0x7b80
movq %rax, -0x1b8(%rbp)
jmp 0x75e6
movq -0x1b8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x1c4(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x16c(%rbp), %rsi
callq 0x8010
movq %rax, -0x1c0(%rbp)
jmp 0x7611
movq -0x1c0(%rbp), %rax
movl -0x1c4(%rbp), %ecx
movl %ecx, (%rax)
leaq -0x160(%rbp), %rdi
callq 0x81d0
jmp 0x7562
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x7771
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x81f0
jmp 0x7771
leaq -0x140(%rbp), %rdi
callq 0x81f0
leaq -0x118(%rbp), %rdi
callq 0x8210
jmp 0x74f1
leaq 0x2aac4(%rip), %rdi     # 0x32153
leaq 0x2aacb(%rip), %rsi     # 0x32161
callq 0x63f0
movq %rax, -0x1d0(%rbp)
jmp 0x76a4
movq -0x1d0(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x108(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rdi
callq 0x8240
movq %rax, -0x188(%rbp)
movq -0x180(%rbp), %rdi
callq 0x8270
movq %rax, -0x190(%rbp)
leaq -0x188(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x82a0
testb $0x1, %al
jne 0x76ff
jmp 0x7744
leaq -0x188(%rbp), %rdi
callq 0x82d0
movq (%rax), %rax
movq %rax, -0x198(%rbp)
movq -0x178(%rbp), %rdi
movl -0x198(%rbp), %edx
movl -0x194(%rbp), %ecx
leaq 0x2aa34(%rip), %rsi     # 0x32163
movb $0x0, %al
callq 0x64d0
leaq -0x188(%rbp), %rdi
callq 0x82f0
jmp 0x76e6
movq -0x178(%rbp), %rdi
callq 0x6360
jmp 0x7752
movl $0x0, -0x4(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x8320
movl -0x4(%rbp), %eax
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
leaq -0x108(%rbp), %rdi
callq 0x8320
movq -0x148(%rbp), %rdi
callq 0x6620
nopl (%rax)