// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdx, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movb $0x0, 0x50(%rsp)
movw $0x200e, 0x10(%rsp)     # imm = 0x200E
testl %ecx, %ecx
je 0xd7fb
movl %ecx, %ebp
movl %esi, %eax
movq %rdi, %r15
xorl %r13d, %r13d
xorl %r14d, %r14d
movl %ebp, %ecx
subl %r14d, %ecx
movl 0x2c(%r15), %ebx
addl $-0xa, %ebx
cmpl %ebx, %ecx
cmovbl %ecx, %ebx
seta 0x12(%rsp)
movb %r13b, 0x13(%rsp)
leal 0x5(%rbx), %ecx
movb %cl, 0x14(%rsp)
movb $0x0, 0x15(%rsp)
movzwl %ax, %r12d
movb %ah, 0x16(%rsp)
movb %al, 0x17(%rsp)
movb %bh, 0x18(%rsp)
movb %bl, 0x19(%rsp)
movl %r14d, %esi
addq 0x8(%rsp), %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 0xd7ff
incb %r13b
addl %ebx, %r14d
addl %ebx, %r12d
movl %r12d, %eax
cmpl %ebp, %r14d
jne 0xd783
jmp 0xd83d
xorl %ebp, %ebp
jmp 0xd83d
leaq 0x13a8b(%rip), %rcx     # 0x21291
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x1, (%rcx)
jne 0xd83d
movq 0x137a1(%rip), %rcx     # 0x20fb8
movq (%rcx), %rdi
movl %eax, (%rsp)
leaq 0xacc4(%rip), %rsi      # 0x184e8
leaq 0xacff(%rip), %rdx      # 0x1852a
movl %ebx, %r9d
movl $0x109, %ecx            # imm = 0x109
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq