// File: moyix[P]pandalog_taint_parser/label_pcs.cpp
// Function: process_chunks(void*)
void *process_chunks(void *args) {
    arg *a = (arg *)args;
    unsigned long chunk_size = hdr->chunk_size;
    uint8_t *outbuf = (uint8_t *) malloc(chunk_size);

    for (unsigned int i = a->start; i < a->start+a->size; i++) {
        PlChunk *chunk = &chunks[i];
        unsigned long uncompressed_size = 
            decompress_chunk(&outbuf, plog+chunk->start, &chunk_size);
        int j = 0;
        while (j < uncompressed_size) {
            panda::LogEntry le;
            uint32_t entry_size = *(uint32_t *)(outbuf+j);
            j += 4;
            assert(le.ParseFromString(std::string((const char *)(outbuf+j), entry_size)));
            //if (i > 0) std::cout << le.DebugString() << std::endl;
            assert(le.instr() >= dir->entries[i].instr);
            if (le.has_tainted_instr()) {
                for (auto tq : le.tainted_instr().taint_query()) {
                    //printf("(%zu, %zu, %zu) %zu %u\n", dir->entries[i].instr, dir->entries[i].start, dir->entries[i].num_entries, le.instr(), tq.tcn());
                    if (tq.has_unique_label_set()) {
                        auto uls = tq.unique_label_set();
                        pthread_mutex_lock(&maplock);
                        std::copy(
                            uls.label().begin(),
                            uls.label().end(),
                            std::back_inserter(ptr_to_label[uls.ptr()])
                        );
                        pthread_mutex_unlock(&maplock);
                    }
                    // Can we get rid of this lock in favor of an atomic?
                    pthread_mutex_lock(&tcn_insert_lock);
                    ptr_to_pc[tq.ptr()].insert(le.pc());
                    pthread_mutex_unlock(&tcn_insert_lock);

                }
            }
            j += entry_size;
        }
        processed++;
    }
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2c0, %rsp            # imm = 0x2C0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq 0x40ca2(%rip), %rax     # 0x47660
movl 0x10(%rax), %eax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6470
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
movl (%rcx), %ecx
movq -0x10(%rbp), %rdx
addl 0x4(%rdx), %ecx
cmpl %ecx, %eax
jae 0x6f82
movq 0x40c5e(%rip), %rax     # 0x47658
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq 0x40c41(%rip), %rsi     # 0x47650
movq -0x30(%rbp), %rax
addq (%rax), %rsi
leaq -0x20(%rbp), %rdi
leaq -0x18(%rbp), %rdx
callq 0x68f0
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movslq -0x3c(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x6f66
leaq -0xf8(%rbp), %rdi
callq 0x7860
movq -0x20(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx), %eax
movl %eax, -0xfc(%rbp)
movl -0x3c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movslq -0x3c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x200(%rbp)
movl -0xfc(%rbp), %eax
movq %rax, -0x1f8(%rbp)
leaq -0x121(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x6640
movq -0x200(%rbp), %rsi
movq -0x1f8(%rbp), %rdx
movq -0x1f0(%rbp), %rcx
leaq -0x120(%rbp), %rdi
callq 0x63b0
jmp 0x6ab7
leaq -0xf8(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x6670
movb %al, -0x201(%rbp)
jmp 0x6ad2
movb -0x201(%rbp), %al
testb $0x1, %al
jne 0x6ade
jmp 0x6ae0
jmp 0x6b36
leaq 0x2c550(%rip), %rdi     # 0x33037
leaq 0x2c58f(%rip), %rsi     # 0x3307d
movl $0x74, %edx
leaq 0x2c591(%rip), %rcx     # 0x3308b
callq 0x6210
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
jmp 0x6ba5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x61e0
jmp 0x6ba5
leaq -0x120(%rbp), %rdi
callq 0x61e0
leaq -0x121(%rbp), %rdi
callq 0x63a0
leaq -0xf8(%rbp), %rdi
callq 0x7880
movq %rax, -0x210(%rbp)
jmp 0x6b63
movq -0x210(%rbp), %rax
movq 0x40af7(%rip), %rcx     # 0x47668
addq $0x4, %rcx
movl -0x24(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jb 0x6b86
jmp 0x6bcd
leaq 0x2c51b(%rip), %rdi     # 0x330a8
leaq 0x2c4e9(%rip), %rsi     # 0x3307d
movl $0x76, %edx
leaq 0x2c4eb(%rip), %rcx     # 0x3308b
callq 0x6210
leaq -0x121(%rbp), %rdi
callq 0x63a0
jmp 0x6f58
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
jmp 0x6f58
leaq -0xf8(%rbp), %rdi
callq 0x78a0
movb %al, -0x211(%rbp)
jmp 0x6be1
movb -0x211(%rbp), %al
testb $0x1, %al
jne 0x6bf0
jmp 0x6f3b
leaq -0xf8(%rbp), %rdi
callq 0x78c0
movq %rax, -0x220(%rbp)
jmp 0x6c05
movq -0x220(%rbp), %rdi
callq 0x78e0
movq %rax, -0x228(%rbp)
jmp 0x6c1a
movq -0x228(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rdi
callq 0x7900
movq %rax, -0x230(%rbp)
jmp 0x6c3d
movq -0x230(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x140(%rbp), %rdi
callq 0x7940
movq %rax, -0x238(%rbp)
jmp 0x6c60
movq -0x238(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0x148(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x79a0
movb %al, -0x239(%rbp)
jmp 0x6c89
movb -0x239(%rbp), %al
testb $0x1, %al
jne 0x6c98
jmp 0x6f39
leaq -0x148(%rbp), %rdi
callq 0x79d0
movq %rax, -0x248(%rbp)
jmp 0x6cad
movq -0x248(%rbp), %rsi
leaq -0x180(%rbp), %rdi
callq 0x18dd0
jmp 0x6cc2
leaq -0x180(%rbp), %rdi
callq 0x79f0
movb %al, -0x249(%rbp)
jmp 0x6cd6
movb -0x249(%rbp), %al
testb $0x1, %al
jne 0x6ce5
jmp 0x6e56
leaq -0x180(%rbp), %rdi
callq 0x7a10
movq %rax, -0x258(%rbp)
jmp 0x6cfa
movq -0x258(%rbp), %rsi
leaq -0x1b0(%rbp), %rdi
callq 0x16930
jmp 0x6d0f
leaq 0x409e2(%rip), %rdi     # 0x476f8
callq 0x6510
leaq -0x1b0(%rbp), %rdi
callq 0x7a80
movq %rax, -0x260(%rbp)
jmp 0x6d30
movq -0x260(%rbp), %rdi
callq 0x62d0
movq %rax, -0x268(%rbp)
jmp 0x6d45
leaq -0x1b0(%rbp), %rdi
callq 0x7a80
movq %rax, -0x270(%rbp)
jmp 0x6d5a
movq -0x270(%rbp), %rdi
callq 0x6680
movq %rax, -0x278(%rbp)
jmp 0x6d6f
leaq -0x1b0(%rbp), %rdi
callq 0x7bb0
movq %rax, -0x280(%rbp)
jmp 0x6d84
movq -0x280(%rbp), %rax
movq %rax, -0x1c0(%rbp)
leaq 0x4092f(%rip), %rdi     # 0x476c8
leaq -0x1c0(%rbp), %rsi
callq 0x7ad0
movq %rax, -0x288(%rbp)
jmp 0x6dae
movq -0x288(%rbp), %rdi
callq 0x7aa0
movq %rax, -0x290(%rbp)
jmp 0x6dc3
movq -0x278(%rbp), %rsi
movq -0x268(%rbp), %rdi
movq -0x290(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rdx
callq 0x7a30
movq %rax, -0x298(%rbp)
jmp 0x6df4
movq -0x298(%rbp), %rax
movq %rax, -0x1c8(%rbp)
leaq 0x408ef(%rip), %rdi     # 0x476f8
callq 0x6260
leaq -0x1b0(%rbp), %rdi
callq 0x16ac0
jmp 0x6e56
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
jmp 0x6f2b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x16ac0
jmp 0x6f2b
leaq 0x40843(%rip), %rdi     # 0x476a0
callq 0x6510
leaq -0x180(%rbp), %rdi
callq 0x7cb0
movq %rax, -0x2a0(%rbp)
jmp 0x6e77
movq -0x2a0(%rbp), %rax
movq %rax, -0x1d0(%rbp)
leaq 0x407e4(%rip), %rdi     # 0x47670
leaq -0x1d0(%rbp), %rsi
callq 0x7bd0
movq %rax, -0x2a8(%rbp)
jmp 0x6ea1
leaq -0xf8(%rbp), %rdi
callq 0x7d20
movq %rax, -0x2b0(%rbp)
jmp 0x6eb6
movq -0x2a8(%rbp), %rdi
movq -0x2b0(%rbp), %rax
movq %rax, -0x1d8(%rbp)
leaq -0x1d8(%rbp), %rsi
callq 0x7cd0
movb %dl, -0x2b9(%rbp)
movq %rax, -0x2b8(%rbp)
jmp 0x6ee6
movb -0x2b9(%rbp), %al
movq -0x2b8(%rbp), %rcx
movq %rcx, -0x1e8(%rbp)
movb %al, -0x1e0(%rbp)
leaq 0x40799(%rip), %rdi     # 0x476a0
callq 0x6260
leaq -0x180(%rbp), %rdi
callq 0x18fc0
leaq -0x148(%rbp), %rdi
callq 0x7d40
jmp 0x6f26
jmp 0x6c6e
leaq -0x180(%rbp), %rdi
callq 0x18fc0
jmp 0x6f58
jmp 0x6f3b
movl -0xfc(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x22600
jmp 0x6a2e
leaq -0xf8(%rbp), %rdi
callq 0x22600
jmp 0x6f8d
leaq 0x407b3(%rip), %rdi     # 0x47720
xorl %esi, %esi
callq 0x7d60
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x69db
xorl %eax, %eax
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbp
retq
movq -0x130(%rbp), %rdi
callq 0x6630
nopl (%rax)