// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0xa
popq %rdx
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
callq *0x38(%rax)
testl %eax, %eax
js 0xc02e
xorl %r13d, %r13d
xorl %r14d, %r14d
movq (%r15), %rax
movq %r15, %rdi
pushq $0xe
popq %rsi
movq %rbp, %rdx
callq *0x28(%rax)
testl %eax, %eax
jne 0xbfdb
movzbl 0x23(%rsp), %eax
cmpl %eax, %r13d
jne 0xbedc
movzbl 0x24(%rsp), %r12d
movl %ebx, %r9d
subl %r14d, %r9d
cmpl %r12d, %r9d
jne 0xbf3d
movl %r14d, %edi
addq 0x18(%rsp), %rdi
movl %r12d, %edx
leaq 0x25(%rsp), %rsi
callq 0x5090
addl %r12d, %r14d
cmpl %ebx, %r14d
je 0xc01d
incl %r13d
cmpl $0x6, 0xc(%rsp)
jl 0xbe76
jmp 0xc01d
movl 0xc(%rsp), %r8d
cmpl $0x5, %r8d
jg 0xc066
incl %r8d
leaq 0x1439c(%rip), %rcx     # 0x20291
cmpb $0x1, (%rcx)
movl %r8d, 0xc(%rsp)
jne 0xbf29
movq 0x140b2(%rip), %rcx     # 0x1ffb8
movq (%rcx), %rdi
movl %eax, (%rsp)
leaq 0xb549(%rip), %rsi      # 0x1745c
leaq 0xb50a(%rip), %rdx      # 0x17424
movl $0xa7, %ecx
movl %r13d, %r9d
xorl %eax, %eax
callq 0x5180
movl 0x8(%rsp), %r14d
movl 0x10(%rsp), %r12d
movl 0x14(%rsp), %r13d
jmp 0xbfc6
leaq 0x1434d(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xbf70
movq 0x14068(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb541(%rip), %rsi      # 0x1749b
leaq 0xb4c3(%rip), %rdx      # 0x17424
movl $0xb4, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
movl 0xc(%rsp), %r8d
cmpl $0x5, %r8d
movl 0x14(%rsp), %r13d
jg 0xc06d
incl %r8d
leaq 0x14303(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
movl %r8d, 0xc(%rsp)
jne 0xbfbc
movq 0x14019(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb51e(%rip), %rsi      # 0x174c7
leaq 0xb474(%rip), %rdx      # 0x17424
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
pushq $0xa
popq %rdx
jmp 0xbe3a
movl %eax, %r12d
leaq 0x142ac(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xc016
movq 0x13fc7(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xb431(%rip), %rsi      # 0x1742c
leaq 0xb422(%rip), %rdx      # 0x17424
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
pushq $-0x1
popq %rbx
leaq 0x14259(%rip), %rcx     # 0x20291
cmpb $0x1, (%rcx)
jne 0xc01d
movq 0x13f74(%rip), %rcx     # 0x1ffb8
movq (%rcx), %rdi
leaq 0xb39b(%rip), %rsi      # 0x173e9
leaq 0xb3cf(%rip), %rdx      # 0x17424
movl $0x9b, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0xc01d
movl $0xfffffe0c, %ebx       # imm = 0xFFFFFE0C
jmp 0xc01d
movl $0xfffffe0b, %ebx       # imm = 0xFFFFFE0B
jmp 0xc01d