// File: /workspace/github_c_makefile_star2_O3/Carton32[P]quectel-CM/QMIThread.c
// Function: WdsStartNwInterfaceReq
static USHORT WdsStartNwInterfaceReq(PQMUX_MSG pMUXMsg, void *arg) {
    PQMIWDS_TECHNOLOGY_PREFERECE pTechPref;
    PQMIWDS_AUTH_PREFERENCE pAuthPref;
    PQMIWDS_USERNAME pUserName;
    PQMIWDS_PASSWD pPasswd;
    PQMIWDS_APNNAME pApnName;
    PQMIWDS_IP_FAMILY_TLV pIpFamily;
    USHORT TLVLength = 0;
    UCHAR *pTLV;
    PROFILE_T *profile = (PROFILE_T *)arg;
    const char *profile_user = profile->user;
    const char *profile_password = profile->password;
    int profile_auth = profile->auth;

    if (s_is_cdma && (profile_user == NULL || profile_user[0] == '\0') && (profile_password == NULL || profile_password[0] == '\0')) {
        profile_user = "ctnet@mycdma.cn";
        profile_password = "vnet.mobi";
        profile_auth = 2; //chap
    }

    pTLV = (UCHAR *)(&pMUXMsg->StartNwInterfaceReq + 1);
    pMUXMsg->StartNwInterfaceReq.Length = 0;

    // Set technology Preferece
    pTechPref = (PQMIWDS_TECHNOLOGY_PREFERECE)(pTLV + TLVLength);
    pTechPref->TLVType = 0x30;
    pTechPref->TLVLength = cpu_to_le16(0x01);
    if (s_is_cdma == 0)
        pTechPref->TechPreference = 0x01;
    else
        pTechPref->TechPreference = 0x02;
    TLVLength +=(le16_to_cpu(pTechPref->TLVLength) + sizeof(QCQMICTL_TLV_HDR));

    // Set APN Name
    if (profile->apn && !s_is_cdma) { //cdma no apn
        pApnName = (PQMIWDS_APNNAME)(pTLV + TLVLength);
        pApnName->TLVType = 0x14;
        pApnName->TLVLength = cpu_to_le16(strlen(profile->apn));
        qstrcpy((char *)&pApnName->ApnName, profile->apn);
        TLVLength +=(le16_to_cpu(pApnName->TLVLength) + sizeof(QCQMICTL_TLV_HDR));
    }

    // Set User Name
    if (profile_user) {
        pUserName = (PQMIWDS_USERNAME)(pTLV + TLVLength);
        pUserName->TLVType = 0x17;
        pUserName->TLVLength = cpu_to_le16(strlen(profile_user));
        qstrcpy((char *)&pUserName->UserName, profile_user);
        TLVLength += (le16_to_cpu(pUserName->TLVLength) + sizeof(QCQMICTL_TLV_HDR));
    }

    // Set Password
    if (profile_password) {
        pPasswd = (PQMIWDS_PASSWD)(pTLV + TLVLength);
        pPasswd->TLVType = 0x18;
        pPasswd->TLVLength = cpu_to_le16(strlen(profile_password));
        qstrcpy((char *)&pPasswd->Passwd, profile_password);
	TLVLength += (le16_to_cpu(pPasswd->TLVLength) + sizeof(QCQMICTL_TLV_HDR));
    }

    // Set Auth Protocol
    if (profile_user && profile_password) {
        pAuthPref = (PQMIWDS_AUTH_PREFERENCE)(pTLV + TLVLength);
        pAuthPref->TLVType = 0x16;
        pAuthPref->TLVLength = cpu_to_le16(0x01);
        pAuthPref->AuthPreference = profile_auth; // 0 ~ None, 1 ~ Pap, 2 ~ Chap, 3 ~ MsChapV2
        TLVLength += (le16_to_cpu(pAuthPref->TLVLength) + sizeof(QCQMICTL_TLV_HDR));
    }

    // Add IP Family Preference
    pIpFamily = (PQMIWDS_IP_FAMILY_TLV)(pTLV + TLVLength);
    pIpFamily->TLVType = 0x19;
    pIpFamily->TLVLength = cpu_to_le16(0x01);
    pIpFamily->IpFamily = profile->curIpFamily;
    TLVLength += (le16_to_cpu(pIpFamily->TLVLength) + sizeof(QCQMICTL_TLV_HDR));

    //Set Profile Index
    if (profile->pdp && !s_is_cdma) { //cdma only support one pdp, so no need to set profile index
        PQMIWDS_PROFILE_IDENTIFIER pProfileIndex = (PQMIWDS_PROFILE_IDENTIFIER)(pTLV + TLVLength);
        pProfileIndex->TLVLength = cpu_to_le16(0x01);
        pProfileIndex->TLVType = 0x31;
        pProfileIndex->ProfileIndex = profile->pdp;
        if (s_is_cdma && s_hdr_personality == 0x02) {
            pProfileIndex->TLVType = 0x32; //profile_index_3gpp2
            pProfileIndex->ProfileIndex = 101;
        }
        TLVLength += (le16_to_cpu(pProfileIndex->TLVLength) + sizeof(QCQMICTL_TLV_HDR));
    }

    return sizeof(QMIWDS_START_NETWORK_INTERFACE_REQ_MSG) + TLVLength;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
movq 0x8(%rsi), %r15
movq 0x10(%rsi), %r12
movl 0x18(%rsi), %eax
cmpl $0x0, 0x1c6a1(%rip)     # 0x2ac34
je 0xe5bf
testq %r15, %r15
je 0xe5a0
cmpb $0x0, (%r15)
jne 0xe5bf
testq %r12, %r12
je 0xe5ac
cmpb $0x0, (%r12)
jne 0xe5bf
movl $0x2, %eax
leaq 0x1264c(%rip), %r12     # 0x20c04
leaq 0x12635(%rip), %r15     # 0x20bf4
movl %eax, 0x4(%rsp)
movw $0x0, 0x5(%rbx)
movb $0x30, 0x7(%rbx)
movl $0x1, %edi
callq 0xee7e
movw %ax, 0x8(%rbx)
cmpl $0x1, 0x1c652(%rip)     # 0x2ac34
movb $0x2, %cl
sbbb $0x0, %cl
movb %cl, 0xa(%rbx)
movzwl %ax, %edi
callq 0xee73
movl %eax, %r13d
leal 0x3(%r13), %ebp
movq (%r14), %rdi
testq %rdi, %rdi
je 0xe660
cmpl $0x0, 0x1c62c(%rip)     # 0x2ac34
jne 0xe660
movzwl %bp, %ebp
movb $0x14, 0x7(%rbx,%rbp)
callq 0x5160
movzwl %ax, %edi
callq 0xee7e
movw %ax, 0x8(%rbx,%rbp)
movq (%r14), %rcx
movb (%rcx), %dl
movb %dl, 0xa(%rbx,%rbp)
testb %dl, %dl
je 0xe651
leaq (%rbx,%rbp), %rax
addq $0xb, %rax
xorl %edx, %edx
movb 0x1(%rcx,%rdx), %sil
movb %sil, (%rax,%rdx)
incq %rdx
testb %sil, %sil
jne 0xe63b
movzwl 0x8(%rbx,%rbp), %eax
movzwl %ax, %edi
callq 0xee73
leal (%rax,%r13), %ebp
addl $0x6, %ebp
testq %r15, %r15
je 0xe6bd
movzwl %bp, %r13d
movb $0x17, 0x7(%rbx,%r13)
movq %r15, %rdi
callq 0x5160
movzwl %ax, %edi
callq 0xee7e
movw %ax, 0x8(%rbx,%r13)
movb (%r15), %cl
movb %cl, 0xa(%rbx,%r13)
testb %cl, %cl
je 0xe6b0
leaq (%rbx,%r13), %rax
addq $0xb, %rax
xorl %ecx, %ecx
movb 0x1(%r15,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
testb %dl, %dl
jne 0xe69b
movzwl 0x8(%rbx,%r13), %eax
movzwl %ax, %edi
callq 0xee73
addl %eax, %ebp
addl $0x3, %ebp
testq %r12, %r12
je 0xe754
movzwl %bp, %r13d
movb $0x18, 0x7(%rbx,%r13)
movq %r12, %rdi
callq 0x5160
movzwl %ax, %edi
callq 0xee7e
movw %ax, 0x8(%rbx,%r13)
movb (%r12), %cl
movb %cl, 0xa(%rbx,%r13)
testb %cl, %cl
je 0xe712
leaq (%rbx,%r13), %rax
addq $0xb, %rax
xorl %ecx, %ecx
movb 0x1(%r12,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
testb %dl, %dl
jne 0xe6fd
movzwl 0x8(%rbx,%r13), %eax
movzwl %ax, %edi
callq 0xee73
addl %eax, %ebp
addl $0x3, %ebp
testq %r15, %r15
je 0xe754
movzwl %bp, %r15d
movb $0x16, 0x7(%rbx,%r15)
movl $0x1, %edi
callq 0xee7e
movw %ax, 0x8(%rbx,%r15)
movl 0x4(%rsp), %ecx
movb %cl, 0xa(%rbx,%r15)
movzwl %ax, %edi
callq 0xee73
addl %eax, %ebp
addl $0x3, %ebp
movzwl %bp, %r15d
movb $0x19, 0x7(%rbx,%r15)
movl $0x1, %edi
callq 0xee7e
movw %ax, 0x8(%rbx,%r15)
movb 0x116(%r14), %cl
movb %cl, 0xa(%rbx,%r15)
movzwl %ax, %edi
callq 0xee73
leal (%rax,%rbp), %r15d
addl $0x3, %r15d
cmpl $0x0, 0x48(%r14)
je 0xe7ea
cmpl $0x0, 0x1c49c(%rip)     # 0x2ac34
jne 0xe7ea
movzwl %r15w, %r12d
movl $0x1, %edi
callq 0xee7e
movw %ax, 0x8(%rbx,%r12)
movb $0x31, 0x7(%rbx,%r12)
movb 0x48(%r14), %cl
movb %cl, 0xa(%rbx,%r12)
cmpl $0x0, 0x1c470(%rip)     # 0x2ac34
je 0xe7db
cmpl $0x2, 0x1c46b(%rip)     # 0x2ac38
jne 0xe7db
movb $0x32, 0x7(%rbx,%r12)
movb $0x65, 0xa(%rbx,%r12)
movzwl %ax, %edi
callq 0xee73
addl %eax, %r15d
addl $0x3, %r15d
addl $0x4, %r15d
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq