// File: /workspace/github_c_makefile_star2_O2/buptlxb[P]xRay/src/basic_block.cpp
// Function: BasicBlock::disasm()
void BasicBlock::disasm()
{
    if (isDisasmed())
        return;
    setDisasmed(true);

    DISASM MyDisasm = insts[0];
    insts.pop_back();
    //printf("\t\tStart BasicBlock::disasm 0x%llx\n", (ADDR)this);
    while (1){
        // Fix SecurityBlock 
        MyDisasm.SecurityBlock = func->getEnd() - MyDisasm.EIP;
        // disasm
        int len = Disasm(&MyDisasm);
        if (len == OUT_OF_BLOCK) {
            puts("\t\t\tOUT_OF_BLOCK : Disasm finished");
            return;
        }else if (len == UNKNOWN_OPCODE) {
            fprintf(stderr, "unknown opcode\n");
            return;
        }else {
            if((MyDisasm.Instruction.Category&0xffff)==CONTROL_TRANSFER && MyDisasm.Instruction.BranchType) {
                switch (MyDisasm.Instruction.BranchType) {
                    case CallType:
                        break;
                    case JO:
                    case JC:
                    case JE:
                    case JA:
                    case JS:
                    case JP:
                    case JL:
                    case JG:
                    //case JB:
                    case JECXZ:
                    case JNO:
                    case JNC:
                    case JNE:
                    case JNA:
                    case JNS:
                    case JNP:
                    case JNL:
                    case JNG:
                    //case JNB:
                        {
                            ADDR mAddr = MyDisasm.EIP + len;
                            if ((ADDR)-1 != func->getVirAddr(mAddr)) {
                                BasicBlock *succ = func->findBasicBlock(mAddr);
                                addSuccessor(succ);
                                succ->addPredecessor(this);
                            }
                        }
                    case JmpType:
                        if (MyDisasm.Instruction.AddrValue != 0) {
                            ADDR mAddr = func->getMapAddr(MyDisasm.Instruction.AddrValue);
                            if ((ADDR)-1 != mAddr) {
                                BasicBlock *succ = func->findBasicBlock(mAddr);
                                addSuccessor(succ);
                                succ->addPredecessor(this);
                            }
                        }
                    case RetType:
                        //puts("\t\t\tCONTROL_TRANSFER : Disasm finished");
                        //char buf[1024];
                        //printf("\t\t%s\n\n", toString(buf, sizeof(buf)));
                        insts.push_back(MyDisasm);
                        return;
                    default:
                        fprintf(stderr, "unknown branchtype %d (0x%llx) in Basic Block 0x%llx\n", MyDisasm.Instruction.BranchType, (ADDR)MyDisasm.VirtualAddr, getFirstInstAddr());
                        return;
                }
            }

            insts.push_back(MyDisasm);
            MyDisasm.EIP = MyDisasm.EIP + len;
            MyDisasm.VirtualAddr = MyDisasm.VirtualAddr + len;
	
            if(MyDisasm.EIP >= func->getEnd()){
                //puts("\t\t\tFUNCTION END : Disasm finished");
                char buf[1024];
                //printf("\t\t%s\n\n", toString(buf, sizeof(buf)));
                return;
            }
        }
    }
}

// Assembly:
cmpb $0x0, 0x58(%rdi)
jne 0x8655
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rdi, %rbx
movb $0x1, 0x58(%rdi)
movq (%rdi), %rsi
leaq 0x8(%rsp), %r14
movl $0x28d, %edx            # imm = 0x28D
movq %r14, %rdi
callq 0x8140
addq $-0x28d, 0x8(%rbx)      # imm = 0xFD73
movq 0x50(%rbx), %rax
movq 0x8(%rax), %rcx
movq (%r14), %rax
leaq 0x21acc(%rip), %r12     # 0x2a004
subl %eax, %ecx
movl %ecx, 0x18(%rsp)
movq %r14, %rdi
callq 0x29136
testl %eax, %eax
je 0x8656
cmpl $-0x1, %eax
je 0x8664
cmpw $0x6, 0x68(%rsp)
sete %cl
movl 0x80(%rsp), %edx
testl %edx, %edx
setne %sil
andb %cl, %sil
movslq %eax, %r15
cmpb $0x1, %sil
jne 0x858e
leal 0x8(%rdx), %eax
cmpl $0x15, %eax
ja 0x8682
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq %rbx, %rdi
movq %r14, %rsi
callq 0x88f4
movq 0x8(%rsp), %rax
addq %r15, %rax
movq %rax, 0x8(%rsp)
addq %r15, 0x10(%rsp)
movq 0x50(%rbx), %rcx
movq 0x8(%rcx), %rcx
cmpq %rcx, %rax
jb 0x8538
jmp 0x8647
addq 0x8(%rsp), %r15
movq 0x50(%rbx), %rdi
movq %r15, %rsi
callq 0x98e4
cmpq $-0x1, %rax
je 0x85f9
movq 0x50(%rbx), %rdi
movq %r15, %rsi
callq 0x9a5e
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x86a8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x86be
movq 0x90(%rsp), %rsi
testq %rsi, %rsi
je 0x863a
movq 0x50(%rbx), %rdi
callq 0x98fa
cmpq $-0x1, %rax
je 0x863a
movq 0x50(%rbx), %rdi
movq %rax, %rsi
callq 0x9a5e
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x86a8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x86be
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x88f4
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x219ff(%rip), %rdi     # 0x2a05c
callq 0x8250
jmp 0x8647
movq 0x2d965(%rip), %rax     # 0x35fd0
movq (%rax), %rcx
leaq 0x21a09(%rip), %rdi     # 0x2a07e
pushq $0xf
popq %rsi
pushq $0x1
popq %rdx
callq 0x82e0
jmp 0x8647
movq 0x2d947(%rip), %rax     # 0x35fd0
movq (%rax), %rdi
movq 0x10(%rsp), %rcx
movq (%rbx), %rax
movq 0x8(%rax), %r8
leaq 0x219ef(%rip), %rsi     # 0x2a08e
xorl %eax, %eax
callq 0x8220
jmp 0x8647