// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x94(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x4c40
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x68(%rbp)
movsbl 0xe76e(%rip), %eax      # 0x13140
cmpl $0x0, %eax
jne 0x49fc
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x20(%rbp), %rax
addq (%rax), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rax
subq (%rax), %rdx
leaq -0x38(%rbp), %rcx
callq 0x4c70
jmp 0x4a20
movq -0x60(%rbp), %rdi
leaq 0xe739(%rip), %rsi      # 0x13140
callq 0x4070
movq -0x60(%rbp), %rdi
callq 0x40a0
movq %rax, -0x38(%rbp)
movb $0x0, 0xe720(%rip)      # 0x13140
cmpq $0x0, -0x38(%rbp)
je 0x4b60
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
addq -0x38(%rbp), %rcx
cmpq %rcx, %rax
jae 0x4b52
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x4aa2
cmpl $0x0, -0x94(%rbp)
je 0x4a9d
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x94(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x28(%rbp), %rax
jb 0x4a9b
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
addq $0x1, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
jmp 0x4a9d
jmp 0x4b3f
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0x4acb
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0x4b3d
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jge 0x4aef
movl $0x1, -0x4c(%rbp)
movl $0x0, -0x94(%rbp)
jmp 0x4b3b
cmpl $0x0, -0x94(%rbp)
je 0x4b39
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x94(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x28(%rbp), %rax
jb 0x4b37
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
addq $0x1, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
jmp 0x4b39
jmp 0x4b3b
jmp 0x4b3d
jmp 0x4b3f
jmp 0x4b41
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x4a36
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
jmp 0x4b62
movq -0x20(%rbp), %rax
movq (%rax), %rcx
xorl %eax, %eax
cmpq -0x18(%rbp), %rcx
movb %al, -0x95(%rbp)
jae 0x4bb0
movq -0x8(%rbp), %rdi
callq 0x4de0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x95(%rbp)
jne 0x4bb0
movq -0x40(%rbp), %rcx
xorl %eax, %eax
cmpq -0x28(%rbp), %rcx
movb %al, -0x95(%rbp)
jae 0x4bb0
cmpl $0x0, -0x4c(%rbp)
setne %al
xorb $-0x1, %al
movb %al, -0x95(%rbp)
movb -0x95(%rbp), %al
testb $0x1, %al
jne 0x49cb
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movb $0x0, (%rax,%rcx)
cmpq $0x0, -0x68(%rbp)
je 0x4bfd
movq -0x68(%rbp), %rsi
leaq 0xe561(%rip), %rdi      # 0x13140
callq 0x4070
movq -0x68(%rbp), %rax
movb $0x0, (%rax)
movq -0x68(%rbp), %rcx
movq -0x60(%rbp), %rax
subq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
leaq -0x90(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rdx
leaq 0x6445(%rip), %rsi      # 0xb057
movb $0x0, %al
callq 0x4240
movq -0x60(%rbp), %rsi
movq -0x20(%rbp), %rax
movq (%rax), %rdx
leaq -0x90(%rbp), %rcx
movl $0x3, %edi
callq 0x4630
addq $0xa0, %rsp
popq %rbp
retq
nop