// File: /workspace/github_cpp_makefile_star2_O3/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, 0x2b(%rax)
movaps %xmm0, 0x1b(%rax)
movaps %xmm0, 0xb(%rax)
movaps %xmm0, -0x5(%rax)
movb $0x0, 0x3b(%rax)
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 0xd679
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 0xd626
movzbl 0x23(%rsp), %eax
cmpl %eax, %r13d
jne 0xd52a
movzbl 0x24(%rsp), %r12d
movl %ebx, %r9d
subl %r14d, %r9d
cmpl %r12d, %r9d
jne 0xd58b
movl %r14d, %edi
addq 0x18(%rsp), %rdi
movl %r12d, %edx
leaq 0x25(%rsp), %rsi
callq 0x5090
addl %r12d, %r14d
cmpl %ebx, %r14d
je 0xd668
incl %r13d
cmpl $0x6, 0xc(%rsp)
jl 0xd4c2
jmp 0xd668
movl 0xc(%rsp), %r8d
cmpl $0x5, %r8d
jg 0xd6b3
incl %r8d
leaq 0x13d4e(%rip), %rcx     # 0x21291
cmpb $0x1, (%rcx)
movl %r8d, 0xc(%rsp)
jne 0xd577
movq 0x13a64(%rip), %rcx     # 0x20fb8
movq (%rcx), %rdi
movl %eax, (%rsp)
leaq 0xaefb(%rip), %rsi      # 0x1845c
leaq 0xaebc(%rip), %rdx      # 0x18424
movl $0xa7, %ecx
movl %r13d, %r9d
xorl %eax, %eax
callq 0x5180
movl 0x8(%rsp), %r14d
movl 0x10(%rsp), %r12d
movl 0x14(%rsp), %r13d
jmp 0xd614
leaq 0x13cff(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xd5be
movq 0x13a1a(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xaef3(%rip), %rsi      # 0x1849b
leaq 0xae75(%rip), %rdx      # 0x18424
movl $0xb4, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
movl 0xc(%rsp), %r8d
cmpl $0x5, %r8d
movl 0x14(%rsp), %r13d
jg 0xd6ba
incl %r8d
leaq 0x13cb5(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
movl %r8d, 0xc(%rsp)
jne 0xd60a
movq 0x139cb(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xaed0(%rip), %rsi      # 0x184c7
leaq 0xae26(%rip), %rdx      # 0x18424
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 0xd481
movl %eax, %r12d
leaq 0x13c61(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0xd661
movq 0x1397c(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xade6(%rip), %rsi      # 0x1842c
leaq 0xadd7(%rip), %rdx      # 0x18424
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 0x13c0c(%rip), %rcx     # 0x21291
cmpb $0x1, (%rcx)
jne 0xd668
movq 0x13927(%rip), %rcx     # 0x20fb8
movq (%rcx), %rdi
leaq 0xad4e(%rip), %rsi      # 0x183e9
leaq 0xad82(%rip), %rdx      # 0x18424
movl $0x9b, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0xd668
movl $0xfffffe0c, %ebx       # imm = 0xFFFFFE0C
jmp 0xd668
movl $0xfffffe0b, %ebx       # imm = 0xFFFFFE0B
jmp 0xd668
nop