// File: /workspace/github_c_makefile_star2_O1/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 0x8897
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
leaq 0x248f0(%rip), %r15     # 0x2d004
movq 0x50(%rbx), %rax
movl 0x8(%rax), %eax
subl 0x8(%rsp), %eax
movl %eax, 0x18(%rsp)
movq %r14, %rdi
callq 0x2bab4
testl %eax, %eax
je 0x887e
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x8898
cmpw $0x6, 0x68(%rsp)
sete %al
movl 0x80(%rsp), %edx
testl %edx, %edx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x876f
leal 0x8(%rdx), %eax
cmpl $0x15, %eax
ja 0x889f
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x8793
movl $0x28d, %edx            # imm = 0x28D
movq %rsi, %rdi
movq %r14, %rsi
callq 0x8120
addq $0x28d, 0x8(%rbx)       # imm = 0x28D
jmp 0x879e
movq %rbx, %rdi
movq %r14, %rdx
callq 0x8b7c
movslq %ebp, %rax
movq 0x8(%rsp), %rcx
addq %rax, %rcx
movq %rcx, 0x8(%rsp)
addq %rax, 0x10(%rsp)
movq 0x50(%rbx), %rax
cmpq 0x8(%rax), %rcx
jb 0x8714
jmp 0x888a
movslq %ebp, %r14
addq 0x8(%rsp), %r14
movq 0x50(%rbx), %rdi
movq %r14, %rsi
callq 0x9a76
cmpq $-0x1, %rax
je 0x8805
movq 0x50(%rbx), %rdi
movq %r14, %rsi
callq 0x9ca4
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x88c6
movq %r14, %rdi
movq %rbx, %rsi
callq 0x88f0
movq 0x90(%rsp), %rsi
testq %rsi, %rsi
je 0x8846
movq 0x50(%rbx), %rdi
callq 0x9a90
cmpq $-0x1, %rax
je 0x8846
movq 0x50(%rbx), %rdi
movq %rax, %rsi
callq 0x9ca4
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x88c6
movq %r14, %rdi
movq %rbx, %rsi
callq 0x88f0
movq 0x8(%rbx), %rax
cmpq 0x10(%rbx), %rax
je 0x886c
leaq 0x8(%rsp), %rsi
movl $0x28d, %edx            # imm = 0x28D
movq %rax, %rdi
callq 0x8120
addq $0x28d, 0x8(%rbx)       # imm = 0x28D
jmp 0x888a
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8b7c
jmp 0x888a
leaq 0x247d7(%rip), %rdi     # 0x2d05c
callq 0x8220
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x8309
jmp 0x888a
movq 0x2f72a(%rip), %rax     # 0x37fd0
movq (%rax), %rdi
movq 0x10(%rsp), %rcx
movq (%rbx), %rax
movq 0x8(%rax), %r8
leaq 0x247d2(%rip), %rsi     # 0x2d08e
xorl %eax, %eax
callq 0x81f0
jmp 0x888a
nop