// File: /workspace/github_c_makefile_star2_O0/Carton32[P]quectel-CM/device.c
// Function: qmidevice_detect
BOOL qmidevice_detect(char *qmichannel, char *usbnet_adapter, unsigned bufsize, PROFILE_T *profile) {
    struct dirent* ent = NULL;
    DIR *pDir;
    const char *rootdir = "/sys/bus/usb/devices";
    struct {
        char path[255*2];
    } *pl;
    pl = (typeof(pl)) malloc(sizeof(*pl));
    memset(pl, 0x00, sizeof(*pl));

    pDir = opendir(rootdir);
    if (!pDir) {
        dbg_time("opendir %s failed: %s", rootdir, strerror(errno));
        goto error;
    }

    while ((ent = readdir(pDir)) != NULL)  {
        char netcard[32+1] = {'\0'};
        char devname[32+5] = {'\0'}; //+strlen("/dev/")
        int netIntf;
        int driver_type;

        if (ent->d_name[0] == 'u')
            continue;

        snprintf(pl->path, sizeof(pl->path), "%s/%s", rootdir, ent->d_name);
        query_usb_device_info(pl->path, &profile->usb_dev);
        if (profile->usb_dev.idVendor == CM_INVALID_VAL)
            continue;

        if (profile->usb_dev.idVendor == 0x2c7c || profile->usb_dev.idVendor == 0x05c6) {
            dbg_time("Find %s/%s idVendor=0x%x idProduct=0x%x, bus=0x%03x, dev=0x%03x",
                rootdir, ent->d_name, profile->usb_dev.idVendor, profile->usb_dev.idProduct,
                profile->usb_dev.busnum, profile->usb_dev.devnum);
        }

        /* get network interface */
        /* NOTICE: there is a case that, bNumberInterface=6, but the net interface is 8 */
        /* toolchain-mips_24kc_gcc-5.4.0_musl donot support GLOB_BRACE */
        /* RG500U's MBIM is at inteface 0 */
        for (netIntf = 0;  netIntf < (profile->usb_dev.bNumInterfaces + 8); netIntf++) {
            snprintf(pl->path, sizeof(pl->path), "%s/%s:1.%d/net", rootdir, ent->d_name, netIntf);
            dir_get_child(pl->path, netcard, sizeof(netcard), NULL);
            if (netcard[0])
                break;
        }

        if (netcard[0] == '\0') { //for centos 2.6.x
            const char *n= "usb0";
            const char *c = "qcqmi0";

            snprintf(pl->path, sizeof(pl->path), "%s/%s:1.4/net:%s", rootdir, ent->d_name, n);
            if (!access(pl->path, F_OK)) {
                snprintf(pl->path, sizeof(pl->path), "%s/%s:1.4/GobiQMI:%s", rootdir, ent->d_name, c);
                if (!access(pl->path, F_OK)) {
                    snprintf(qmichannel, bufsize, "/dev/%s", c);
                    snprintf(usbnet_adapter, bufsize, "%s", n);
                    snprintf(pl->path, sizeof(pl->path), "%s/%s:1.4", rootdir, ent->d_name);
                    query_usb_interface_info(pl->path, &profile->usb_intf);
                    break;
                }
            }
        }

        if (netcard[0] == '\0')
            continue;

        /* not '-i iface' */
        if (usbnet_adapter[0] && strcmp(usbnet_adapter, netcard))
            continue;

        snprintf(pl->path, sizeof(pl->path), "%s/%s:1.%d", rootdir, ent->d_name, netIntf);
        query_usb_interface_info(pl->path, &profile->usb_intf);
        driver_type = get_driver_type(profile);

        if (driver_type == SOFTWARE_QMI || driver_type == SOFTWARE_MBIM) {
            detect_path_cdc_wdm_or_qcqmi(pl->path, devname, sizeof(devname));
        }
        else if (driver_type == SOFTWARE_ECM_RNDIS_NCM)
        {
            int atIntf = -1;

            if (profile->usb_dev.idVendor == 0x2c7c) { //Quectel
                switch (profile->usb_dev.idProduct) { //EC200U
                case 0x0901: //EC200U
                case 0x8101: //RG801H
                    atIntf = 2;
                break;
                case 0x0900: //RG500U
                    atIntf = 4;
                break;
                case 0x6026: //EC200T
                case 0x6002: //EC200S
                    atIntf = 3;
                break;
                default:
                   dbg_time("unknow at interface for USB idProduct:%04x\n", profile->usb_dev.idProduct);
                break;
                }
            }

            if (atIntf != -1) {
                snprintf(pl->path, sizeof(pl->path), "%s/%s:1.%d", rootdir, ent->d_name, atIntf);
                dir_get_child(pl->path, devname, sizeof(devname), "tty");
                if (devname[0] && !strcmp(devname, "tty")) {
                    snprintf(pl->path, sizeof(pl->path), "%s/%s:1.%d/tty", rootdir, ent->d_name, atIntf);
                    dir_get_child(pl->path, devname, sizeof(devname), "tty");
                }
            }
        }
        
        if (netcard[0] && devname[0]) {
            if (devname[0] == '/')
                snprintf(qmichannel, bufsize, "%s", devname);
            else
                snprintf(qmichannel, bufsize, "/dev/%s", devname);
            snprintf(usbnet_adapter, bufsize, "%s", netcard);
            dbg_time("Auto find qmichannel = %s", qmichannel);
            dbg_time("Auto find usbnet_adapter = %s", usbnet_adapter);
            break;
        }
    }
    closedir(pDir);

    if (qmichannel[0] == '\0' || usbnet_adapter[0] == '\0') {
        dbg_time("network interface '%s' or qmidev '%s' is not exist", usbnet_adapter, qmichannel);
        goto error;
    }
    free(pl);
    return TRUE;
error:
    free(pl);
    return FALSE;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
leaq 0xf294(%rip), %rax      # 0x2fa8d
movq %rax, -0x40(%rbp)
movl $0x1fe, %edi            # imm = 0x1FE
callq 0x5370
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
xorl %esi, %esi
movl $0x1fe, %edx            # imm = 0x1FE
callq 0x5200
movq -0x40(%rbp), %rdi
callq 0x5140
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x208fd
jmp 0x20835
movq 0x167a4(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
callq 0x13930
movq %rax, -0xd0(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xc8(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rdx
movq -0xc8(%rbp), %rcx
movq %rax, %r8
leaq 0xf21a(%rip), %rsi      # 0x2faa2
movb $0x0, %al
callq 0x52c0
leaq 0x175d2(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x208f6
leaq 0x175c5(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0xf0(%rbp)
callq 0x13930
movq %rax, -0xe8(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xe0(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0xf0(%rbp), %rdi
movq -0xe8(%rbp), %rdx
movq -0xe0(%rbp), %rcx
movq %rax, %r8
leaq 0xf1b3(%rip), %rsi      # 0x2faa2
movb $0x0, %al
callq 0x52c0
jmp 0x208f8
jmp 0x21107
jmp 0x208ff
movq -0x38(%rbp), %rdi
callq 0x5320
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x21055
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x21, %edx
callq 0x5200
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x25, %edx
callq 0x5200
movq -0x30(%rbp), %rax
movsbl 0x13(%rax), %eax
cmpl $0x75, %eax
jne 0x20948
jmp 0x208ff
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r8
addq $0x13, %r8
movl $0x1fe, %esi            # imm = 0x1FE
leaq 0xb5e0(%rip), %rdx      # 0x2bf44
movb $0x0, %al
callq 0x51c0
movq -0x48(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x1c5, %rsi            # imm = 0x1C5
callq 0x21130
movq -0x28(%rbp), %rax
cmpl $-0x1, 0x1c5(%rax)
jne 0x20991
jmp 0x208ff
movq -0x28(%rbp), %rax
cmpl $0x2c7c, 0x1c5(%rax)    # imm = 0x2C7C
je 0x209b5
movq -0x28(%rbp), %rax
cmpl $0x5c6, 0x1c5(%rax)     # imm = 0x5C6
jne 0x20aac
jmp 0x209b7
movq 0x16622(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
callq 0x13930
movq -0xf8(%rbp), %rdi
movq %rax, %rdx
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r8
addq $0x13, %r8
movq -0x28(%rbp), %rax
movl 0x1c5(%rax), %r9d
movq -0x28(%rbp), %rax
movl 0x1c9(%rax), %r11d
movq -0x28(%rbp), %rax
movl 0x1cd(%rax), %r10d
movq -0x28(%rbp), %rax
movl 0x1d1(%rax), %eax
leaq 0xf0a9(%rip), %rsi      # 0x2fabe
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x52c0
leaq 0x17438(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x20aa8
leaq 0x1742b(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x100(%rbp)
callq 0x13930
movq -0x100(%rbp), %rdi
movq %rax, %rdx
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r8
addq $0x13, %r8
movq -0x28(%rbp), %rax
movl 0x1c5(%rax), %r9d
movq -0x28(%rbp), %rax
movl 0x1c9(%rax), %r11d
movq -0x28(%rbp), %rax
movl 0x1cd(%rax), %r10d
movq -0x28(%rbp), %rax
movl 0x1d1(%rax), %eax
leaq 0xf02a(%rip), %rsi      # 0x2fabe
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x52c0
jmp 0x20aaa
jmp 0x20aac
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movq -0x28(%rbp), %rcx
movl 0x1d5(%rcx), %ecx
addl $0x8, %ecx
cmpl %ecx, %eax
jge 0x20b28
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r8
addq $0x13, %r8
movl -0xa4(%rbp), %r9d
movl $0x1fe, %esi            # imm = 0x1FE
leaq 0xf014(%rip), %rdx      # 0x2fb04
movb $0x0, %al
callq 0x51c0
movq -0x48(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movl $0x21, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x21280
cmpb $0x0, -0x70(%rbp)
je 0x20b15
jmp 0x20b28
jmp 0x20b17
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x20ab6
movsbl -0x70(%rbp), %eax
cmpl $0x0, %eax
jne 0x20c45
leaq 0xefd7(%rip), %rax      # 0x2fb13
movq %rax, -0xb0(%rbp)
leaq 0xefce(%rip), %rax      # 0x2fb18
movq %rax, -0xb8(%rbp)
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r8
addq $0x13, %r8
movq -0xb0(%rbp), %r9
movl $0x1fe, %esi            # imm = 0x1FE
leaq 0xefab(%rip), %rdx      # 0x2fb1f
movb $0x0, %al
callq 0x51c0
movq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x5460
cmpl $0x0, %eax
jne 0x20c43
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r8
addq $0x13, %r8
movq -0xb8(%rbp), %r9
movl $0x1fe, %esi            # imm = 0x1FE
leaq 0xef7e(%rip), %rdx      # 0x2fb30
movb $0x0, %al
callq 0x51c0
movq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x5460
cmpl $0x0, %eax
jne 0x20c41
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
movq -0xb8(%rbp), %rcx
leaq 0xef65(%rip), %rdx      # 0x2fb45
movb $0x0, %al
callq 0x51c0
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
movq -0xb0(%rbp), %rcx
leaq 0xf45c(%rip), %rdx      # 0x3005a
movb $0x0, %al
callq 0x51c0
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r8
addq $0x13, %r8
movl $0x1fe, %esi            # imm = 0x1FE
leaq 0xef2c(%rip), %rdx      # 0x2fb4d
movb $0x0, %al
callq 0x51c0
movq -0x48(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x1d9, %rsi            # imm = 0x1D9
callq 0x21370
jmp 0x21055
jmp 0x20c43
jmp 0x20c45
movsbl -0x70(%rbp), %eax
cmpl $0x0, %eax
jne 0x20c53
jmp 0x208ff
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x20c76
movq -0x18(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x52a0
cmpl $0x0, %eax
je 0x20c76
jmp 0x208ff
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r8
addq $0x13, %r8
movl -0xa4(%rbp), %r9d
movl $0x1fe, %esi            # imm = 0x1FE
leaq 0xeebe(%rip), %rdx      # 0x2fb57
movb $0x0, %al
callq 0x51c0
movq -0x48(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x1d9, %rsi            # imm = 0x1D9
callq 0x21370
movq -0x28(%rbp), %rdi
callq 0x215e0
movl %eax, -0xa8(%rbp)
cmpl $0x1, -0xa8(%rbp)
je 0x20cd5
cmpl $0x2, -0xa8(%rbp)
jne 0x20cef
movq -0x48(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0x25, %edx
callq 0x21740
jmp 0x20ee4
cmpl $0x3, -0xa8(%rbp)
jne 0x20ee2
movl $0xffffffff, -0xbc(%rbp) # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rax
cmpl $0x2c7c, 0x1c5(%rax)    # imm = 0x2C7C
jne 0x20e21
movq -0x28(%rbp), %rax
movl 0x1c9(%rax), %eax
movl %eax, -0x104(%rbp)
subl $0x900, %eax            # imm = 0x900
je 0x20d7e
jmp 0x20d33
movl -0x104(%rbp), %eax
subl $0x901, %eax            # imm = 0x901
je 0x20d6f
jmp 0x20d42
movl -0x104(%rbp), %eax
subl $0x6002, %eax           # imm = 0x6002
je 0x20d8d
jmp 0x20d51
movl -0x104(%rbp), %eax
subl $0x6026, %eax           # imm = 0x6026
je 0x20d8d
jmp 0x20d60
movl -0x104(%rbp), %eax
subl $0x8101, %eax           # imm = 0x8101
jne 0x20d9c
jmp 0x20d6f
movl $0x2, -0xbc(%rbp)
jmp 0x20e1f
movl $0x4, -0xbc(%rbp)
jmp 0x20e1f
movl $0x3, -0xbc(%rbp)
jmp 0x20e1f
jmp 0x20d9e
movq 0x1623b(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x110(%rbp)
callq 0x13930
movq -0x110(%rbp), %rdi
movq %rax, %rdx
movq -0x28(%rbp), %rax
movl 0x1c9(%rax), %ecx
leaq 0xed93(%rip), %rsi      # 0x2fb62
movb $0x0, %al
callq 0x52c0
leaq 0x1708b(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x20e1b
leaq 0x1707e(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x118(%rbp)
callq 0x13930
movq -0x118(%rbp), %rdi
movq %rax, %rdx
movq -0x28(%rbp), %rax
movl 0x1c9(%rax), %ecx
leaq 0xed4e(%rip), %rsi      # 0x2fb62
movb $0x0, %al
callq 0x52c0
jmp 0x20e1d
jmp 0x20e1f
jmp 0x20e21
cmpl $-0x1, -0xbc(%rbp)
je 0x20ee0
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r8
addq $0x13, %r8
movl -0xbc(%rbp), %r9d
movl $0x1fe, %esi            # imm = 0x1FE
leaq 0xed06(%rip), %rdx      # 0x2fb57
movb $0x0, %al
callq 0x51c0
movq -0x48(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0x25, %edx
leaq 0xed30(%rip), %rcx      # 0x2fb9f
callq 0x21280
movsbl -0xa0(%rbp), %eax
cmpl $0x0, %eax
je 0x20ede
leaq -0xa0(%rbp), %rdi
leaq 0xed11(%rip), %rsi      # 0x2fb9f
callq 0x52a0
cmpl $0x0, %eax
jne 0x20ede
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r8
addq $0x13, %r8
movl -0xbc(%rbp), %r9d
movl $0x1fe, %esi            # imm = 0x1FE
leaq 0xecd9(%rip), %rdx      # 0x2fb94
movb $0x0, %al
callq 0x51c0
movq -0x48(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0x25, %edx
leaq 0xecc6(%rip), %rcx      # 0x2fb9f
callq 0x21280
jmp 0x20ee0
jmp 0x20ee2
jmp 0x20ee4
movsbl -0x70(%rbp), %eax
cmpl $0x0, %eax
je 0x21050
movsbl -0xa0(%rbp), %eax
cmpl $0x0, %eax
je 0x21050
movsbl -0xa0(%rbp), %eax
cmpl $0x2f, %eax
jne 0x20f2d
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
leaq -0xa0(%rbp), %rcx
leaq 0xf136(%rip), %rdx      # 0x3005a
movb $0x0, %al
callq 0x51c0
jmp 0x20f4b
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
leaq -0xa0(%rbp), %rcx
leaq 0xec01(%rip), %rdx      # 0x2fb45
movb $0x0, %al
callq 0x51c0
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
leaq -0x70(%rbp), %rcx
leaq 0xf0fb(%rip), %rdx      # 0x3005a
movb $0x0, %al
callq 0x51c0
movq 0x16073(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x120(%rbp)
callq 0x13930
movq -0x120(%rbp), %rdi
movq %rax, %rdx
movq -0x10(%rbp), %rcx
leaq 0xec12(%rip), %rsi      # 0x2fba3
movb $0x0, %al
callq 0x52c0
leaq 0x16ec9(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x20fd7
leaq 0x16ebc(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x128(%rbp)
callq 0x13930
movq -0x128(%rbp), %rdi
movq %rax, %rdx
movq -0x10(%rbp), %rcx
leaq 0xebd3(%rip), %rsi      # 0x2fba3
movb $0x0, %al
callq 0x52c0
jmp 0x20fd9
jmp 0x20fdb
movq 0x15ffe(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x130(%rbp)
callq 0x13930
movq -0x130(%rbp), %rdi
movq %rax, %rdx
movq -0x18(%rbp), %rcx
leaq 0xebbd(%rip), %rsi      # 0x2fbc3
movb $0x0, %al
callq 0x52c0
leaq 0x16e54(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x2104c
leaq 0x16e47(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x138(%rbp)
callq 0x13930
movq -0x138(%rbp), %rdi
movq %rax, %rdx
movq -0x18(%rbp), %rcx
leaq 0xeb7e(%rip), %rsi      # 0x2fbc3
movb $0x0, %al
callq 0x52c0
jmp 0x2104e
jmp 0x21055
jmp 0x208ff
movq -0x38(%rbp), %rdi
callq 0x5240
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x21076
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x210f5
jmp 0x21078
movq 0x15f61(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x140(%rbp)
callq 0x13930
movq -0x140(%rbp), %rdi
movq %rax, %rdx
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %r8
leaq 0xeb40(%rip), %rsi      # 0x2fbe7
movb $0x0, %al
callq 0x52c0
leaq 0x16db3(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x210f1
leaq 0x16da6(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x148(%rbp)
callq 0x13930
movq -0x148(%rbp), %rdi
movq %rax, %rdx
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %r8
leaq 0xeafd(%rip), %rsi      # 0x2fbe7
movb $0x0, %al
callq 0x52c0
jmp 0x210f3
jmp 0x21107
movq -0x48(%rbp), %rdi
callq 0x5040
movl $0x1, -0x4(%rbp)
jmp 0x21117
movq -0x48(%rbp), %rdi
callq 0x5040
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nopw %cs:(%rax,%rax)