// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x50(%rax)
movaps %xmm0, 0x60(%rax)
movaps %xmm0, 0x70(%rax)
movl 0x80(%rdi), %ecx
leal 0x3f(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
movl %edx, %edi
andl $-0x40, %edi
subl %edi, %ecx
movl $0x1, %edi
shlq %cl, %rdi
sarl $0x6, %edx
movslq %edx, %rcx
orq %rdi, 0x10(%rsp,%rcx,8)
movq %rsi, %r12
movq %rsp, %r8
movq $0x0, (%r8)
movq $0x7a120, 0x8(%r8)      # imm = 0x7A120
movl $0x400, %edi            # imm = 0x400
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4150
cmpl $0x1, %eax
jne 0x4986
movl 0x80(%rbx), %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x4110
movq %rax, (%r14)
jmp 0x4997
movq $0x0, (%r14)
movl $0x0, 0xfc(%rbx)
movq (%r14), %rdx
leaq 0x4706(%rip), %rsi      # 0x90a7
xorl %ebp, %ebp
leaq 0x10(%rsp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x4230
movq (%r14), %rdx
movl $0x5, %edi
movq %r12, %rsi
movq %r15, %rcx
callq 0x4507
cmpq $0x0, (%r14)
jne 0x49e0
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