// File: chenhn123[P]wdt_util_src/wdt_ct/w8755_funcs.cpp
// Function: wh_w8755_i2c_delay(WdtDevice*, unsigned long)
int wh_w8755_i2c_delay(WDT_DEV* pdev, unsigned long delay)
{
        /* if the fw version is not supported,  just delay the max period and return */
        if (pdev->board_info.dev_info.w8755_dev_info.protocol_version < 0x01000006)
                wh_sleep(delay);
        else {
                BYTE rc = W8755_ISP_RSP_BUSY;
                unsigned long   start_tick = get_current_ms();
                int retval;
                BYTE readByte[8];
                int count = 0;
                unsigned long time_period = 0;
                int delay_slot = 10;

                if (delay < 100)
                        delay_slot = 5;

                if (!pdev || !pdev->dev_handle)
                                return 0;

                while (rc != W8755_ISP_RSP_OK && ((get_current_ms() - start_tick) < (delay + 100))) {

                        /* polling interval => 10ms */
                        wh_sleep(delay_slot);

                        retval = wh_i2c_rx(pdev, 0x2C, readByte, 3);
                        if (!retval)
                                continue;
                        else
                                /* 3 byte format: 0x0, 0x0, status */
                                rc = readByte[2];

                        count ++;
                }

                time_period = (get_current_ms() - start_tick);
                if (time_period > (delay + 50))
                        wh_printf("%s: timeout %d occured!\n", __func__, (int) time_period);

                return time_period;
        }

        return delay;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x1000006, 0x1f8(%rax) # imm = 0x1000006
jae 0x3540
movq -0x18(%rbp), %rax
movl %eax, %edi
callq 0x2590
jmp 0x3642
movb $-0x2, -0x19(%rbp)
callq 0x25d0
movq %rax, -0x28(%rbp)
movl $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movl $0xa, -0x44(%rbp)
cmpq $0x64, -0x18(%rbp)
jae 0x3571
movl $0x5, -0x44(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x3583
movq -0x10(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jne 0x358f
movl $0x0, -0x4(%rbp)
jmp 0x3649
jmp 0x3591
movzbl -0x19(%rbp), %ecx
xorl %eax, %eax
cmpl $0x80, %ecx
movb %al, -0x45(%rbp)
je 0x35bc
callq 0x25d0
subq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
addq $0x64, %rcx
cmpq %rcx, %rax
setb %al
movb %al, -0x45(%rbp)
movb -0x45(%rbp), %al
testb $0x1, %al
jne 0x35c5
jmp 0x3600
movl -0x44(%rbp), %edi
callq 0x2590
movq -0x10(%rbp), %rdi
leaq -0x34(%rbp), %rdx
movl $0x2c, %esi
movl $0x3, %ecx
callq 0xd070
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x35ef
jmp 0x3591
movb -0x32(%rbp), %al
movb %al, -0x19(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x3591
callq 0x25d0
subq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
addq $0x32, %rcx
cmpq %rcx, %rax
jbe 0x3639
movq -0x40(%rbp), %rax
movl %eax, %edx
leaq 0xc20c(%rip), %rdi      # 0xf837
leaq 0xc21e(%rip), %rsi      # 0xf850
movb $0x0, %al
callq 0x24e0
movq -0x40(%rbp), %rax
movl %eax, -0x4(%rbp)
jmp 0x3649
movq -0x18(%rbp), %rax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)