// File: jstaursky[P]hutch/src/hutch.cpp
// Function: Hutch::disassemble_iter(unsigned long, Hutch_Emit*)
uint Hutch::disassemble_iter(uintb offset, Hutch_Emit* emitter)
{
    uintb baseaddr = this->loader->getBaseAddr ();

    Address addr (this->trans->getDefaultSpace (), baseaddr);
    Address lastaddr (this->trans->getDefaultSpace (),
                      baseaddr + this->loader->getBufferSize ());

    addr = addr + offset;

    if (lastaddr <= addr) {
        // cout << "exceeded last available address\n";
        return 0;
    }
    auto len = 0;

    try {
        len = this->trans->printAssembly(*emitter, addr);
        this->trans->oneInstruction(*emitter, addr);
    } catch (const BadDataError&) {
        len = 0;
        emitter->removeBadInstruction ();
    }

    if (auto e = dynamic_cast<Hutch_Instructions*>(emitter))
        storeRawInstructionBytes(*e->currentinsn);

    return len;
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq 0xa8(%rsp), %rdi
movq %rdi, 0x20(%rsp)
addq $0x140, %rdi            # imm = 0x140
callq 0xceff0
movq %rax, %rdi
callq 0xd3e60
movq 0x20(%rsp), %rdi
movq %rax, 0x90(%rsp)
addq $0x148, %rdi            # imm = 0x148
callq 0xddcb0
movq %rax, %rdi
callq 0xd9770
movq %rax, %rsi
movq 0x90(%rsp), %rdx
leaq 0x80(%rsp), %rdi
callq 0xd81b0
movq 0x20(%rsp), %rdi
addq $0x148, %rdi            # imm = 0x148
callq 0xddcb0
movq %rax, %rdi
callq 0xd9770
movq 0x20(%rsp), %rdi
movq %rax, 0x30(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x28(%rsp)
addq $0x140, %rdi            # imm = 0x140
callq 0xceff0
movq %rax, %rdi
callq 0xdccc0
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rsi
addq %rax, %rdx
leaq 0x70(%rsp), %rdi
callq 0xd81b0
movq 0xa0(%rsp), %rax
movl %eax, %edx
leaq 0x60(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xd8150
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xdafc0
leaq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xdd4d0
testb $0x1, %al
jne 0x1713ea
jmp 0x1713fa
movl $0x0, 0xb4(%rsp)
jmp 0x171546
movq 0x20(%rsp), %rdi
movl $0x0, 0x5c(%rsp)
addq $0x148, %rdi            # imm = 0x148
callq 0xddcb0
movq %rax, %rdi
movq 0x98(%rsp), %rsi
addq $0x8, %rsi
movq (%rdi), %rax
movq 0x68(%rax), %rax
leaq 0x80(%rsp), %rdx
callq *%rax
movl %eax, 0x1c(%rsp)
jmp 0x171439
movq 0x20(%rsp), %rdi
movl 0x1c(%rsp), %eax
movl %eax, 0x5c(%rsp)
addq $0x148, %rdi            # imm = 0x148
callq 0xddcb0
movq %rax, %rdi
movq 0x98(%rsp), %rsi
movq (%rdi), %rax
movq 0x60(%rax), %rax
leaq 0x80(%rsp), %rdx
callq *%rax
jmp 0x171470
jmp 0x1714c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x171555
movq 0x50(%rsp), %rdi
callq 0xd1d20
movq %rax, 0x40(%rsp)
movl $0x0, 0x5c(%rsp)
movq 0x98(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
jmp 0x1714bb
callq 0xdf2c0
movq 0x98(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x17150d
movq 0x10(%rsp), %rdi
movq 0x56969(%rip), %rsi     # 0x1c7e48
movq 0x568c2(%rip), %rdx     # 0x1c7da8
xorl %eax, %eax
movl %eax, %ecx
callq 0xd99a0
movq %rax, 0x8(%rsp)
jmp 0x171516
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
callq 0xdf2c0
jmp 0x17150b
jmp 0x171555
xorl %eax, %eax
movq %rax, 0x8(%rsp)
jmp 0x171516
movq 0x8(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
je 0x17153b
movq 0x20(%rsp), %rdi
movq 0x38(%rsp), %rax
movq 0x28(%rax), %rsi
callq 0xd4530
movl 0x5c(%rsp), %eax
movl %eax, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
movq 0x50(%rsp), %rdi
callq 0xe0be0
movq %rax, %rdi
callq 0xe7140
nopw (%rax,%rax)