// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
leaq 0x953b(%rip), %rax      # 0x106d0
movq %rax, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0x28(%rbp)
cmpl $0x0, -0x8(%rbp)
jne 0x71c6
movq -0x10(%rbp), %rax
movzwl 0x2a(%rax), %eax
cmpl $0x8723, %eax           # imm = 0x8723
jne 0x71e3
cmpl $0x2, -0x8(%rbp)
jne 0x73dd
movq -0x10(%rbp), %rax
movzwl 0x2a(%rax), %eax
cmpl $0x1200, %eax           # imm = 0x1200
jne 0x73dd
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq (%rax), %rax
movabsq $0x686365746c616552, %rcx # imm = 0x686365746C616552
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x7234
jmp 0x7208
movq 0x8de9(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x4ca5(%rip), %rsi      # 0xbebe
leaq 0x4369(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movq -0x10(%rbp), %rax
movb $0x0, 0x3c(%rax)
jmp 0x76a8
movq -0x10(%rbp), %rax
movl 0x60(%rax), %ecx
movq -0x10(%rbp), %rax
addl 0x5c(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x64(%rax)
movq -0x10(%rbp), %rax
movslq 0x64(%rax), %rdi
callq 0x2190
movq -0x10(%rbp), %rcx
movq %rax, 0x78(%rcx)
cmpq $0x0, %rax
jne 0x72ac
jmp 0x7266
movq 0x8d8b(%rip), %rax      # 0xfff8
movq (%rax), %rax
movq %rax, -0x40(%rbp)
callq 0x2040
movq -0x40(%rbp), %rdi
movl (%rax), %ecx
leaq 0x4c5a(%rip), %rsi      # 0xbee0
leaq 0x42fc(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movq -0x10(%rbp), %rax
movb $0x0, 0x3c(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x64(%rax)
jmp 0x76a8
jmp 0x72ae
cmpb $0x0, 0x901b(%rip)      # 0x102d0
je 0x72d6
movq 0x8d3a(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x4c4b(%rip), %rsi      # 0xbf13
leaq 0x42ba(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0x72d8
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rdi
movq -0x10(%rbp), %rax
movslq 0x5c(%rax), %rax
addq %rax, %rdi
addq $-0x8, %rdi
callq 0x4ab0
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rdi
movq -0x10(%rbp), %rax
movslq 0x5c(%rax), %rax
addq %rax, %rdi
addq $-0xc, %rdi
callq 0x4ab0
movl %eax, -0x30(%rbp)
movq 0x8cdb(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl -0x2c(%rbp), %ecx
leaq 0x4b38(%rip), %rsi      # 0xbe62
leaq 0x4258(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movl -0x30(%rbp), %eax
shrl $0x10, %eax
andl $0x7ff, %eax            # imm = 0x7FF
movl -0x30(%rbp), %ecx
shrl $0x1b, %ecx
imull $0x2710, %ecx, %ecx     # imm = 0x2710
addl %ecx, %eax
movl %eax, -0x34(%rbp)
movq 0x8c9d(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl -0x34(%rbp), %ecx
movl -0x30(%rbp), %r8d
andl $0xffff, %r8d           # imm = 0xFFFF
leaq 0x4b06(%rip), %rsi      # 0xbe79
leaq 0x420f(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rsi
movq -0x10(%rbp), %rax
movslq 0x5c(%rax), %rdx
callq 0x2180
movq -0x10(%rbp), %rax
cmpl $0x0, 0x60(%rax)
je 0x73d0
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x10(%rbp), %rax
movslq 0x5c(%rax), %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rsi
movq -0x10(%rbp), %rax
movslq 0x60(%rax), %rdx
callq 0x2180
movq -0x10(%rbp), %rax
movb $0x1, 0x3c(%rax)
jmp 0x76a8
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq (%rax), %rax
movabsq $0x686365746c616552, %rcx # imm = 0x686365746C616552
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x7439
jmp 0x7402
cmpb $0x0, 0x8ec7(%rip)      # 0x102d0
je 0x742a
movq 0x8be6(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x4b0c(%rip), %rsi      # 0xbf28
leaq 0x4166(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0x742c
movq -0x10(%rbp), %rax
movb $0x0, 0x3c(%rax)
jmp 0x76a8
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x5c(%rcx), %rcx
addq %rcx, %rax
addq $-0x4, %rax
movl (%rax), %eax
subl $0x77fd0451, %eax       # imm = 0x77FD0451
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x7490
jmp 0x7464
movq 0x8b8d(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x4acf(%rip), %rsi      # 0xbf44
leaq 0x410d(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movq -0x10(%rbp), %rax
movb $0x0, 0x3c(%rax)
jmp 0x76a8
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rdi
movq -0x10(%rbp), %rax
movslq 0x5c(%rax), %rax
addq %rax, %rdi
addq $-0x5, %rdi
callq 0x6d00
movb %al, -0x11(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x2a(%rax), %eax
cmpl $0x8703, %eax           # imm = 0x8703
je 0x7525
movq -0x10(%rbp), %rax
movzwl 0x2a(%rax), %eax
movzbl -0x11(%rbp), %ecx
movl %ecx, %edx
leaq 0x8e0d(%rip), %rcx      # 0x102e0
movzwl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
je 0x7523
jmp 0x74dd
movq 0x8b14(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movzwl 0x2a(%rax), %ecx
movzbl -0x11(%rbp), %eax
movl %eax, %edx
leaq 0x8de4(%rip), %rax      # 0x102e0
movzwl (%rax,%rdx,2), %r8d
leaq 0x4a6f(%rip), %rsi      # 0xbf77
leaq 0x407a(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movq -0x10(%rbp), %rax
movb $0x0, 0x3c(%rax)
jmp 0x76a8
jmp 0x7527
jmp 0x7527
callq 0x6de0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x754f
movq -0x20(%rbp), %rax
movzwl 0x2(%rax), %ecx
movq -0x10(%rbp), %rax
addl 0x60(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x64(%rax)
jmp 0x755c
movq -0x10(%rbp), %rax
movb $0x0, 0x3c(%rax)
jmp 0x76a8
movq -0x10(%rbp), %rax
movslq 0x64(%rax), %rdi
callq 0x2190
movq -0x10(%rbp), %rcx
movq %rax, 0x78(%rcx)
cmpq $0x0, %rax
jne 0x75bf
jmp 0x7579
movq 0x8a78(%rip), %rax      # 0xfff8
movq (%rax), %rax
movq %rax, -0x48(%rbp)
callq 0x2040
movq -0x48(%rbp), %rdi
movl (%rax), %ecx
leaq 0x4a15(%rip), %rsi      # 0xbfae
leaq 0x3fe9(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movq -0x10(%rbp), %rax
movb $0x0, 0x3c(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x64(%rax)
jmp 0x76a8
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rsi
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
addq %rax, %rsi
movq -0x20(%rbp), %rax
movzwl 0x2(%rax), %eax
movl %eax, %edx
callq 0x2180
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movq -0x20(%rbp), %rcx
movzwl 0x2(%rcx), %ecx
movslq %ecx, %rcx
movq -0x28(%rbp), %rdx
movl 0x8(%rdx), %edx
movl %edx, -0x4(%rax,%rcx)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x60(%rax)
je 0x763a
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x20(%rbp), %rax
movzwl 0x2(%rax), %eax
cltq
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rsi
movq -0x10(%rbp), %rax
movslq 0x60(%rax), %rdx
callq 0x2180
movq -0x10(%rbp), %rax
movb $0x1, 0x3c(%rax)
jmp 0x7644
cmpb $0x0, 0x8c85(%rip)      # 0x102d0
je 0x76a4
movq 0x89a4(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x5c(%rax), %edx
leaq 0x49ae(%rip), %rcx      # 0xc013
leaq 0x49ce(%rip), %rax      # 0xc03a
cmpl $0x0, %edx
cmovgq %rax, %rcx
movq -0x10(%rbp), %rax
movl 0x60(%rax), %edx
leaq 0x4992(%rip), %r8       # 0xc013
leaq 0x49b2(%rip), %rax      # 0xc03a
cmpl $0x0, %edx
cmovgq %rax, %r8
leaq 0x4954(%rip), %rsi      # 0xbfea
leaq 0x3eec(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0x76a6
jmp 0x76a8
movq -0x10(%rbp), %rax
cmpl $0x0, 0x5c(%rax)
jle 0x76ca
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0x2030
movq -0x10(%rbp), %rax
movl $0x0, 0x5c(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x60(%rax)
jle 0x76ec
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x2030
movq -0x10(%rbp), %rax
movl $0x0, 0x60(%rax)
cmpq $0x0, -0x20(%rbp)
je 0x76fc
movq -0x20(%rbp), %rdi
callq 0x2030
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)