// File: /workspace/github_cpp_makefile_star2_O2/goodix[P]gdix_hid_firmware_update/gtx9/gtx9.cpp
// Function: GTx9Device::ReadPkg(unsigned int, unsigned char*, unsigned int)
int GTx9Device::ReadPkg(unsigned int addr, unsigned char *buf, unsigned int len)
{
	uint8_t HidBuf[PACKAGE_LEN] = {0};
	int ret;
	int retry = 5;

	while (retry--) {
		HidBuf[0] = REPORT_ID;			 /* report id */
		HidBuf[1] = I2C_DIRECT_RW;		 /* cmd type */
		HidBuf[2] = 0;					 /* data flag */
		HidBuf[3] = 0;					 /* frame num */
		HidBuf[4] = 7;					 /* data length */
		HidBuf[5] = I2C_READ_FLAG;		 /* read operation flag */
		HidBuf[6] = (addr >> 24) & 0xFF; /* 32 bit addr */
		HidBuf[7] = (addr >> 16) & 0xFF;
		HidBuf[8] = (addr >> 8) & 0xFF;
		HidBuf[9] = addr & 0xFF;
		HidBuf[10] = (len >> 8) & 0xFF; /* 16 bit length */
		HidBuf[11] = len & 0xFF;
		ret = SetReport(HidBuf, 12);
		if (ret < 0) {
			gdix_err("Failed set report, ret:%d\n", ret);
			return ret;
		}
		ret = GetReport(HidBuf);
		if (ret < 0) {
			gdix_err("Failed get report, ret:%d\n", ret);
			return ret;
		}
		if (HidBuf[3] == 0 && HidBuf[4] == len) {
			memcpy(buf, &HidBuf[5], HidBuf[4]);
			return len;
		}
		usleep(1000);
	}

	return -EINVAL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %ebx
movq %rdx, 0x18(%rsp)
movq %rdi, %r12
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, %eax
shrl $0x18, %eax
movl %eax, 0x10(%rsp)
movl %esi, %eax
shrl $0x10, %eax
movl %eax, 0xc(%rsp)
movl %esi, 0x14(%rsp)
shrl $0x8, %esi
movl %esi, 0x8(%rsp)
movl %ecx, %eax
shrl $0x8, %eax
movl %eax, 0x4(%rsp)
pushq $-0x5
popq %r15
leaq 0x20(%rsp), %r14
testl %r15d, %r15d
je 0xee65
movl $0x200e, 0x20(%rsp)     # imm = 0x200E
movw $0x107, 0x24(%rsp)      # imm = 0x107
movl 0x10(%rsp), %eax
movb %al, 0x26(%rsp)
movl 0xc(%rsp), %eax
movb %al, 0x27(%rsp)
movl 0x8(%rsp), %eax
movb %al, 0x28(%rsp)
movl 0x14(%rsp), %eax
movb %al, 0x29(%rsp)
movl 0x4(%rsp), %eax
movb %al, 0x2a(%rsp)
movb %bl, 0x2b(%rsp)
movq %r12, %rdi
movq %r14, %rsi
pushq $0xc
popq %rdx
callq 0xeef0
testl %eax, %eax
js 0xee6b
movq %r12, %rdi
movq %r14, %rbp
movq %r14, %rsi
callq 0xef90
testl %eax, %eax
js 0xee96
cmpb $0x0, 0x23(%rsp)
sete %al
movzbl 0x24(%rsp), %edx
cmpl %ebx, %edx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
je 0xeecc
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x5170
incl %r15d
movq %rbp, %r14
jmp 0xedcf
pushq $-0x16
popq %r13
jmp 0xeede
movl %eax, %r13d
leaq 0x1141c(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xeede
movq 0x11157(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0x8ba6(%rip), %rsi      # 0x17a31
leaq 0x8592(%rip), %rdx      # 0x17424
pushq $0x5f
jmp 0xeebf
movl %eax, %r13d
leaq 0x113f1(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xeede
movq 0x1112c(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0x8ba7(%rip), %rsi      # 0x17a5d
leaq 0x8567(%rip), %rdx      # 0x17424
pushq $0x64
popq %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0xeede
movq 0x18(%rsp), %rdi
leaq 0x25(%rsp), %rsi
callq 0x5090
movl %ebx, %r13d
movl %r13d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq