// File: /workspace/github_cpp_makefile_star2_O2/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, 0xc(%rsp)
movq %rsi, 0x30(%rsp)
movl %edi, %ebx
movzwl 0xee6f(%rip), %eax      # 0x20288
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)
movw $0x1, 0x12(%rdx)
leaq 0x50(%rsp), %rax
movq %rax, 0x18(%rdx)
leaq 0x40(%rsp), %r12
leaq 0xee41(%rip), %r14      # 0x20291
xorl %r15d, %r15d
movl %edi, 0x8(%rsp)
movl 0xc(%rsp), %ebp
subl %r15d, %ebp
je 0x11575
movl %r15d, 0x4(%rsp)
movl $0x100, %eax            # imm = 0x100
cmpl %eax, %ebp
cmovgel %eax, %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)
movb $0x1, %r15b
xorl %r13d, %r13d
testb $0x1, %r15b
je 0x11516
movl 0xedd3(%rip), %edi      # 0x20298
movl $0x707, %esi            # imm = 0x707
movq %r12, %rdx
xorl %eax, %eax
callq 0x5160
testl %eax, %eax
jg 0x1151d
movb $0x1, %r13b
xorl %r15d, %r15d
cmpb $0x1, (%r14)
jne 0x114b9
movq 0xeaed(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
xorl %r15d, %r15d
leaq 0x706f(%rip), %rsi      # 0x18567
leaq 0x705f(%rip), %rdx      # 0x1855e
movl $0xd1, %ecx
pushq $0x1
popq %r8
movl 0x8(%rsp), %r9d
xorl %eax, %eax
callq 0x5180
jmp 0x114b9
movl 0x4(%rsp), %r15d
jmp 0x1153c
movl 0x4(%rsp), %r15d
movslq %r15d, %rdi
addq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
movslq %ebp, %rdx
callq 0x5090
addl %ebp, %r15d
addl %ebp, %ebx
testb $0x1, %r13b
je 0x11457
pushq $-0x1
popq %rbx
cmpb $0x1, (%r14)
jne 0x11577
movq 0xea82(%rip), %rax      # 0x1ffd8
movq (%rax), %rdi
leaq 0x7031(%rip), %rsi      # 0x18591
leaq 0x6ff7(%rip), %rdx      # 0x1855e
movl $0xd4, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0x11577
xorl %ebx, %ebx
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq