// File: /workspace/github_c_makefile_star2_O3/baines[P]MiniGBS/debug.c
// Function: debug_get_regs
static void debug_get_regs(const uint8_t* op, uint32_t* mask_out, uint32_t* len_out){
	const char* str = opcodes[*op];
	uint32_t mask = 0;

	if(mask_out){
		if(*op == 0xcb){
			size_t z = op[1] & 7;
			char* tmp = alloca(strlen(cb_regnames[z])+2);
			*tmp = ' ';
			strcpy(tmp+1, cb_regnames[z]);
			str = tmp;
		}

		if(strstr(str, " a")) mask |= DBG_REG_A;
		if(strstr(str, " b")) mask |= DBG_REG_B;

		// hack to avoid jumps on carry
		if(*op != 0x38 && *op != 0xd8 && *op != 0xda && *op != 0xdc && strstr(str, " c")) mask |= DBG_REG_C;

		if(strstr(str, " d")) mask |= DBG_REG_D;
		if(strstr(str, " e")) mask |= DBG_REG_E;
		if(strstr(str, " h")) mask |= DBG_REG_H;
		if(strstr(str, " l")) mask |= DBG_REG_L;

		if(strstr(str, "af")) mask |= (DBG_REG_A | DBG_REG_F);
		if(strstr(str, "bc")) mask |= (DBG_REG_B | DBG_REG_C);
		if(strstr(str, "hl")) mask |= (DBG_REG_H | DBG_REG_L);

		if(strstr(str, "sp")) mask |= (DBG_REG_SP);

		char* p;
		if((p = strstr(str, "de")) && p[2] != 'c') mask |= (DBG_REG_D | DBG_REG_E);

		switch(*op){
			case 0x07: // rlca
			case 0x0f: // rrca
			case 0x17: // rla
			case 0x1f: // rra
			case 0x27: // daa
			case 0x2f: // cpl
				mask |= DBG_REG_A;
				break;
			case 0x37: // scf
			case 0x3f: // ccf

				// might as well highlight flags for conditional jumps too
			case 0x20:
			case 0x28:
			case 0x30:
			case 0x38:
			case 0xc0:
			case 0xc2:
			case 0xc4:
			case 0xc8:
			case 0xca:
			case 0xcc:
			case 0xd0:
			case 0xd2:
			case 0xd4:
			case 0xd8:
			case 0xda:
			case 0xdc:
				mask |= DBG_REG_F;
				break;
		}

		*mask_out = mask;
	}

	char* p = strchr(str, '%');
	if(p){
		while(strchr("%+0", *p)){
			++p;
		}

		*len_out = (*p == '4') ? 3 : 2;
	} else if(*op == 0xcb){
		*len_out = 2;
	} else {
		*len_out = 1;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r13
movq %rdi, %r14
movzbl (%rdi), %ebx
leaq 0x85c1(%rip), %rax      # 0x104d0
movq (%rax,%rbx,8), %r12
testq %rsi, %rsi
je 0x816a
movq %rsi, %r15
movq %r13, -0x38(%rbp)
cmpl $0xcb, %ebx
jne 0x7f6e
movzbl 0x1(%r14), %eax
andl $0x7, %eax
leaq 0x8556(%rip), %rcx      # 0x10490
movq (%rcx,%rax,8), %r13
movq %r13, %rdi
callq 0x5260
addq $0x11, %rax
andq $-0x10, %rax
movq %rsp, %rcx
movq %rcx, %r12
subq %rax, %r12
negq %rax
movq %r12, %rsp
movb $0x20, (%rcx,%rax)
leaq 0x1(%r12), %rdi
movq %r13, %rsi
callq 0x5470
movq %r14, -0x30(%rbp)
leaq 0x4bb3(%rip), %rsi      # 0xcb2c
movq %r12, %rdi
callq 0x5330
xorl %r14d, %r14d
testq %rax, %rax
setne %r14b
leaq 0x4e40(%rip), %rsi      # 0xcdd2
movq %r12, %rdi
callq 0x5330
leal 0x4(%r14), %r13d
testq %rax, %rax
cmovel %r14d, %r13d
leal -0xd8(%rbx), %eax
cmpl $0x4, %eax
ja 0x7fba
movl $0x15, %ecx
btl %eax, %ecx
jb 0x7fdc
cmpl $0x38, %ebx
je 0x7fdc
leaq 0x4e17(%rip), %rsi      # 0xcddd
movq %r12, %rdi
callq 0x5330
leal 0x8(%r13), %ecx
testq %rax, %rax
cmovel %r13d, %ecx
movl %ecx, %r13d
leaq 0x4e05(%rip), %rsi      # 0xcde8
movq %r12, %rdi
callq 0x5330
leal 0x10(%r13), %r14d
testq %rax, %rax
cmovel %r13d, %r14d
leaq 0x4df6(%rip), %rsi      # 0xcdf3
movq %r12, %rdi
callq 0x5330
leal 0x20(%r14), %r13d
testq %rax, %rax
cmovel %r14d, %r13d
leaq 0x4de7(%rip), %rsi      # 0xcdfe
movq %r12, %rdi
callq 0x5330
leal 0x40(%r13), %r14d
testq %rax, %rax
cmovel %r13d, %r14d
leaq 0x4dd8(%rip), %rsi      # 0xce09
movq %r12, %rdi
callq 0x5330
movl %r14d, %r13d
orl $0x80, %r13d
testq %rax, %rax
cmovel %r14d, %r13d
leaq 0x51ee(%rip), %rsi      # 0xd23f
movq %r12, %rdi
callq 0x5330
movl %r13d, %r14d
orl $0x3, %r14d
testq %rax, %rax
cmovel %r13d, %r14d
leaq 0x49e2(%rip), %rsi      # 0xca50
movq %r12, %rdi
callq 0x5330
movl %r14d, %r13d
orl $0xc, %r13d
testq %rax, %rax
cmovel %r14d, %r13d
leaq 0x4add(%rip), %rsi      # 0xcb68
movq %r12, %rdi
callq 0x5330
movl %r13d, %r14d
orl $0xc0, %r14d
testq %rax, %rax
cmovel %r13d, %r14d
leaq 0x499a(%rip), %rsi      # 0xca45
movq %r12, %rdi
callq 0x5330
movl %r14d, %r13d
orl $0x100, %r13d           # imm = 0x100
testq %rax, %rax
cmovel %r14d, %r13d
leaq 0x4a0c(%rip), %rsi      # 0xcad7
movq %r12, %rdi
callq 0x5330
testq %rax, %rax
je 0x80e6
movl %r13d, %ecx
orl $0x30, %ecx
cmpb $0x63, 0x2(%rax)
cmovnel %ecx, %r13d
movq -0x30(%rbp), %r14
cmpl $0xbf, %ebx
jg 0x8114
cmpl $0x2e, %ebx
jg 0x812b
cmpl $0x1e, %ebx
jle 0x8148
addl $-0x1f, %ebx
cmpl $0x9, %ebx
ja 0x8163
leaq 0x46d5(%rip), %rax      # 0xc7e0
movslq (%rax,%rbx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
addl $0xffffff40, %ebx       # imm = 0xFFFFFF40
cmpl $0x1c, %ebx
ja 0x8163
movl $0x15151515, %eax       # imm = 0x15151515
btl %ebx, %eax
jb 0x8142
jmp 0x8163
cmpl $0x3f, %ebx
ja 0x8163
movl %ebx, %eax
movabsq $-0x7e7f000000000000, %rcx # imm = 0x8181000000000000
btq %rax, %rcx
jae 0x8159
orl $0x2, %r13d
jmp 0x8163
cmpl $0x7, %ebx
je 0x815f
cmpl $0xf, %ebx
je 0x815f
cmpl $0x17, %ebx
je 0x815f
jmp 0x8163
cmpq $0x2f, %rax
jne 0x8163
orl $0x1, %r13d
movl %r13d, (%r15)
movq -0x38(%rbp), %r13
movq %r12, %rdi
movl $0x25, %esi
callq 0x53e0
testq %rax, %rax
je 0x81ae
movq %rax, %r15
leaq 0x47e7(%rip), %r14      # 0xc96d
movb (%r15), %bl
movsbl %bl, %esi
movl $0x4, %edx
movq %r14, %rdi
callq 0x5100
incq %r15
testq %rax, %rax
jne 0x8186
xorl %eax, %eax
cmpb $0x34, %bl
sete %al
orl $0x2, %eax
jmp 0x81b9
xorl %eax, %eax
cmpb $-0x35, (%r14)
sete %al
incl %eax
movl %eax, (%r13)
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq