// File: /workspace/github_c_makefile_star2_O1/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 0x8519(%rip), %rax      # 0x104d0
movq (%rax,%rbx,8), %r12
testq %rsi, %rsi
je 0x8212
movq %rsi, %r15
movq %r13, -0x38(%rbp)
cmpl $0xcb, %ebx
jne 0x8016
movzbl 0x1(%r14), %eax
andl $0x7, %eax
leaq 0x84ae(%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 0x4add(%rip), %rsi      # 0xcafe
movq %r12, %rdi
callq 0x5330
xorl %r14d, %r14d
testq %rax, %rax
setne %r14b
leaq 0x4d6a(%rip), %rsi      # 0xcda4
movq %r12, %rdi
callq 0x5330
leal 0x4(%r14), %r13d
testq %rax, %rax
cmovel %r14d, %r13d
leal -0xd8(%rbx), %eax
cmpl $0x4, %eax
ja 0x8062
movl $0x15, %ecx
btl %eax, %ecx
jb 0x8084
cmpl $0x38, %ebx
je 0x8084
leaq 0x4d41(%rip), %rsi      # 0xcdaf
movq %r12, %rdi
callq 0x5330
leal 0x8(%r13), %ecx
testq %rax, %rax
cmovel %r13d, %ecx
movl %ecx, %r13d
leaq 0x4d2f(%rip), %rsi      # 0xcdba
movq %r12, %rdi
callq 0x5330
leal 0x10(%r13), %r14d
testq %rax, %rax
cmovel %r13d, %r14d
leaq 0x4d20(%rip), %rsi      # 0xcdc5
movq %r12, %rdi
callq 0x5330
leal 0x20(%r14), %r13d
testq %rax, %rax
cmovel %r14d, %r13d
leaq 0x4d11(%rip), %rsi      # 0xcdd0
movq %r12, %rdi
callq 0x5330
leal 0x40(%r13), %r14d
testq %rax, %rax
cmovel %r13d, %r14d
leaq 0x4d02(%rip), %rsi      # 0xcddb
movq %r12, %rdi
callq 0x5330
movl %r14d, %r13d
orl $0x80, %r13d
testq %rax, %rax
cmovel %r14d, %r13d
leaq 0x5118(%rip), %rsi      # 0xd211
movq %r12, %rdi
callq 0x5330
movl %r13d, %r14d
orl $0x3, %r14d
testq %rax, %rax
cmovel %r13d, %r14d
leaq 0x490c(%rip), %rsi      # 0xca22
movq %r12, %rdi
callq 0x5330
movl %r14d, %r13d
orl $0xc, %r13d
testq %rax, %rax
cmovel %r14d, %r13d
leaq 0x4a07(%rip), %rsi      # 0xcb3a
movq %r12, %rdi
callq 0x5330
movl %r13d, %r14d
orl $0xc0, %r14d
testq %rax, %rax
cmovel %r13d, %r14d
leaq 0x48c4(%rip), %rsi      # 0xca17
movq %r12, %rdi
callq 0x5330
movl %r14d, %r13d
orl $0x100, %r13d           # imm = 0x100
testq %rax, %rax
cmovel %r14d, %r13d
leaq 0x4936(%rip), %rsi      # 0xcaa9
movq %r12, %rdi
callq 0x5330
testq %rax, %rax
je 0x818e
movl %r13d, %ecx
orl $0x30, %ecx
cmpb $0x63, 0x2(%rax)
cmovnel %ecx, %r13d
movq -0x30(%rbp), %r14
cmpl $0xbf, %ebx
jg 0x81bc
cmpl $0x2e, %ebx
jg 0x81d3
cmpl $0x1e, %ebx
jle 0x81f0
addl $-0x1f, %ebx
cmpl $0x9, %ebx
ja 0x820b
leaq 0x45f5(%rip), %rax      # 0xc7a8
movslq (%rax,%rbx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
addl $0xffffff40, %ebx       # imm = 0xFFFFFF40
cmpl $0x1c, %ebx
ja 0x820b
movl $0x15151515, %eax       # imm = 0x15151515
btl %ebx, %eax
jb 0x81ea
jmp 0x820b
cmpl $0x3f, %ebx
ja 0x820b
movl %ebx, %eax
movabsq $-0x7e7f000000000000, %rcx # imm = 0x8181000000000000
btq %rax, %rcx
jae 0x8201
orl $0x2, %r13d
jmp 0x820b
cmpl $0x7, %ebx
je 0x8207
cmpl $0xf, %ebx
je 0x8207
cmpl $0x17, %ebx
je 0x8207
jmp 0x820b
cmpq $0x2f, %rax
jne 0x820b
orl $0x1, %r13d
movl %r13d, (%r15)
movq -0x38(%rbp), %r13
movq %r12, %rdi
movl $0x25, %esi
callq 0x53e0
testq %rax, %rax
je 0x8256
movq %rax, %r15
leaq 0x4707(%rip), %r14      # 0xc935
movb (%r15), %bl
movsbl %bl, %esi
movl $0x4, %edx
movq %r14, %rdi
callq 0x5100
incq %r15
testq %rax, %rax
jne 0x822e
xorl %eax, %eax
cmpb $0x34, %bl
sete %al
orl $0x2, %eax
jmp 0x8261
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