// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq 0xf037(%rip), %rbx      # 0x29318
movb (%rbx), %al
testb %al, %al
je 0x1a316
incq %rbx
cmpb $0xd, %al
je 0x1a2f6
movzbl %al, %eax
cmpl $0xa, %eax
jne 0x1a301
movq %rbx, 0xf01b(%rip)      # 0x29318
movb (%rbx), %al
jmp 0x1a2e7
decq %rbx
movq %rbx, %rdi
callq 0x1a5e2
testq %rax, %rax
je 0x1a32e
xorl %r14d, %r14d
jmp 0x1a35e
leaq 0xfa13(%rip), %rbx      # 0x29d30
movq %rbx, 0xeff4(%rip)      # 0x29318
movb $0x0, (%rbx)
xorl %eax, %eax
movq %rbx, %r14
jmp 0x1a35e
movq %rbx, %rdi
callq 0x5160
movq %rax, %r14
leaq 0x1(%rax), %rdx
leaq 0xf9ec(%rip), %r15      # 0x29d30
movq %r15, %rdi
movq %rbx, %rsi
callq 0x53f0
addq %r15, %r14
movq %r15, 0xefbf(%rip)      # 0x29318
xorl %eax, %eax
movq %r15, %rbx
movl $0x1000, %ecx           # imm = 0x1000
addq 0xec3e(%rip), %rcx      # 0x28fa8
leaq 0x7179(%rip), %r13      # 0x214ea
leaq 0xf9b8(%rip), %rdx      # 0x29d30
leaq 0x8(%rsp), %r15
pushq $0x1
popq %r12
pushq $-0x1
popq %rbp
movq %rcx, 0x10(%rsp)
testq %rax, %rax
jne 0x1a589
cmpq %rcx, %r14
jne 0x1a3eb
movq %rdx, %r14
movq 0xec17(%rip), %rax      # 0x28fb8
movq (%rax), %rbx
callq 0xe343
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0xf8e8(%rip), %rax      # 0x29ca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x1a3dd
callq 0xe343
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
movq %r14, %rdx
movq %r14, 0xef31(%rip)      # 0x29318
movb $0x0, (%r14)
movq %rdx, %rbx
subq %r14, %rbx
addq $0x1000, %rbx           # imm = 0x1000
movl 0xef12(%rip), %edi      # 0x29310
testl %edi, %edi
jle 0x1a438
movl %edi, 0x8(%rsp)
movl $0x1, 0xc(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x53d0
testl %eax, %eax
jns 0x1a429
callq 0x5070
cmpl $0x4, (%rax)
je 0x1a40e
movzwl 0xe(%rsp), %eax
movl 0xeedc(%rip), %edi      # 0x29310
testb $0x39, %al
je 0x1a3fe
cmpl $-0x1, %edi
je 0x1a4b2
movq %r14, %rsi
movq %rbx, %rdx
callq 0x5250
testq %rax, %rax
jns 0x1a45c
callq 0x5070
cmpl $0x4, (%rax)
je 0x1a3f8
jmp 0x1a507
je 0x1a4b2
movq %rax, %r13
addl %eax, 0x10b39(%rip)     # 0x2afa0
addq %r14, %r13
movb $0x0, (%r14,%rax)
movq 0xeea2(%rip), %rbx      # 0x29318
movzbl (%rbx), %eax
cmpl $0xd, %eax
je 0x1a483
cmpl $0xa, %eax
jne 0x1a48f
incq %rbx
movq %rbx, 0xee8b(%rip)      # 0x29318
jmp 0x1a476
movq %rbx, %rdi
callq 0x1a5e2
movq %r13, %r14
movq 0x10(%rsp), %rcx
leaq 0x7044(%rip), %r13      # 0x214ea
leaq 0xf883(%rip), %rdx      # 0x29d30
jmp 0x1a389
movq 0xeaff(%rip), %rax      # 0x28fb8
movq (%rax), %r14
callq 0xe343
leaq 0x704b(%rip), %rsi      # 0x21513
xorl %ebx, %ebx
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0xf7ca(%rip), %rax      # 0x29ca8
movq (%rax), %r14
testq %r14, %r14
je 0x1a577
callq 0xe343
leaq 0x701d(%rip), %rsi      # 0x21513
xorl %ebx, %ebx
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
jmp 0x1a577
movq %rax, %r13
movq 0xeaa7(%rip), %rax      # 0x28fb8
movq (%rax), %r14
callq 0xe343
movq %rax, %r15
movl (%r13), %edi
callq 0x5540
leaq 0x7004(%rip), %rsi      # 0x21530
xorl %ebx, %ebx
movq %r14, %rdi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x52c0
leaq 0xf763(%rip), %rax      # 0x29ca8
movq (%rax), %r14
testq %r14, %r14
je 0x1a577
callq 0xe343
movq %rax, %r15
movl (%r13), %edi
callq 0x5540
leaq 0x6fcb(%rip), %rsi      # 0x21530
xorl %ebx, %ebx
movq %r14, %rdi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x52c0
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, (%rax)
incq %rax
movq %rax, 0xed82(%rip)      # 0x29318
movq 0xea1b(%rip), %rax      # 0x28fb8
movq (%rax), %r14
callq 0xe343
leaq 0x6fa3(%rip), %rsi      # 0x2154f
movq %r14, %rdi
movq %rax, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x52c0
leaq 0xf6e5(%rip), %rax      # 0x29ca8
movq (%rax), %r14
testq %r14, %r14
je 0x1a577
callq 0xe343
leaq 0x6f78(%rip), %rsi      # 0x2154f
movq %r14, %rdi
movq %rax, %rdx
movq %rbx, %rcx
jmp 0x1a570