// File: /workspace/github_c_makefile_star2_O0/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: hci_download_patch_h4
static int hci_download_patch_h4(int dd, int index, uint8_t * data, int len)
{
	unsigned char bytes[257] = { 0 };
	unsigned char buf[257] = { 0x01, 0x20, 0xfc, 00 };
	uint16_t readbytes = 0;
	int cur_index = index;
	int ret_Index = -1;
	uint16_t res = 0;
	int i = 0;
	size_t total_len;
	uint16_t w_len;
	uint8_t rstatus;
	int ret;
	uint8_t opcode[2] = {
		0x20, 0xfc,
	};

	RS_DBG("dd:%d, index:%d, len:%d", dd, index, len);
	if (NULL != data) {
		memcpy(&buf[5], data, len);
	}

	buf[3] = len + 1;
	buf[4] = cur_index;
	total_len = len + 5;

	w_len = write(dd, buf, total_len);
	RS_DBG("h4 write success with len: %d\n", w_len);

	ret = read_hci_evt(dd, bytes, 0x0e);
	if (ret < 0) {
		RS_ERR("%s: read hci evt error.\n", __func__);
		return -1;
	}

	/* RS_DBG("%s: bytes: %x %x %x %x %x %x.\n",
	 *      __func__, bytes[0], bytes[1], bytes[2],
	 *      bytes[3], bytes[4], bytes[5]); */

	if ((0x04 == bytes[0]) && (opcode[0] == bytes[4])
	    && (opcode[1] == bytes[5])) {
		ret_Index = bytes[7];
		rstatus = bytes[6];
		RS_DBG("---->ret_Index:%d, ----->rstatus:%d\n", ret_Index,
		       rstatus);
		if (0x00 != rstatus) {
			RS_ERR("---->read event status is wrong\n");
			return -1;
		}
	} else {
		RS_ERR("==========>Didn't read curret data\n");
		return -1;
	}

	return ret_Index;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
leaq -0x120(%rbp), %rdi
xorl %esi, %esi
movl $0x101, %edx            # imm = 0x101
callq 0x20d0
leaq -0x230(%rbp), %rdi
xorl %esi, %esi
movl $0x101, %edx            # imm = 0x101
callq 0x20d0
movb $0x1, -0x230(%rbp)
movb $0x20, -0x22f(%rbp)
movb $-0x4, -0x22e(%rbp)
movw $0x0, -0x232(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x238(%rbp)
movl $0xffffffff, -0x23c(%rbp) # imm = 0xFFFFFFFF
movw $0x0, -0x23e(%rbp)
movl $0x0, -0x244(%rbp)
movw 0x1336(%rip), %ax       # 0xb57f
movw %ax, -0x25a(%rbp)
cmpb $0x0, 0x6079(%rip)      # 0x102d0
je 0xa283
movq 0x5d98(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl -0x8(%rbp), %ecx
movl -0xc(%rbp), %r8d
movl -0x1c(%rbp), %r9d
leaq 0x2e4b(%rip), %rsi      # 0xd0c0
leaq 0x130d(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0xa285
xorl %eax, %eax
cmpq -0x18(%rbp), %rax
je 0xa2a5
leaq -0x230(%rbp), %rdi
addq $0x5, %rdi
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
callq 0x2180
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movb %al, -0x22d(%rbp)
movl -0x238(%rbp), %eax
movb %al, -0x22c(%rbp)
movl -0x1c(%rbp), %eax
addl $0x5, %eax
cltq
movq %rax, -0x250(%rbp)
movl -0x8(%rbp), %edi
leaq -0x230(%rbp), %rsi
movq -0x250(%rbp), %rdx
callq 0x2070
movw %ax, -0x252(%rbp)
cmpb $0x0, 0x5fe0(%rip)      # 0x102d0
je 0xa318
movq 0x5cff(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzwl -0x252(%rbp), %ecx
leaq 0x2dd3(%rip), %rsi      # 0xd0dd
leaq 0x1278(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0xa31a
movl -0x8(%rbp), %edi
leaq -0x120(%rbp), %rsi
movl $0xe, %edx
callq 0x3f70
movl %eax, -0x258(%rbp)
cmpl $0x0, -0x258(%rbp)
jge 0xa371
jmp 0xa33f
movq 0x5cb2(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x2db1(%rip), %rsi      # 0xd101
leaq 0x1232(%rip), %rdx      # 0xb589
leaq 0x2dc7(%rip), %rcx      # 0xd125
movb $0x0, %al
callq 0x2150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa46e
movzbl -0x120(%rbp), %ecx
movl $0x4, %eax
cmpl %ecx, %eax
jne 0xa43b
movzbl -0x25a(%rbp), %eax
movzbl -0x11c(%rbp), %ecx
cmpl %ecx, %eax
jne 0xa43b
movzbl -0x259(%rbp), %eax
movzbl -0x11b(%rbp), %ecx
cmpl %ecx, %eax
jne 0xa43b
movzbl -0x119(%rbp), %eax
movl %eax, -0x23c(%rbp)
movb -0x11a(%rbp), %al
movb %al, -0x253(%rbp)
cmpb $0x0, 0x5eff(%rip)      # 0x102d0
je 0xa400
movq 0x5c1e(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl -0x23c(%rbp), %ecx
movzbl -0x253(%rbp), %r8d
leaq 0x2d49(%rip), %rsi      # 0xd13b
leaq 0x1190(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0xa402
movzbl -0x253(%rbp), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0xa439
jmp 0xa411
movq 0x5be0(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x2d43(%rip), %rsi      # 0xd165
leaq 0x1160(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa46e
jmp 0xa465
jmp 0xa43d
movq 0x5bb4(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x2d43(%rip), %rsi      # 0xd191
leaq 0x1134(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa46e
movl -0x23c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
nopw (%rax,%rax)