// File: /workspace/github_c_makefile_star2_O1/Carton32[P]quectel-CM/quectel-qmi-proxy.c
// Function: main
int main(int argc, char *argv[]) {
    int opt;
    char cdc_wdm[32+1] = "/dev/cdc-wdm0";
    int retry_times = 0;
    char servername[64] = {0};

    optind = 1;

    signal(SIGINT, sig_action);

    while ( -1 != (opt = getopt(argc, argv, "d:i:vh"))) {
        switch (opt) {
            case 'd':
                strcpy(cdc_wdm, optarg);
                break;
            case 'v':
                verbose_debug = 1;
                break;
            default:
                usage();
                return 0;
        }
    }

    if (access(cdc_wdm, R_OK | W_OK)) {
        dprintf("Fail to access %s, errno: %d (%s). break\n", cdc_wdm, errno, strerror(errno));
        return -1;
    }

    sprintf(servername, "quectel-qmi-proxy%c", cdc_wdm[strlen(cdc_wdm)-1]);
    dprintf("Will use cdc-wdm='%s', proxy='%s'\n", cdc_wdm, servername);

    while (qmi_proxy_quit == 0) {
        if (access(cdc_wdm, R_OK | W_OK)) {
            dprintf("Fail to access %s, errno: %d (%s). continue\n", cdc_wdm, errno, strerror(errno));
            // wait device
            sleep(3);
            continue;
        }

        cdc_wdm_fd = open(cdc_wdm, O_RDWR | O_NONBLOCK | O_NOCTTY);
        if (cdc_wdm_fd == -1) {
            dprintf("Failed to open %s, errno: %d (%s). break\n", cdc_wdm, errno, strerror(errno));
            return -1;
        }
        cfmakenoblock(cdc_wdm_fd);
        
        /* no qmi_proxy_loop lives, create one */
        pthread_create(&thread_id, NULL, qmi_proxy_loop, NULL);
        /* try to redo init if failed, init function must be successfully */
        while (qmi_proxy_init() != 0) {
            if (retry_times < 5) {
                dprintf("fail to init proxy, try again in 2 seconds.\n");
                sleep(2);
                retry_times++;
            } else {
                dprintf("has failed too much times, restart the modem and have a try...\n");
                break;
            }
            /* break loop if modem is detached */
            if (access(cdc_wdm, F_OK|R_OK|W_OK))
                break;
        }
        retry_times = 0;
        qmi_start_server(servername);
        if (qmi_proxy_server_fd == -1)
            pthread_cancel(thread_id); 
        pthread_join(thread_id, NULL);

        /* close local server at last */
        qmi_close_server(servername);
        close(cdc_wdm_fd);
        /* DO RESTART IN 20s IF MODEM RESET ITSELF */
        if (modem_reset_flag) {
            unsigned int time_to_wait = 20;
            while (time_to_wait) {
                time_to_wait = sleep(time_to_wait);
            }
            modem_reset_flag = 0;
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movaps 0x2171(%rip), %xmm0     # 0x3570
leaq 0x10(%rsp), %r14
movaps %xmm0, 0x10(%r14)
movaps 0x2150(%rip), %xmm0     # 0x3560
movaps %xmm0, (%r14)
movb $0x0, 0x20(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq 0x3ba1(%rip), %rax      # 0x4fd8
movl $0x1, (%rax)
leaq 0x660(%rip), %rsi       # 0x1aa4
movl $0x2, %edi
callq 0x1110
leaq 0x1bcf(%rip), %r15      # 0x3024
movq 0x3b84(%rip), %r12      # 0x4fe0
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x11c0
cmpl $0x64, %eax
je 0x147c
cmpl $0x76, %eax
jne 0x148a
movb $0x1, 0x3d56(%rip)      # 0x51d0
jmp 0x145c
movq (%r12), %rsi
movq %r14, %rdi
callq 0x1070
jmp 0x145c
cmpl $-0x1, %eax
jne 0x14f0
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movl $0x6, %esi
callq 0x11b0
testl %eax, %eax
je 0x152d
movq 0x3b20(%rip), %rax      # 0x4fd0
movq (%rax), %rbx
callq 0x133c
callq 0x1060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
leaq 0x1b5e(%rip), %rsi      # 0x302b
leaq 0x3c7c(%rip), %rdx      # 0x5150
leaq 0x10(%rsp), %rcx
movq %rbx, %rdi
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x1120
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x1519
movq 0x3ad9(%rip), %rax      # 0x4fd0
movq (%rax), %r14
callq 0x133c
leaq 0x1c5c(%rip), %rsi      # 0x3162
leaq 0x3c43(%rip), %rdx      # 0x5150
xorl %ebx, %ebx
movq %r14, %rdi
xorl %eax, %eax
callq 0x1120
movl %ebx, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0x10b0
movsbl -0x1(%rax,%r13), %edx
leaq 0x1b16(%rip), %rsi      # 0x3058
xorl %ebx, %ebx
leaq 0x40(%rsp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x11e0
movq 0x3a76(%rip), %r14      # 0x4fd0
movq (%r14), %r12
callq 0x133c
leaq 0x1b03(%rip), %rsi      # 0x306c
leaq 0x3be0(%rip), %rdx      # 0x5150
movq %r12, %rdi
movq %r13, %rcx
movq %r15, %r8
xorl %eax, %eax
callq 0x1120
cmpb $0x0, 0x3c4d(%rip)      # 0x51d4
jne 0x1519
leaq 0x10(%rsp), %r15
leaq 0x3bbb(%rip), %rbp      # 0x5150
leaq 0x1eb2(%rip), %rbx      # 0x344e
leaq 0x80(%rsp), %r12
movq %r15, %rdi
movl $0x6, %esi
callq 0x11b0
testl %eax, %eax
je 0x15fe
movq (%r14), %r15
callq 0x133c
callq 0x1060
movl (%rax), %r13d
movl %r13d, %edi
callq 0x1200
movq %r15, %rdi
leaq 0x10(%rsp), %r15
leaq 0x1ab6(%rip), %rsi      # 0x3092
movq %rbp, %rdx
movq %r15, %rcx
movl %r13d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x1120
movl $0x3, %edi
callq 0x1220
jmp 0x1a6e
movq %r15, %rdi
movl $0x902, %esi            # imm = 0x902
xorl %eax, %eax
callq 0x11a0
movl %eax, 0x3b1d(%rip)      # 0x5130
cmpl $-0x1, %eax
je 0x1a82
movl %eax, %r15d
xorl %r13d, %r13d
movl %eax, %edi
movl $0x3, %esi
xorl %eax, %eax
callq 0x1090
orl $0x800, %eax            # imm = 0x800
movl %r15d, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x1090
leaq 0x3b8b(%rip), %rdi      # 0x51d8
xorl %esi, %esi
leaq 0x471(%rip), %rdx       # 0x1ac7
xorl %ecx, %ecx
callq 0x1180
movq (%r14), %r15
callq 0x133c
movq %r15, %rdi
leaq 0x1dd2(%rip), %rsi      # 0x3441
movq %rbp, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1120
movb $0x1, 0x80(%rsp)
movl $0x0, 0x83(%rsp)
movb $0x0, 0x4b7a(%rip)      # 0x6210
movl $0xfffffff6, %r15d      # imm = 0xFFFFFFF6
testl %r15d, %r15d
je 0x16e3
leal 0xb(%r15), %eax
movb %al, 0x87(%rsp)
movl $0x27, 0x88(%rsp)
movw $0xb, 0x81(%rsp)
movq %r12, %rdi
callq 0x252d
testl %eax, %eax
jle 0x16e3
movl $0x1, %edi
callq 0x1220
incl %r15d
cmpb $0x1, 0x4b2f(%rip)      # 0x6210
jne 0x169c
movq (%r14), %r15
callq 0x133c
cmpb $0x0, 0x4b1e(%rip)      # 0x6210
leaq 0x1d76(%rip), %r8       # 0x346f
leaq 0x1d67(%rip), %rax      # 0x3467
cmovneq %rax, %r8
movq %r15, %rdi
leaq 0x1d4f(%rip), %rsi      # 0x345d
movq %rbp, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1120
cmpb $0x0, 0x4aee(%rip)      # 0x6210
jne 0x177d
movq (%r14), %r15
callq 0x133c
movq %r15, %rdi
cmpl $0x5, %r13d
je 0x176c
leaq 0x19b3(%rip), %rsi      # 0x30ef
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1120
movl $0x2, %edi
callq 0x1220
incl %r13d
leaq 0x10(%rsp), %rdi
movl $0x6, %esi
callq 0x11b0
testl %eax, %eax
je 0x165d
jmp 0x177d
leaq 0x19ac(%rip), %rsi      # 0x311f
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1120
movl $0x1, 0xc(%rsp)
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1230
testl %eax, %eax
js 0x18b3
movl %eax, %r13d
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movups %xmm0, 0xde(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movw $0x1, 0x80(%rsp)
movb $0x0, 0x82(%rsp)
leaq 0x40(%rsp), %r15
movq %r15, %rdi
callq 0x10b0
leaq 0x83(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x1130
movq %r15, %rdi
callq 0x10b0
movq %rax, %r15
movl %r13d, %edi
movl $0x1, %esi
movl $0x2, %edx
leaq 0xc(%rsp), %rcx
movl $0x4, %r8d
callq 0x1080
testl %eax, %eax
js 0x18ed
addl $0x3, %r15d
movl %r13d, %edi
movq %r12, %rsi
movl %r15d, %edx
callq 0x1170
testl %eax, %eax
js 0x1927
movq (%r14), %r15
callq 0x133c
movq %r15, %rdi
leaq 0x1c9f(%rip), %rsi      # 0x3509
movq %rbp, %rdx
leaq 0x40(%rsp), %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x1120
movl %r13d, %edi
movl $0x3, %esi
xorl %eax, %eax
callq 0x1090
orl $0x800, %eax            # imm = 0x800
movl %r13d, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x1090
movl %r13d, %edi
movl $0x1, %esi
callq 0x1150
jmp 0x196c
movq (%r14), %r15
callq 0x133c
callq 0x1060
movq %rax, %r13
movl (%rax), %edi
callq 0x1200
movl (%r13), %ecx
movl %ecx, (%rsp)
movq %r15, %rdi
leaq 0x1bde(%rip), %rsi      # 0x34b9
movq %rbp, %rdx
leaq 0x1bf5(%rip), %rcx      # 0x34da
movl $0x1e8, %r8d            # imm = 0x1E8
jmp 0x195c
movq (%r14), %r15
callq 0x133c
callq 0x1060
movq %rax, %r13
movl (%rax), %edi
callq 0x1200
movl (%r13), %ecx
movl %ecx, (%rsp)
movq %r15, %rdi
leaq 0x1ba4(%rip), %rsi      # 0x34b9
movq %rbp, %rdx
leaq 0x1bbb(%rip), %rcx      # 0x34da
movl $0x1f0, %r8d            # imm = 0x1F0
jmp 0x195c
movl %r13d, %edi
callq 0x10e0
movq (%r14), %r15
callq 0x133c
callq 0x1060
movl (%rax), %r13d
movl %r13d, %edi
callq 0x1200
movq %r15, %rdi
leaq 0x1b9d(%rip), %rsi      # 0x34ee
movq %rbp, %rdx
leaq 0x40(%rsp), %rcx
movl %r13d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x1120
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl %r13d, 0x37c1(%rip)     # 0x5134
movq (%r14), %r15
callq 0x133c
movl 0x37b3(%rip), %ecx      # 0x5134
movq %r15, %rdi
leaq 0x1ae9(%rip), %rsi      # 0x3474
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1120
cmpl $-0x1, 0x3798(%rip)     # 0x5134
jne 0x19d5
movq (%r14), %r15
callq 0x133c
callq 0x1060
movl (%rax), %r13d
movl %r13d, %edi
callq 0x1200
movq %r15, %rdi
leaq 0x1ad1(%rip), %rsi      # 0x3491
movq %rbp, %rdx
leaq 0x40(%rsp), %rcx
movl %r13d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x1120
cmpl $-0x1, 0x3758(%rip)     # 0x5134
jne 0x19ea
movq 0x37f3(%rip), %rdi      # 0x51d8
callq 0x1210
movq 0x37e7(%rip), %rdi      # 0x51d8
xorl %esi, %esi
callq 0x11f0
cmpl $-0x1, 0x3735(%rip)     # 0x5134
je 0x1a3e
movq (%r14), %r15
callq 0x133c
movq %r15, %rdi
leaq 0x1b17(%rip), %rsi      # 0x352a
movq %rbp, %rdx
leaq 0x1b24(%rip), %rcx      # 0x3541
leaq 0x40(%rsp), %r8
xorl %eax, %eax
callq 0x1120
movl 0x3705(%rip), %edi      # 0x5134
callq 0x10e0
movl $0xffffffff, 0x36f6(%rip) # imm = 0xFFFFFFFF
movl 0x36ec(%rip), %edi      # 0x5130
callq 0x10e0
cmpb $0x1, 0x3790(%rip)      # 0x51e0
leaq 0x10(%rsp), %r15
jne 0x1a6e
movl $0x14, %eax
movl %eax, %edi
callq 0x1220
testl %eax, %eax
jne 0x1a5c
movb $0x0, 0x3772(%rip)      # 0x51e0
cmpb $0x0, 0x375f(%rip)      # 0x51d4
je 0x15a4
xorl %ebx, %ebx
jmp 0x1519
movq (%r14), %rbx
callq 0x133c
callq 0x1060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
leaq 0x1623(%rip), %rsi      # 0x30c2
jmp 0x14cd