// File: /workspace/github_c_makefile_star2_O2/bokunimowakaru[P]petit15term/lpc21isp/lpc21isp.c
// Function: ReceiveComPortBlock
static void ReceiveComPortBlock(ISP_ENVIRONMENT *IspEnvironment,
                                          void *answer, unsigned long max_size,
                                          unsigned long *real_size)
{
    char tmp_string[32];

#if defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN

    if (IspEnvironment->HalfDuplex == 0)
        ReadFile(IspEnvironment->hCom, answer, max_size, real_size, NULL);
    else
    {
        *real_size = strlen (pRxTmpBuf);
        if (*real_size)
        {
            if (max_size >= *real_size)
            {
                strncpy((char*) answer, pRxTmpBuf, *real_size);
                RxTmpBuf[0] = 0;
                pRxTmpBuf = RxTmpBuf;
            }
            else
            {
                strncpy((char*) answer, pRxTmpBuf, max_size);
                *real_size = max_size;
                pRxTmpBuf += max_size;
            }
        }
        else
            ReadFile(IspEnvironment->hCom, answer, max_size, real_size, NULL);
    }

#endif // defined COMPILE_FOR_WINDOWS || defined COMPILE_FOR_CYGWIN

#if defined COMPILE_FOR_LPC21

    *real_size = read(IspEnvironment->fdCom, answer, max_size);

#endif // defined COMPILE_FOR_LPC21

#if defined COMPILE_FOR_LINUX
    {
        fd_set
            readSet;
        struct timeval
            timeVal;

        FD_ZERO(&readSet);                             // clear the set
        FD_SET(IspEnvironment->fdCom,&readSet);        // add this descriptor to the set
        timeVal.tv_sec=0;                              // set up the timeout waiting for one to come ready (500ms)
        timeVal.tv_usec=500*1000;
        if(select(FD_SETSIZE,&readSet,NULL,NULL,&timeVal)==1)    // wait up to 500 ms or until our data is ready
        {
            *real_size=read(IspEnvironment->fdCom, answer, max_size);
        }
        else
        {
            // timed out, show no characters received and timer expired
            *real_size=0;
            IspEnvironment->serial_timeout_count=0;
        }
    }
#endif // defined COMPILE_FOR_LINUX

    sprintf(tmp_string, "Read(Length=%ld): ", (*real_size));
    DumpString(5, answer, (*real_size), tmp_string);

    if (*real_size == 0)
    {
        SerialTimeoutTick(IspEnvironment);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x90, %rsp
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbx
xorl %eax, %eax
cmpq $0x10, %rax
je 0x48de
andq $0x0, 0x10(%rsp,%rax,8)
incq %rax
jmp 0x48cd
movl 0x80(%rbx), %ecx
pushq $0x40
popq %rsi
movl %ecx, %eax
cltd
idivl %esi
movslq %eax, %rdx
shll $0x6, %eax
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
orq %rax, 0x10(%rsp,%rdx,8)
movq %rsp, %r8
andq $0x0, (%r8)
movq $0x7a120, 0x8(%r8)      # imm = 0x7A120
leaq 0x10(%rsp), %rsi
movl $0x400, %edi            # imm = 0x400
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4150
cmpl $0x1, %eax
jne 0x493c
movl 0x80(%rbx), %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4110
movq %rax, %rdx
jmp 0x4945
andl $0x0, 0xfc(%rbx)
xorl %edx, %edx
movq %rdx, (%r14)
leaq 0x4758(%rip), %rsi      # 0x90a7
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0x4230
movq (%r14), %rdx
pushq $0x5
popq %rdi
movq %r15, %rsi
movq %r12, %rcx
callq 0x4501
cmpq $0x0, (%r14)
jne 0x498c
movl 0xfc(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %eax
cmovbl %ebp, %ecx
movl %ecx, 0xfc(%rbx)
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq