// File: /workspace/github_c_makefile_star2_O2/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: rtk_get_patch_entry
struct rtk_epatch_entry *rtk_get_patch_entry(void)
{
	uint16_t i;
	struct rtk_epatch *patch;
	struct rtk_epatch_entry *entry;
	uint8_t *p;
	uint16_t chip_id;
	uint32_t tmp;

	patch = (struct rtk_epatch *)rtk_hw_cfg.fw_buf;
	entry = (struct rtk_epatch_entry *)malloc(sizeof(*entry));
	if (!entry) {
		RS_ERR("failed to allocate mem for patch entry");
		return NULL;
	}

	patch->number_of_patch = le16_to_cpu(patch->number_of_patch);

	RS_DBG("fw_ver 0x%08x, patch_num %d",
	       le32_to_cpu(patch->fw_version), patch->number_of_patch);

	for (i = 0; i < patch->number_of_patch; i++) {
		RS_DBG("chip id 0x%04x",
			get_unaligned_le16(rtk_hw_cfg.fw_buf + 14 + 2 * i));
		if (get_unaligned_le16(rtk_hw_cfg.fw_buf + 14 + 2 * i) ==
		    rtk_hw_cfg.eversion + 1) {
			entry->chipID = rtk_hw_cfg.eversion + 1;
			entry->patch_length =
			    get_unaligned_le16(rtk_hw_cfg.fw_buf + 14 +
					       2 * patch->number_of_patch +
					       2 * i);
			entry->start_offset =
			    get_unaligned_le32(rtk_hw_cfg.fw_buf + 14 +
					       4 * patch->number_of_patch +
					       4 * i);
			RS_DBG("patch length is 0x%x", entry->patch_length);
			RS_DBG("start offset is 0x%x", entry->start_offset);

			entry->svn_ver = get_unaligned_le32(rtk_hw_cfg.fw_buf +
						entry->start_offset +
						entry->patch_length - 8);
			entry->coex_ver = get_unaligned_le32(rtk_hw_cfg.fw_buf +
						entry->start_offset +
						entry->patch_length - 12);

			RS_INFO("Svn version: %8d\n", entry->svn_ver);
			tmp = ((entry->coex_ver >> 16) & 0x7ff) +
			      (entry->coex_ver >> 27) * 10000;
			RS_INFO("Coexistence: BTCOEX_20%06d-%04x\n", tmp,
				(entry->coex_ver & 0xffff));

			break;
		}

	}

	if (i == patch->number_of_patch) {
		RS_ERR("failed to get entry");
		free(entry);
		entry = NULL;
	}

	return entry;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq 0x6d5d(%rip), %r12      # 0xc748
pushq $0x10
popq %rdi
callq 0x2190
testq %rax, %rax
je 0x5ab4
movq %rax, -0x30(%rbp)
cmpb $0x0, 0x68d9(%rip)      # 0xc2e0
jne 0x5ad8
movq 0x65e4(%rip), %rdi      # 0xbff8
leaq 0x33ac(%rip), %r14      # 0x8dc7
leaq 0x2b17(%rip), %r15      # 0x8539
xorl %r13d, %r13d
movzwl 0xc(%r12), %eax
cmpq %rax, %r13
jae 0x5a98
cmpb $0x0, 0x68a9(%rip)      # 0xc2e0
movq 0x6d0a(%rip), %rax      # 0xc748
jne 0x5a63
leaq (,%r13,2), %rbx
movzwl 0xe(%rax,%rbx), %ecx
movzbl 0x6cbb(%rip), %edx      # 0xc70f
incl %edx
cmpl %ecx, %edx
je 0x5b36
incq %r13
jmp 0x5a25
movq 0x658e(%rip), %rcx      # 0xbff8
movq (%rcx), %rdi
leaq (,%r13,2), %rbx
movzwl 0xe(%rax,%r13,2), %ecx
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2150
movq 0x6569(%rip), %rdi      # 0xbff8
movq 0x6cb2(%rip), %rax      # 0xc748
jmp 0x5a48
movq -0x30(%rbp), %r15
cmpw %ax, %r13w
je 0x5b07
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x653d(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x32af(%rip), %rsi      # 0x8d74
leaq 0x2a6d(%rip), %rdx      # 0x8539
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2150
jmp 0x5aa2
movzwl 0xc(%r12), %r8d
movq 0x6513(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movl 0x8(%r12), %ecx
leaq 0x32b2(%rip), %rsi      # 0x8da6
leaq 0x2a3e(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
jmp 0x5a0d
movq 0x64ea(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x3334(%rip), %rsi      # 0x8e4c
leaq 0x2a1a(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movq %r15, %rdi
callq 0x2030
xorl %r15d, %r15d
jmp 0x5aa2
addq $0xe, %rax
movq -0x30(%rbp), %rsi
movw %cx, (%rsi)
movzwl 0xc(%r12), %ecx
leaq (%rax,%rcx,2), %rdx
movzwl (%rbx,%rdx), %r14d
movw %r14w, 0x2(%rsi)
leaq (%rax,%rcx,4), %rax
leal (,%r13,4), %ecx
movl (%rcx,%rax), %r15d
movl %r15d, 0x4(%rsi)
cmpb $0x0, 0x6770(%rip)      # 0xc2e0
je 0x5bcd
movq 0x647f(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x3258(%rip), %rsi      # 0x8ddb
leaq 0x29af(%rip), %rdx      # 0x8539
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2150
movq 0x645d(%rip), %rdi      # 0xbff8
cmpb $0x0, 0x673e(%rip)      # 0xc2e0
je 0x5bcd
movq 0x644d(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x3240(%rip), %rsi      # 0x8df5
leaq 0x297d(%rip), %rdx      # 0x8539
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2150
movq 0x642b(%rip), %rdi      # 0xbff8
addq 0x6b74(%rip), %r15      # 0xc748
movl %r14d, %eax
movl -0x8(%rax,%r15), %ecx
movq -0x30(%rbp), %rdx
movl %ecx, 0x8(%rdx)
movl -0xc(%rax,%r15), %ebx
movq %rdx, %r15
movl %ebx, 0xc(%rdx)
movq %rdi, %r14
movq (%rdi), %rdi
leaq 0x3214(%rip), %rsi      # 0x8e0f
leaq 0x2937(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movl %ebx, %eax
shrl $0x10, %eax
andl $0x7ff, %eax            # imm = 0x7FF
movl %ebx, %ecx
shrl $0x1b, %ecx
imull $0x2710, %ecx, %ecx     # imm = 0x2710
addl %eax, %ecx
movq (%r14), %rdi
movzwl %bx, %r8d
leaq 0x31f8(%rip), %rsi      # 0x8e26
leaq 0x2904(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movzwl 0xc(%r12), %eax
jmp 0x5a9c