// File: /workspace/github_cpp_makefile_star2_O2/goodix[P]gdix_hid_firmware_update/gtx9/gtx9_i2c_update.cpp
// Function: i2c_write(unsigned int, unsigned char*, int)
static int i2c_write(uint32_t reg, unsigned char *data, int len)
{
	struct i2c_rdwr_ioctl_data packets;
	int pos = 0, transfer_length = 0;
	int address = reg;
	unsigned char put_buf[128];
	int retry, r = 0;
	struct i2c_msg msgs = {
		.addr = g_client_addr,
		.flags = !I2C_M_RD,
	};

	if (len + TS_ADDR_LENGTH < (int)sizeof(put_buf)) {
		/* code optimize,use stack memory*/
		msgs.buf = &put_buf[0];
	} else {
		msgs.buf = (uint8_t *)malloc(len + TS_ADDR_LENGTH);
		if (msgs.buf == NULL) {
			gdix_err("Malloc failed\n");
			return -1;
		}
	}
	while (pos != len) {
		if (len - pos > I2C_MAX_TRANSFER_SIZE - TS_ADDR_LENGTH)
			transfer_length = I2C_MAX_TRANSFER_SIZE - TS_ADDR_LENGTH;
		else
			transfer_length = len - pos;
		msgs.buf[0] = (address >> 24) & 0xFF;
		msgs.buf[1] = (address >> 16) & 0xFF;
		msgs.buf[2] = (address >> 8) & 0xFF;
		msgs.buf[3] = address & 0xFF;
		msgs.len = transfer_length + TS_ADDR_LENGTH;
		memcpy(&msgs.buf[TS_ADDR_LENGTH], &data[pos], transfer_length);
		packets.msgs = &msgs;
		packets.nmsgs = 1;

		for (retry = 0; retry < GOODIX_BUS_RETRY_TIMES; retry++) {
			if (ioctl(g_fd, I2C_RDWR, &packets) > 0) {
				pos += transfer_length;
				address += transfer_length;
				break;
			}
			gdix_err("I2c write retry[%d]\n", retry + 1);
		}
		if (retry == GOODIX_BUS_RETRY_TIMES) {
			gdix_err("I2c write failed\n");
			r = -1;
			goto write_exit;
		}
	}

write_exit:
	if (len + TS_ADDR_LENGTH >= (int)sizeof(put_buf))
		free(msgs.buf);
	return r < 0 ? r : 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %edx, %r15d
movq %rsi, 0x28(%rsp)
movl %edi, %ebx
movzwl 0xf23a(%rip), %eax      # 0x20288
movw %ax, 0x8(%rsp)
andl $0x0, 0xa(%rsp)
cmpl $0x7b, %edx
jg 0x11069
leaq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x11080
leal 0x4(%r15), %edi
callq 0x5140
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x111cd
leaq 0x30(%rsp), %r12
leaq 0xf205(%rip), %r13      # 0x20291
xorl %edx, %edx
movq %r15, 0x20(%rsp)
movl %r15d, %r14d
subl %edx, %r14d
je 0x111b9
movl $0xfc, %eax
cmpl %eax, %r14d
cmovgel %eax, %r14d
movl %ebx, %eax
shrl $0x18, %eax
movq 0x10(%rsp), %rcx
movb %al, (%rcx)
movl %ebx, %eax
shrl $0x10, %eax
movq 0x10(%rsp), %rcx
movb %al, 0x1(%rcx)
movq 0x10(%rsp), %rax
movb %bh, 0x2(%rax)
movq 0x10(%rsp), %rax
movb %bl, 0x3(%rax)
leal 0x4(%r14), %eax
movw %ax, 0xc(%rsp)
movq 0x10(%rsp), %rdi
addq $0x4, %rdi
movl %edx, 0x1c(%rsp)
movslq %edx, %rsi
addq 0x28(%rsp), %rsi
movslq %r14d, %rdx
callq 0x5090
leaq 0x8(%rsp), %rax
movq %rax, 0x30(%rsp)
movl $0x1, 0x38(%rsp)
movb $0x1, %r15b
xorl %ebp, %ebp
testb $0x1, %r15b
je 0x1116a
movl 0xf17b(%rip), %edi      # 0x20298
movl $0x707, %esi            # imm = 0x707
movq %r12, %rdx
xorl %eax, %eax
callq 0x5160
testl %eax, %eax
jg 0x11170
movb $0x1, %bpl
xorl %r15d, %r15d
cmpb $0x1, (%r13)
jne 0x11111
movq 0xee94(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
xorl %r15d, %r15d
leaq 0x71b7(%rip), %rsi      # 0x18308
leaq 0x71a6(%rip), %rdx      # 0x182fe
movl $0x10a, %ecx            # imm = 0x10A
pushq $0x1
popq %r8
xorl %eax, %eax
callq 0x5180
jmp 0x11111
movl 0x1c(%rsp), %edx
jmp 0x1117a
movl 0x1c(%rsp), %edx
addl %r14d, %edx
addl %r14d, %ebx
testb $0x1, %bpl
movq 0x20(%rsp), %r15
je 0x11093
pushq $-0x1
popq %rbx
cmpb $0x1, (%r13)
jne 0x111bb
movq 0xee3e(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x718a(%rip), %rsi      # 0x1832e
leaq 0x7153(%rip), %rdx      # 0x182fe
movl $0x10d, %ecx            # imm = 0x10D
xorl %eax, %eax
callq 0x5180
jmp 0x111bb
xorl %ebx, %ebx
cmpl $0x7c, %r15d
jl 0x111dc
movq 0x10(%rsp), %rdi
callq 0x5110
jmp 0x111dc
leaq 0xf0bd(%rip), %rax      # 0x20291
pushq $-0x1
popq %rbx
cmpb $0x1, (%rax)
je 0x111f0
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xede1(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x70dd(%rip), %rsi      # 0x182de
leaq 0x70f6(%rip), %rdx      # 0x182fe
movl $0xf2, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x111dc