// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movl %ecx, %r12d
movq %rdx, 0x60(%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
movb $0x5, 0x14(%rsp)
testl %ecx, %ecx
je 0xd92e
movl %esi, %eax
movq %rdi, %r15
xorl %r13d, %r13d
xorl %r14d, %r14d
movl %r12d, %ecx
subl %r14d, %ecx
movl 0x2c(%r15), %ebx
addl $-0xa, %ebx
cmpl %ebx, %ecx
cmovbl %ecx, %ebx
jbe 0xd88c
movb $0x1, 0x12(%rsp)
jmp 0xd891
movb $0x0, 0x12(%rsp)
movb %r13b, 0x13(%rsp)
leal 0x5(%rbx), %ecx
movb %cl, 0x14(%rsp)
movb $0x0, 0x15(%rsp)
movzwl %ax, %ebp
movb %ah, 0x16(%rsp)
movb %al, 0x17(%rsp)
movb %bh, 0x18(%rsp)
movb %bl, 0x19(%rsp)
movl %r14d, %esi
addq 0x60(%rsp), %rsi
movl %ebx, %edx
leaq 0x1a(%rsp), %rdi
callq 0x5090
leal 0xa(%rbx), %edx
movq (%r15), %rax
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq *0x38(%rax)
testl %eax, %eax
js 0xd8ef
incb %r13b
addl %ebx, %r14d
addl %ebx, %ebp
movl %ebp, %eax
cmpl %r12d, %r14d
jne 0xd871
jmp 0xd92e
leaq 0x1399b(%rip), %rcx     # 0x21291
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpb $0x1, (%rcx)
jne 0xd92e
movq 0x136b0(%rip), %rcx     # 0x20fb8
movq (%rcx), %rdi
movl %eax, (%rsp)
leaq 0xac0d(%rip), %rsi      # 0x18522
leaq 0xac48(%rip), %rdx      # 0x18564
movl %ebx, %r9d
movl $0x109, %ecx            # imm = 0x109
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
movl %r12d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq