// File: /workspace/github_cpp_makefile_star2_O2/goodix[P]gdix_hid_firmware_update/gtx5/gtx5.cpp
// Function: GTx5Device::Write(unsigned int, unsigned char const*, unsigned int)
int GTx5Device::Write(unsigned int addr, const unsigned char *buf,
					  unsigned int len)
{
	unsigned char tmpBuf[65] = {0x0e, _I2C_DIRECT_RW, 0, 0, 5};
	unsigned short current_addr = addr;
	unsigned int pos = 0, transfer_length = 0;
	bool has_follow_pkg = false;
	unsigned char pkg_num = 0;
	int ret = 0;
	while (pos != len) {
		if (len - pos >
			m_outputReportSize - GDIX_PRE_HEAD_LEN - GDIX_DATA_HEAD_LEN) {
			/* data len more than 55, need to send with subpackages */
			transfer_length =
				m_outputReportSize - GDIX_PRE_HEAD_LEN - GDIX_DATA_HEAD_LEN;
			has_follow_pkg = true;
		} else {
			transfer_length = len - pos;
			has_follow_pkg = false;
		}
		if (has_follow_pkg)
			tmpBuf[2] = 0x01; /* set follow-up package flag */
		else
			tmpBuf[2] = 0x00;
		tmpBuf[3] = pkg_num++; /* set pack num */
		/* set HID package length = data_len + GDIX_DATA_HEAD_LEN */
		tmpBuf[4] = (unsigned char)(transfer_length + GDIX_DATA_HEAD_LEN);
		tmpBuf[5] = 0; /* write operation flag */
		tmpBuf[6] = (current_addr >> 8) & 0xff;
		tmpBuf[7] = current_addr & 0xff;
		tmpBuf[8] = (unsigned char)((transfer_length >> 8) & 0xff);
		tmpBuf[9] = (unsigned char)(transfer_length & 0xff);
		memcpy(&tmpBuf[GDIX_PRE_HEAD_LEN + GDIX_DATA_HEAD_LEN], &buf[pos],
			   transfer_length);
		ret = Write(tmpBuf,
					transfer_length + GDIX_PRE_HEAD_LEN + GDIX_DATA_HEAD_LEN);
		if (ret < 0) {
			gdix_dbg("Failed write data to addr=0x%x, len=%d,ret = %d\n",
					 current_addr, transfer_length, ret);
			break;
		} else {
			pos += transfer_length;
			current_addr += transfer_length;
		}
	}
	if (ret < 0)
		return -1;
	else
		return len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, 0xc(%rsp)
movq %rdx, %r14
movl %esi, %eax
movq %rdi, %r15
xorps %xmm0, %xmm0
leaq 0x1a(%rsp), %rcx
movaps %xmm0, -0xa(%rcx)
movaps %xmm0, 0x26(%rcx)
movaps %xmm0, 0x16(%rcx)
movaps %xmm0, 0x6(%rcx)
movb $0x0, 0x36(%rcx)
movw $0x200e, -0xa(%rcx)     # imm = 0x200E
xorl %r12d, %r12d
xorl %ebp, %ebp
movl 0xc(%rsp), %ecx
subl %r12d, %ecx
je 0xc1ec
movl 0x2c(%r15), %ebx
addl $-0xa, %ebx
cmpl %ebx, %ecx
cmovbl %ecx, %ebx
seta 0x12(%rsp)
movb %bpl, 0x13(%rsp)
leal 0x5(%rbx), %ecx
movb %cl, 0x14(%rsp)
movb $0x0, 0x15(%rsp)
movzwl %ax, %r13d
movb %ah, 0x16(%rsp)
movb %al, 0x17(%rsp)
movb %bh, 0x18(%rsp)
movb %bl, 0x19(%rsp)
movl %r12d, %esi
addq %r14, %rsi
leaq 0x1a(%rsp), %rdi
movq %rbx, %rdx
callq 0x5090
leal 0xa(%rbx), %edx
movq (%r15), %rax
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq *0x38(%rax)
testl %eax, %eax
js 0xc1ac
incb %bpl
addl %ebx, %r12d
addl %ebx, %r13d
movl %r13d, %eax
jmp 0xc133
leaq 0x140de(%rip), %rcx     # 0x20291
pushq $-0x1
popq %rdx
movl %edx, 0xc(%rsp)
cmpb $0x1, (%rcx)
jne 0xc1ec
movq 0x13df2(%rip), %rcx     # 0x1ffb8
movq (%rcx), %rdi
movl %eax, (%rsp)
leaq 0xb315(%rip), %rsi      # 0x174e8
leaq 0xb350(%rip), %rdx      # 0x1752a
movl %ebx, %r9d
movl $0x109, %ecx            # imm = 0x109
movl %r13d, %r8d
xorl %eax, %eax
callq 0x5180
movl 0xc(%rsp), %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop