// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %rcx, %r14
movq %rdx, %r12
movq %rdi, %r15
movl %r9d, %eax
imulq $0x51eb851f, %rax, %rax # imm = 0x51EB851F
shrq $0x25, %rax
movl %eax, 0xfc(%rdi)
movq $0x0, (%rcx)
movq %rsi, (%rsp)
leaq 0x1(%rsi), %rax
movq %rax, 0x28(%rsp)
xorl %r9d, %r9d
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %ebx, %ebx
movq %r8, 0x8(%rsp)
movq %r9, 0x10(%rsp)
movq %rbp, 0x18(%rsp)
cmpb $0x0, 0xc9be(%rip)      # 0x11140
je 0x47bf
movq %r15, %rbp
movq %r14, %r15
movq %r12, %r14
movq (%rsp), %r12
movq %r12, %rdi
leaq 0xc9a5(%rip), %rsi      # 0x11140
callq 0x4070
movq %r12, %rdi
movq %r14, %r12
movq %r15, %r14
movq %rbp, %r15
callq 0x40a0
movq %rax, 0x20(%rsp)
movb $0x0, 0xc983(%rip)      # 0x11140
jmp 0x47dd
movq (%r14), %rax
movq (%rsp), %rcx
leaq (%rcx,%rax), %rsi
movq %r12, %rdx
subq %rax, %rdx
movq %r15, %rdi
leaq 0x20(%rsp), %rcx
callq 0x48de
movq 0x20(%rsp), %rcx
testq %rcx, %rcx
je 0x4845
movq (%r14), %rdx
leaq (%rdx,%rcx), %rax
cmpq %rax, %rdx
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %rbp
movq 0x10(%rsp), %r9
jae 0x4856
addq 0x28(%rsp), %rdx
movzbl -0x1(%rdx), %edi
cmpl $0xd, %edi
je 0x482c
xorl %esi, %esi
cmpl $0xa, %edi
je 0x481c
testb %dil, %dil
js 0x4833
testl %ebx, %ebx
je 0x4839
incq %r13
cmpq %r8, %r13
cmovaeq %rdx, %rbp
jmp 0x4839
movl $0x1, %esi
jmp 0x4839
movl $0x1, %r9d
incq %rdx
movl %esi, %ebx
decq %rcx
jne 0x4807
jmp 0x4858
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %rbp
movq 0x10(%rsp), %r9
jmp 0x485d
movl %ebx, %esi
movq %rax, (%r14)
movl %esi, %ebx
movq (%r14), %rax
cmpq %r12, %rax
jae 0x487d
cmpl $0x0, 0xfc(%r15)
je 0x487d
cmpq %r8, %r13
jae 0x487d
testl %r9d, %r9d
je 0x4771
movq (%rsp), %rbx
movb $0x0, (%rbx,%rax)
testq %rbp, %rbp
je 0x48a3
leaq 0xc8af(%rip), %rdi      # 0x11140
movq %rbp, %rsi
callq 0x4070
movb $0x0, (%rbp)
subq %rbx, %rbp
movq %rbp, (%r14)
movq (%r14), %rdx
leaq 0x47c2(%rip), %rsi      # 0x906f
leaq 0x30(%rsp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x4230
movq (%r14), %rdx
movl $0x3, %edi
movq %rbx, %rsi
movq %r15, %rcx
callq 0x4507
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq