// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0xb78c(%rip), %rax      # 0x15dd0
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0xa8db
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xcb, %eax
jne 0xa6d2
movq -0x8(%rbp), %rax
movzbl 0x1(%rax), %eax
andl $0x7, %eax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rcx
leaq 0xb70a(%rip), %rax      # 0x15d90
movq (%rax,%rcx,8), %rdi
callq 0x5260
movq %rax, %rcx
addq $0x11, %rcx
andq $-0x10, %rcx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movb $0x20, (%rax)
movq -0x38(%rbp), %rdi
addq $0x1, %rdi
movq -0x30(%rbp), %rcx
leaq 0xb6cf(%rip), %rax      # 0x15d90
movq (%rax,%rcx,8), %rsi
callq 0x5480
movq -0x38(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x6a41(%rip), %rsi      # 0x1111e
callq 0x5330
cmpq $0x0, %rax
je 0xa6f1
movl -0x24(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x6cc8(%rip), %rsi      # 0x113c4
callq 0x5330
cmpq $0x0, %rax
je 0xa710
movl -0x24(%rbp), %eax
orl $0x4, %eax
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x38, %eax
je 0xa765
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xd8, %eax
je 0xa765
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xda, %eax
je 0xa765
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xdc, %eax
je 0xa765
movq -0x20(%rbp), %rdi
leaq 0x6c7e(%rip), %rsi      # 0x113cf
callq 0x5330
cmpq $0x0, %rax
je 0xa765
movl -0x24(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x6c6a(%rip), %rsi      # 0x113da
callq 0x5330
cmpq $0x0, %rax
je 0xa784
movl -0x24(%rbp), %eax
orl $0x10, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x6c56(%rip), %rsi      # 0x113e5
callq 0x5330
cmpq $0x0, %rax
je 0xa7a3
movl -0x24(%rbp), %eax
orl $0x20, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x6c42(%rip), %rsi      # 0x113f0
callq 0x5330
cmpq $0x0, %rax
je 0xa7c2
movl -0x24(%rbp), %eax
orl $0x40, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x6c2e(%rip), %rsi      # 0x113fb
callq 0x5330
cmpq $0x0, %rax
je 0xa7e3
movl -0x24(%rbp), %eax
orl $0x80, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x7043(%rip), %rsi      # 0x11831
callq 0x5330
cmpq $0x0, %rax
je 0xa802
movl -0x24(%rbp), %eax
orl $0x3, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x6835(%rip), %rsi      # 0x11042
callq 0x5330
cmpq $0x0, %rax
je 0xa821
movl -0x24(%rbp), %eax
orl $0xc, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x692e(%rip), %rsi      # 0x1115a
callq 0x5330
cmpq $0x0, %rax
je 0xa842
movl -0x24(%rbp), %eax
orl $0xc0, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x67ea(%rip), %rsi      # 0x11037
callq 0x5330
cmpq $0x0, %rax
je 0xa863
movl -0x24(%rbp), %eax
orl $0x100, %eax            # imm = 0x100
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x685b(%rip), %rsi      # 0x110c9
callq 0x5330
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0xa893
movq -0x40(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x63, %eax
je 0xa893
movl -0x24(%rbp), %eax
orl $0x30, %eax
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
addl $-0x7, %eax
movl %eax, %ecx
movq %rcx, -0x50(%rbp)
subl $0xd5, %eax
ja 0xa8d2
movq -0x50(%rbp), %rax
leaq 0x61e3(%rip), %rcx      # 0x10a98
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x24(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xa8d2
movl -0x24(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rdi
movl $0x25, %esi
callq 0x53e0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xa93c
jmp 0xa8f6
movq -0x48(%rbp), %rax
movsbl (%rax), %esi
leaq 0x6651(%rip), %rdi      # 0x10f55
callq 0x53e0
cmpq $0x0, %rax
je 0xa91d
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0xa8f6
movq -0x48(%rbp), %rax
movsbl (%rax), %edx
movl $0x2, %ecx
movl $0x3, %eax
cmpl $0x34, %edx
cmovel %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
jmp 0xa962
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xcb, %eax
jne 0xa956
movq -0x18(%rbp), %rax
movl $0x2, (%rax)
jmp 0xa960
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
jmp 0xa962
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)