// File: /workspace/github_c_makefile_star2_O3/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 0xe8d8
movl %eax, %ebx
movq 0x1188e(%rip), %r13     # 0x29fc0
movq (%r13), %r15
callq 0xea3b
movq %rax, %r12
testl %ebx, %ebx
jle 0x188de
leaq 0xa8fa(%rip), %rsi      # 0x23047
movq %r15, %rdi
movq %r12, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x52c0
leaq 0x12545(%rip), %rax     # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0x18786
callq 0xea3b
leaq 0xa8d0(%rip), %rsi      # 0x23047
movq %r14, %rdi
movq %rax, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x52c0
leaq 0x1ed4(%rip), %rsi      # 0x1a661
movl %ebx, %edi
callq 0x1a9a3
testl %eax, %eax
jne 0x189f9
leaq 0x18(%rsp), %r14
leaq 0x1f29(%rip), %rdi      # 0x1a6d1
callq 0x1b4d4
leaq 0x1f7a(%rip), %rdi      # 0x1a72e
callq 0x1b4dc
movl $0x1002, %edi           # imm = 0x1002
callq 0x7354
movl %ebx, -0x8(%r14)
movl $0x1, %ecx
movl %ecx, -0x4(%r14)
leaq 0x11c01(%rip), %rax     # 0x2a3d8
movl 0x4(%rax), %eax
movl %eax, (%r14)
movl %ecx, 0x4(%r14)
leaq 0x10(%rsp), %rdi
movl $0x2, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x53c0
movl %eax, %ebp
xorl %r13d, %r13d
testl %ebp, %ebp
js 0x18a6e
movb $0x1, %r15b
leaq 0x10(%rsp), %rax
movl (%rax), %ebp
movswl 0x6(%rax), %r12d
testb $0x38, %r12b
jne 0x18952
testb $0x1, %r12b
je 0x18878
cmpl %ebp, %ebx
jne 0x18831
movl $0x2710, %edi           # imm = 0x2710
callq 0x5590
jmp 0x18878
leaq 0x11ba0(%rip), %rax     # 0x2a3d8
cmpl 0x4(%rax), %ebp
jne 0x18878
movl $0x4, %edx
movl %ebp, %edi
leaq 0x24(%rsp), %rsi
callq 0x5250
cmpq $0x4, %rax
jne 0x18872
movl $0x1, %eax
movl 0x24(%rsp), %ecx
cmpl $0x2, %ecx
je 0x18875
movl %r13d, %eax
cmpl $0x1000, %ecx           # imm = 0x1000
jne 0x18875
jmp 0x189f2
movl %r13d, %eax
movl %eax, %r13d
movq %r14, %rax
testb $0x1, %r15b
movl $0x0, %r15d
jne 0x1880a
movl %ebx, 0x10(%rsp)
movl $0x1, %ecx
movl %ecx, 0x14(%rsp)
leaq 0x11b3d(%rip), %rax     # 0x2a3d8
movl 0x4(%rax), %eax
movl %eax, 0x18(%rsp)
movl %ecx, 0x1c(%rsp)
xorl %edx, %edx
testl %r13d, %r13d
setne %r15b
sete %dl
negl %edx
orl $0x3e8, %edx            # imm = 0x3E8
movl $0x2, %esi
leaq 0x10(%rsp), %rdi
callq 0x53c0
movl %eax, %ebp
testl %eax, %eax
sete %al
testb %al, %r15b
je 0x187fa
jmp 0x189f2
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x9ac5(%rip), %rsi      # 0x223bb
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
leaq 0x12395(%rip), %rax     # 0x2aca8
movq (%rax), %r15
testq %r15, %r15
je 0x189f9
callq 0xea3b
movq %rax, %r12
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
leaq 0x9a84(%rip), %rsi      # 0x223bb
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
jmp 0x189f9
movq 0x11667(%rip), %r14     # 0x29fc0
movq (%r14), %rbx
callq 0xea3b
leaq 0x5830(%rip), %rsi      # 0x1e198
leaq 0xa6ea(%rip), %rcx      # 0x23059
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x12325(%rip), %r15     # 0x2aca8
movq (%r15), %rbx
testq %rbx, %rbx
je 0x189ab
callq 0xea3b
leaq 0x5801(%rip), %rsi      # 0x1e198
leaq 0xa6bb(%rip), %rcx      # 0x23059
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
movq (%r14), %rbx
callq 0xea3b
leaq 0x59dc(%rip), %rsi      # 0x1e396
movq %rbx, %rdi
movq %rax, %rdx
movl %ebp, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x52c0
movq (%r15), %rbx
testq %rbx, %rbx
je 0x189f2
callq 0xea3b
leaq 0x59b6(%rip), %rsi      # 0x1e396
movq %rbx, %rdi
movq %rax, %rdx
movl %ebp, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x52c0
movq 0x115c7(%rip), %r13     # 0x29fc0
xorl %eax, %eax
callq 0x1ae25
movl $0x1003, %edi           # imm = 0x1003
callq 0x7354
movq (%r13), %rbx
callq 0xea3b
leaq 0x580e(%rip), %rsi      # 0x1e228
leaq 0xa638(%rip), %rcx      # 0x23059
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x12273(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x18a5d
callq 0xea3b
leaq 0x57df(%rip), %rsi      # 0x1e228
leaq 0xa609(%rip), %rcx      # 0x23059
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 0x1154b(%rip), %rax     # 0x29fc0
movq (%rax), %r14
callq 0xea3b
movq %rax, %r15
callq 0x5070
movq %rax, %rbx
movl (%rax), %r12d
movl %r12d, %edi
callq 0x5530
movq %rax, (%rsp)
leaq 0x56d9(%rip), %rsi      # 0x1e177
leaq 0xa5b4(%rip), %rcx      # 0x23059
movq %r14, %rdi
movq %r15, %rdx
movl %ebp, %r8d
movl %r12d, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x121e9(%rip), %rax     # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0x189f2
callq 0xea3b
movq %rax, %r15
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
movq %rax, (%rsp)
leaq 0x5690(%rip), %rsi      # 0x1e177
leaq 0xa56b(%rip), %rcx      # 0x23059
movq %r14, %rdi
movq %r15, %rdx
movl %ebp, %r8d
movl %ebx, %r9d
xorl %eax, %eax
callq 0x52c0
jmp 0x189f2