// File: /workspace/github_c_makefile_star2_O3/Carton32[P]quectel-CM/atchannel.c
// Function: readline
static const char *readline()
{
    ssize_t count;

    char *p_read = NULL;
    char *p_eol = NULL;
    char *ret;

    /* this is a little odd. I use *s_ATBufferCur == 0 to
     * mean "buffer consumed completely". If it points to a character, than
     * the buffer continues until a \0
     */
    if (*s_ATBufferCur == '\0') {
        /* empty buffer */
        s_ATBufferCur = s_ATBuffer;
        *s_ATBufferCur = '\0';
        p_read = s_ATBuffer;
    } else {   /* *s_ATBufferCur != '\0' */
        /* there's data in the buffer from the last read */

        // skip over leading newlines
        while (*s_ATBufferCur == '\r' || *s_ATBufferCur == '\n')
            s_ATBufferCur++;

        p_eol = findNextEOL(s_ATBufferCur);

        if (p_eol == NULL) {
            /* a partial line. move it up and prepare to read more */
            size_t len;

            len = strlen(s_ATBufferCur);

            memmove(s_ATBuffer, s_ATBufferCur, len + 1);
            p_read = s_ATBuffer + len;
            s_ATBufferCur = s_ATBuffer;
        }
        /* Otherwise, (p_eol !- NULL) there is a complete line  */
        /* that will be returned the while () loop below        */
    }

    while (p_eol == NULL) {
        if (0 == MAX_AT_RESPONSE - (p_read - s_ATBuffer)) {
            LOGE("ERROR: Input line exceeded buffer\n");
            /* ditch buffer and start over again */
            s_ATBufferCur = s_ATBuffer;
            *s_ATBufferCur = '\0';
            p_read = s_ATBuffer;
        }

        do {
            while (s_fd > 0) {
                struct pollfd pollfds[1] = {{s_fd, POLLIN, 0}};
                int ret;
            
                do {
                    ret = poll(pollfds, 1, -1);
                } while ((ret < 0) && (errno == EINTR));

                if (pollfds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
                    break;
                } else if   (pollfds[0].revents & (POLLIN))  {
                    break;
                }
            };

            count = (s_fd == -1) ? 0 : read(s_fd, p_read,
                            MAX_AT_RESPONSE - (p_read - s_ATBuffer));
        } while (count < 0 && errno == EINTR);

        if (count > 0) {
            AT_DUMP( "<< ", p_read, count );
            s_readCount += count;

            p_read[count] = '\0';

            // skip over leading newlines
            while (*s_ATBufferCur == '\r' || *s_ATBufferCur == '\n')
                s_ATBufferCur++;

            p_eol = findNextEOL(s_ATBufferCur);
            p_read += count;
        } else if (count <= 0) {
            /* read error encountered or EOF reached */
            if(count == 0) {
                LOGD("atchannel: EOF reached");
            } else {
                LOGD("atchannel: read error %s", strerror(errno));
            }
            return NULL;
        }
    }

    /* a full line in the buffer. Place a \0 over the \r and return */

    ret = s_ATBufferCur;
    *p_eol = '\0';
    s_ATBufferCur = p_eol + 1; /* this will always be <= p_read,    */
                              /* and there will be a \0 at *p_read */

    LOGD("AT< %s", ret);
    return ret;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq 0xee25(%rip), %rbx      # 0x2a318
movb (%rbx), %cl
testb %cl, %cl
jne 0x1b516
leaq 0xf830(%rip), %r14      # 0x2ad30
movq %r14, 0xee11(%rip)      # 0x2a318
movb $0x0, (%r14)
jmp 0x1b58c
movq %rbx, 0xee04(%rip)      # 0x2a318
movb (%rbx), %cl
incq %rbx
cmpb $0xa, %cl
je 0x1b50d
movzbl %cl, %eax
cmpl $0xd, %eax
je 0x1b50d
cmpl $0x3e, %eax
jne 0x1b53a
cmpb $0x20, (%rbx)
jne 0x1b53a
cmpb $0x0, 0x1(%rbx)
je 0x1b7bf
decq %rbx
movq %rbx, %rax
testb %cl, %cl
je 0x1b561
movzbl %cl, %ecx
cmpl $0xa, %ecx
je 0x1b7c8
cmpl $0xd, %ecx
je 0x1b7c8
movb 0x1(%rax), %cl
incq %rax
jmp 0x1b540
movq %rbx, %rdi
callq 0x5160
movq %rax, %r14
leaq 0x1(%rax), %rdx
leaq 0xf7b9(%rip), %r15      # 0x2ad30
movq %r15, %rdi
movq %rbx, %rsi
callq 0x53e0
addq %r15, %r14
movq %r15, 0xed8c(%rip)      # 0x2a318
movl $0x1000, %eax           # imm = 0x1000
addq 0xea18(%rip), %rax      # 0x29fb0
movq %r14, %rbx
cmpq %rax, %r14
jne 0x1b5fc
movq 0xea19(%rip), %rax      # 0x29fc0
movq (%rax), %rbx
callq 0xea3b
leaq 0x7edc(%rip), %rsi      # 0x23492
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0xf6de(%rip), %rax      # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x1b5eb
callq 0xea3b
leaq 0x7eb4(%rip), %rsi      # 0x23492
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0xf73e(%rip), %rbx      # 0x2ad30
movq %rbx, 0xed1f(%rip)      # 0x2a318
movb $0x0, (%rbx)
leaq 0xf72d(%rip), %r15      # 0x2ad30
subq %rbx, %r15
addq $0x1000, %r15           # imm = 0x1000
movl 0xecfd(%rip), %edi      # 0x2a310
testl %edi, %edi
jle 0x1b651
movl %edi, (%rsp)
movl $0x1, 0x4(%rsp)
movq %rsp, %rdi
movl $0x1, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x53c0
testl %eax, %eax
jns 0x1b642
callq 0x5070
cmpl $0x4, (%rax)
je 0x1b622
movzwl 0x6(%rsp), %eax
movl 0xecc3(%rip), %edi      # 0x2a310
testb $0x39, %al
je 0x1b613
cmpl $-0x1, %edi
je 0x1b6f1
movq %rbx, %rsi
movq %r15, %rdx
callq 0x5250
testq %rax, %rax
jns 0x1b679
callq 0x5070
cmpl $0x4, (%rax)
je 0x1b60d
jmp 0x1b749
je 0x1b6f1
movq %rax, %r14
addl %eax, 0x1091c(%rip)     # 0x2bfa0
addq %rbx, %r14
movb $0x0, (%rbx,%rax)
movq 0xec86(%rip), %rcx      # 0x2a318
addq $0x2, %rcx
movzbl -0x2(%rcx), %edx
cmpl $0xa, %edx
je 0x1b6a4
cmpl $0xd, %edx
jne 0x1b6b4
leaq -0x1(%rcx), %rax
movq %rax, 0xec69(%rip)      # 0x2a318
incq %rcx
jmp 0x1b696
cmpl $0x3e, %edx
jne 0x1b6c8
cmpb $0x20, -0x1(%rcx)
jne 0x1b6c8
cmpb $0x0, (%rcx)
je 0x1b835
leaq -0x2(%rcx), %rax
testb %dl, %dl
je 0x1b58c
movzbl %dl, %edx
cmpl $0xa, %edx
je 0x1b7b6
cmpl $0xd, %edx
je 0x1b7b6
movb 0x1(%rax), %dl
incq %rax
jmp 0x1b6cc
movq 0xe8c8(%rip), %rax      # 0x29fc0
movq (%rax), %r14
callq 0xea3b
leaq 0x7db4(%rip), %rsi      # 0x234bb
xorl %ebx, %ebx
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0xf58b(%rip), %rax      # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0x1b826
callq 0xea3b
leaq 0x7d86(%rip), %rsi      # 0x234bb
xorl %ebx, %ebx
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
jmp 0x1b826
movq %rax, %r14
movq 0xe86d(%rip), %rax      # 0x29fc0
movq (%rax), %r15
callq 0xea3b
movq %rax, %r12
movl (%r14), %edi
callq 0x5530
leaq 0x7d6b(%rip), %rsi      # 0x234d8
xorl %ebx, %ebx
movq %r15, %rdi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x52c0
leaq 0xf522(%rip), %rax      # 0x2aca8
movq (%rax), %r15
testq %r15, %r15
je 0x1b826
callq 0xea3b
movq %rax, %r12
movl (%r14), %edi
callq 0x5530
leaq 0x7d2f(%rip), %rsi      # 0x234d8
xorl %ebx, %ebx
movq %r15, %rdi
movq %r12, %rdx
movq %rax, %rcx
jmp 0x1b81f
addq $-0x2, %rcx
movq %rcx, %rbx
jmp 0x1b7c8
movq %rbx, %rax
decq %rbx
incq %rax
movb $0x0, (%rax)
incq %rax
movq %rax, 0xeb43(%rip)      # 0x2a318
movq 0xe7e4(%rip), %rax      # 0x29fc0
movq (%rax), %r14
callq 0xea3b
leaq 0x7d0c(%rip), %rsi      # 0x234f7
movq %r14, %rdi
movq %rax, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x52c0
leaq 0xf4a6(%rip), %rax      # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0x1b826
callq 0xea3b
leaq 0x7ce1(%rip), %rsi      # 0x234f7
movq %r14, %rdi
movq %rax, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x52c0
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq -0x2(%rcx), %rbx
movq %rcx, %rax
jmp 0x1b7c8