// File: /workspace/github_c_makefile_star2_O0/Carton32[P]quectel-CM/quectel-mbim-proxy.c
// Function: proxy_loop
static int proxy_loop(int mbim_dev_fd)
{
    int i;
    int mbim_server_fd = -1;

    while (mbim_dev_fd > 0) {
        struct pollfd pollfds[2+CM_MAX_CLIENT];
        int ne, ret, nevents = 0;

        pollfds[nevents].fd = mbim_dev_fd;
        pollfds[nevents].events = POLLIN;
        pollfds[nevents].revents= 0;
        nevents++;
        
        if (mbim_server_fd > 0) {
            pollfds[nevents].fd = mbim_server_fd;
            pollfds[nevents].events = POLLIN;
            pollfds[nevents].revents= 0;
            nevents++;

            for (i = 0; i < CM_MAX_CLIENT; i++) {
                if (cm_clients[i].client_fd > 0) {
                    pollfds[nevents].fd = cm_clients[i].client_fd;
                    pollfds[nevents].events = POLLIN;
                    pollfds[nevents].revents= 0;
                    nevents++;
                }
            }
        }

        ret = poll(pollfds, nevents, (mbim_server_fd > 0) ? -1 : (10*1000));
        if (ret <= 0) {
            goto error;
        }

        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 fd = %d, revents = %04x\n", __func__, fd, revents);
                if (fd == mbim_dev_fd) {
                    goto error;
                } else if(fd == mbim_server_fd) {
                
                } else {
                    handle_client_disconnect(fd);
                }
                continue;
            }

            if (!(pollfds[ne].revents & POLLIN)) {
                continue;
            }

            if (fd == mbim_server_fd) {
                handle_client_connect(fd);
            }
            else {
                    int len = read(fd, cm_recv_buffer, sizeof(cm_recv_buffer));

                    if (len <= 0) {
                        mbim_debug("%s read fd=%d, len=%d, errno: %d(%s)\n", __func__, fd, len, errno, strerror(errno));
                        if (fd == mbim_dev_fd)
                            goto error;
                        else
                            handle_client_disconnect(fd);

                        return len;
                    }

                    if (fd == mbim_dev_fd) {
                        if (mbim_server_fd == -1) {
                            MBIM_OPEN_DONE_T *pOpenDone = (MBIM_OPEN_DONE_T *)cm_recv_buffer;

                            if (le32toh(pOpenDone->MessageHeader.MessageType) == MBIM_OPEN_DONE) {
                                mbim_debug("receive MBIM_OPEN_DONE, status=%d\n", htole32(pOpenDone->Status));
                                if (htole32(pOpenDone->Status))
                                    goto error;
                                mbim_server_fd = proxy_make_server(QUECTEL_MBIM_PROXY);
                                mbim_debug("mbim_server_fd=%d\n", mbim_server_fd);
                            }
                        }
                        else {
                            handle_device_response(cm_recv_buffer, len);
                        }
                    }
                    else {
                        handle_client_request(mbim_dev_fd, fd, cm_recv_buffer, len);
                    }
            }
        }
    }

error:
    safe_close(mbim_server_fd);
    for (i = 0; i < CM_MAX_CLIENT; i++) {
        safe_close(cm_clients[i].client_fd);
    }

    mbim_debug("%s exit\n", __func__);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movl %edi, -0x8(%rbp)
movl $0xffffffff, -0x10(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, -0x8(%rbp)
jle 0x1ba5
movl $0x0, -0x12c(%rbp)
movl -0x8(%rbp), %ecx
movslq -0x12c(%rbp), %rax
movl %ecx, -0x120(%rbp,%rax,8)
movslq -0x12c(%rbp), %rax
movw $0x1, -0x11c(%rbp,%rax,8)
movslq -0x12c(%rbp), %rax
movw $0x0, -0x11a(%rbp,%rax,8)
movl -0x12c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12c(%rbp)
cmpl $0x0, -0x10(%rbp)
jle 0x1851
movl -0x10(%rbp), %ecx
movslq -0x12c(%rbp), %rax
movl %ecx, -0x120(%rbp,%rax,8)
movslq -0x12c(%rbp), %rax
movw $0x1, -0x11c(%rbp,%rax,8)
movslq -0x12c(%rbp), %rax
movw $0x0, -0x11a(%rbp,%rax,8)
movl -0x12c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12c(%rbp)
movl $0x0, -0xc(%rbp)
cmpl $0x20, -0xc(%rbp)
jge 0x184f
movslq -0xc(%rbp), %rcx
leaq 0x3991(%rip), %rax      # 0x5180
cmpl $0x0, (%rax,%rcx,8)
jle 0x1842
movslq -0xc(%rbp), %rcx
leaq 0x3980(%rip), %rax      # 0x5180
movl (%rax,%rcx,8), %ecx
movslq -0x12c(%rbp), %rax
movl %ecx, -0x120(%rbp,%rax,8)
movslq -0x12c(%rbp), %rax
movw $0x1, -0x11c(%rbp,%rax,8)
movslq -0x12c(%rbp), %rax
movw $0x0, -0x11a(%rbp,%rax,8)
movl -0x12c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12c(%rbp)
jmp 0x1844
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x17de
jmp 0x1851
leaq -0x120(%rbp), %rdi
movslq -0x12c(%rbp), %rsi
movl -0x10(%rbp), %ecx
movl $0x2710, %edx           # imm = 0x2710
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x0, %ecx
cmovgl %eax, %edx
callq 0x1120
movl %eax, -0x128(%rbp)
cmpl $0x0, -0x128(%rbp)
jg 0x188b
jmp 0x1ba7
movl $0x0, -0x124(%rbp)
movl -0x124(%rbp), %eax
cmpl -0x12c(%rbp), %eax
jge 0x1ba0
movslq -0x124(%rbp), %rax
movl -0x120(%rbp,%rax,8), %eax
movl %eax, -0x130(%rbp)
movslq -0x124(%rbp), %rax
movw -0x11a(%rbp,%rax,8), %ax
movw %ax, -0x132(%rbp)
movswl -0x132(%rbp), %eax
andl $0x38, %eax
cmpl $0x0, %eax
je 0x1955
jmp 0x18e2
movq 0x36e7(%rip), %rax      # 0x4fd0
movq (%rax), %rax
movq %rax, -0x148(%rbp)
callq 0x12a0
movq -0x148(%rbp), %rdi
movq %rax, %rdx
movl -0x130(%rbp), %r8d
movswl -0x132(%rbp), %r9d
leaq 0x186a(%rip), %rsi      # 0x3182
leaq 0x1887(%rip), %rcx      # 0x31a6
movb $0x0, %al
callq 0x10f0
movl -0x130(%rbp), %eax
cmpl -0x8(%rbp), %eax
jne 0x1936
jmp 0x1ba7
movl -0x130(%rbp), %eax
cmpl -0x10(%rbp), %eax
jne 0x1943
jmp 0x194e
movl -0x130(%rbp), %edi
callq 0x1da0
jmp 0x1950
jmp 0x1b8c
movslq -0x124(%rbp), %rax
movswl -0x11a(%rbp,%rax,8), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x1971
jmp 0x1b8c
movl -0x130(%rbp), %eax
cmpl -0x10(%rbp), %eax
jne 0x198c
movl -0x130(%rbp), %edi
callq 0x1e80
jmp 0x1b8a
movl -0x130(%rbp), %edi
leaq 0x38e7(%rip), %rsi      # 0x5280
movl $0x1000, %edx           # imm = 0x1000
callq 0x10e0
movl %eax, -0x138(%rbp)
cmpl $0x0, -0x138(%rbp)
jg 0x1a70
jmp 0x19b8
movq 0x3611(%rip), %rax      # 0x4fd0
movq (%rax), %rax
movq %rax, -0x168(%rbp)
callq 0x12a0
movq %rax, -0x160(%rbp)
movl -0x130(%rbp), %eax
movl %eax, -0x154(%rbp)
movl -0x138(%rbp), %eax
movl %eax, -0x150(%rbp)
callq 0x1040
movl (%rax), %eax
movl %eax, -0x14c(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1170
movq -0x168(%rbp), %rdi
movq -0x160(%rbp), %rdx
movl -0x154(%rbp), %r8d
movl -0x150(%rbp), %r9d
movl -0x14c(%rbp), %r10d
leaq 0x1781(%rip), %rsi      # 0x31b1
leaq 0x176f(%rip), %rcx      # 0x31a6
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x10f0
movl -0x130(%rbp), %eax
cmpl -0x8(%rbp), %eax
jne 0x1a57
jmp 0x1ba7
movl -0x130(%rbp), %edi
callq 0x1da0
movl -0x138(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1c52
movl -0x130(%rbp), %eax
cmpl -0x8(%rbp), %eax
jne 0x1b6d
cmpl $-0x1, -0x10(%rbp)
jne 0x1b59
leaq 0x37f0(%rip), %rax      # 0x5280
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
movl (%rax), %edi
callq 0x1c60
cmpl $0x80000001, %eax       # imm = 0x80000001
jne 0x1b57
jmp 0x1ab2
movq 0x3517(%rip), %rax      # 0x4fd0
movq (%rax), %rax
movq %rax, -0x178(%rbp)
callq 0x12a0
movq %rax, -0x170(%rbp)
movq -0x140(%rbp), %rax
movl 0xc(%rax), %edi
callq 0x1c60
movq -0x178(%rbp), %rdi
movq -0x170(%rbp), %rdx
movl %eax, %ecx
leaq 0x16e5(%rip), %rsi      # 0x31da
movb $0x0, %al
callq 0x10f0
movq -0x140(%rbp), %rax
movl 0xc(%rax), %edi
callq 0x1c60
cmpl $0x0, %eax
je 0x1b15
jmp 0x1ba7
leaq 0x16e4(%rip), %rdi      # 0x3200
callq 0x2040
movl %eax, -0x10(%rbp)
movq 0x34a5(%rip), %rax      # 0x4fd0
movq (%rax), %rax
movq %rax, -0x180(%rbp)
callq 0x12a0
movq -0x180(%rbp), %rdi
movq %rax, %rdx
movl -0x10(%rbp), %ecx
leaq 0x16c5(%rip), %rsi      # 0x3213
movb $0x0, %al
callq 0x10f0
jmp 0x1b57
jmp 0x1b6b
movl -0x138(%rbp), %esi
leaq 0x371a(%rip), %rdi      # 0x5280
callq 0x22f0
jmp 0x1b88
movl -0x8(%rbp), %edi
movl -0x130(%rbp), %esi
movl -0x138(%rbp), %ecx
leaq 0x36fd(%rip), %rdx      # 0x5280
callq 0x24a0
jmp 0x1b8a
jmp 0x1b8c
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x1895
jmp 0x1735
jmp 0x1ba7
jmp 0x1ba9
cmpl $0x0, -0x10(%rbp)
jle 0x1bbe
movl -0x10(%rbp), %edi
callq 0x10d0
movl $0xffffffff, -0x10(%rbp) # imm = 0xFFFFFFFF
jmp 0x1bc0
movl $0x0, -0xc(%rbp)
cmpl $0x20, -0xc(%rbp)
jge 0x1c14
jmp 0x1bcf
movslq -0xc(%rbp), %rcx
leaq 0x35a6(%rip), %rax      # 0x5180
cmpl $0x0, (%rax,%rcx,8)
jle 0x1c05
movslq -0xc(%rbp), %rcx
leaq 0x3595(%rip), %rax      # 0x5180
movl (%rax,%rcx,8), %edi
callq 0x10d0
movslq -0xc(%rbp), %rcx
leaq 0x3582(%rip), %rax      # 0x5180
movl $0xffffffff, (%rax,%rcx,8) # imm = 0xFFFFFFFF
jmp 0x1c07
jmp 0x1c09
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x1bc7
jmp 0x1c16
movq 0x33b3(%rip), %rax      # 0x4fd0
movq (%rax), %rax
movq %rax, -0x188(%rbp)
callq 0x12a0
movq -0x188(%rbp), %rdi
movq %rax, %rdx
leaq 0x15ec(%rip), %rsi      # 0x3229
leaq 0x1562(%rip), %rcx      # 0x31a6
movb $0x0, %al
callq 0x10f0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
nop