// File: /workspace/github_cpp_makefile_star2_O3/chenhn123[P]wdt_util_src/wdt_ct/func_i2c.cpp
// Function: wh_i2c_get_feature(WdtDevice*, unsigned char*, unsigned int)
int wh_i2c_get_feature(WDT_DEV *pdev, BYTE* buf, UINT32 buf_size)
{
	int retval;
	REQ_DATA* p_req_data = (REQ_DATA*) buf;
	int data_len = 0;
	BYTE cmd;
	BYTE tx_buffer[10] = {0}; 
	BYTE rx_buffer[80] = {0};
	bool retryflag = true;

	if (buf_size > 64)
		buf_size = 64;

	if (!buf || !pdev) {
		wh_printf("%s : pointer is null\n", __func__);
		return 0;
	}

retry:
	data_len = 0;
	cmd = buf[0];
	tx_buffer[data_len++] = pdev->board_info.dev_hid_desc.wCommandRegister; 
	tx_buffer[data_len++] = pdev->board_info.dev_hid_desc.wCommandRegister>>8; 


	if (p_req_data->DD.report_id > 0xF)
	{
		if(retryflag == true)
			tx_buffer[data_len++] = 0x3F;
		else 
			tx_buffer[data_len++] = 0x30;
		tx_buffer[data_len++] = I2C_HID_OPCODE_GET_REPORT;
		tx_buffer[data_len++] = cmd;
	}
	else
	{
		tx_buffer[data_len++] = 0x30 | cmd;
		tx_buffer[data_len++] = I2C_HID_OPCODE_GET_REPORT;
	}

        tx_buffer[data_len++] = pdev->board_info.dev_hid_desc.wDataRegister;
        tx_buffer[data_len++] = pdev->board_info.dev_hid_desc.wDataRegister>>8;


	if (pdev->pparam->options & EXEC_I2C_REDUNDANT) {
		tx_buffer[data_len++] = 0x00;
		tx_buffer[data_len++] = 0x00;
	}

	retval = wh_i2c_xfer(pdev, pdev->board_info.i2c_address, tx_buffer, data_len, rx_buffer, buf_size + 2);
	if (!retval)
		return 0;

	UINT32 	xfer_length = rx_buffer[1];
	xfer_length = (xfer_length << 8) | rx_buffer[0];

	if (buf_size < xfer_length)
		xfer_length = buf_size;

	memcpy(buf, &rx_buffer[2], xfer_length);

	if( (rx_buffer[2] != cmd) && (retryflag == true) &&
		(p_req_data->DD.report_id > 0xF) ) {
		retryflag = false;
		goto  retry;
	}	

	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq $0x0, 0x4(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
cmpl $0x40, %edx
movl $0x40, %r13d
cmovbl %edx, %r13d
testq %rsi, %rsi
setne %al
testq %rdi, %rdi
setne %cl
testb %al, %cl
je 0x96e0
movq %rsi, %rbx
movq %rdi, %r14
movb (%rsi), %bpl
leal 0x2(%r13), %eax
movl %eax, 0xc(%rsp)
movb $0x1, %r12b
movl $0x1, %r15d
movzwl 0x1e7(%r14), %eax
movw %ax, 0x2(%rsp)
cmpb $0x10, %bpl
jb 0x9633
testb $0x1, %r12b
movl $0x30, %eax
movl $0x3f, %ecx
cmovnel %ecx, %eax
movb %al, 0x4(%rsp)
movb $0x2, 0x5(%rsp)
movb %bpl, 0x6(%rsp)
movl $0x5, %eax
jmp 0x9645
movl %ebp, %eax
orb $0x30, %al
movb %al, 0x4(%rsp)
movb $0x2, 0x5(%rsp)
movl $0x4, %eax
movzwl 0x1e9(%r14), %ecx
movl %eax, %edx
movw %cx, 0x2(%rsp,%rdx)
movq %rdx, %rcx
orq $0x2, %rcx
movq 0x280(%r14), %rsi
testb $0x2, 0x9(%rsi)
je 0x967e
xorl %esi, %esi
movb %sil, 0x2(%rsp,%rcx)
addl $0x4, %eax
leaq 0x2(%rsp), %rcx
movb %sil, 0x3(%rdx,%rcx)
movl %eax, %ecx
movzbl 0x1d6(%r14), %esi
movq %r14, %rdi
leaq 0x2(%rsp), %rdx
leaq 0x10(%rsp), %r8
movl 0xc(%rsp), %r9d
callq 0x9022
testl %eax, %eax
je 0x96fa
movzwl 0x10(%rsp), %edx
cmpl %edx, %r13d
cmovbl %r13d, %edx
movq %rbx, %rdi
leaq 0x12(%rsp), %rsi
callq 0x2150
cmpb %bpl, 0x12(%rsp)
sete %al
notb %r12b
orb %al, %r12b
testb $0x1, %r12b
jne 0x96fd
movb (%rbx), %bpl
xorl %r12d, %r12d
cmpb $0xf, %bpl
ja 0x95fa
jmp 0x96fd
leaq 0x32d7(%rip), %rdi      # 0xc9be
leaq 0x333b(%rip), %rsi      # 0xca29
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2506
jmp 0x96fd
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq