// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x40(%rbp), %rdi
leaq 0x313c(%rip), %rsi      # 0x4560
movl $0x21, %edx
callq 0x1130
movl $0x0, -0x44(%rbp)
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x10d0
movq 0x4b89(%rip), %rax      # 0x5fd8
movl $0x1, (%rax)
movl $0x2, %edi
leaq 0x44f(%rip), %rsi       # 0x18b0
callq 0x1110
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x2bb0(%rip), %rdx      # 0x4024
callq 0x11c0
movl %eax, %ecx
movl %ecx, -0x14(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
je 0x14d8
movl -0x14(%rbp), %eax
movl %eax, -0x98(%rbp)
subl $0x64, %eax
je 0x14a4
jmp 0x1497
movl -0x98(%rbp), %eax
subl $0x76, %eax
je 0x14b9
jmp 0x14c5
leaq -0x40(%rbp), %rdi
movq 0x4b31(%rip), %rax      # 0x5fe0
movq (%rax), %rsi
callq 0x1060
jmp 0x14d6
movl $0x1, 0x4d0d(%rip)      # 0x61d0
jmp 0x14d6
callq 0x18f0
movl $0x0, -0x4(%rbp)
jmp 0x189e
jmp 0x1466
leaq -0x40(%rbp), %rdi
movl $0x6, %esi
callq 0x11b0
cmpl $0x0, %eax
je 0x1567
jmp 0x14ed
movq 0x4adc(%rip), %rax      # 0x5fd0
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
callq 0x1340
movq %rax, -0xb0(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0xa8(%rbp)
callq 0x1050
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x1200
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movl -0x9c(%rbp), %r8d
movq %rax, %r9
leaq 0x2ad7(%rip), %rsi      # 0x402b
movb $0x0, %al
callq 0x1120
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x189e
leaq -0x90(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x10a0
movq -0xc0(%rbp), %rdi
subq $0x1, %rax
movsbl -0x40(%rbp,%rax), %edx
leaq 0x2ac3(%rip), %rsi      # 0x4058
movb $0x0, %al
callq 0x11e0
movq 0x4a2d(%rip), %rax      # 0x5fd0
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
callq 0x1340
movq -0xc8(%rbp), %rdi
movq %rax, %rdx
leaq -0x40(%rbp), %rcx
leaq -0x90(%rbp), %r8
leaq 0x2a9e(%rip), %rsi      # 0x406c
movb $0x0, %al
callq 0x1120
jmp 0x15d7
cmpl $0x0, 0x4bf6(%rip)      # 0x61d4
jne 0x1897
leaq -0x40(%rbp), %rdi
movl $0x6, %esi
callq 0x11b0
cmpl $0x0, %eax
je 0x1676
jmp 0x15f9
movq 0x49d0(%rip), %rax      # 0x5fd0
movq (%rax), %rax
movq %rax, -0xe8(%rbp)
callq 0x1340
movq %rax, -0xe0(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0xd8(%rbp)
callq 0x1050
movl (%rax), %eax
movl %eax, -0xcc(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x1200
movq -0xe8(%rbp), %rdi
movq -0xe0(%rbp), %rdx
movq -0xd8(%rbp), %rcx
movl -0xcc(%rbp), %r8d
movq %rax, %r9
leaq 0x2a32(%rip), %rsi      # 0x4092
movb $0x0, %al
callq 0x1120
movl $0x3, %edi
callq 0x1220
jmp 0x15d7
leaq -0x40(%rbp), %rdi
movl $0x902, %esi            # imm = 0x902
movb $0x0, %al
callq 0x11a0
movl %eax, 0x4aa4(%rip)      # 0x6130
cmpl $-0x1, 0x4a9d(%rip)     # 0x6130
jne 0x1711
jmp 0x1697
movq 0x4932(%rip), %rax      # 0x5fd0
movq (%rax), %rax
movq %rax, -0x108(%rbp)
callq 0x1340
movq %rax, -0x100(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0xf8(%rbp)
callq 0x1050
movl (%rax), %eax
movl %eax, -0xec(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x1200
movq -0x108(%rbp), %rdi
movq -0x100(%rbp), %rdx
movq -0xf8(%rbp), %rcx
movl -0xec(%rbp), %r8d
movq %rax, %r9
leaq 0x29c4(%rip), %rsi      # 0x40c2
movb $0x0, %al
callq 0x1120
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x189e
jmp 0x1713
movl 0x4a17(%rip), %eax      # 0x6130
movl %eax, -0x10c(%rbp)
movl 0x4a0b(%rip), %edi      # 0x6130
movl $0x3, %esi
movb $0x0, %al
callq 0x1080
movl -0x10c(%rbp), %edi
movl %eax, %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x1080
leaq 0x4a86(%rip), %rdi      # 0x61d8
xorl %eax, %eax
movl %eax, %ecx
leaq 0x1d3(%rip), %rdx       # 0x1930
movq %rcx, %rsi
callq 0x1180
callq 0x20b0
cmpl $0x0, %eax
je 0x180a
cmpl $0x5, -0x44(%rbp)
jge 0x17be
jmp 0x177b
movq 0x484e(%rip), %rax      # 0x5fd0
movq (%rax), %rax
movq %rax, -0x118(%rbp)
callq 0x1340
movq -0x118(%rbp), %rdi
movq %rax, %rdx
leaq 0x294d(%rip), %rsi      # 0x40ef
movb $0x0, %al
callq 0x1120
movl $0x2, %edi
callq 0x1220
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x17f0
jmp 0x17c0
movq 0x4809(%rip), %rax      # 0x5fd0
movq (%rax), %rax
movq %rax, -0x120(%rbp)
callq 0x1340
movq -0x120(%rbp), %rdi
movq %rax, %rdx
leaq 0x2938(%rip), %rsi      # 0x411f
movb $0x0, %al
callq 0x1120
jmp 0x180a
leaq -0x40(%rbp), %rdi
movl $0x6, %esi
callq 0x11b0
cmpl $0x0, %eax
je 0x1805
jmp 0x180a
jmp 0x1765
movl $0x0, -0x44(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x2220
cmpl $-0x1, 0x4910(%rip)     # 0x6134
jne 0x1832
movq 0x49ab(%rip), %rdi      # 0x61d8
callq 0x1210
movq 0x499f(%rip), %rdi      # 0x61d8
xorl %eax, %eax
movl %eax, %esi
callq 0x11f0
leaq -0x90(%rbp), %rdi
callq 0x22e0
movl 0x48dc(%rip), %edi      # 0x6130
callq 0x10e0
cmpl $0x0, 0x4980(%rip)      # 0x61e0
je 0x1892
movl $0x14, -0x94(%rbp)
cmpl $0x0, -0x94(%rbp)
je 0x1888
movl -0x94(%rbp), %edi
callq 0x1220
movl %eax, -0x94(%rbp)
jmp 0x186c
movl $0x0, 0x494e(%rip)      # 0x61e0
jmp 0x15d7
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw (%rax,%rax)