// File: /workspace/github_cpp_makefile_star2_O1/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
movl %edx, %r15d
movq %rsi, 0x30(%rsp)
movl %edi, %ebx
movzwl 0xebe7(%rip), %eax      # 0x21288
leaq 0x10(%rsp), %rdx
movw %ax, (%rdx)
movl $0x40000, 0x2(%rdx)     # imm = 0x40000
leaq 0x3c(%rsp), %rcx
movq %rcx, 0x8(%rdx)
movw %ax, 0x10(%rdx)
movl $0x1, 0x12(%rdx)
leaq 0x50(%rsp), %rax
movq %rax, 0x18(%rdx)
movl $0x100, %r14d           # imm = 0x100
xorl %r12d, %r12d
movl %edi, 0xc(%rsp)
movl %r15d, %ebp
subl %r12d, %ebp
je 0x127af
cmpl %r14d, %ebp
cmovgel %r14d, %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 0xeb65(%rip), %edi      # 0x21298
movl $0x707, %esi            # imm = 0x707
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x5160
movl %eax, %r13d
testl %eax, %eax
jle 0x12767
movslq %r12d, %rdi
addq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
movslq %ebp, %rdx
callq 0x5090
addl %ebp, %r12d
addl %ebp, %ebx
jmp 0x12773
leaq 0xeb23(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
je 0x1277e
testl %r13d, %r13d
jg 0x126da
jmp 0x127b3
movq 0xe853(%rip), %rax      # 0x20fd8
movq (%rax), %rdi
leaq 0x6e18(%rip), %rsi      # 0x195a7
leaq 0x6e08(%rip), %rdx      # 0x1959e
movl $0xd1, %ecx
movl $0x1, %r8d
movl 0xc(%rsp), %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x12773
xorl %ebx, %ebx
jmp 0x127c4
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0xead2(%rip), %rax      # 0x21291
cmpb $0x1, (%rax)
je 0x127d8
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x64a4
jmp 0x127c4