// File: /workspace/github_cpp_makefile_star2_O3/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)
movl $0xfffffffb, %r15d      # imm = 0xFFFFFFFB
leaq 0x20(%rsp), %r14
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
movl $0xc, %edx
callq 0x10300
testl %eax, %eax
js 0x10275
movq %r12, %rdi
movq %r14, %rbp
movq %r14, %rsi
callq 0x1039e
testl %eax, %eax
js 0x102a3
cmpb $0x0, 0x23(%rsp)
sete %al
movzbl 0x24(%rsp), %edx
cmpl %ebx, %edx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x102db
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x5170
incl %r15d
movq %rbp, %r14
jne 0x101d9
movl $0xffffffea, %r13d      # imm = 0xFFFFFFEA
jmp 0x102ed
movl %eax, %r13d
leaq 0x11012(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x102ed
movq 0x10d4d(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x879c(%rip), %rsi      # 0x18a31
leaq 0x8188(%rip), %rdx      # 0x18424
movl $0x5f, %ecx
jmp 0x102cf
movl %eax, %r13d
leaq 0x10fe4(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x102ed
movq 0x10d1f(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x879a(%rip), %rsi      # 0x18a5d
leaq 0x815a(%rip), %rdx      # 0x18424
movl $0x64, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x102ed
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
nop