// File: /workspace/github_c_makefile_star2_O1/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: rtk_get_final_patch
void rtk_get_final_patch(int fd, int proto)
{
	struct btrtl_info *rtl = &rtk_hw_cfg;
	uint8_t proj_id = 0;
	struct rtk_epatch_entry *entry = NULL;
	struct rtk_epatch *patch = (struct rtk_epatch *)rtl->fw_buf;
	uint32_t svn_ver, coex_ver, tmp;

	if ((proto == HCI_UART_H4 && rtl->lmp_subver != ROM_LMP_8723b) ||
	    ((proto == HCI_UART_3WIRE) && (rtl->lmp_subver == ROM_LMP_8723a))) {
		if (memcmp(rtl->fw_buf, RTK_EPATCH_SIGNATURE, 8) == 0) {
			RS_ERR("Check signature error!");
			rtl->dl_fw_flag = 0;
			goto free_buf;
		} else {
			rtl->total_len = rtl->config_len + rtl->fw_len;
			if (!(rtl->total_buf = malloc(rtl->total_len))) {
				RS_ERR("Can't alloc mem for fw/config, errno:%d",
				       errno);
				rtl->dl_fw_flag = 0;
				rtl->total_len = 0;
				goto free_buf;
			} else {
				RS_DBG("fw copy directy");

				svn_ver = get_unaligned_le32(
						rtl->fw_buf + rtl->fw_len - 8);
				coex_ver = get_unaligned_le32(
						rtl->fw_buf + rtl->fw_len - 12);

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

				memcpy(rtl->total_buf, rtl->fw_buf,
				       rtl->fw_len);
				if (rtl->config_len)
					memcpy(rtl->total_buf +
					       rtl->fw_len,
					       rtl->config_buf,
					       rtl->config_len);
				rtl->dl_fw_flag = 1;
				goto free_buf;
			}
		}
	}

	if (memcmp(rtl->fw_buf, RTK_EPATCH_SIGNATURE, 8)) {
		RS_DBG("check signature error!");
		rtl->dl_fw_flag = 0;
		goto free_buf;
	}

	if (memcmp
	    (rtl->fw_buf + rtl->fw_len - 4,
	     Extension_Section_SIGNATURE, 4)) {
		RS_ERR("check extension section signature error");
		rtl->dl_fw_flag = 0;
		goto free_buf;
	}

	proj_id = rtk_get_fw_project_id(rtl->fw_buf + rtl->fw_len - 5);

#ifdef RTL_8703A_SUPPORT
	if (rtl->hci_ver == 0x4 && rtl->lmp_subver == ROM_LMP_8723b) {
		RS_DBG("HCI version = 0x4, IC is 8703A.");
	} else {
		RS_ERR("error: lmp_version %x, hci_version %x, project_id %x",
		       rtl->lmp_subver, rtl->hci_ver, project_id[proj_id]);
		rtk_hw_cfg.dl_fw_flag = 0;
		goto free_buf;
	}
#else
	if (rtl->lmp_subver != ROM_LMP_8703b) {
		if (rtl->lmp_subver != project_id[proj_id]) {
			RS_ERR("lmp_subver %04x, project id %04x, mismatch\n",
			       rtl->lmp_subver, project_id[proj_id]);
			rtl->dl_fw_flag = 0;
			goto free_buf;
		}
	} else {
		/* if (rtk_hw_cfg.patch_ent->proj_id != project_id[proj_id]) {
		 * 	RS_ERR("proj_id %04x, version %04x from firmware "
		 * 	       "project_id[%u], mismatch\n",
		 * 	       rtk_hw_cfg.patch_ent->proj_id,
		 * 	       project_id[proj_id], proj_id);
		 * 	rtk_hw_cfg.dl_fw_flag = 0;
		 * 	goto free_buf;
		 * } */
	}
#endif

	entry = rtk_get_patch_entry();

	if (entry)
		rtl->total_len =
		    entry->patch_length + rtl->config_len;
	else {
		rtl->dl_fw_flag = 0;
		goto free_buf;
	}

	if (!(rtl->total_buf = malloc(rtl->total_len))) {
		RS_ERR("Can't alloc memory for multi fw&config, errno:%d",
		       errno);
		rtl->dl_fw_flag = 0;
		rtl->total_len = 0;
		goto free_buf;
	} else {
		memcpy(rtl->total_buf,
		       rtl->fw_buf + entry->start_offset,
		       entry->patch_length);
		memcpy(rtl->total_buf + entry->patch_length - 4,
		       &patch->fw_version, 4);

		if (rtl->config_len)
			memcpy(rtl->total_buf + entry->patch_length,
			       rtl->config_buf, rtl->config_len);
		rtl->dl_fw_flag = 1;
	}

	RS_DBG("fw:%s exists, config file:%s exists",
	       (rtl->fw_len > 0) ? "" : "not",
	       (rtl->config_len > 0) ? "" : "not");

free_buf:
	if (rtl->fw_len > 0) {
		free(rtl->fw_buf);
		rtl->fw_len = 0;
	}

	if (rtl->config_len > 0) {
		free(rtl->config_buf);
		rtl->config_len = 0;
	}

	if (entry)
		free(entry);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq 0x8620(%rip), %r13      # 0xf748
testl %esi, %esi
setne %cl
movzwl 0x85d6(%rip), %eax      # 0xf70a
cmpl $0x8723, %eax           # imm = 0x8723
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x72cc
cmpl $0x2, %esi
sete %cl
cmpl $0x1200, %eax           # imm = 0x1200
sete %al
andb %cl, %al
cmpb $0x1, %al
je 0x72cc
movabsq $0x686365746c616552, %rax # imm = 0x686365746C616552
cmpq %rax, (%r13)
je 0x71ed
cmpb $0x0, 0x816a(%rip)      # 0xf2e0
jne 0x7318
movb $0x0, 0x8599(%rip)      # 0xf71c
xorl %ebx, %ebx
cmpl $0x0, 0x85b0(%rip)      # 0xf73c
jle 0x71a4
movq 0x85b3(%rip), %rdi      # 0xf748
callq 0x2030
movl $0x0, 0x8598(%rip)      # 0xf73c
cmpl $0x0, 0x8595(%rip)      # 0xf740
jle 0x71c3
movq 0x859c(%rip), %rdi      # 0xf750
callq 0x2030
movl $0x0, 0x857d(%rip)      # 0xf740
testq %rbx, %rbx
je 0x71de
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2030
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq 0x8548(%rip), %rdi      # 0xf73c
cmpl $0x77fd0451, -0x4(%r13,%rdi) # imm = 0x77FD0451
jne 0x73f3
addq %r13, %rdi
addq $-0x5, %rdi
callq 0x6ea2
movzwl 0x84f4(%rip), %ecx      # 0xf70a
cmpl $0x8703, %ecx           # imm = 0x8703
je 0x7237
movzbl %al, %eax
leaq 0x80c8(%rip), %rdx      # 0xf2f0
movzwl (%rdx,%rax,2), %r8d
cmpw %r8w, %cx
jne 0x7407
callq 0x6f44
testq %rax, %rax
je 0x717c
movq %rax, %rbx
movzwl 0x2(%rax), %r15d
movslq 0x84ec(%rip), %r14      # 0xf740
leaq (%r14,%r15), %rdi
movl %edi, 0x84e6(%rip)      # 0xf744
callq 0x2190
movq %rax, 0x84ee(%rip)      # 0xf758
testq %rax, %rax
je 0x73fd
movq %rax, %r12
movl 0x4(%rbx), %esi
addq 0x84c8(%rip), %rsi      # 0xf748
movq %rax, %rdi
movq %r15, %rdx
callq 0x2180
movl 0x8(%r13), %eax
movl %eax, -0x4(%r12,%r15)
testl %r14d, %r14d
je 0x72ae
addq %r15, %r12
movq 0x84ad(%rip), %rsi      # 0xf750
movq %r12, %rdi
movq %r14, %rdx
callq 0x2180
movb $0x1, 0x8467(%rip)      # 0xf71c
cmpb $0x0, 0x8024(%rip)      # 0xf2e0
je 0x7185
callq 0x349d
jmp 0x7185
movabsq $0x686365746c616552, %rax # imm = 0x686365746C616552
cmpq %rax, (%r13)
jne 0x72e6
callq 0x3594
jmp 0x7183
movslq 0x8453(%rip), %rax      # 0xf740
movslq 0x8448(%rip), %rdi      # 0xf73c
addq %rax, %rdi
movl %edi, 0x8447(%rip)      # 0xf744
callq 0x2190
movq %rax, 0x844f(%rip)      # 0xf758
testq %rax, %rax
jne 0x7322
callq 0x3550
jmp 0x7183
callq 0x344d
jmp 0x717c
cmpb $0x0, 0x7fb7(%rip)      # 0xf2e0
je 0x7330
callq 0x352c
movq 0x8411(%rip), %rax      # 0xf748
movslq 0x83fe(%rip), %rdx      # 0xf73c
movl -0x8(%rax,%rdx), %ecx
movzwl -0xc(%rax,%rdx), %ebx
movzbl -0xa(%rax,%rdx), %r15d
movzbl -0x9(%rax,%rdx), %r12d
movq 0x7c9e(%rip), %r13      # 0xeff8
movq (%r13), %rdi
leaq 0x2aaa(%rip), %rsi      # 0x9e0f
leaq 0x21cd(%rip), %r14      # 0x9539
movq %r14, %rdx
xorl %eax, %eax
callq 0x2150
movl %r12d, %eax
andl $0x7, %eax
shll $0x8, %eax
orl %r15d, %eax
shrl $0x3, %r12d
imull $0x2710, %r12d, %ecx    # imm = 0x2710
addl %eax, %ecx
movq (%r13), %rdi
leaq 0x2a8c(%rip), %rsi      # 0x9e26
movq %r14, %rdx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x2150
movq 0x83aa(%rip), %rdi      # 0xf758
movq 0x8393(%rip), %rsi      # 0xf748
movslq 0x8380(%rip), %rdx      # 0xf73c
callq 0x2180
movslq 0x8378(%rip), %rdx      # 0xf740
testq %rdx, %rdx
je 0x73e7
movslq 0x8368(%rip), %rdi      # 0xf73c
addq 0x837d(%rip), %rdi      # 0xf758
movq 0x836e(%rip), %rsi      # 0xf750
callq 0x2180
movb $0x1, 0x832e(%rip)      # 0xf71c
jmp 0x7183
callq 0x3471
jmp 0x7183
callq 0x34e8
jmp 0x7185
movq 0x7bea(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x2b0c(%rip), %rsi      # 0x9f24
leaq 0x211a(%rip), %rdx      # 0x9539
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2150
movb $0x0, 0x82ed(%rip)      # 0xf71c
jmp 0x7185