// File: moyix[P]pandalog_taint_parser/label_instr.cpp
// Function: main
int main(int argc, char **argv) {
    if (argc != 5) {
        fprintf(stderr, "usage: %s <log> <num_instr> <first_instr> <buckets>\n", argv[0]);
        return 1;
    }

    num_instr = strtoull(argv[2], NULL, 0);
    first_instr = strtoull(argv[3], NULL, 0);
    buckets = atoi(argv[4]);
    bucket_to_ptr.resize(buckets);

    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);

    FILE *f = fopen("label_instr.txt", "w");
    printf("Final pass to compute number of labels...\n");
    for (size_t i = 0; i < bucket_to_ptr.size(); i++) {
        std::set<int> full_set;
        for (auto ls : bucket_to_ptr[i]) full_set.insert(ptr_to_label[ls].begin(), ptr_to_label[ls].end());
        fprintf(f, "%zu", i*((num_instr-first_instr)/buckets)+first_instr);
        for (auto l : full_set) fprintf(f, " %d", l);
        fprintf(f, "\n");
    }
    fclose(f);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x5, -0x8(%rbp)
je 0x71fa
movq 0x3ddfa(%rip), %rax     # 0x44fd0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x2af61(%rip), %rsi     # 0x32148
movb $0x0, %al
callq 0x64f0
movl $0x1, -0x4(%rbp)
jmp 0x7870
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x6480
movq %rax, 0x3e514(%rip)     # 0x45728
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x6480
movq %rax, 0x3e502(%rip)     # 0x45730
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x6580
movl %eax, 0x3e4df(%rip)     # 0x45720
movslq 0x3e4d8(%rip), %rsi     # 0x45720
leaq 0x3e459(%rip), %rdi     # 0x456a8
callq 0x7eb0
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 0x6670
movq %rax, 0x3e3be(%rip)     # 0x45660
movq 0x3e3b7(%rip), %rax     # 0x45660
movq %rax, 0x3e3c0(%rip)     # 0x45670
movq 0x3e3b9(%rip), %rax     # 0x45670
movl (%rax), %esi
movq 0x3e3b0(%rip), %rax     # 0x45670
movq 0x8(%rax), %rdx
movq 0x3e3a5(%rip), %rax     # 0x45670
movl 0x10(%rax), %ecx
leaq 0x2aea8(%rip), %rdi     # 0x3217d
movb $0x0, %al
callq 0x6040
movq 0x3e37d(%rip), %rax     # 0x45660
movq 0x3e386(%rip), %rcx     # 0x45670
addq 0x8(%rcx), %rax
movq %rax, 0x3e383(%rip)     # 0x45678
movq 0x3e37c(%rip), %rax     # 0x45678
movl (%rax), %eax
movl %eax, 0x3e418(%rip)     # 0x4571c
movslq 0x3e411(%rip), %rdi     # 0x4571c
shlq $0x4, %rdi
callq 0x6470
movq %rax, 0x3e34d(%rip)     # 0x45668
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl 0x3e3eb(%rip), %eax     # 0x4571c
jge 0x7424
movq 0x3e33a(%rip), %rax     # 0x45678
addq $0x4, %rax
movslq -0xac(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq 0x3e30d(%rip), %rax     # 0x45668
movslq -0xac(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl -0xac(%rbp), %eax
addl $0x1, %eax
cmpl 0x3e3a1(%rip), %eax     # 0x4571c
je 0x73d2
movq 0x3e2f4(%rip), %rax     # 0x45678
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 0x3e2c2(%rip), %rax     # 0x45668
movslq -0xac(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
subq (%rax), %rcx
movq 0x3e2aa(%rip), %rax     # 0x45668
movslq -0xac(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x740e
movq 0x3e297(%rip), %rax     # 0x45670
movq 0x8(%rax), %rcx
movq 0x3e284(%rip), %rax     # 0x45668
movslq -0xac(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
subq (%rax), %rcx
movq 0x3e26c(%rip), %rax     # 0x45668
movslq -0xac(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x7410
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x7325
movl $0x54, %edi
callq 0x6200
movl %eax, -0xb0(%rbp)
movslq -0xb0(%rbp), %rdi
shlq $0x3, %rdi
callq 0x6470
movq %rax, -0xb8(%rbp)
movslq -0xb0(%rbp), %rdi
shlq $0x3, %rdi
callq 0x6470
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpl -0xb0(%rbp), %eax
jge 0x7550
movl 0x3e298(%rip), %eax     # 0x4571c
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 0x74cd
movl 0x3e25e(%rip), %eax     # 0x4571c
cltd
idivl -0xb0(%rbp)
movl %eax, -0x178(%rbp)
jmp 0x74ea
movl 0x3e249(%rip), %eax     # 0x4571c
movq -0xc0(%rbp), %rcx
movslq -0xc4(%rbp), %rdx
subl (%rcx,%rdx,8), %eax
movl %eax, -0x178(%rbp)
movl -0x178(%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 -0xb77(%rip), %rdx      # 0x69c0
callq 0x6680
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x746c
leaq -0xd0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x4c2(%rip), %rdx      # 0x70a0
movq %rcx, %rsi
callq 0x6680
movl $0x0, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
cmpl -0xb0(%rbp), %eax
jge 0x75ae
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 0x7574
movq -0xd0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6300
leaq 0x2abea(%rip), %rdi     # 0x321af
leaq 0x2abf3(%rip), %rsi     # 0x321bf
callq 0x6400
movq %rax, -0xe0(%rbp)
leaq 0x2abe2(%rip), %rdi     # 0x321c1
movb $0x0, %al
callq 0x6040
movq $0x0, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x180(%rbp)
leaq 0x3e0a2(%rip), %rdi     # 0x456a8
callq 0x7f40
movq %rax, %rcx
movq -0x180(%rbp), %rax
cmpq %rcx, %rax
jae 0x785d
leaq -0x118(%rbp), %rdi
callq 0x7f70
movq -0xe8(%rbp), %rsi
leaq 0x3e070(%rip), %rdi     # 0x456a8
callq 0x7cc0
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rdi
callq 0x7f90
movq %rax, -0x128(%rbp)
movq -0x120(%rbp), %rdi
callq 0x7fc0
movq %rax, -0x130(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x7ff0
testb $0x1, %al
jne 0x7686
jmp 0x774c
leaq -0x128(%rbp), %rdi
callq 0x8020
movq (%rax), %rax
movq %rax, -0x138(%rbp)
leaq 0x3e01d(%rip), %rdi     # 0x456c0
leaq -0x138(%rbp), %rsi
callq 0x8080
movq %rax, -0x188(%rbp)
jmp 0x76b8
movq -0x188(%rbp), %rdi
callq 0x8160
movq %rax, -0x140(%rbp)
leaq 0x3dfee(%rip), %rdi     # 0x456c0
leaq -0x138(%rbp), %rsi
callq 0x8080
movq %rax, -0x190(%rbp)
jmp 0x76e7
movq -0x190(%rbp), %rdi
callq 0x8190
movq %rax, -0x158(%rbp)
movq -0x140(%rbp), %rsi
movq -0x158(%rbp), %rdx
leaq -0x118(%rbp), %rdi
callq 0x8040
jmp 0x7716
jmp 0x7718
leaq -0x128(%rbp), %rdi
callq 0x81c0
jmp 0x766a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x82d0
jmp 0x787c
movq -0xe0(%rbp), %rdi
movq -0xe8(%rbp), %rax
movq %rax, -0x198(%rbp)
movq 0x3dfc0(%rip), %rax     # 0x45728
subq 0x3dfc1(%rip), %rax     # 0x45730
movslq 0x3dfaa(%rip), %rcx     # 0x45720
xorl %edx, %edx
divq %rcx
movq -0x198(%rbp), %rdx
imulq %rax, %rdx
addq 0x3dfa3(%rip), %rdx     # 0x45730
leaq 0x2aa58(%rip), %rsi     # 0x321ec
movb $0x0, %al
callq 0x64f0
leaq -0x118(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rdi
callq 0x81f0
movq %rax, -0x168(%rbp)
movq -0x160(%rbp), %rdi
callq 0x8220
movq %rax, -0x170(%rbp)
leaq -0x168(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x8250
testb $0x1, %al
jne 0x77e8
jmp 0x7825
leaq -0x168(%rbp), %rdi
callq 0x8280
movl (%rax), %eax
movl %eax, -0x174(%rbp)
movq -0xe0(%rbp), %rdi
movl -0x174(%rbp), %edx
leaq 0x2a9e0(%rip), %rsi     # 0x321f0
movb $0x0, %al
callq 0x64f0
leaq -0x168(%rbp), %rdi
callq 0x82a0
jmp 0x77cf
movq -0xe0(%rbp), %rdi
leaq 0x2a9b7(%rip), %rsi     # 0x321ea
movb $0x0, %al
callq 0x64f0
leaq -0x118(%rbp), %rdi
callq 0x82d0
movq -0xe8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe8(%rbp)
jmp 0x75f1
movq -0xe0(%rbp), %rdi
callq 0x6360
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
movq -0x148(%rbp), %rdi
callq 0x6650
nopl (%rax,%rax)