// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
movl %edx, %r15d
movq %rsi, %r14
movl %edi, %ebx
movzwl 0xf0c5(%rip), %eax      # 0x21288
movw %ax, (%rsp)
movl $0x0, 0x2(%rsp)
cmpl $0x7b, %edx
jg 0x121e0
leaq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x121f7
leal 0x4(%r15), %edi
callq 0x5140
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x122c5
testl %r15d, %r15d
je 0x12299
movl $0xfc, %ebp
xorl %r13d, %r13d
movl %r15d, %r12d
subl %r13d, %r12d
cmpl %ebp, %r12d
cmovgel %ebp, %r12d
movl %ebx, %eax
shrl $0x18, %eax
movq 0x8(%rsp), %rcx
movb %al, (%rcx)
movl %ebx, %eax
shrl $0x10, %eax
movq 0x8(%rsp), %rcx
movb %al, 0x1(%rcx)
movq 0x8(%rsp), %rax
movb %bh, 0x2(%rax)
movq 0x8(%rsp), %rax
movb %bl, 0x3(%rax)
leal 0x4(%r12), %eax
movw %ax, 0x4(%rsp)
movq 0x8(%rsp), %rdi
addq $0x4, %rdi
movslq %r13d, %rsi
addq %r14, %rsi
movslq %r12d, %rdx
callq 0x5090
movq %rsp, %rax
movq %rax, 0x10(%rsp)
movl $0x1, 0x18(%rsp)
movl 0xf023(%rip), %edi      # 0x21298
movl $0x707, %esi            # imm = 0x707
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x5160
testl %eax, %eax
jle 0x1229d
addl %r12d, %r13d
addl %r12d, %ebx
cmpl %r15d, %r13d
jne 0x12208
xorl %ebp, %ebp
jmp 0x122b3
leaq 0xefed(%rip), %rbx      # 0x21291
cmpb $0x1, (%rbx)
je 0x122ea
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x1, (%rbx)
je 0x122f1
cmpl $0x7c, %r15d
jl 0x122d6
movq 0x8(%rsp), %rdi
callq 0x5110
jmp 0x122d6
leaq 0xefc5(%rip), %rax      # 0x21291
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x1, (%rax)
je 0x122f8
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x63b0
jmp 0x122a9
callq 0x63de
jmp 0x122b3
callq 0x638c
jmp 0x122d6