// File: /workspace/github_cpp_makefile_star2_O1/goodix[P]gdix_hid_firmware_update/gtx5/gtx5.cpp
// Function: GTx5Device::ReadPkg(unsigned int, unsigned char*, unsigned int)
int GTx5Device::ReadPkg(unsigned int addr, unsigned char *buf, unsigned int len)
{
	int ret;
	int retry = 0;
	unsigned char HidBuf[65] = {0x0e, _I2C_DIRECT_RW, 0, 0, 5, 1};
	unsigned int pkg_index = 0;
	unsigned int read_data_len = 0;
re_start:
	pkg_index = 0;
	read_data_len = 0;
	HidBuf[0] = 0x0e;
	HidBuf[1] = _I2C_DIRECT_RW;
	HidBuf[2] = 0;
	HidBuf[3] = 0;
	HidBuf[4] = 5;
	HidBuf[5] = 1; /* read operation flag */
	HidBuf[6] = (addr >> 8) & 0xff;
	HidBuf[7] = addr & 0xff;
	HidBuf[8] = (len >> 8) & 0xff;
	HidBuf[9] = len & 0xff;
	ret = Write(HidBuf, 10);

	if (ret < 0) {
		gdix_dbg("Failed send read start package, ret = %d\n", ret);
		return -1;
	}
	do {
		ret = GetReport(0x0e, HidBuf);
		if (ret) {
			gdix_dbg("Failed read addr=0x%x, len=%d\n", addr, len);
			break;
		} else {
			if (pkg_index != HidBuf[3]) {
				if (retry++ < GDIX_RETRY_TIMES) {
					gdix_dbg("Read retry %d, pkg_index %d != HidBuf[3](%d)\n",
							 retry, pkg_index, HidBuf[3]);
					usleep(1000);
					goto re_start;
				}
				ret = -E_HID_PKG_INDEX;
				break;
			} else {
				if (HidBuf[4] == len - read_data_len) {
					memcpy(buf + read_data_len, &HidBuf[5], HidBuf[4]);
					read_data_len += HidBuf[4];
					pkg_index++;
				} else {
					gdix_dbg("Data length err: %d != %d\n", HidBuf[4],
							 len - read_data_len);
					gdix_dbg_array(HidBuf, 6);
					if (retry++ < GDIX_RETRY_TIMES) {
						gdix_dbg("Read retry: %d\n", retry);
						usleep(1000);
						goto re_start;
					}
					ret = -E_HID_PKG_LEN;
					break;
				}
			}
		}
	} while (read_data_len != len && (retry < GDIX_RETRY_TIMES));
	if (ret < 0)
		return ret;
	else
		return len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %ebx
movq %rdx, 0x18(%rsp)
movl %esi, %r14d
movq %rdi, %r15
xorps %xmm0, %xmm0
leaq 0x25(%rsp), %rax
movaps %xmm0, -0x5(%rax)
movaps %xmm0, 0x2b(%rax)
movaps %xmm0, 0x1b(%rax)
movaps %xmm0, 0xb(%rax)
movb $0x0, 0x3b(%rax)
movw $0x200e, -0x5(%rax)     # imm = 0x200E
movw $0x105, -0x1(%rax)      # imm = 0x105
movl %esi, %r12d
shrl $0x8, %r12d
movl %ecx, %r13d
shrl $0x8, %r13d
leaq 0x20(%rsp), %rsi
movl $0x0, 0xc(%rsp)
movl %r14d, 0x8(%rsp)
movl %r12d, 0x10(%rsp)
movl %r13d, 0x14(%rsp)
movl $0x200e, 0x20(%rsp)     # imm = 0x200E
movw $0x105, 0x24(%rsp)      # imm = 0x105
movb %r12b, 0x26(%rsp)
movb %r14b, 0x27(%rsp)
movb %r13b, 0x28(%rsp)
movb %bl, 0x29(%rsp)
movq (%r15), %rax
movq %r15, %rdi
movq %rsi, %rbp
movl $0xa, %edx
callq *0x38(%rax)
testl %eax, %eax
js 0xd760
xorl %r13d, %r13d
xorl %r14d, %r14d
movq (%r15), %rax
movq %r15, %rdi
movl $0xe, %esi
movq %rbp, %rdx
callq *0x28(%rax)
testl %eax, %eax
jne 0xd70d
movzbl 0x23(%rsp), %eax
cmpl %eax, %r13d
jne 0xd611
movzbl 0x24(%rsp), %r12d
movl %ebx, %r9d
subl %r14d, %r9d
cmpl %r12d, %r9d
jne 0xd672
movl %r14d, %edi
addq 0x18(%rsp), %rdi
movl %r12d, %edx
leaq 0x25(%rsp), %rsi
callq 0x5090
addl %r12d, %r14d
xorl %r12d, %r12d
cmpl %ebx, %r14d
je 0xd748
incl %r13d
cmpl $0x6, 0xc(%rsp)
jl 0xd5a6
jmp 0xd748
movl 0xc(%rsp), %r8d
cmpl $0x5, %r8d
jg 0xd79a
incl %r8d
leaq 0x13c67(%rip), %rcx     # 0x21291
cmpb $0x1, (%rcx)
movl %r8d, 0xc(%rsp)
jne 0xd65e
movq 0x1397d(%rip), %rcx     # 0x20fb8
movq (%rcx), %rdi
movl %eax, (%rsp)
leaq 0xae4e(%rip), %rsi      # 0x18496
leaq 0xae0f(%rip), %rdx      # 0x1845e
movl $0xa7, %ecx
movl %r13d, %r9d
xorl %eax, %eax
callq 0x5180
movl 0x8(%rsp), %r14d
movl 0x10(%rsp), %r12d
movl 0x14(%rsp), %r13d
jmp 0xd6fb
leaq 0x13c18(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xd6a5
movq 0x13933(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xae46(%rip), %rsi      # 0x184d5
leaq 0xadc8(%rip), %rdx      # 0x1845e
movl $0xb4, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
movl 0xc(%rsp), %r8d
cmpl $0x5, %r8d
movl 0x14(%rsp), %r13d
jg 0xd7a2
incl %r8d
leaq 0x13bce(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
movl %r8d, 0xc(%rsp)
jne 0xd6f1
movq 0x138e4(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xae23(%rip), %rsi      # 0x18501
leaq 0xad79(%rip), %rdx      # 0x1845e
movl $0xb7, %ecx
xorl %eax, %eax
callq 0x5180
movl 0x8(%rsp), %r14d
movl 0x10(%rsp), %r12d
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x5170
movq %rbp, %rsi
jmp 0xd565
movl %eax, %r12d
leaq 0x13b7a(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xd748
movq 0x13895(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xad39(%rip), %rsi      # 0x18466
leaq 0xad2a(%rip), %rdx      # 0x1845e
movl $0xa1, %ecx
movl 0x8(%rsp), %r8d
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5180
testl %r12d, %r12d
cmovsl %r12d, %ebx
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x13b25(%rip), %rcx     # 0x21291
cmpb $0x1, (%rcx)
jne 0xd74f
movq 0x13840(%rip), %rcx     # 0x20fb8
movq (%rcx), %rdi
leaq 0xaca1(%rip), %rsi      # 0x18423
leaq 0xacd5(%rip), %rdx      # 0x1845e
movl $0x9b, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0xd74f
movl $0xfffffe0c, %r12d      # imm = 0xFFFFFE0C
jmp 0xd748
movl $0xfffffe0b, %r12d      # imm = 0xFFFFFE0B
jmp 0xd748