// File: /workspace/github_c_makefile_star2_O2/Carton32[P]quectel-CM/atc.c
// Function: atc_read_thread
static void * atc_read_thread(void *param) {
    PROFILE_T *profile = (PROFILE_T *)param;
    const char *cdc_wdm = (const char *)profile->qmichannel;
    int wait_for_request_quit = 0;
    int atc_fd;

    atc_fd = cm_open_dev(cdc_wdm);
    if (atc_fd <= 0) {
        dbg_time("fail to open (%s), errno: %d (%s)", cdc_wdm, errno, strerror(errno));
        goto __quit;
    }

    dbg_time("atc_fd = %d", atc_fd);

    if (at_open(atc_fd, onUnsolicited))
        goto __quit;

    at_set_on_timeout(onTimeout);
    at_set_on_reader_closed(onClose);
    qmidevice_send_event_to_main(RIL_INDICATE_DEVICE_CONNECTED);

    while (atc_fd > 0) {
        struct pollfd pollfds[] = {{atc_fd, POLLIN, 0}, {qmidevice_control_fd[1], POLLIN, 0}};
        int ne, ret, nevents = 2;

        ret = poll(pollfds, nevents, wait_for_request_quit ? 1000 : -1);

        if (ret == 0 && wait_for_request_quit) {
            break;
        }

        if (ret < 0) {
            dbg_time("%s poll=%d, errno: %d (%s)", __func__, ret, errno, strerror(errno));
            break;
        }

        for (ne = 0; ne < nevents; ne++) {
            int fd = pollfds[ne].fd;
            short revents = pollfds[ne].revents;

            if (revents & (POLLERR | POLLHUP | POLLNVAL)) {
                dbg_time("%s poll err/hup/inval", __func__);
                dbg_time("epoll fd = %d, events = 0x%04x", fd, revents);
                if (revents & (POLLERR | POLLHUP | POLLNVAL))
                goto __quit;
            }

            if ((revents & POLLIN) == 0)
                continue;

            if (atc_fd == fd) {
                usleep(10*1000); //let atchannel.c read at response.
            }
            else if (fd == qmidevice_control_fd[1]) {
                int triger_event;
                if (read(fd, &triger_event, sizeof(triger_event)) == sizeof(triger_event)) {
                    //dbg_time("triger_event = 0x%x", triger_event);
                    switch (triger_event) {
                        case RIL_REQUEST_QUIT:
                            goto __quit;
                        break;
                        case SIG_EVENT_STOP:
                            wait_for_request_quit = 1;
                        break;
                        default:
                        break;
                    }
                }
            }
        }
    }

__quit:
    at_close();
    qmidevice_send_event_to_main(RIL_INDICATE_DEVICE_DISCONNECTED);
    dbg_time("%s exit", __func__);

    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
addq $0x8a, %r14
movq %r14, %rdi
callq 0xe1ec
movl %eax, %ebx
movq 0x119b5(%rip), %r13     # 0x28fb8
movq (%r13), %r15
callq 0xe343
movq %rax, %r12
testl %ebx, %ebx
jle 0x1777f
leaq 0x9a49(%rip), %rsi      # 0x21067
movq %r15, %rdi
movq %r12, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x52c0
leaq 0x12674(%rip), %rax     # 0x29ca8
movq (%rax), %r14
testq %r14, %r14
je 0x17657
callq 0xe343
leaq 0x9a1f(%rip), %rsi      # 0x21067
movq %r14, %rdi
movq %rax, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x52c0
leaq 0x1dcb(%rip), %rsi      # 0x19429
movl %ebx, %edi
callq 0x19760
testl %eax, %eax
jne 0x1789c
leaq 0x1e25(%rip), %rdi      # 0x19499
callq 0x1a2bc
leaq 0x1e76(%rip), %rdi      # 0x194f6
callq 0x1a2c4
movl $0x1002, %edi           # imm = 0x1002
callq 0x7244
xorl %r12d, %r12d
pushq $0x1
popq %r15
leaq 0x11d3b(%rip), %r13     # 0x293d8
leaq 0x10(%rsp), %rdi
pushq $0x2
popq %rsi
movl %ebx, 0x10(%rsp)
movl %r15d, 0x14(%rsp)
movl 0x4(%r13), %eax
movl %eax, 0x18(%rsp)
movl %r15d, 0x1c(%rsp)
xorl %edx, %edx
testl %r12d, %r12d
setne %r14b
sete %dl
negl %edx
orl $0x3e8, %edx            # imm = 0x3E8
callq 0x53d0
movl %eax, %ebp
testl %eax, %eax
sete %al
testb %al, %r14b
jne 0x17895
testl %ebp, %ebp
js 0x17911
movq %r12, 0x8(%rsp)
xorl %r12d, %r12d
cmpq $0x2, %r12
je 0x1776d
movl 0x10(%rsp,%r12,8), %ebp
movswl 0x16(%rsp,%r12,8), %r14d
testb $0x38, %r14b
jne 0x177f3
testb $0x1, %r14b
je 0x17768
cmpl %ebp, %ebx
jne 0x17725
movl $0x2710, %edi           # imm = 0x2710
callq 0x55a0
jmp 0x17768
cmpl 0x4(%r13), %ebp
jne 0x17768
movl %ebp, %edi
leaq 0x24(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x5250
cmpq $0x4, %rax
jne 0x1775e
movl 0x24(%rsp), %ecx
movl %r15d, %eax
cmpl $0x2, %ecx
je 0x17763
movq 0x8(%rsp), %rax
cmpl $0x1000, %ecx           # imm = 0x1000
jne 0x17763
jmp 0x17895
movq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
incq %r12
jmp 0x176f4
leaq 0x10(%rsp), %rdi
pushq $0x2
popq %rsi
movq 0x8(%rsp), %r12
jmp 0x176a5
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5540
leaq 0x8c39(%rip), %rsi      # 0x203d0
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
leaq 0x124f4(%rip), %rax     # 0x29ca8
movq (%rax), %r15
testq %r15, %r15
je 0x1789c
callq 0xe343
movq %rax, %r12
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5540
leaq 0x8bf8(%rip), %rsi      # 0x203d0
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
jmp 0x1789c
movq 0x117be(%rip), %r15     # 0x28fb8
movq (%r15), %rbx
callq 0xe343
leaq 0x498f(%rip), %rsi      # 0x1c198
leaq 0x9869(%rip), %rcx      # 0x21079
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x12484(%rip), %r12     # 0x29ca8
movq (%r12), %rbx
testq %rbx, %rbx
je 0x1784d
callq 0xe343
leaq 0x495f(%rip), %rsi      # 0x1c198
leaq 0x9839(%rip), %rcx      # 0x21079
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
movq (%r15), %rbx
callq 0xe343
leaq 0x4b3a(%rip), %rsi      # 0x1c396
movq %rbx, %rdi
movq %rax, %rdx
movl %ebp, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x52c0
movq (%r12), %rbx
testq %rbx, %rbx
je 0x17895
callq 0xe343
leaq 0x4b13(%rip), %rsi      # 0x1c396
movq %rbx, %rdi
movq %rax, %rdx
movl %ebp, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x52c0
movq 0x1171c(%rip), %r13     # 0x28fb8
xorl %eax, %eax
callq 0x19c91
movl $0x1003, %edi           # imm = 0x1003
callq 0x7244
movq (%r13), %rbx
callq 0xe343
leaq 0x496b(%rip), %rsi      # 0x1c228
leaq 0x97b5(%rip), %rcx      # 0x21079
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x123d0(%rip), %rax     # 0x29ca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x17900
callq 0xe343
leaq 0x493c(%rip), %rsi      # 0x1c228
leaq 0x9786(%rip), %rcx      # 0x21079
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x116a0(%rip), %rax     # 0x28fb8
movq (%rax), %r14
callq 0xe343
movq %rax, %r15
callq 0x5070
movq %rax, %rbx
movl (%rax), %r12d
movl %r12d, %edi
callq 0x5540
movq %rax, (%rsp)
leaq 0x4836(%rip), %rsi      # 0x1c177
leaq 0x9731(%rip), %rcx      # 0x21079
movq %r14, %rdi
movq %r15, %rdx
movl %ebp, %r8d
movl %r12d, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x12346(%rip), %rax     # 0x29ca8
movq (%rax), %r14
testq %r14, %r14
je 0x17895
callq 0xe343
movq %rax, %r15
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5540
movq %rax, (%rsp)
leaq 0x47ed(%rip), %rsi      # 0x1c177
leaq 0x96e8(%rip), %rcx      # 0x21079
movq %r14, %rdi
movq %r15, %rdx
movl %ebp, %r8d
movl %ebx, %r9d
xorl %eax, %eax
callq 0x52c0
jmp 0x17895