// File: moyix[P]pandalog_taint_parser/label_instr.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);
                    int index = ((le.instr()-first_instr)/((double)(num_instr-first_instr)))*buckets;
                    assert(le.instr() < num_instr);
                    assert(index < buckets);
                    bucket_to_ptr[index].insert(tq.ptr());
                    pthread_mutex_unlock(&tcn_insert_lock);

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2d0, %rsp            # imm = 0x2D0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq 0x3ec92(%rip), %rax     # 0x45670
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 0x707b
movq 0x3ec4e(%rip), %rax     # 0x45668
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq 0x3ec31(%rip), %rsi     # 0x45660
movq -0x30(%rbp), %rax
addq (%rax), %rsi
leaq -0x20(%rbp), %rdi
leaq -0x18(%rbp), %rdx
callq 0x6910
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movslq -0x3c(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x705f
leaq -0xf8(%rbp), %rdi
callq 0x7950
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 0x6660
movq -0x200(%rbp), %rsi
movq -0x1f8(%rbp), %rdx
movq -0x1f0(%rbp), %rcx
leaq -0x120(%rbp), %rdi
callq 0x63b0
jmp 0x6ad7
leaq -0xf8(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x6690
movb %al, -0x201(%rbp)
jmp 0x6af2
movb -0x201(%rbp), %al
testb $0x1, %al
jne 0x6afe
jmp 0x6b00
jmp 0x6b56
leaq 0x2b55c(%rip), %rdi     # 0x32063
leaq 0x2b59b(%rip), %rsi     # 0x320a9
movl $0x78, %edx
leaq 0x2b59f(%rip), %rcx     # 0x320b9
callq 0x6210
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
jmp 0x6bc5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x61e0
jmp 0x6bc5
leaq -0x120(%rbp), %rdi
callq 0x61e0
leaq -0x121(%rbp), %rdi
callq 0x63a0
leaq -0xf8(%rbp), %rdi
callq 0x7970
movq %rax, -0x210(%rbp)
jmp 0x6b83
movq -0x210(%rbp), %rax
movq 0x3eae7(%rip), %rcx     # 0x45678
addq $0x4, %rcx
movl -0x24(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jb 0x6ba6
jmp 0x6bed
leaq 0x2b529(%rip), %rdi     # 0x320d6
leaq 0x2b4f5(%rip), %rsi     # 0x320a9
movl $0x7a, %edx
leaq 0x2b4f9(%rip), %rcx     # 0x320b9
callq 0x6210
leaq -0x121(%rbp), %rdi
callq 0x63a0
jmp 0x7051
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
jmp 0x7051
leaq -0xf8(%rbp), %rdi
callq 0x7990
movb %al, -0x211(%rbp)
jmp 0x6c01
movb -0x211(%rbp), %al
testb $0x1, %al
jne 0x6c10
jmp 0x7034
leaq -0xf8(%rbp), %rdi
callq 0x79b0
movq %rax, -0x220(%rbp)
jmp 0x6c25
movq -0x220(%rbp), %rdi
callq 0x79d0
movq %rax, -0x228(%rbp)
jmp 0x6c3a
movq -0x228(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rdi
callq 0x79f0
movq %rax, -0x230(%rbp)
jmp 0x6c5d
movq -0x230(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x140(%rbp), %rdi
callq 0x7a30
movq %rax, -0x238(%rbp)
jmp 0x6c80
movq -0x238(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0x148(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x7a90
movb %al, -0x239(%rbp)
jmp 0x6ca9
movb -0x239(%rbp), %al
testb $0x1, %al
jne 0x6cb8
jmp 0x7032
leaq -0x148(%rbp), %rdi
callq 0x7ac0
movq %rax, -0x248(%rbp)
jmp 0x6ccd
movq -0x248(%rbp), %rsi
leaq -0x180(%rbp), %rdi
callq 0x17370
jmp 0x6ce2
leaq -0x180(%rbp), %rdi
callq 0x7ae0
movb %al, -0x249(%rbp)
jmp 0x6cf6
movb -0x249(%rbp), %al
testb $0x1, %al
jne 0x6d05
jmp 0x6e76
leaq -0x180(%rbp), %rdi
callq 0x7b00
movq %rax, -0x258(%rbp)
jmp 0x6d1a
movq -0x258(%rbp), %rsi
leaq -0x1b0(%rbp), %rdi
callq 0x14ed0
jmp 0x6d2f
leaq 0x3e9ba(%rip), %rdi     # 0x456f0
callq 0x6520
leaq -0x1b0(%rbp), %rdi
callq 0x7b70
movq %rax, -0x260(%rbp)
jmp 0x6d50
movq -0x260(%rbp), %rdi
callq 0x62d0
movq %rax, -0x268(%rbp)
jmp 0x6d65
leaq -0x1b0(%rbp), %rdi
callq 0x7b70
movq %rax, -0x270(%rbp)
jmp 0x6d7a
movq -0x270(%rbp), %rdi
callq 0x66a0
movq %rax, -0x278(%rbp)
jmp 0x6d8f
leaq -0x1b0(%rbp), %rdi
callq 0x7ca0
movq %rax, -0x280(%rbp)
jmp 0x6da4
movq -0x280(%rbp), %rax
movq %rax, -0x1c0(%rbp)
leaq 0x3e907(%rip), %rdi     # 0x456c0
leaq -0x1c0(%rbp), %rsi
callq 0x7bc0
movq %rax, -0x288(%rbp)
jmp 0x6dce
movq -0x288(%rbp), %rdi
callq 0x7b90
movq %rax, -0x290(%rbp)
jmp 0x6de3
movq -0x278(%rbp), %rsi
movq -0x268(%rbp), %rdi
movq -0x290(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rdx
callq 0x7b20
movq %rax, -0x298(%rbp)
jmp 0x6e14
movq -0x298(%rbp), %rax
movq %rax, -0x1c8(%rbp)
leaq 0x3e8c7(%rip), %rdi     # 0x456f0
callq 0x6260
leaq -0x1b0(%rbp), %rdi
callq 0x15060
jmp 0x6e76
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
jmp 0x7024
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x15060
jmp 0x7024
leaq 0x3e803(%rip), %rdi     # 0x45680
callq 0x6520
leaq -0xf8(%rbp), %rdi
callq 0x7970
movq %rax, -0x2a0(%rbp)
jmp 0x6e97
movq -0x2a0(%rbp), %rax
movq 0x3e88b(%rip), %rcx     # 0x45730
subq %rcx, %rax
movq %rax, %xmm0
movaps 0x2b15c(%rip), %xmm3    # 0x32010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x2b160(%rip), %xmm2    # 0x32020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq 0x3e852(%rip), %rax     # 0x45728
subq %rcx, %rax
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
cvtsi2sdl 0x3e823(%rip), %xmm1 # 0x45720
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x1cc(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x7970
movq %rax, -0x2a8(%rbp)
jmp 0x6f20
movq -0x2a8(%rbp), %rax
cmpq 0x3e7fa(%rip), %rax     # 0x45728
jae 0x6f32
jmp 0x6f51
leaq 0x2b1c1(%rip), %rdi     # 0x320fa
leaq 0x2b169(%rip), %rsi     # 0x320a9
movl $0x8b, %edx
leaq 0x2b16d(%rip), %rcx     # 0x320b9
callq 0x6210
movl -0x1cc(%rbp), %eax
cmpl 0x3e7c3(%rip), %eax     # 0x45720
jge 0x6f61
jmp 0x6f80
leaq 0x2b1a9(%rip), %rdi     # 0x32111
leaq 0x2b13a(%rip), %rsi     # 0x320a9
movl $0x8c, %edx
leaq 0x2b13e(%rip), %rcx     # 0x320b9
callq 0x6210
movslq -0x1cc(%rbp), %rsi
leaq 0x3e71a(%rip), %rdi     # 0x456a8
callq 0x7cc0
movq %rax, -0x2b8(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x7d30
movq %rax, -0x2b0(%rbp)
jmp 0x6faf
movq -0x2b8(%rbp), %rdi
movq -0x2b0(%rbp), %rax
movq %rax, -0x1d8(%rbp)
leaq -0x1d8(%rbp), %rsi
callq 0x7ce0
movb %dl, -0x2c1(%rbp)
movq %rax, -0x2c0(%rbp)
jmp 0x6fdf
movb -0x2c1(%rbp), %al
movq -0x2c0(%rbp), %rcx
movq %rcx, -0x1e8(%rbp)
movb %al, -0x1e0(%rbp)
leaq 0x3e680(%rip), %rdi     # 0x45680
callq 0x6260
leaq -0x180(%rbp), %rdi
callq 0x17560
leaq -0x148(%rbp), %rdi
callq 0x7d50
jmp 0x701f
jmp 0x6c8e
leaq -0x180(%rbp), %rdi
callq 0x17560
jmp 0x7051
jmp 0x7034
movl -0xfc(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x20ba0
jmp 0x6a4e
leaq -0xf8(%rbp), %rdi
callq 0x20ba0
jmp 0x7086
leaq 0x3e6b2(%rip), %rdi     # 0x45718
xorl %esi, %esi
callq 0x7d70
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x69fb
xorl %eax, %eax
addq $0x2d0, %rsp            # imm = 0x2D0
popq %rbp
retq
movq -0x130(%rbp), %rdi
callq 0x6650
nopw %cs:(%rax,%rax)