// File: /workspace/github_c_makefile_star2_O1/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 0xea0b(%rip), %rax      # 0x2a318
cmpb $0x0, (%rax)
je 0x1b974
movq 0xe9ff(%rip), %rbx      # 0x2a318
incq %rbx
movzbl -0x1(%rbx), %eax
cmpl $0xa, %eax
je 0x1b92a
cmpl $0xd, %eax
jne 0x1b933
movq %rbx, 0xe9e7(%rip)      # 0x2a318
jmp 0x1b919
cmpl $0x3e, %eax
jne 0x1b947
cmpb $0x20, (%rbx)
jne 0x1b947
cmpb $0x0, 0x1(%rbx)
je 0x1bc77
leaq -0x1(%rbx), %rax
movl $0x2401, %ecx           # imm = 0x2401
movzbl (%rax), %edx
cmpl $0xd, %edx
ja 0x1b95d
btl %edx, %ecx
jb 0x1b962
incq %rax
jmp 0x1b950
xorl %ecx, %ecx
testb %dl, %dl
cmoveq %rcx, %rax
testq %rax, %rax
je 0x1b988
xorl %r15d, %r15d
jmp 0x1b9b8
leaq 0xf3b5(%rip), %r15      # 0x2ad30
movq %r15, 0xe996(%rip)      # 0x2a318
movb $0x0, (%r15)
jmp 0x1b9b6
decq %rbx
movq %rbx, %rdi
callq 0x5160
movq %rax, %r15
leaq 0x1(%rax), %rdx
leaq 0xf38f(%rip), %r14      # 0x2ad30
movq %r14, %rdi
movq %rbx, %rsi
callq 0x53e0
addq %r14, %r15
movq %r14, 0xe962(%rip)      # 0x2a318
xorl %eax, %eax
testq %rax, %rax
jne 0x1bb41
movl $0x1000, %eax           # imm = 0x1000
addq 0xe5e3(%rip), %rax      # 0x29fb0
movq %rax, 0x10(%rsp)
leaq 0x7a99(%rip), %rbp      # 0x23472
leaq 0x8(%rsp), %r14
movl $0x2401, %ebx           # imm = 0x2401
movq %r15, %r12
cmpq 0x10(%rsp), %r15
jne 0x1ba43
movq 0xe5cc(%rip), %rax      # 0x29fc0
movq (%rax), %r15
callq 0xeb2f
movq %r15, %rdi
movq %rbp, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0xf295(%rip), %rax      # 0x2aca8
movq (%rax), %r15
testq %r15, %r15
je 0x1ba30
callq 0xeb2f
movq %r15, %rdi
movq %rbp, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0xf2f9(%rip), %r12      # 0x2ad30
movq %r12, 0xe8da(%rip)      # 0x2a318
movb $0x0, (%r12)
leaq 0xf2e6(%rip), %r15      # 0x2ad30
subq %r12, %r15
addq $0x1000, %r15           # imm = 0x1000
cmpl $0x0, 0xe8b5(%rip)      # 0x2a310
jle 0x1ba98
movl 0xe8ad(%rip), %eax      # 0x2a310
movl %eax, 0x8(%rsp)
movl $0x1, 0xc(%rsp)
movl $0x1, %esi
movq %r14, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x53c0
testl %eax, %eax
jns 0x1ba8f
callq 0x5070
cmpl $0x4, (%rax)
je 0x1ba6f
movzwl 0xe(%rsp), %eax
testb $0x39, %al
je 0x1ba54
movl 0xe872(%rip), %edi      # 0x2a310
cmpl $-0x1, %edi
je 0x1bab3
movq %r12, %rsi
movq %r15, %rdx
callq 0x5250
movq %rax, %r13
jmp 0x1bab6
xorl %r13d, %r13d
testq %r13, %r13
jns 0x1bac5
callq 0x5070
cmpl $0x4, (%rax)
je 0x1ba54
testq %r13, %r13
jle 0x1bbb8
addl %r13d, 0x104cb(%rip)    # 0x2bfa0
leaq (%r12,%r13), %r15
movb $0x0, (%r12,%r13)
movq 0xe833(%rip), %rax      # 0x2a318
addq $0x2, %rax
movzbl -0x2(%rax), %ecx
cmpl $0xa, %ecx
je 0x1baf7
cmpl $0xd, %ecx
jne 0x1bb07
leaq -0x1(%rax), %rcx
movq %rcx, 0xe816(%rip)      # 0x2a318
incq %rax
jmp 0x1bae9
cmpl $0x3e, %ecx
jne 0x1bb17
cmpb $0x20, -0x1(%rax)
jne 0x1bb17
cmpb $0x0, (%rax)
je 0x1bb38
addq $-0x2, %rax
movzbl (%rax), %ecx
cmpl $0xd, %ecx
ja 0x1bb28
btl %ecx, %ebx
jb 0x1bb2d
incq %rax
jmp 0x1bb1b
testb %cl, %cl
movl $0x0, %ecx
cmoveq %rcx, %rax
testq %rax, %rax
je 0x1b9e3
movq 0xe7d0(%rip), %rbx      # 0x2a318
movb $0x0, (%rax)
incq %rax
movq %rax, 0xe7c3(%rip)      # 0x2a318
movq 0xe464(%rip), %rax      # 0x29fc0
movq (%rax), %r14
callq 0xeb2f
leaq 0x796c(%rip), %rsi      # 0x234d7
movq %r14, %rdi
movq %rax, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x52c0
leaq 0xf126(%rip), %rax      # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0x1bba6
callq 0xeb2f
leaq 0x7941(%rip), %rsi      # 0x234d7
movq %r14, %rdi
movq %rax, %rdx
movq %rbx, %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
movq 0xe401(%rip), %rax      # 0x29fc0
movq (%rax), %r14
callq 0xeb2f
movq %rax, %r15
testq %r13, %r13
je 0x1bc2e
callq 0x5070
movq %rax, %r12
movl (%rax), %edi
callq 0x5530
leaq 0x78d3(%rip), %rsi      # 0x234b8
xorl %ebx, %ebx
movq %r14, %rdi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x52c0
leaq 0xf0aa(%rip), %rax      # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0x1bba6
callq 0xeb2f
movq %rax, %r15
movl (%r12), %edi
callq 0x5530
leaq 0x789a(%rip), %rsi      # 0x234b8
xorl %ebx, %ebx
movq %r14, %rdi
movq %r15, %rdx
movq %rax, %rcx
jmp 0x1bb9f
leaq 0x7866(%rip), %rsi      # 0x2349b
xorl %ebx, %ebx
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0xf05d(%rip), %rax      # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0x1bba6
callq 0xeb2f
leaq 0x7838(%rip), %rsi      # 0x2349b
xorl %ebx, %ebx
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
jmp 0x1bba6
leaq 0x1(%rbx), %rax
jmp 0x1b96a