// File: /workspace/github_c_makefile_star2_O2/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 0x9740(%rip), %rax      # 0x114d0
movq (%rax,%rbx,8), %r12
testq %rsi, %rsi
je 0x7fd5
movq %rsi, %r15
movq %r13, -0x38(%rbp)
cmpl $0xcb, %ebx
jne 0x7def
movzbl 0x1(%r14), %eax
andl $0x7, %eax
leaq 0x96d5(%rip), %rcx      # 0x11490
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 0x5480
movq %r14, -0x30(%rbp)
leaq 0x4d32(%rip), %rsi      # 0xcb2c
movq %r12, %rdi
callq 0x5330
xorl %r14d, %r14d
testq %rax, %rax
setne %r14b
leaq 0x4fbf(%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 0x7e39
pushq $0x15
popq %rcx
btl %eax, %ecx
jb 0x7e5b
cmpl $0x38, %ebx
je 0x7e5b
leaq 0x4f98(%rip), %rsi      # 0xcddd
movq %r12, %rdi
callq 0x5330
leal 0x8(%r13), %ecx
testq %rax, %rax
cmovel %r13d, %ecx
movl %ecx, %r13d
leaq 0x4f86(%rip), %rsi      # 0xcde8
movq %r12, %rdi
callq 0x5330
leal 0x10(%r13), %r14d
testq %rax, %rax
cmovel %r13d, %r14d
leaq 0x4f77(%rip), %rsi      # 0xcdf3
movq %r12, %rdi
callq 0x5330
leal 0x20(%r14), %r13d
testq %rax, %rax
cmovel %r14d, %r13d
leaq 0x4f68(%rip), %rsi      # 0xcdfe
movq %r12, %rdi
callq 0x5330
leal 0x40(%r13), %r14d
testq %rax, %rax
cmovel %r13d, %r14d
leaq 0x4f59(%rip), %rsi      # 0xce09
movq %r12, %rdi
callq 0x5330
movl %r14d, %r13d
orl $0x80, %r13d
testq %rax, %rax
cmovel %r14d, %r13d
leaq 0x536f(%rip), %rsi      # 0xd23f
movq %r12, %rdi
callq 0x5330
movl %r13d, %r14d
orl $0x3, %r14d
testq %rax, %rax
cmovel %r13d, %r14d
leaq 0x4b63(%rip), %rsi      # 0xca50
movq %r12, %rdi
callq 0x5330
movl %r14d, %r13d
orl $0xc, %r13d
testq %rax, %rax
cmovel %r14d, %r13d
leaq 0x4c5e(%rip), %rsi      # 0xcb68
movq %r12, %rdi
callq 0x5330
movl %r13d, %r14d
orl $0xc0, %r14d
testq %rax, %rax
cmovel %r13d, %r14d
leaq 0x4b1b(%rip), %rsi      # 0xca45
movq %r12, %rdi
callq 0x5330
movl %r14d, %r13d
orl $0x100, %r13d           # imm = 0x100
testq %rax, %rax
cmovel %r14d, %r13d
leaq 0x4b8d(%rip), %rsi      # 0xcad7
movq %r12, %rdi
callq 0x5330
testq %rax, %rax
je 0x7f65
movl %r13d, %ecx
orl $0x30, %ecx
cmpb $0x63, 0x2(%rax)
cmovnel %ecx, %r13d
movq -0x30(%rbp), %r14
leal -0xc0(%rbx), %eax
cmpl $0x1c, %eax
ja 0x7f7e
movl $0x15151515, %ecx       # imm = 0x15151515
btl %eax, %ecx
jb 0x7fad
cmpl $0x3f, %ebx
jbe 0x7f9b
leal -0x1f(%rbx), %eax
cmpl $0x9, %eax
ja 0x7fb3
leaq 0x484e(%rip), %rcx      # 0xc7e0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl %ebx, %eax
movabsq $-0x7e7f000000000000, %rcx # imm = 0x8181000000000000
btq %rax, %rcx
jae 0x7fc4
orl $0x2, %r13d
jmp 0x7fce
cmpl $0x7, %ebx
je 0x7fca
cmpl $0x17, %ebx
je 0x7fca
cmpl $0xf, %ebx
je 0x7fca
jmp 0x7fce
cmpq $0x2f, %rax
jne 0x7f83
orl $0x1, %r13d
movl %r13d, (%r15)
movq -0x38(%rbp), %r13
pushq $0x25
popq %rsi
movq %r12, %rdi
callq 0x53f0
testq %rax, %rax
je 0x8019
movq %rax, %r15
leaq 0x497e(%rip), %r14      # 0xc96d
pushq $0x4
popq %r12
movb (%r15), %bl
movsbl %bl, %esi
movq %r14, %rdi
movq %r12, %rdx
callq 0x5100
incq %r15
testq %rax, %rax
jne 0x7ff3
xorl %eax, %eax
cmpb $0x34, %bl
sete %al
orl $0x2, %eax
jmp 0x8024
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