// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x8(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0xc8(%rbp)
movw 0x109e6(%rip), %ax      # 0x26290
movw %ax, -0xd8(%rbp)
movw $0x0, -0xd6(%rbp)
movw $0x0, -0xd4(%rbp)
movq $0x0, -0xd0(%rbp)
movl -0x14(%rbp), %eax
addl $0x4, %eax
cmpl $0x80, %eax
jge 0x158eb
leaq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x1594c
movl -0x14(%rbp), %eax
addl $0x4, %eax
movslq %eax, %rdi
callq 0x5150
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xd0(%rbp)
jne 0x1594a
jmp 0x1590c
leaq 0x10986(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x1593c
movq 0x106b9(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x99db(%rip), %rsi      # 0x1f304
leaq 0x99f4(%rip), %rdx      # 0x1f324
movl $0xf2, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x1593e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x15b3d
jmp 0x1594c
jmp 0x1594e
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
je 0x15af8
movl -0x14(%rbp), %eax
subl -0x2c(%rbp), %eax
cmpl $0xfc, %eax
jle 0x15970
movl $0xfc, -0x30(%rbp)
jmp 0x15979
movl -0x14(%rbp), %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x34(%rbp), %eax
sarl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0xd0(%rbp), %rax
movb %cl, (%rax)
movl -0x34(%rbp), %eax
sarl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0xd0(%rbp), %rax
movb %cl, 0x1(%rax)
movl -0x34(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0xd0(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0x34(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0xd0(%rbp), %rax
movb %cl, 0x3(%rax)
movl -0x30(%rbp), %eax
addl $0x4, %eax
movw %ax, -0xd4(%rbp)
movq -0xd0(%rbp), %rdi
addq $0x4, %rdi
movq -0x10(%rbp), %rsi
movslq -0x2c(%rbp), %rax
addq %rax, %rsi
movslq -0x30(%rbp), %rdx
callq 0x50b0
leaq -0xd8(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x1, -0x20(%rbp)
movl $0x0, -0xc4(%rbp)
cmpl $0x1, -0xc4(%rbp)
jge 0x15aaa
movl 0x10874(%rip), %edi     # 0x262a0
movl $0x707, %esi            # imm = 0x707
leaq -0x28(%rbp), %rdx
movb $0x0, %al
callq 0x5170
cmpl $0x0, %eax
jle 0x15a55
movl -0x30(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x15aaa
jmp 0x15a57
leaq 0x1083b(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x15a92
movq 0x1056e(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
movl -0xc4(%rbp), %r8d
addl $0x1, %r8d
leaq 0x98af(%rip), %rsi      # 0x1f32e
leaq 0x989e(%rip), %rdx      # 0x1f324
movl $0x10a, %ecx            # imm = 0x10A
movb $0x0, %al
callq 0x5190
jmp 0x15a94
jmp 0x15a96
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x15a19
cmpl $0x1, -0xc4(%rbp)
jne 0x15af3
jmp 0x15ab5
leaq 0x107dd(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0x15ae5
movq 0x10510(%rip), %rax     # 0x25fd8
movq (%rax), %rdi
leaq 0x9882(%rip), %rsi      # 0x1f354
leaq 0x984b(%rip), %rdx      # 0x1f324
movl $0x10d, %ecx            # imm = 0x10D
movb $0x0, %al
callq 0x5190
jmp 0x15ae7
movl $0xffffffff, -0xc8(%rbp) # imm = 0xFFFFFFFF
jmp 0x15afa
jmp 0x1594e
jmp 0x15afa
movl -0x14(%rbp), %eax
addl $0x4, %eax
cmpl $0x80, %eax
jl 0x15b13
movq -0xd0(%rbp), %rdi
callq 0x5120
cmpl $0x0, -0xc8(%rbp)
jge 0x15b2a
movl -0xc8(%rbp), %eax
movl %eax, -0xdc(%rbp)
jmp 0x15b34
xorl %eax, %eax
movl %eax, -0xdc(%rbp)
jmp 0x15b34
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)