// File: goodix[P]gdix_hid_firmware_update/gtx9/gtx9_i2c_update.cpp
// Function: i2c_read(unsigned int, unsigned char*, int)
static int i2c_read(uint32_t reg, unsigned char *data, int len)
{
	struct i2c_rdwr_ioctl_data packets;
	int transfer_length = 0;
	int pos = 0, address = reg;
	unsigned char get_buf[128], addr_buf[TS_ADDR_LENGTH];
	int retry, r = 0;

	struct i2c_msg msgs[] = {
		{
			.addr = g_client_addr,
			.flags = !I2C_M_RD,
			.len = TS_ADDR_LENGTH,
			.buf = addr_buf,
		},
		{
			.addr = g_client_addr,
			.flags = I2C_M_RD,
		},
	};

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

		packets.msgs = msgs;
		packets.nmsgs = 2;
		for (retry = 0; retry < GOODIX_BUS_RETRY_TIMES; retry++) {
			if (ioctl(g_fd, I2C_RDWR, &packets) > 0) {
				memcpy(&data[pos], msgs[1].buf, transfer_length);
				pos += transfer_length;
				address += transfer_length;
				break;
			}
			gdix_err("I2c read retry[%d]:0x%x\n", retry + 1, reg);
		}
		if (retry == GOODIX_BUS_RETRY_TIMES) {
			gdix_err("I2c read failed\n");
			r = -1;
			goto read_exit;
		}
	}

read_exit:
	if (len >= (int)sizeof(get_buf))
		free(msgs[1].buf);
	return r < 0 ? r : 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
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, -0xcc(%rbp)
movw 0xf986(%rip), %ax       # 0x26290
movw %ax, -0xf0(%rbp)
movw $0x0, -0xee(%rbp)
movw $0x4, -0xec(%rbp)
leaq -0xc4(%rbp), %rax
movq %rax, -0xe8(%rbp)
movw 0xf958(%rip), %ax       # 0x26290
movw %ax, -0xe0(%rbp)
movw $0x1, -0xde(%rbp)
movw $0x0, -0xdc(%rbp)
movq $0x0, -0xd8(%rbp)
cmpl $0x80, -0x14(%rbp)
jge 0x16975
leaq -0xc0(%rbp), %rax
movq %rax, -0xd8(%rbp)
jmp 0x169d1
movslq -0x14(%rbp), %rdi
callq 0x5150
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xd8(%rbp)
jne 0x169cf
jmp 0x16991
leaq 0xf901(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x169c1
movq 0xf614(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
leaq 0x8bd6(%rip), %rsi      # 0x1f584
leaq 0x8bef(%rip), %rdx      # 0x1f5a4
movl $0xb9, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x169c3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16bbb
jmp 0x169d1
jmp 0x169d3
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
je 0x16b7a
movl -0x14(%rbp), %eax
subl -0x30(%rbp), %eax
cmpl $0x100, %eax            # imm = 0x100
jle 0x169f5
movl $0x100, -0x2c(%rbp)     # imm = 0x100
jmp 0x169fe
movl -0x14(%rbp), %eax
subl -0x30(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
sarl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0xe8(%rbp), %rax
movb %cl, (%rax)
movl -0x34(%rbp), %eax
sarl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0xe8(%rbp), %rax
movb %cl, 0x1(%rax)
movl -0x34(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0xe8(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0x34(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0xe8(%rbp), %rax
movb %cl, 0x3(%rax)
movl -0x2c(%rbp), %eax
movw %ax, -0xdc(%rbp)
leaq -0xf0(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x2, -0x20(%rbp)
movl $0x0, -0xc8(%rbp)
cmpl $0x1, -0xc8(%rbp)
jge 0x16b2c
movl 0xf811(%rip), %edi      # 0x262a0
movl $0x707, %esi            # imm = 0x707
leaq -0x28(%rbp), %rdx
movb $0x0, %al
callq 0x5170
cmpl $0x0, %eax
jle 0x16ad3
movq -0x10(%rbp), %rdi
movslq -0x30(%rbp), %rax
addq %rax, %rdi
movq -0xd8(%rbp), %rsi
movslq -0x2c(%rbp), %rdx
callq 0x50b0
movl -0x2c(%rbp), %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x16b2c
jmp 0x16ad5
leaq 0xf7bd(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x16b14
movq 0xf4f0(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
movl -0xc8(%rbp), %r8d
addl $0x1, %r8d
movl -0x8(%rbp), %r9d
leaq 0x8aac(%rip), %rsi      # 0x1f5ad
leaq 0x8a9c(%rip), %rdx      # 0x1f5a4
movl $0xd1, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x16b16
jmp 0x16b18
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x16a7c
cmpl $0x1, -0xc8(%rbp)
jne 0x16b75
jmp 0x16b37
leaq 0xf75b(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x16b67
movq 0xf48e(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x8a83(%rip), %rsi      # 0x1f5d7
leaq 0x8a49(%rip), %rdx      # 0x1f5a4
movl $0xd4, %ecx
movb $0x0, %al
callq 0x5190
jmp 0x16b69
movl $0xffffffff, -0xcc(%rbp) # imm = 0xFFFFFFFF
jmp 0x16b7c
jmp 0x169d3
jmp 0x16b7c
cmpl $0x80, -0x14(%rbp)
jl 0x16b91
movq -0xd8(%rbp), %rdi
callq 0x5120
cmpl $0x0, -0xcc(%rbp)
jge 0x16ba8
movl -0xcc(%rbp), %eax
movl %eax, -0xf4(%rbp)
jmp 0x16bb2
xorl %eax, %eax
movl %eax, -0xf4(%rbp)
jmp 0x16bb2
movl -0xf4(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw (%rax,%rax)