// File: /workspace/github_c_makefile_star2_O0/Carton32[P]quectel-CM/atchannel.c
// Function: writeline
static int writeline (const char *s)
{
    size_t cur = 0;
    size_t len = strlen(s);
    ssize_t written;
    static char at_command[64];

    if (s_fd < 0 || s_readerClosed > 0) {
        return AT_ERROR_CHANNEL_CLOSED;
    }

    LOGD("AT> %s", s);

    AT_DUMP( ">> ", s, strlen(s) );

#if 1 //send '\r' maybe fail via USB controller: Intel Corporation 7 Series/C210 Series Chipset Family USB xHCI Host Controller (rev 04)
    if (len < (sizeof(at_command) - 1)) {
        strcpy(at_command, s);
        at_command[len++] = '\r';
        s = (const char *)at_command;
    }
#endif

    /* the main string */
    while (cur < len) {
        do {
            written = write (s_fd, s + cur, len - cur);
        } while (written < 0 && errno == EINTR);

        if (written < 0) {
            return AT_ERROR_GENERIC;
        }

        cur += written;
    }

#if 1 //Quectel send '\r' maybe fail via USB controller: Intel Corporation 7 Series/C210 Series Chipset Family USB xHCI Host Controller (rev 04)
    if (s == (const char *)at_command) {
        return 0;
    }
#endif

    /* the \r  */

    do {
        written = write (s_fd, "\r" , 1);
    } while ((written < 0 && errno == EINTR) || (written == 0));
    
    if (written < 0) {
        return AT_ERROR_GENERIC;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5160
movq %rax, -0x20(%rbp)
cmpl $0x0, 0xf1d8(%rip)      # 0x37440
jl 0x28273
cmpl $0x0, 0x10eb7(%rip)     # 0x39128
jle 0x2827f
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x2840c
jmp 0x28281
movq 0xed58(%rip), %rax      # 0x36fe0
movq (%rax), %rax
movq %rax, -0x30(%rbp)
callq 0x13930
movq -0x30(%rbp), %rdi
movq %rax, %rdx
movq -0x10(%rbp), %rcx
leaq 0x8729(%rip), %rsi      # 0x309cf
movb $0x0, %al
callq 0x52c0
leaq 0xfbb4(%rip), %rax      # 0x37e68
cmpq $0x0, (%rax)
je 0x282e6
leaq 0xfba7(%rip), %rax      # 0x37e68
movq (%rax), %rax
movq %rax, -0x38(%rbp)
callq 0x13930
movq -0x38(%rbp), %rdi
movq %rax, %rdx
movq -0x10(%rbp), %rcx
leaq 0x86f0(%rip), %rsi      # 0x309cf
movb $0x0, %al
callq 0x52c0
jmp 0x282e8
jmp 0x282ea
jmp 0x282ec
cmpq $0x3f, -0x20(%rbp)
jae 0x28328
movq -0x10(%rbp), %rsi
leaq 0x10e82(%rip), %rdi     # 0x39180
callq 0x50a0
movq -0x20(%rbp), %rcx
movq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
leaq 0x10e67(%rip), %rax     # 0x39180
movb $0xd, (%rax,%rcx)
leaq 0x10e5c(%rip), %rax     # 0x39180
movq %rax, -0x10(%rbp)
jmp 0x2832a
movq -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x28397
jmp 0x28336
movl 0xf104(%rip), %edi      # 0x37440
movq -0x10(%rbp), %rsi
addq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
subq -0x18(%rbp), %rdx
callq 0x5100
movq %rax, -0x28(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x28(%rbp)
movb %al, -0x39(%rbp)
jge 0x2836f
callq 0x5070
cmpl $0x4, (%rax)
sete %al
movb %al, -0x39(%rbp)
movb -0x39(%rbp), %al
testb $0x1, %al
jne 0x28336
cmpq $0x0, -0x28(%rbp)
jge 0x28389
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2840c
movq -0x28(%rbp), %rax
addq -0x18(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x2832a
leaq 0x10de2(%rip), %rax     # 0x39180
cmpq %rax, -0x10(%rbp)
jne 0x283ad
movl $0x0, -0x4(%rbp)
jmp 0x2840c
jmp 0x283af
movl 0xf08b(%rip), %edi      # 0x37440
leaq 0x8620(%rip), %rsi      # 0x309dc
movl $0x1, %edx
callq 0x5100
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jge 0x283e3
callq 0x5070
movq %rax, %rcx
movb $0x1, %al
cmpl $0x4, (%rcx)
movb %al, -0x3a(%rbp)
je 0x283ee
cmpq $0x0, -0x28(%rbp)
sete %al
movb %al, -0x3a(%rbp)
movb -0x3a(%rbp), %al
testb $0x1, %al
jne 0x283af
cmpq $0x0, -0x28(%rbp)
jge 0x28405
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2840c
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)