// File: /workspace/github_c_makefile_star2_O0/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: rtk_download_fw_config
static int rtk_download_fw_config(int fd, RT_U8 * buf, size_t filesize,
				  int is_sent_changerate, int proto,
				  struct termios *ti)
{
	uint8_t iCurIndex = 0;
	uint8_t iCurLen = 0;
	uint8_t iEndIndex = 0;
	uint8_t iLastPacketLen = 0;
	uint8_t iAdditionPkt = 0;
	uint8_t iTotalIndex = 0;
	uint8_t iCmdSentNum = 0;
	unsigned char *bufpatch;
	uint8_t i, j;

	iEndIndex = (uint8_t) ((filesize - 1) / PATCH_DATA_FIELD_MAX_SIZE);
	iLastPacketLen = (filesize) % PATCH_DATA_FIELD_MAX_SIZE;

	/* if (is_sent_changerate)
	 * 	iCmdSentNum++;
	 * if (rtk_hw_cfg.rom_version_cmd_state >= cmd_has_sent)
	 * 	iCmdSentNum++;
	 * if (rtk_hw_cfg.hci_version_cmd_state >= cmd_has_sent)
	 * 	iCmdSentNum++; */
	iCmdSentNum += rtk_hw_cfg.num_of_cmd_sent;

	iAdditionPkt =
	    (iEndIndex + 1 + iCmdSentNum) % 8 ? (8 -
						 (iEndIndex + 1 +
						  iCmdSentNum) % 8) : 0;
	iTotalIndex = iAdditionPkt + iEndIndex;
	rtk_hw_cfg.total_num = iTotalIndex;	//init TotalIndex

	RS_DBG("iEndIndex:%d  iLastPacketLen:%d iAdditionpkt:%d\n", iEndIndex,
	       iLastPacketLen, iAdditionPkt);

	if (iLastPacketLen == 0)
		iLastPacketLen = PATCH_DATA_FIELD_MAX_SIZE;

	bufpatch = buf;

	for (i = 0; i <= iTotalIndex; i++) {
		/* Index will roll over when it reaches 0x80. */
		if (i > 0x7f)
			j = (i & 0x7f) + 1;
		else
			j = i;

		if (i < iEndIndex) {
			iCurIndex = j;
			iCurLen = PATCH_DATA_FIELD_MAX_SIZE;
		} else if (i == iEndIndex) {
			/* Send last data packets */
			if (i == iTotalIndex)
				iCurIndex = j | 0x80;
			else
				iCurIndex = j;
			iCurLen = iLastPacketLen;
		} else if (i < iTotalIndex) {
			/* Send additional packets */
			iCurIndex = j;
			bufpatch = NULL;
			iCurLen = 0;
			RS_DBG("Send additional packet %u", iCurIndex);
		} else {
			/* Send end packet */
			iCurIndex = j | 0x80;
			bufpatch = NULL;
			iCurLen = 0;
			RS_DBG("Send end packet %u", iCurIndex);
		}

		if (iCurIndex & 0x80)
			RS_DBG("Send FW last command");

		if (proto == HCI_UART_H4) {
			iCurIndex = hci_download_patch_h4(fd, iCurIndex,
							  bufpatch, iCurLen);
			if ((iCurIndex != j) && (i != rtk_hw_cfg.total_num)) {
				RS_DBG(
				  "index mismatch j %d, iCurIndex:%d, fail\n",
				  j, iCurIndex);
				return -1;
			}
		} else if (proto == HCI_UART_3WIRE) {
			if (hci_download_patch(fd, iCurIndex, bufpatch, iCurLen,
					       ti) < 0)
				return -1;
		}

		if (iCurIndex < iEndIndex) {
			bufpatch += PATCH_DATA_FIELD_MAX_SIZE;
		}
	}

	/* Set last ack packet down */
	if (proto == HCI_UART_3WIRE) {
		rtk_send_pure_ack_down(fd);
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movb $0x0, -0x29(%rbp)
movb $0x0, -0x2a(%rbp)
movb $0x0, -0x2b(%rbp)
movb $0x0, -0x2c(%rbp)
movb $0x0, -0x2d(%rbp)
movb $0x0, -0x2e(%rbp)
movb $0x0, -0x2f(%rbp)
movq -0x18(%rbp), %rax
subq $0x1, %rax
movl $0xfc, %ecx
xorl %edx, %edx
divq %rcx
movb %al, -0x2b(%rbp)
movq -0x18(%rbp), %rax
movl $0xfc, %ecx
xorl %edx, %edx
divq %rcx
movb %dl, %al
movb %al, -0x2c(%rbp)
movzwl 0x697f(%rip), %ecx      # 0x106f8
movzbl -0x2f(%rbp), %eax
addl %ecx, %eax
movb %al, -0x2f(%rbp)
movzbl -0x2b(%rbp), %eax
addl $0x1, %eax
movzbl -0x2f(%rbp), %ecx
addl %ecx, %eax
movl $0x8, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x9dbd
movzbl -0x2b(%rbp), %eax
addl $0x1, %eax
movzbl -0x2f(%rbp), %ecx
addl %ecx, %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl $0x8, %eax
subl %edx, %eax
movl %eax, -0x40(%rbp)
jmp 0x9dc4
xorl %eax, %eax
movl %eax, -0x40(%rbp)
jmp 0x9dc4
movl -0x40(%rbp), %eax
movb %al, -0x2d(%rbp)
movzbl -0x2d(%rbp), %eax
movzbl -0x2b(%rbp), %ecx
addl %ecx, %eax
movb %al, -0x2e(%rbp)
movzbl -0x2e(%rbp), %eax
movl %eax, 0x693f(%rip)      # 0x10720
cmpb $0x0, 0x64e8(%rip)      # 0x102d0
je 0x9e17
movq 0x6207(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzbl -0x2b(%rbp), %ecx
movzbl -0x2c(%rbp), %r8d
movzbl -0x2d(%rbp), %r9d
leaq 0x3202(%rip), %rsi      # 0xd00b
leaq 0x1779(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0x9e19
movzbl -0x2c(%rbp), %eax
cmpl $0x0, %eax
jne 0x9e26
movb $-0x4, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movb $0x0, -0x39(%rbp)
movzbl -0x39(%rbp), %eax
movzbl -0x2e(%rbp), %ecx
cmpl %ecx, %eax
jg 0xa056
movzbl -0x39(%rbp), %eax
cmpl $0x7f, %eax
jle 0x9e5a
movzbl -0x39(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movb %al, -0x3a(%rbp)
jmp 0x9e60
movb -0x39(%rbp), %al
movb %al, -0x3a(%rbp)
movzbl -0x39(%rbp), %eax
movzbl -0x2b(%rbp), %ecx
cmpl %ecx, %eax
jge 0x9e7b
movb -0x3a(%rbp), %al
movb %al, -0x29(%rbp)
movb $-0x4, -0x2a(%rbp)
jmp 0x9f4c
movzbl -0x39(%rbp), %eax
movzbl -0x2b(%rbp), %ecx
cmpl %ecx, %eax
jne 0x9eb2
movzbl -0x39(%rbp), %eax
movzbl -0x2e(%rbp), %ecx
cmpl %ecx, %eax
jne 0x9ea1
movzbl -0x3a(%rbp), %eax
orl $0x80, %eax
movb %al, -0x29(%rbp)
jmp 0x9ea7
movb -0x3a(%rbp), %al
movb %al, -0x29(%rbp)
movb -0x2c(%rbp), %al
movb %al, -0x2a(%rbp)
jmp 0x9f4a
movzbl -0x39(%rbp), %eax
movzbl -0x2e(%rbp), %ecx
cmpl %ecx, %eax
jge 0x9f00
movb -0x3a(%rbp), %al
movb %al, -0x29(%rbp)
movq $0x0, -0x38(%rbp)
movb $0x0, -0x2a(%rbp)
cmpb $0x0, 0x63f9(%rip)      # 0x102d0
je 0x9efc
movq 0x6118(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzbl -0x29(%rbp), %ecx
leaq 0x3153(%rip), %rsi      # 0xd041
leaq 0x1694(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0x9efe
jmp 0x9f48
movzbl -0x3a(%rbp), %eax
orl $0x80, %eax
movb %al, -0x29(%rbp)
movq $0x0, -0x38(%rbp)
movb $0x0, -0x2a(%rbp)
cmpb $0x0, 0x63b1(%rip)      # 0x102d0
je 0x9f44
movq 0x60d0(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzbl -0x29(%rbp), %ecx
leaq 0x312a(%rip), %rsi      # 0xd060
leaq 0x164c(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0x9f46
jmp 0x9f48
jmp 0x9f4a
jmp 0x9f4c
movzbl -0x29(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x9f88
jmp 0x9f5c
cmpb $0x0, 0x636d(%rip)      # 0x102d0
je 0x9f84
movq 0x608c(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x3102(%rip), %rsi      # 0xd078
leaq 0x160c(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0x9f86
jmp 0x9f88
cmpl $0x0, -0x20(%rbp)
jne 0x9ffd
movl -0x8(%rbp), %edi
movzbl -0x29(%rbp), %esi
movq -0x38(%rbp), %rdx
movzbl -0x2a(%rbp), %ecx
callq 0xa1c0
movb %al, -0x29(%rbp)
movzbl -0x29(%rbp), %eax
movzbl -0x3a(%rbp), %ecx
cmpl %ecx, %eax
je 0x9ffb
movzbl -0x39(%rbp), %eax
cmpl 0x6765(%rip), %eax      # 0x10720
je 0x9ffb
jmp 0x9fbf
cmpb $0x0, 0x630a(%rip)      # 0x102d0
je 0x9ff0
movq 0x6029(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzbl -0x3a(%rbp), %ecx
movzbl -0x29(%rbp), %r8d
leaq 0x30b0(%rip), %rsi      # 0xd092
leaq 0x15a0(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0x9ff2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa06b
jmp 0xa02d
cmpl $0x2, -0x20(%rbp)
jne 0xa02b
movl -0x8(%rbp), %edi
movzbl -0x29(%rbp), %esi
movq -0x38(%rbp), %rdx
movzbl -0x2a(%rbp), %ecx
movq -0x28(%rbp), %r8
callq 0xa480
cmpl $0x0, %eax
jge 0xa029
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa06b
jmp 0xa02b
jmp 0xa02d
movzbl -0x29(%rbp), %eax
movzbl -0x2b(%rbp), %ecx
cmpl %ecx, %eax
jge 0xa047
movq -0x38(%rbp), %rax
addq $0xfc, %rax
movq %rax, -0x38(%rbp)
jmp 0xa049
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x9e32
cmpl $0x2, -0x20(%rbp)
jne 0xa064
movl -0x8(%rbp), %edi
callq 0x62b0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)