// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movzwl 0xedb4(%rip), %eax      # 0x21288
movw %ax, 0x10(%rsp)
movl $0x40000, 0x12(%rsp)    # imm = 0x40000
leaq 0x3c(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movw %ax, 0x20(%rsp)
movw $0x1, 0x22(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
testl %edx, %edx
je 0x125a7
movl %edx, %r15d
movq %rsi, %r14
movl $0x100, %r12d           # imm = 0x100
movl %edi, 0xc(%rsp)
movl %edi, %ebx
xorl %r13d, %r13d
movl %r15d, %ebp
subl %r13d, %ebp
cmpl %r12d, %ebp
cmovgel %r12d, %ebp
movl %ebx, %eax
shrl $0x18, %eax
movq 0x18(%rsp), %rcx
movb %al, (%rcx)
movl %ebx, %eax
shrl $0x10, %eax
movq 0x18(%rsp), %rcx
movb %al, 0x1(%rcx)
movq 0x18(%rsp), %rax
movb %bh, 0x2(%rax)
movq 0x18(%rsp), %rax
movb %bl, 0x3(%rax)
movw %bp, 0x24(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0x40(%rsp)
movl $0x2, 0x48(%rsp)
movl 0xed27(%rip), %edi      # 0x21298
movl $0x707, %esi            # imm = 0x707
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x5160
testl %eax, %eax
jle 0x125ab
movslq %r13d, %rdi
addq %r14, %rdi
movq 0x28(%rsp), %rsi
movslq %ebp, %rdx
callq 0x5090
addl %ebp, %r13d
addl %ebp, %ebx
cmpl %r15d, %r13d
jne 0x1251e
xorl %ebx, %ebx
jmp 0x125c3
leaq 0xecdf(%rip), %r14      # 0x21291
cmpb $0x1, (%r14)
je 0x125d7
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpb $0x1, (%r14)
je 0x12608
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe9fa(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x6f7f(%rip), %rsi      # 0x19567
leaq 0x6f6f(%rip), %rdx      # 0x1955e
movl $0xd1, %ecx
movl $0x1, %r8d
movl 0xc(%rsp), %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x125b8
callq 0x6450
jmp 0x125c3