// File: /workspace/github_cpp_makefile_star2_O2/goodix[P]gdix_hid_firmware_update/gtx9/gtx9.cpp
// Function: GTx9Device::Write(unsigned int, unsigned char const*, unsigned int)
int GTx9Device::Write(unsigned int addr, const unsigned char *buf,
					  unsigned int len)
{
	uint8_t HidBuf[PACKAGE_LEN] = {0};
	uint32_t current_addr = addr;
	uint32_t transfer_length = 0;
	uint32_t pos = 0;
	uint8_t pkg_num = 0;
	int ret;

	if (!m_deviceOpen) {
		gdix_err("Device not open\n");
		return -EINVAL;
	}

	while (pos != len) {
		HidBuf[0] = REPORT_ID;
		HidBuf[1] = I2C_DIRECT_RW;
		if (len - pos > PACKAGE_LEN - 12) {
			transfer_length = PACKAGE_LEN - 12;
			HidBuf[2] = 0x01;
		} else {
			transfer_length = len - pos;
			HidBuf[2] = 0x00;
		}
		HidBuf[3] = pkg_num++;
		HidBuf[4] = transfer_length + 7;
		HidBuf[5] = I2C_WRITE_FLAG;
		HidBuf[6] = (current_addr >> 24) & 0xFF;
		HidBuf[7] = (current_addr >> 16) & 0xFF;
		HidBuf[8] = (current_addr >> 8) & 0xFF;
		HidBuf[9] = current_addr & 0xFF;
		HidBuf[10] = (transfer_length >> 8) & 0xFF;
		HidBuf[11] = transfer_length & 0xFF;
		memcpy(&HidBuf[12], &buf[pos], transfer_length);
		ret = SetReport(HidBuf, transfer_length + 12);
		if (ret < 0) {
			gdix_err("Failed write data to addr=0x%x, len=%d,ret = %d\n",
					 current_addr, transfer_length, ret);
			return ret;
		}
		pos += transfer_length;
		current_addr += transfer_length;
	}

	return len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, 0xc(%rsp)
movq %rdx, 0x60(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movb $0x0, 0x50(%rsp)
cmpb $0x1, 0x8(%rdi)
jne 0xf1ee
movl %esi, %ecx
movq %rdi, %r15
xorl %r14d, %r14d
xorl %ebp, %ebp
movl 0xc(%rsp), %eax
movl %eax, %r12d
subl %r14d, %r12d
je 0xf223
movw $0x200e, 0x10(%rsp)     # imm = 0x200E
cmpl $0x35, %r12d
pushq $0x35
popq %rax
cmovael %eax, %r12d
seta 0x12(%rsp)
movb %bpl, 0x13(%rsp)
leal 0x7(%r12), %eax
movb %al, 0x14(%rsp)
xorl %edx, %edx
movb %dl, 0x15(%rsp)
movl %ecx, %eax
shrl $0x18, %eax
movb %al, 0x16(%rsp)
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x17(%rsp)
movb %ch, 0x18(%rsp)
movb %cl, 0x19(%rsp)
movb %dl, 0x1a(%rsp)
movb %r12b, 0x1b(%rsp)
movl %r14d, %esi
addq 0x60(%rsp), %rsi
leaq 0x1c(%rsp), %rdi
movq %r12, %rdx
movl %ecx, %r13d
callq 0x5090
leal 0xc(%r12), %edx
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0xeef0
testl %eax, %eax
js 0xf229
incb %bpl
addl %r12d, %r14d
addl %r12d, %r13d
movl %r13d, %ecx
jmp 0xf151
leaq 0x1109c(%rip), %rax     # 0x20291
pushq $-0x16
popq %rbx
cmpb $0x1, (%rax)
jne 0xf237
movq 0x10dd4(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0x887b(%rip), %rsi      # 0x17a89
leaq 0x8315(%rip), %rdx      # 0x1752a
movl $0x9e, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0xf237
movl 0xc(%rsp), %ebx
jmp 0xf237
movl %eax, %ebx
leaq 0x1105f(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
je 0xf248
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10d89(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
movl %ebx, (%rsp)
leaq 0x8854(%rip), %rsi      # 0x17ab0
leaq 0x82c7(%rip), %rdx      # 0x1752a
movl %r13d, %r8d
movl $0xb9, %ecx
movl %r12d, %r9d
xorl %eax, %eax
callq 0x5180
jmp 0xf237
nop