// File: /workspace/github_cpp_makefile_star2_O3/ksherlock[P]wdc-utils/disassembler.cpp
// Function: disassembler::operator()(unsigned char)
void disassembler::operator()(uint8_t byte) {

	if (_st == 0 || !_code)
		check_labels();

	if (!_code) {
		_bytes[_st++] = byte;

		if (_inline_data && --_inline_data <= 0) {
			dump();
			_code = true;
			return;
		}

		if (_st == 4) dump();
		return;
	}

	_bytes[_st++] = byte;
	if (_st == 1) {
		_op = byte;
		_mode = modes[_op];

		// bit hack
		if (_traits & bit_hacks && _op == 0x2c) {
			if (_next_label == _pc + 1) {
				dump();
				return;
			}
		}

		if (_traits & pea_immediate && _op == 0xf4) _mode = 2 | mImmediate;
		_size = _mode & 0x0f;
		if (_mode & _flags & m_I) _size++;
		if (_mode & _flags & m_M) _size++;

		if (!_size) {
			print();

			if (branchlike(byte)) fputs("\n", stdout);
		}
		return;
	}
	unsigned shift = (_st - 2) * 8;
	_arg = _arg + (byte << shift);
	if (_st <= _size) return;

	uint8_t op = _op;
	uint32_t arg = _arg;

	if (_traits & track_rep_sep) {
		switch(_op) {
			case 0xc2: // REP
				_flags |= (_arg & 0x30);
				break;
			case 0xe2: // SEP
				_flags &= ~(_arg & 0x30);
				break;
		}
	}

	// all done... now print it.
	print();

	if (branchlike(op)) fputs("\n", stdout);

	// todo -- subscribe to before/after events...
	switch(op) {
		case 0xc2:
		case 0xe2:
		case 0x22:
		case 0x5c:
		case 0xdc:
			event(op, arg);
			break;
	}



}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movl %esi, %ebp
movq %rdi, %rbx
cmpl $0x0, 0x8(%rdi)
je 0x873e
movb 0x28(%rbx), %dl
testb %dl, %dl
jne 0x8749
movq %rbx, %rdi
callq 0x81ca
movb 0x28(%rbx), %dl
movl 0x8(%rbx), %ecx
leal 0x1(%rcx), %eax
movl %eax, 0x8(%rbx)
movb %bpl, 0x18(%rbx,%rcx)
testb $0x1, %dl
je 0x87b9
testl %ecx, %ecx
je 0x87e6
leal (,%rcx,8), %ecx
movzbl %bpl, %ebp
addb $-0x8, %cl
shll %cl, %ebp
addl 0x24(%rbx), %ebp
movl %ebp, 0x24(%rbx)
cmpl 0x10(%rbx), %eax
jbe 0x890f
movzbl 0xc(%rbx), %r14d
testb $0x10, 0x34(%rbx)
je 0x88a8
cmpl $0xe2, %r14d
je 0x889e
cmpl $0xc2, %r14d
jne 0x88a8
movl %ebp, %eax
andl $0x30, %eax
orl %eax, 0x1c(%rbx)
jmp 0x88a8
movl 0x2c(%rbx), %ecx
testl %ecx, %ecx
je 0x87dc
leal -0x1(%rcx), %edx
movl %edx, 0x2c(%rbx)
cmpl $0x1, %ecx
jg 0x87dc
movq %rbx, %rdi
callq 0x7cfc
movb $0x1, 0x28(%rbx)
jmp 0x890f
cmpl $0x4, %eax
je 0x881c
jmp 0x890f
movb %bpl, 0xc(%rbx)
movzbl %bpl, %r14d
leaq 0x6d5b(%rip), %rax      # 0xf550
movl (%rax,%r14,4), %eax
movl %eax, 0x14(%rbx)
movl 0x34(%rbx), %ecx
testb $0x20, %cl
sete %dl
cmpb $0x2c, %bpl
setne %sil
orb %dl, %sil
jne 0x8828
movl 0x20(%rbx), %ecx
incl %ecx
cmpl %ecx, 0x30(%rbx)
jne 0x8845
movq %rbx, %rdi
popq %rbx
popq %r14
popq %rbp
jmp 0x7cfc
testb $0x1, %cl
sete %cl
cmpb $-0xc, %bpl
setne %dl
orb %cl, %dl
jne 0x8845
movl $0x1002, 0x14(%rbx)     # imm = 0x1002
movl $0x1002, %eax           # imm = 0x1002
movl %eax, %ecx
andl $0xf, %ecx
movl %ecx, 0x10(%rbx)
andl 0x1c(%rbx), %eax
testb $0x10, %al
je 0x8859
incl %ecx
movl %ecx, 0x10(%rbx)
testb $0x20, %al
jne 0x8897
testl %ecx, %ecx
jne 0x890f
movq %rbx, %rdi
callq 0x8914
movl %r14d, %edi
callq 0x8e4a
testb %al, %al
je 0x890f
movq 0xb744(%rip), %rax      # 0x13fc8
movq (%rax), %rsi
leaq 0x6ab5(%rip), %rdi      # 0xf343
popq %rbx
popq %r14
popq %rbp
jmp 0x21e0
incl %ecx
movl %ecx, 0x10(%rbx)
jmp 0x890f
movl %ebp, %eax
notl %eax
orl $-0x31, %eax
andl %eax, 0x1c(%rbx)
movq %rbx, %rdi
callq 0x8914
movl %r14d, %edi
callq 0x8e4a
testb %al, %al
je 0x88d2
movq 0xb705(%rip), %rax      # 0x13fc8
movq (%rax), %rsi
leaq 0x6a76(%rip), %rdi      # 0xf343
callq 0x21e0
leal -0xc2(%r14), %eax
cmpl $0x20, %eax
ja 0x88ee
movabsq $0x104000001, %rcx      # imm = 0x104000001
btq %rax, %rcx
jb 0x88fa
cmpl $0x5c, %r14d
je 0x88fa
cmpl $0x22, %r14d
jne 0x890f
movq (%rbx), %rax
movq 0x40(%rax), %rax
movq %rbx, %rdi
movl %r14d, %esi
movl %ebp, %edx
popq %rbx
popq %r14
popq %rbp
jmpq *%rax
popq %rbx
popq %r14
popq %rbp
retq