// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0x6f0, %rsp            # imm = 0x6F0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x6c8(%rbp)
callq 0x90e0
testb $0x1, %al
jne 0x8655
jmp 0x865a
jmp 0x8959
movq -0x6c8(%rbp), %rdi
movl $0x1, %esi
callq 0x9100
movq -0x6c8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x9120
movq %rax, %rsi
leaq -0x295(%rbp), %rdi
movl $0x28d, %edx            # imm = 0x28D
callq 0x8180
movq -0x6c8(%rbp), %rdi
callq 0x9140
movq -0x6c8(%rbp), %rax
movq 0x50(%rax), %rdi
callq 0x9170
subq -0x295(%rbp), %rax
movl %eax, -0x285(%rbp)
leaq -0x295(%rbp), %rdi
callq 0x4ec50
movl %eax, -0x29c(%rbp)
cmpl $0x0, -0x29c(%rbp)
jne 0x86e4
leaq 0x48982(%rip), %rdi     # 0x5105c
callq 0x82d0
jmp 0x8959
cmpl $-0x1, -0x29c(%rbp)
jne 0x870a
movq 0x598dc(%rip), %rax     # 0x61fd0
movq (%rax), %rdi
leaq 0x48980(%rip), %rsi     # 0x5107e
movb $0x0, %al
callq 0x82a0
jmp 0x8959
movl -0x235(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cmpl $0x6, %eax
jne 0x88de
cmpl $0x0, -0x21d(%rbp)
je 0x88de
movl -0x21d(%rbp), %eax
addl $0x8, %eax
movl %eax, %ecx
movq %rcx, -0x6d0(%rbp)
subl $0x15, %eax
ja 0x887e
movq -0x6d0(%rbp), %rax
leaq 0x488b0(%rip), %rcx     # 0x51004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x88dc
movq -0x6c8(%rbp), %rax
movq -0x295(%rbp), %rcx
movslq -0x29c(%rbp), %rdx
addq %rdx, %rcx
movq %rcx, -0x2a8(%rbp)
movq 0x50(%rax), %rdi
movq -0x2a8(%rbp), %rsi
callq 0xe950
movq %rax, %rcx
movq $-0x1, %rax
cmpq %rcx, %rax
je 0x87e4
movq -0x6c8(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x2a8(%rbp), %rsi
callq 0xecf0
movq -0x6c8(%rbp), %rdi
movq %rax, -0x2b0(%rbp)
movq -0x2b0(%rbp), %rsi
callq 0x8970
movq -0x6c8(%rbp), %rsi
movq -0x2b0(%rbp), %rdi
callq 0x89a0
jmp 0x87e6
cmpq $0x0, -0x20d(%rbp)
je 0x8864
movq -0x6c8(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x20d(%rbp), %rsi
callq 0xe9a0
movq %rax, -0x2b8(%rbp)
movq $-0x1, %rax
cmpq -0x2b8(%rbp), %rax
je 0x8862
movq -0x6c8(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x2b8(%rbp), %rsi
callq 0xecf0
movq -0x6c8(%rbp), %rdi
movq %rax, -0x2c0(%rbp)
movq -0x2c0(%rbp), %rsi
callq 0x8970
movq -0x6c8(%rbp), %rsi
movq -0x2c0(%rbp), %rdi
callq 0x89a0
jmp 0x8864
jmp 0x8866
movq -0x6c8(%rbp), %rdi
leaq -0x295(%rbp), %rsi
callq 0x8fb0
jmp 0x8959
movq -0x6c8(%rbp), %rdi
movq 0x59744(%rip), %rax     # 0x61fd0
movq (%rax), %rax
movq %rax, -0x6e8(%rbp)
movl -0x21d(%rbp), %eax
movl %eax, -0x6dc(%rbp)
movq -0x28d(%rbp), %rax
movq %rax, -0x6d8(%rbp)
callq 0x9190
movq -0x6e8(%rbp), %rdi
movl -0x6dc(%rbp), %edx
movq -0x6d8(%rbp), %rcx
movq %rax, %r8
leaq 0x487bb(%rip), %rsi     # 0x5108e
movb $0x0, %al
callq 0x82a0
jmp 0x8959
jmp 0x88de
movq -0x6c8(%rbp), %rdi
leaq -0x295(%rbp), %rsi
callq 0x8fb0
movq -0x6c8(%rbp), %rax
movq -0x295(%rbp), %rcx
movslq -0x29c(%rbp), %rdx
addq %rdx, %rcx
movq %rcx, -0x295(%rbp)
movq -0x28d(%rbp), %rcx
movslq -0x29c(%rbp), %rdx
addq %rdx, %rcx
movq %rcx, -0x28d(%rbp)
movq -0x295(%rbp), %rcx
movq %rcx, -0x6f0(%rbp)
movq 0x50(%rax), %rdi
callq 0x9170
movq %rax, %rcx
movq -0x6f0(%rbp), %rax
cmpq %rcx, %rax
jb 0x8950
jmp 0x8959
jmp 0x8952
jmp 0x8954
jmp 0x869b
addq $0x6f0, %rsp            # imm = 0x6F0
popq %rbp
retq
nopw %cs:(%rax,%rax)