// File: /workspace/github_c_makefile_star2_O3/bokunimowakaru[P]petit15term/lpc21isp/lpc21isp.c
// Function: ReceiveComPort
void ReceiveComPort(ISP_ENVIRONMENT *IspEnvironment,
                                    const char *Ans, unsigned long MaxSize,
                                    unsigned long *RealSize, unsigned long WantedNr0x0A,
                                    unsigned timeOutMilliseconds)
{
    unsigned long tmp_realsize;
    unsigned long nr_of_0x0A = 0;
    unsigned long nr_of_0x0D = 0;
    int eof = 0;
    unsigned long p;
    unsigned char *Answer;
    unsigned char *endPtr;
    char tmp_string[32];
    static char residual_data[128] = {'\0'};
    int lf = 0;

    Answer  = (unsigned char*) Ans;

    SerialTimeoutSet(IspEnvironment, timeOutMilliseconds);

    *RealSize = 0;
    endPtr = NULL;

    do
    {
        if (residual_data[0] == '\0')
        {
            /* Receive new data */
            ReceiveComPortBlock(IspEnvironment, Answer + (*RealSize), MaxSize - (*RealSize), &tmp_realsize);
        }
        else
        {
            /* Take over any old residual data */
            strcpy((char *)Answer, residual_data);
            tmp_realsize = strlen((char *)Answer);
            residual_data[0] = '\0';
        }

        if (tmp_realsize != 0)
        {
            for (p = (*RealSize); p < (*RealSize) + tmp_realsize; p++)
            {
                /* Torsten Lang 2013-05-06 Scan for 0x0d,0x0a,0x0a and 0x0d,0x0a as linefeed pattern */
                if (Answer[p] == 0x0a)
                {
                    if (lf != 0)
                    {
                        nr_of_0x0A++;
                        lf = 0;
                        if (nr_of_0x0A >= WantedNr0x0A)
                        {
                            endPtr = &Answer[p+1];
                        }
                    }
                }
                else if (Answer[p] == 0x0d)
                {
                    nr_of_0x0D++;
                    lf = 1;
                }
                else if (((signed char) Answer[p]) < 0)
                {
                    eof = 1;
                    lf  = 0;
                }
                else if (lf != 0)
                {
                    nr_of_0x0D++;
                    nr_of_0x0A++;
                    lf = 0;
                    if (nr_of_0x0A >= WantedNr0x0A)
                    {
                        endPtr = &Answer[p+1];
                    }
                }
            }
            (*RealSize) += tmp_realsize;
        }
    } while (((*RealSize) < MaxSize) && (SerialTimeoutCheck(IspEnvironment) == 0) && (nr_of_0x0A < WantedNr0x0A) && !eof);

    /* Torsten Lang 2013-05-06 Store residual data and cut answer after expected nr. of 0x0a */
    Answer[*RealSize] = '\0';
    if (endPtr != NULL)
    {
        strcpy(residual_data, (char *)endPtr);
        *endPtr = '\0';
        /* Torsten Lang 2013-06-28 Update size info */
        *RealSize = endPtr-Answer;
    }

    sprintf(tmp_string, "Answer(Length=%ld): ", (*RealSize));
    DumpString(3, Answer, (*RealSize), tmp_string);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %r12
movq %rcx, %r14
movq %rdx, 0x8(%rsp)
movq %rsi, %rbx
movl %r9d, %eax
imulq $0x51eb851f, %rax, %rax # imm = 0x51EB851F
shrq $0x25, %rax
movq %rdi, (%rsp)
movl %eax, 0xfc(%rdi)
movq $0x0, (%rcx)
leaq 0x1(%rsi), %rax
movq %rax, 0x18(%rsp)
xorl %ecx, %ecx
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpb $0x0, 0xc9c7(%rip)      # 0x11140
je 0x47a0
movq %rbx, %rdi
leaq 0xc9bb(%rip), %rsi      # 0x11140
callq 0x4070
movq %rbx, %rdi
callq 0x40a0
movq %rax, 0x10(%rsp)
movb $0x0, 0xc9a2(%rip)      # 0x11140
jmp 0x47bf
leaq (%rbx,%rcx), %rsi
movq 0x8(%rsp), %rdx
subq %rcx, %rdx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rcx
callq 0x48ac
movq 0x10(%rsp), %rax
movq (%r14), %rdx
testq %rax, %rax
je 0x481e
leaq (%rdx,%rax), %rcx
movb $0x1, %sil
cmpq %rcx, %rdx
jae 0x4819
addq 0x18(%rsp), %rdx
xorl %esi, %esi
movzbl -0x1(%rdx), %edi
cmpl $0xd, %edi
je 0x47fd
cmpl $0xa, %edi
je 0x47ed
testb %dil, %dil
js 0x4804
testl %ebp, %ebp
je 0x4809
incq %r13
cmpq %r12, %r13
cmovaeq %rdx, %r15
jmp 0x4809
movl $0x1, %ebp
jmp 0x480b
movl $0x1, %esi
xorl %ebp, %ebp
incq %rdx
decq %rax
jne 0x47da
testl %esi, %esi
sete %sil
movq %rcx, (%r14)
jmp 0x4824
movb $0x1, %sil
movq %rdx, %rcx
cmpq 0x8(%rsp), %rcx
jae 0x484a
movq (%rsp), %rax
cmpl $0x0, 0xfc(%rax)
setne %al
cmpq %r12, %r13
setb %dl
andb %al, %dl
testb %sil, %dl
jne 0x4772
movb $0x0, (%rbx,%rcx)
testq %r15, %r15
je 0x486e
leaq 0xc8e6(%rip), %rdi      # 0x11140
movq %r15, %rsi
callq 0x4070
movb $0x0, (%r15)
subq %rbx, %r15
movq %r15, (%r14)
jmp 0x4871
movq (%r14), %r15
leaq 0x47f7(%rip), %rsi      # 0x906f
leaq 0x20(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x4230
movq (%r14), %rdx
movl $0x3, %edi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x4507
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq