I am writing a tool which uses libbfd
and libopcodes
in x86-32 and x86-64 Linux to perform disassembly. The problem is that whilst I am able to get
Libopcodes prints disassembled instructions into the stream which is intercepted by your custom_printf function. Your mistake is that you assume that custom_printf is called once each time a single instruction is disassembled, however, it is called more often, particularly, to print each mnemonic,operand, address or separator.
So, resulting disassembly of your binary is
xor %ebp, %ebp
mov %rdx, %r9
pop %rsi
mov %rsp, %rdx
and $0xfffffffffffffff0, %rsp
push %rax
push %rsp
mov $0x401450,%r8
...