// File: /workspace/github_cpp_makefile_star2_O1/chenhn123[P]wdt_util_src/wdt_ct/func_i2c.cpp
// Function: wh_i2c_get_desc(WdtDevice*, unsigned char, unsigned char, unsigned char*, unsigned int)
int wh_i2c_get_desc(WDT_DEV *pdev, BYTE desc_type, BYTE string_idx, BYTE* target_buf, UINT32 buf_size)
{
	int	retval;
	int	ret_size = 0;
	UINT16  cmd_reg = pdev->board_info.dev_hid_desc.wCommandRegister;
        UINT16  data_reg = pdev->board_info.dev_hid_desc.wDataRegister;
        char    str_txdata[10] = { (char)cmd_reg, (char)(cmd_reg>>8), 0x13, 0x0E, 0x00, (char)data_reg, (char)(data_reg>>8), 0x00, 0x00, 0x00 };
        char    desc_txdata[10] = { (char)cmd_reg, (char)(cmd_reg>>8), 0x10, 0x0E, (char)data_reg, (char)(data_reg>>8), 0x00, 0x00, 0x00, 0x00 };
	
	BYTE*	txbuf;
	int 	txlen, rxlen;
	BYTE	xfer_buffer[80] ={0}; 

	if (buf_size > 64)
		buf_size = 64;

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

	if (desc_type == GD_STRING)	{
		str_txdata[4] = string_idx;
		txlen = 7;

		txbuf = (BYTE*) str_txdata;
		rxlen = buf_size + 2;
	} else {
		desc_txdata[2] = 0x10 | (desc_type & 0xF);
		txlen = 6;
		txbuf = (BYTE*) desc_txdata;
		rxlen = buf_size;
	}

	if (pdev->pparam->options & EXEC_I2C_REDUNDANT) 
		txlen += 2;

	retval = wh_i2c_xfer(pdev, pdev->board_info.i2c_address, (BYTE*) txbuf, txlen, (BYTE*) xfer_buffer, rxlen);

	if (!retval)
		return 0;
	
	if (desc_type == GD_STRING)	{
		if (xfer_buffer[1] != 0x03) {
			printf("packet error on string id: %d\n", xfer_buffer[1]);
			return 0;
		}
	
		UINT32 	xfer_length = xfer_buffer[0] - 2;

		if (buf_size < xfer_length)
			xfer_length = buf_size;

		memcpy(target_buf, &xfer_buffer[2], xfer_length);
		ret_size = xfer_length;
	} else {
		if (xfer_buffer[0] == buf_size) {
			memcpy(target_buf, &xfer_buffer[0], buf_size);
			ret_size = buf_size;
		} else	{
			printf("packet error on desc: %x %x %x %x\n", xfer_buffer[0],
				xfer_buffer[1], xfer_buffer[2], xfer_buffer[3]);
		}
	}
	
	return ret_size;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x78, %rsp
movq %rcx, %rbx
movzwl 0x1e7(%rdi), %ecx
movzwl 0x1e9(%rdi), %eax
movb %cl, 0xc(%rsp)
movb %cl, 0x16(%rsp)
shrl $0x8, %ecx
movb %cl, 0xd(%rsp)
movw $0xe13, 0xe(%rsp)       # imm = 0xE13
xorl %r14d, %r14d
movb %r14b, 0x10(%rsp)
movb %al, 0x11(%rsp)
movb %cl, 0x17(%rsp)
movw $0xe10, 0x18(%rsp)      # imm = 0xE10
movb %al, 0x1a(%rsp)
shrl $0x8, %eax
movb %al, 0x12(%rsp)
movw $0x0, 0x13(%rsp)
movb %r14b, 0x15(%rsp)
movb %al, 0x1b(%rsp)
movl $0x0, 0x1c(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
cmpl $0x40, %r8d
movl $0x40, %r15d
cmovbl %r8d, %r15d
testq %rbx, %rbx
setne %al
testq %rdi, %rdi
setne %cl
testb %al, %cl
je 0x9831
movl %esi, %ebp
cmpb $0x3, %bpl
jne 0x984b
leaq 0xc(%rsp), %rax
movb %dl, 0x4(%rax)
leal 0x2(%r15), %r9d
movl $0x7, %edx
jmp 0x9863
leaq 0x318a(%rip), %rdi      # 0xc9c2
leaq 0x3201(%rip), %rsi      # 0xca40
xorl %eax, %eax
callq 0x2506
jmp 0x9916
movl %ebp, %ecx
andb $0xf, %cl
orb $0x10, %cl
leaq 0x16(%rsp), %rax
movb %cl, 0x2(%rax)
movl $0x6, %edx
movl %r15d, %r9d
movq 0x280(%rdi), %rcx
movl 0x8(%rcx), %ecx
shrl $0x8, %ecx
andl $0x2, %ecx
addl %edx, %ecx
movzbl 0x1d6(%rdi), %esi
leaq 0x20(%rsp), %r8
movq %rax, %rdx
callq 0x9096
testl %eax, %eax
je 0x9916
cmpb $0x3, %bpl
jne 0x98c3
movzbl 0x21(%rsp), %esi
cmpl $0x3, %esi
jne 0x98e2
leaq 0x22(%rsp), %rsi
movzbl -0x2(%rsi), %r14d
addl $-0x2, %r14d
cmpl %r14d, %r15d
cmovbl %r15d, %r14d
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2150
jmp 0x9916
movzbl 0x20(%rsp), %esi
cmpl %esi, %r15d
jne 0x98f5
movl %r15d, %edx
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x2150
movl %r15d, %r14d
jmp 0x9916
leaq 0x3167(%rip), %rdi      # 0xca50
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2050
jmp 0x9916
movzbl 0x21(%rsp), %edx
movzbl 0x22(%rsp), %ecx
movzbl 0x23(%rsp), %r8d
leaq 0x3163(%rip), %rdi      # 0xca6f
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2050
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq