// File: /workspace/github_c_makefile_star2_O0/Carton32[P]quectel-CM/mbim-cm.c
// Function: mbim_read_thread
static void * mbim_read_thread(void *param) {
    PROFILE_T *profile = (PROFILE_T *)param;
    const char *cdc_wdm = (const char *)profile->qmichannel;
    int wait_for_request_quit = 0;

    mbim_verbose = debug_qmi;
    s_tid_reader = pthread_self();

    if (profile->qmap_mode > 1 && profile->qmapnet_adapter[0]) {
        if (!profile->proxy[0])
             sprintf(profile->proxy, "%s", QUECTEL_MBIM_PROXY);
        mbim_sessionID = profile->pdp;
    }

    if (profile->proxy[0]) {
        mbim_fd = cm_open_proxy(profile->proxy);
    } 
    else {
        mbim_fd = cm_open_dev(cdc_wdm);
    }

    if (mbim_fd <= 0) {
        mbim_debug("fail to open (%s), errno: %d (%s)", cdc_wdm, errno, strerror(errno));
        goto __quit;
    }

    dbg_time("cdc_wdm_fd = %d", mbim_fd);

    qmidevice_send_event_to_main(RIL_INDICATE_DEVICE_CONNECTED);

    while (mbim_fd > 0) {
        struct pollfd pollfds[] = {{mbim_fd, POLLIN, 0}, {qmidevice_control_fd[1], POLLIN, 0}, {qmi_over_mbim_sk[1], POLLIN, 0}};
        int ne, ret, nevents = 2;

        if (pollfds[nevents].fd != -1)
            nevents++;

        ret = poll(pollfds, nevents, wait_for_request_quit ? 1000 : -1);

        if (ret == 0 && wait_for_request_quit) {
            break;
        }

        if (ret < 0) {
            mbim_debug("%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)) {
                mbim_debug("%s poll err/hup/inval", __func__);
                mbim_debug("epoll fd = %d, events = 0x%04x", fd, revents);
                if (revents & (POLLERR | POLLHUP | POLLNVAL))
                goto __quit;
            }

            if ((revents & POLLIN) == 0)
                continue;

            if (mbim_fd == fd) {
                ssize_t nreads;
                MBIM_MESSAGE_HEADER *pResponse = (MBIM_MESSAGE_HEADER *) cm_recv_buf;

                if (profile->proxy[0])
                    nreads = mbim_proxy_read(fd, pResponse, sizeof(cm_recv_buf));
                else
                    nreads = read(fd, pResponse, sizeof(cm_recv_buf));

                if (nreads <= 0) {
                    mbim_debug("%s read=%d errno: %d (%s)",  __func__, (int)nreads, errno, strerror(errno));
                    break;
                }

                mbim_recv_command(pResponse, nreads);
            }
            else if (fd == qmidevice_control_fd[1]) {
                int triger_event;
                if (read(fd, &triger_event, sizeof(triger_event)) == sizeof(triger_event)) {
                    //mbim_debug("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;
                    }
                }
            }
            else if (fd == qmi_over_mbim_sk[1]) {
                    ssize_t nreads = read(fd, cm_recv_buf, sizeof(cm_recv_buf));
                    if (nreads > 0)
                        QmiThreadRecvQMI((PQCQMIMSG)cm_recv_buf);
            }
        }
    }

__quit:
    if (mbim_fd != -1) { close(mbim_fd); mbim_fd = -1; }
    mbim_recv_command(NULL, 0);
    qmidevice_send_event_to_main(RIL_INDICATE_DEVICE_DISCONNECTED);
    mbim_debug("%s exit", __func__);
    s_tid_reader = 0;

    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
addq $0x8a, %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
leaq 0x1f359(%rip), %rax     # 0x3758c
movl (%rax), %eax
movl %eax, 0x20cb5(%rip)     # 0x38ef0
callq 0x5410
movq %rax, 0x20cc1(%rip)     # 0x38f08
movq -0x10(%rbp), %rax
cmpl $0x1, 0x10a(%rax)
jle 0x18298
movq -0x10(%rbp), %rax
movsbl 0xca(%rax), %eax
cmpl $0x0, %eax
je 0x18298
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
jne 0x1828b
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
leaq 0x17ddd(%rip), %rsi     # 0x3005a
leaq 0x133cb(%rip), %rdx     # 0x2b64f
movb $0x0, %al
callq 0x54f0
movq -0x10(%rbp), %rax
movl 0x48(%rax), %eax
movl %eax, 0x20e4c(%rip)     # 0x390e4
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
je 0x182b7
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
callq 0x139f0
movl %eax, 0x1f17b(%rip)     # 0x37430
jmp 0x182c6
movq -0x18(%rbp), %rdi
callq 0x13770
movl %eax, 0x1f16a(%rip)     # 0x37430
cmpl $0x0, 0x1f163(%rip)     # 0x37430
jg 0x183bf
jmp 0x182d5
movq 0x1ed04(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x98(%rbp)
callq 0x13930
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x88(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0x7c(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rdx
movq -0x88(%rbp), %rcx
movl -0x7c(%rbp), %r8d
movq %rax, %r9
leaq 0x172e6(%rip), %rsi     # 0x2f61c
movb $0x0, %al
callq 0x52c0
leaq 0x1fb24(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x183b8
leaq 0x1fb17(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
callq 0x13930
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xa8(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movl -0x9c(%rbp), %r8d
movq %rax, %r9
leaq 0x1726b(%rip), %rsi     # 0x2f61c
movb $0x0, %al
callq 0x52c0
jmp 0x183ba
jmp 0x18958
jmp 0x183c1
movq 0x1ec18(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
callq 0x13930
movq -0xc0(%rbp), %rdi
movq %rax, %rdx
movl 0x1f049(%rip), %ecx     # 0x37430
leaq 0x12daf(%rip), %rsi     # 0x2b19d
movb $0x0, %al
callq 0x52c0
leaq 0x1fa6c(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x18436
leaq 0x1fa5f(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
callq 0x13930
movq -0xc8(%rbp), %rdi
movq %rax, %rdx
movl 0x1f008(%rip), %ecx     # 0x37430
leaq 0x12d6e(%rip), %rsi     # 0x2b19d
movb $0x0, %al
callq 0x52c0
jmp 0x18438
movl $0x1002, %edi           # imm = 0x1002
callq 0x8240
cmpl $0x0, 0x1efe7(%rip)     # 0x37430
jle 0x18956
movl 0x1efdb(%rip), %eax     # 0x37430
movl %eax, -0x40(%rbp)
movw $0x1, -0x3c(%rbp)
movw $0x0, -0x3a(%rbp)
leaq 0x1f129(%rip), %rax     # 0x37594
movl 0x4(%rax), %eax
movl %eax, -0x38(%rbp)
movw $0x1, -0x34(%rbp)
movw $0x0, -0x32(%rbp)
movl 0x1efa5(%rip), %eax     # 0x37428
movl %eax, -0x30(%rbp)
movw $0x1, -0x2c(%rbp)
movw $0x0, -0x2a(%rbp)
movl $0x2, -0x4c(%rbp)
movslq -0x4c(%rbp), %rax
cmpl $-0x1, -0x40(%rbp,%rax,8)
je 0x184ad
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
leaq -0x40(%rbp), %rdi
movslq -0x4c(%rbp), %rsi
movl -0x1c(%rbp), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x3e8, %eax            # imm = 0x3E8
cmpl $0x0, %ecx
cmovnel %eax, %edx
callq 0x53d0
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jne 0x184e1
cmpl $0x0, -0x1c(%rbp)
je 0x184e1
jmp 0x18956
cmpl $0x0, -0x48(%rbp)
jge 0x185e9
jmp 0x184ed
movq 0x1eaec(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0xe0(%rbp)
callq 0x13930
movq %rax, -0xd8(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0xd0(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0xcc(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0xe0(%rbp), %rdi
movq -0xd8(%rbp), %rdx
movl -0xd0(%rbp), %r8d
movl -0xcc(%rbp), %r9d
leaq 0x12c64(%rip), %rsi     # 0x2b1b3
leaq 0x170ee(%rip), %rcx     # 0x2f644
movq %rax, (%rsp)
movb $0x0, %al
callq 0x52c0
leaq 0x1f900(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x185e2
leaq 0x1f8f3(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
callq 0x13930
movq %rax, -0xf0(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0xe8(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0xe4(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rdx
movl -0xe8(%rbp), %r8d
movl -0xe4(%rbp), %r9d
leaq 0x12be3(%rip), %rsi     # 0x2b1b3
leaq 0x1706d(%rip), %rcx     # 0x2f644
movq %rax, (%rsp)
movb $0x0, %al
callq 0x52c0
jmp 0x185e4
jmp 0x18956
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x18951
movslq -0x44(%rbp), %rax
movl -0x40(%rbp,%rax,8), %eax
movl %eax, -0x50(%rbp)
movslq -0x44(%rbp), %rax
movw -0x3a(%rbp,%rax,8), %ax
movw %ax, -0x52(%rbp)
movswl -0x52(%rbp), %eax
andl $0x38, %eax
cmpl $0x0, %eax
je 0x1872f
jmp 0x18626
movq 0x1e9b3(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x100(%rbp)
callq 0x13930
movq -0x100(%rbp), %rdi
movq %rax, %rdx
leaq 0x12b87(%rip), %rsi     # 0x2b1d4
leaq 0x16ff0(%rip), %rcx     # 0x2f644
movb $0x0, %al
callq 0x52c0
leaq 0x1f806(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x1869d
leaq 0x1f7f9(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x108(%rbp)
callq 0x13930
movq -0x108(%rbp), %rdi
movq %rax, %rdx
leaq 0x12b45(%rip), %rsi     # 0x2b1d4
leaq 0x16fae(%rip), %rcx     # 0x2f644
movb $0x0, %al
callq 0x52c0
jmp 0x1869f
jmp 0x186a1
movq 0x1e938(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x110(%rbp)
callq 0x13930
movq -0x110(%rbp), %rdi
movq %rax, %rdx
movl -0x50(%rbp), %ecx
movswl -0x52(%rbp), %r8d
leaq 0x12d3e(%rip), %rsi     # 0x2b40e
movb $0x0, %al
callq 0x52c0
leaq 0x1f78a(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x1871a
leaq 0x1f77d(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x118(%rbp)
callq 0x13930
movq -0x118(%rbp), %rdi
movq %rax, %rdx
movl -0x50(%rbp), %ecx
movswl -0x52(%rbp), %r8d
leaq 0x12cfb(%rip), %rsi     # 0x2b40e
movb $0x0, %al
callq 0x52c0
jmp 0x1871c
movswl -0x52(%rbp), %eax
andl $0x38, %eax
cmpl $0x0, %eax
je 0x1872d
jmp 0x18958
jmp 0x1872f
movswl -0x52(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x18740
jmp 0x18943
movl 0x1ecea(%rip), %eax     # 0x37430
cmpl -0x50(%rbp), %eax
jne 0x188af
leaq 0x1f79a(%rip), %rax     # 0x37ef0
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
je 0x1877b
movl -0x50(%rbp), %edi
movq -0x68(%rbp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x1ce40
movq %rax, -0x60(%rbp)
jmp 0x18790
movl -0x50(%rbp), %edi
movq -0x68(%rbp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x5250
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jg 0x1889b
jmp 0x1879d
movq 0x1e83c(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x130(%rbp)
callq 0x13930
movq %rax, -0x128(%rbp)
movq -0x60(%rbp), %rax
movl %eax, -0x120(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0x11c(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0x130(%rbp), %rdi
movq -0x128(%rbp), %rdx
movl -0x120(%rbp), %r8d
movl -0x11c(%rbp), %r9d
leaq 0x12a14(%rip), %rsi     # 0x2b214
leaq 0x16e3d(%rip), %rcx     # 0x2f644
movq %rax, (%rsp)
movb $0x0, %al
callq 0x52c0
leaq 0x1f64f(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x18894
leaq 0x1f642(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x148(%rbp)
callq 0x13930
movq %rax, -0x140(%rbp)
movq -0x60(%rbp), %rax
movl %eax, -0x138(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0x134(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0x148(%rbp), %rdi
movq -0x140(%rbp), %rdx
movl -0x138(%rbp), %r8d
movl -0x134(%rbp), %r9d
leaq 0x12992(%rip), %rsi     # 0x2b214
leaq 0x16dbb(%rip), %rcx     # 0x2f644
movq %rax, (%rsp)
movb $0x0, %al
callq 0x52c0
jmp 0x18896
jmp 0x18951
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
callq 0x1ced0
jmp 0x18941
movl -0x50(%rbp), %eax
leaq 0x1ecdb(%rip), %rcx     # 0x37594
cmpl 0x4(%rcx), %eax
jne 0x18905
movl -0x50(%rbp), %edi
leaq -0x6c(%rbp), %rsi
movl $0x4, %edx
callq 0x5250
cmpq $0x4, %rax
jne 0x18903
movl -0x6c(%rbp), %eax
movl %eax, -0x14c(%rbp)
subl $0x2, %eax
je 0x188f6
jmp 0x188e5
movl -0x14c(%rbp), %eax
subl $0x1000, %eax           # imm = 0x1000
jne 0x188ff
jmp 0x188f4
jmp 0x18958
movl $0x1, -0x1c(%rbp)
jmp 0x18901
jmp 0x18901
jmp 0x18903
jmp 0x1893f
movl -0x50(%rbp), %eax
cmpl 0x1eb1a(%rip), %eax     # 0x37428
jne 0x1893d
movl -0x50(%rbp), %edi
leaq 0x1f5d6(%rip), %rsi     # 0x37ef0
movl $0x1000, %edx           # imm = 0x1000
callq 0x5250
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jle 0x1893b
leaq 0x1f5ba(%rip), %rdi     # 0x37ef0
callq 0xc800
jmp 0x1893d
jmp 0x1893f
jmp 0x18941
jmp 0x18943
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x185f0
jmp 0x18442
jmp 0x18958
cmpl $-0x1, 0x1ead1(%rip)    # 0x37430
je 0x18976
movl 0x1eac9(%rip), %edi     # 0x37430
callq 0x5230
movl $0xffffffff, 0x1eaba(%rip) # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %edi
xorl %esi, %esi
callq 0x1ced0
movl $0x1003, %edi           # imm = 0x1003
callq 0x8240
movq 0x1e64e(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x158(%rbp)
callq 0x13930
movq -0x158(%rbp), %rdi
movq %rax, %rdx
leaq 0x128b2(%rip), %rsi     # 0x2b264
leaq 0x16c8b(%rip), %rcx     # 0x2f644
movb $0x0, %al
callq 0x52c0
leaq 0x1f4a1(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x18a02
leaq 0x1f494(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x160(%rbp)
callq 0x13930
movq -0x160(%rbp), %rdi
movq %rax, %rdx
leaq 0x12870(%rip), %rsi     # 0x2b264
leaq 0x16c49(%rip), %rcx     # 0x2f644
movb $0x0, %al
callq 0x52c0
jmp 0x18a04
movq $0x0, 0x204f9(%rip) # 0x38f08
xorl %eax, %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopw (%rax,%rax)