// File: /workspace/github_c_makefile_star2_O3/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 0x8876
pushq %rbp
pushq %r15
pushq %r14
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 0x8120
addq $-0x28d, 0x8(%rbx)      # imm = 0xFD73
movq 0x50(%rbx), %rax
movq 0x8(%rax), %rcx
movq (%r14), %rax
leaq 0x2490b(%rip), %r15     # 0x2d004
subl %eax, %ecx
movl %ecx, 0x18(%rsp)
movq %r14, %rdi
callq 0x2b364
testl %eax, %eax
je 0x885d
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x8877
cmpw $0x6, 0x68(%rsp)
sete %al
movl 0x80(%rsp), %edx
testl %edx, %edx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x874b
leal 0x8(%rdx), %eax
cmpl $0x15, %eax
ja 0x887e
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x876f
movl $0x28d, %edx            # imm = 0x28D
movq %rsi, %rdi
movq %r14, %rsi
callq 0x8120
addq $0x28d, 0x8(%rbx)       # imm = 0x28D
jmp 0x877a
movq %rbx, %rdi
movq %r14, %rdx
callq 0x8b56
movslq %ebp, %rcx
movq 0x8(%rsp), %rax
addq %rcx, %rax
movq %rax, 0x8(%rsp)
addq %rcx, 0x10(%rsp)
movq 0x50(%rbx), %rcx
movq 0x8(%rcx), %rcx
cmpq %rcx, %rax
jb 0x86f9
jmp 0x8869
movslq %ebp, %r14
addq 0x8(%rsp), %r14
movq 0x50(%rbx), %rdi
movq %r14, %rsi
callq 0x99fa
cmpq $-0x1, %rax
je 0x87e4
movq 0x50(%rbx), %rdi
movq %r14, %rsi
callq 0x9c3a
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x88a4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x88ce
movq 0x90(%rsp), %rsi
testq %rsi, %rsi
je 0x8825
movq 0x50(%rbx), %rdi
callq 0x9a14
cmpq $-0x1, %rax
je 0x8825
movq 0x50(%rbx), %rdi
movq %rax, %rsi
callq 0x9c3a
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x88a4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x88ce
movq 0x8(%rbx), %rax
cmpq 0x10(%rbx), %rax
je 0x884b
leaq 0x8(%rsp), %rsi
movl $0x28d, %edx            # imm = 0x28D
movq %rax, %rdi
callq 0x8120
addq $0x28d, 0x8(%rbx)       # imm = 0x28D
jmp 0x8869
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8b56
jmp 0x8869
leaq 0x247f8(%rip), %rdi     # 0x2d05c
callq 0x8220
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x8309
jmp 0x8869
movq 0x2f74b(%rip), %rax     # 0x37fd0
movq (%rax), %rdi
movq 0x10(%rsp), %rcx
movq (%rbx), %rax
movq 0x8(%rax), %r8
leaq 0x247f3(%rip), %rsi     # 0x2d08e
xorl %eax, %eax
callq 0x81f0
jmp 0x8869