// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movl %edx, %r14d
movq %rsi, 0x18(%rsp)
movl %edi, %ebx
movzwl 0xef17(%rip), %eax      # 0x21288
movw %ax, 0x8(%rsp)
movl $0x0, 0xa(%rsp)
movq $0x0, 0x10(%rsp)
cmpl $0x7b, %edx
jg 0x12398
leaq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x123af
leal 0x4(%r14), %edi
callq 0x5140
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x12492
movl $0xfc, %ebp
xorl %r15d, %r15d
movl %r14d, %r13d
subl %r15d, %r13d
je 0x1246b
cmpl %ebp, %r13d
cmovgel %ebp, %r13d
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(%r13), %eax
movw %ax, 0xc(%rsp)
movq 0x10(%rsp), %rdi
addq $0x4, %rdi
movslq %r15d, %rsi
addq 0x18(%rsp), %rsi
movslq %r13d, %rdx
callq 0x5090
leaq 0x8(%rsp), %rax
movq %rax, 0x20(%rsp)
movl $0x1, 0x28(%rsp)
movl 0xee6b(%rip), %edi      # 0x21298
movl $0x707, %esi            # imm = 0x707
leaq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x5160
movl %eax, %r12d
testl %eax, %eax
jle 0x1244d
addl %r13d, %r15d
addl %r13d, %ebx
jmp 0x12459
leaq 0xee3d(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
je 0x12464
testl %r12d, %r12d
jg 0x123b7
jmp 0x1246f
callq 0x6404
jmp 0x12459
xorl %ebx, %ebx
jmp 0x12480
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0xee16(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
je 0x124b7
cmpl $0x7c, %r14d
jl 0x124a3
movq 0x10(%rsp), %rdi
callq 0x5110
jmp 0x124a3
leaq 0xedf8(%rip), %rax      # 0x21291
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpb $0x1, (%rax)
je 0x124be
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x6432
jmp 0x12480
callq 0x63e0
jmp 0x124a3