// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %rbx
cmpl $0x5, %edi
jne 0x70b3
movq 0x10(%rbx), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
xorl %edx, %edx
callq 0x6410
movq %rax, 0x1b980(%rip)     # 0x226c8
movq 0x18(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x6410
movq %rax, 0x1b974(%rip)     # 0x226d0
movq 0x20(%rbx), %rdi
callq 0x6510
movl %eax, 0x1b955(%rip)     # 0x226c0
movslq %eax, %rsi
leaq 0x1b8d3(%rip), %rdi     # 0x22648
callq 0x7168
movq 0x8(%rbx), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x6230
movl %eax, %ebp
movq 0x8(%rbx), %rdi
leaq 0x58(%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 0x65d0
movq %rax, 0x1b848(%rip)     # 0x22600
movq %rax, 0x1b851(%rip)     # 0x22610
movl (%rax), %esi
movq 0x8(%rax), %rdx
movl 0x10(%rax), %ecx
leaq 0xe39a(%rip), %rdi      # 0x15169
xorl %eax, %eax
callq 0x6030
movq 0x1b823(%rip), %rax     # 0x22600
movq 0x1b82c(%rip), %rcx     # 0x22610
movq 0x8(%rcx), %rbx
leaq (%rax,%rbx), %rcx
movq %rcx, 0x1b825(%rip)     # 0x22618
leaq 0xc(%rax,%rbx), %r15
movl -0xc(%r15), %r12d
movl %r12d, 0x1b8b9(%rip)    # 0x226bc
movslq %r12d, %r13
movq %r13, %rdi
shlq $0x4, %rdi
callq 0x6400
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
movq %rax, 0x1b7e3(%rip)     # 0x22608
shlq $0x4, %rcx
shlq $0x4, %r12
addq $-0x10, %r12
cmpq %r14, %rcx
je 0x6e5b
movq (%r15), %rdx
movq %rdx, (%rax,%r14)
movq %rbx, %rsi
cmpq %r14, %r12
je 0x6e49
movq 0x18(%r15), %rsi
subq %rdx, %rsi
movq %rsi, 0x8(%rax,%r14)
addq $0x10, %r14
addq $0x18, %r15
jmp 0x6e31
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 0x6400
movq %rax, %rbp
movq %r15, %rdi
callq 0x6400
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, 0x10(%rsp)
cmpq %r14, %r12
je 0x6f02
movl 0x1b7f9(%rip), %ecx     # 0x226bc
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 -0x65e(%rip), %rdx      # 0x688f
movq %r13, %rcx
callq 0x65f0
incq %r14
addq $0x8, %rbp
addq $0x8, %r13
jmp 0x6eb8
leaq -0x269(%rip), %rdx      # 0x6ca0
xorl %ebx, %ebx
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x65f0
movq 0x10(%rsp), %r14
cmpq %rbx, %r12
je 0x6f33
movq (%r14,%rbx,8), %rdi
xorl %esi, %esi
callq 0x62c0
incq %rbx
jmp 0x6f1e
movq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x62c0
leaq 0xe255(%rip), %rdi      # 0x1519b
leaq 0xe25e(%rip), %rsi      # 0x151ab
callq 0x6390
movq %rax, %rbx
leaq 0xe32e(%rip), %rdi      # 0x1528a
callq 0x6500
leaq 0x30(%rsp), %r15
leaq 0x1b6f3(%rip), %r13     # 0x22660
leaq 0x18(%rsp), %r14
xorl %edi, %edi
movq 0x1b6d5(%rip), %rax     # 0x22650
movq 0x1b6c6(%rip), %rcx     # 0x22648
subq %rcx, %rax
cqto
pushq $0x30
popq %rsi
idivq %rsi
cmpq %rax, %rdi
jae 0x7097
andl $0x0, 0x30(%rsp)
andq $0x0, 0x38(%rsp)
movq %r15, 0x40(%rsp)
movq %r15, 0x48(%rsp)
andq $0x0, 0x50(%rsp)
movq %rdi, 0x8(%rsp)
imulq $0x30, %rdi, %rax
leaq (%rcx,%rax), %r15
addq $0x8, %r15
movq 0x10(%r15), %rbp
cmpq %r15, %rbp
je 0x700b
movq 0x20(%rbp), %rax
movq %rax, 0x18(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x719e
movq (%rax), %r12
movq %r13, %rdi
movq %r14, %rsi
callq 0x719e
movq 0x8(%rax), %rdx
leaq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x7e1a
movq %rbp, %rdi
callq 0x6370
movq %rax, %rbp
jmp 0x6fc6
movq 0x1b6b6(%rip), %rax     # 0x226c8
movq 0x1b6b7(%rip), %rcx     # 0x226d0
subq %rcx, %rax
movslq 0x1b69d(%rip), %rsi     # 0x226c0
xorl %edx, %edx
divq %rsi
imulq 0x8(%rsp), %rax
addq %rax, %rcx
movq %rbx, %rdi
leaq 0xe172(%rip), %rsi      # 0x151ad
movq %rcx, %rdx
xorl %eax, %eax
callq 0x6480
movq 0x40(%rsp), %rbp
leaq 0x30(%rsp), %r15
cmpq %r15, %rbp
je 0x7075
movl 0x20(%rbp), %edx
movq %rbx, %rdi
leaq 0xe150(%rip), %rsi      # 0x151b1
xorl %eax, %eax
callq 0x6480
movq %rbp, %rdi
callq 0x6370
movq %rax, %rbp
jmp 0x704f
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x63d0
leaq 0x28(%rsp), %rdi
callq 0x738c
movq 0x8(%rsp), %rdi
incq %rdi
jmp 0x6f74
movq %rbx, %rdi
callq 0x6330
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1af16(%rip), %rax     # 0x21fd0
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0xe06d(%rip), %rsi      # 0x15134
xorl %eax, %eax
callq 0x6480
pushq $0x1
popq %rax
jmp 0x70a1
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x738c
movq %rbx, %rdi
callq 0x65c0