// File: /workspace/github_c_makefile_star2_O2/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
movl %r9d, %eax
movq %rcx, %rbx
movq %rdx, %r12
movq %rdi, %r13
pushq $0x64
popq %rdi
xorl %edx, %edx
divl %edi
movl %eax, 0xfc(%r13)
andq $0x0, (%rcx)
xorl %r9d, %r9d
movq %rsi, 0x8(%rsp)
leaq 0x1(%rsi), %rax
movq %rax, 0x28(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %r8, 0x10(%rsp)
movq %r15, 0x18(%rsp)
cmpb $0x0, 0xc9f0(%rip)      # 0x11140
je 0x4788
movq %r13, %r15
movq %r12, %r13
movq 0x8(%rsp), %r12
movq %r12, %rdi
leaq 0xc9d9(%rip), %rsi      # 0x11140
callq 0x4070
movq %r12, %rdi
movq %r13, %r12
movq %r15, %r13
callq 0x40a0
movq %rax, 0x20(%rsp)
movb $0x0, 0xc9ba(%rip)      # 0x11140
jmp 0x47a9
movq 0x8(%rsp), %rax
leaq (%rax,%r9), %rsi
movq %r12, %rdx
subq %r9, %rdx
movq %r13, %rdi
leaq 0x20(%rsp), %rcx
callq 0x48b0
movq 0x20(%rsp), %rax
movq (%rbx), %r9
testq %rax, %rax
je 0x481b
addq %r9, %rax
movq 0x28(%rsp), %rdx
leaq (%rdx,%r9), %rsi
xorl %edx, %edx
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r15
pushq $0x1
popq %r11
cmpq %rax, %r9
jae 0x480e
movzbl -0x1(%rsi), %r10d
cmpl $0xd, %r10d
je 0x47fb
xorl %edi, %edi
cmpl $0xa, %r10d
je 0x47ea
testb %r10b, %r10b
js 0x4800
testl %r14d, %r14d
je 0x4803
incq %rbp
cmpq %r8, %rbp
cmovaeq %rsi, %r15
jmp 0x4803
movl %r11d, %edi
jmp 0x4803
movl %r11d, %edx
incq %r9
incq %rsi
movl %edi, %r14d
jmp 0x47cd
movq %rax, (%rbx)
testl %edx, %edx
sete %dl
movq %rax, %r9
jmp 0x4827
movb $0x1, %dl
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r15
cmpq %r12, %r9
jae 0x484a
cmpl $0x0, 0xfc(%r13)
setne %al
cmpq %r8, %rbp
setb %sil
andb %al, %sil
testb %dl, %sil
jne 0x4744
movq 0x8(%rsp), %r14
movb $0x0, (%r14,%r9)
testq %r15, %r15
je 0x4874
leaq 0xc8e0(%rip), %rdi      # 0x11140
movq %r15, %rsi
callq 0x4070
movb $0x0, (%r15)
subq %r14, %r15
movq %r15, (%rbx)
jmp 0x4877
movq (%rbx), %r15
leaq 0x47f1(%rip), %rsi      # 0x906f
leaq 0x30(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x4230
movq (%rbx), %rdx
pushq $0x3
popq %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x4501
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq