// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdi, %r14
addq $0x8a, %r14
movq %r14, %rdi
callq 0xe9cc
movl %eax, %r13d
movq 0x11660(%rip), %rax     # 0x29fc0
movq (%rax), %r15
callq 0xeb2f
movq %rax, %r12
testl %r13d, %r13d
jle 0x18c8c
leaq 0xa6ac(%rip), %rsi      # 0x23027
movq %r15, %rdi
movq %r12, %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x52c0
leaq 0x12316(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
movl %r13d, %r12d
je 0x189b9
callq 0xeb2f
leaq 0xa67e(%rip), %rsi      # 0x23027
movq %rbx, %rdi
movq %rax, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x52c0
leaq 0x1f1e(%rip), %rsi      # 0x1a8de
movl %r12d, %edi
callq 0x1ac27
testl %eax, %eax
jne 0x18cf7
leaq 0x1f77(%rip), %rdi      # 0x1a94e
callq 0x1b8e8
leaq 0x1fc8(%rip), %rdi      # 0x1a9ab
callq 0x1b8f0
movl $0x1002, %edi           # imm = 0x1002
callq 0x7444
xorl %ebp, %ebp
movl %r12d, 0x18(%rsp)
movl %r12d, 0x20(%rsp)
movl $0x1, %eax
movl %eax, 0x24(%rsp)
leaq 0x119ca(%rip), %rcx     # 0x2a3d8
movl 0x4(%rcx), %ecx
movl %ecx, 0x28(%rsp)
movl %eax, 0x2c(%rsp)
xorl %edx, %edx
testl %ebp, %ebp
setne %bl
sete %dl
negl %edx
orl $0x3e8, %edx            # imm = 0x3E8
movl $0x2, %esi
leaq 0x20(%rsp), %rdi
callq 0x53c0
movl %eax, %r14d
testl %eax, %eax
sete %al
movl $0x8, %r15d
testb %al, %bl
je 0x18a56
movl $0x1, %ebp
jmp 0x18c72
movl %ebp, 0x14(%rsp)
testl %r14d, %r14d
leaq 0x5734(%rip), %rbp      # 0x1e198
js 0x18bd7
xorl %r12d, %r12d
movl 0x20(%rsp,%r12,8), %r13d
movswl 0x26(%rsp,%r12,8), %r14d
testb $0x38, %r14b
je 0x18b37
movq 0x11537(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xeb2f
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
leaq 0xa598(%rip), %r15      # 0x23039
movq %r15, %rcx
xorl %eax, %eax
callq 0x52c0
leaq 0x121f6(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x18ad2
callq 0xeb2f
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x52c0
movq 0x114e7(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xeb2f
movq %rbx, %rdi
leaq 0x58ab(%rip), %rbp      # 0x1e396
movq %rbp, %rsi
movq %rax, %rdx
movl %r13d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x52c0
leaq 0x121a3(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
movl $0x4, %r15d
testq %rbx, %rbx
je 0x18b2e
callq 0xeb2f
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movl %r13d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x52c0
leaq 0x5663(%rip), %rbp      # 0x1e198
jmp 0x18bb2
movl $0xd, %r15d
testb $0x1, %r14b
je 0x18bb2
cmpl %r13d, 0x18(%rsp)
jne 0x18b60
movl $0x2710, %edi           # imm = 0x2710
callq 0x5590
leaq 0x563d(%rip), %rbp      # 0x1e198
xorl %r15d, %r15d
jmp 0x18bb2
xorl %r15d, %r15d
leaq 0x1186e(%rip), %rax     # 0x2a3d8
cmpl 0x4(%rax), %r13d
jne 0x18bb2
movl $0x4, %edx
movl %r13d, %edi
leaq 0x1c(%rsp), %rsi
callq 0x5250
xorl %r15d, %r15d
cmpq $0x4, %rax
jne 0x18b2e
movl 0x1c(%rsp), %eax
cmpl $0x1000, %eax           # imm = 0x1000
leaq 0x55fd(%rip), %rbp      # 0x1e198
je 0x18bac
cmpl $0x2, %eax
jne 0x18bb2
movl $0x1, 0x14(%rsp)
jmp 0x18bb2
movl $0x4, %r15d
cmpl $0xd, %r15d
je 0x18bbd
testl %r15d, %r15d
jne 0x18bcd
incq %r12
cmpq $0x1, %r12
je 0x18a6d
xorl %r15d, %r15d
movl 0x18(%rsp), %r12d
jmp 0x18c6e
movq 0x113e2(%rip), %rax     # 0x29fc0
movq (%rax), %r13
callq 0xeb2f
movq %rax, %rbp
callq 0x5070
movq %rax, 0x40(%rsp)
movl (%rax), %ebx
movl %ebx, %edi
callq 0x5530
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0x556d(%rip), %r13      # 0x1e177
movq %r13, %rsi
movq %rbp, %rdx
leaq 0xa422(%rip), %rcx      # 0x23039
movl %r14d, %r8d
movl %ebx, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x1207d(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x18c6e
callq 0xeb2f
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rax
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r13, %rsi
movq 0x38(%rsp), %rdx
leaq 0xa3d8(%rip), %rcx      # 0x23039
movl %r14d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52c0
movl 0x14(%rsp), %ebp
testl %r15d, %r15d
je 0x189f9
cmpl $0x8, %r15d
je 0x18cf7
cmpl $0x4, %r15d
je 0x18cf7
jmp 0x18d61
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x96ef(%rip), %rsi      # 0x22393
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
leaq 0x11fe7(%rip), %rax     # 0x2aca8
movq (%rax), %r15
testq %r15, %r15
je 0x18cf7
callq 0xeb2f
movq %rax, %r12
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
leaq 0x96b2(%rip), %rsi      # 0x22393
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
xorl %eax, %eax
callq 0x1b24a
movl $0x1003, %edi           # imm = 0x1003
callq 0x7444
movq 0x112b1(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xeb2f
leaq 0x550a(%rip), %rsi      # 0x1e228
leaq 0xa314(%rip), %rcx      # 0x23039
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x11f6f(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x18d61
callq 0xeb2f
leaq 0x54db(%rip), %rsi      # 0x1e228
leaq 0xa2e5(%rip), %rcx      # 0x23039
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq