// File: /workspace/github_cpp_makefile_star2_O2/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 0x63ba
movb 0x28(%rbx), %dl
testb %dl, %dl
jne 0x63c5
movq %rbx, %rdi
callq 0x5fe2
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 0x6435
testl %ecx, %ecx
je 0x6462
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 0x658b
movzbl 0xc(%rbx), %r14d
testb $0x10, 0x34(%rbx)
je 0x6524
cmpl $0xe2, %r14d
je 0x651a
cmpl $0xc2, %r14d
jne 0x6524
movl %ebp, %eax
andl $0x30, %eax
orl %eax, 0x1c(%rbx)
jmp 0x6524
movl 0x2c(%rbx), %ecx
testl %ecx, %ecx
je 0x6458
leal -0x1(%rcx), %edx
movl %edx, 0x2c(%rbx)
cmpl $0x1, %ecx
jg 0x6458
movq %rbx, %rdi
callq 0x5c7e
movb $0x1, 0x28(%rbx)
jmp 0x658b
cmpl $0x4, %eax
je 0x6498
jmp 0x658b
movb %bpl, 0xc(%rbx)
movzbl %bpl, %r14d
leaq 0x512f(%rip), %rax      # 0xb5a0
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 0x64a4
movl 0x20(%rbx), %edx
incl %edx
cmpl %edx, 0x30(%rbx)
jne 0x64a4
movq %rbx, %rdi
popq %rbx
popq %r14
popq %rbp
jmp 0x5c7e
testb $0x1, %cl
sete %cl
cmpb $-0xc, %bpl
setne %dl
orb %cl, %dl
jne 0x64c1
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 0x64d5
incl %ecx
movl %ecx, 0x10(%rbx)
testb $0x20, %al
jne 0x6513
testl %ecx, %ecx
jne 0x658b
movq %rbx, %rdi
callq 0x6590
movl %r14d, %edi
callq 0x68eb
testb %al, %al
je 0x658b
movq 0xbac8(%rip), %rax      # 0x11fc8
movq (%rax), %rsi
leaq 0x4e95(%rip), %rdi      # 0xb39f
popq %rbx
popq %r14
popq %rbp
jmp 0x21f0
incl %ecx
movl %ecx, 0x10(%rbx)
jmp 0x658b
movl %ebp, %eax
notl %eax
orl $-0x31, %eax
andl %eax, 0x1c(%rbx)
movq %rbx, %rdi
callq 0x6590
movl %r14d, %edi
callq 0x68eb
testb %al, %al
je 0x654e
movq 0xba89(%rip), %rax      # 0x11fc8
movq (%rax), %rsi
leaq 0x4e56(%rip), %rdi      # 0xb39f
callq 0x21f0
leal -0xc2(%r14), %eax
cmpl $0x20, %eax
ja 0x656a
movabsq $0x104000001, %rcx      # imm = 0x104000001
btq %rax, %rcx
jb 0x6576
cmpl $0x5c, %r14d
je 0x6576
cmpl $0x22, %r14d
jne 0x658b
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