// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r12
movaps %xmm0, 0x30(%r12)
movaps %xmm0, 0x20(%r12)
movaps %xmm0, 0x10(%r12)
movaps %xmm0, (%r12)
movb $0x0, 0x40(%r12)
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, %r14d      # imm = 0xFFFFFFFB
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 %r15, %rdi
movq %r12, %rsi
movl $0xc, %edx
callq 0x10444
testl %eax, %eax
js 0x103b9
movq %r15, %rdi
movq %r12, %rbp
movq %r12, %rsi
callq 0x104e2
testl %eax, %eax
js 0x103e7
cmpb $0x0, 0x23(%rsp)
sete %al
movzbl 0x24(%rsp), %edx
cmpl %ebx, %edx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x1041f
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x5170
incl %r14d
movq %rbp, %r12
jne 0x1031d
movl $0xffffffea, %r13d      # imm = 0xFFFFFFEA
jmp 0x10431
movl %eax, %r13d
leaq 0x10ece(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x10431
movq 0x10c09(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x8692(%rip), %rsi      # 0x18a6b
leaq 0x807e(%rip), %rdx      # 0x1845e
movl $0x5f, %ecx
jmp 0x10413
movl %eax, %r13d
leaq 0x10ea0(%rip), %rax     # 0x21291
cmpb $0x1, (%rax)
jne 0x10431
movq 0x10bdb(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0x8690(%rip), %rsi      # 0x18a97
leaq 0x8050(%rip), %rdx      # 0x1845e
movl $0x64, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0x10431
leaq 0x25(%rsp), %rsi
movq 0x18(%rsp), %rdi
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