// File: /workspace/github_c_makefile_star2_O1/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, 0x1c5aa(%rip)     # 0x2ac34
je 0xe6b6
testq %r15, %r15
je 0xe697
cmpb $0x0, (%r15)
jne 0xe6b6
testq %r12, %r12
je 0xe6a3
cmpb $0x0, (%r12)
jne 0xe6b6
movl $0x2, %eax
leaq 0x12559(%rip), %r12     # 0x20c08
leaq 0x12542(%rip), %r15     # 0x20bf8
movl %eax, 0x4(%rsp)
movw $0x0, 0x5(%rbx)
movb $0x30, 0x7(%rbx)
movl $0x1, %edi
callq 0xef6c
movw %ax, 0x8(%rbx)
cmpl $0x1, 0x1c55b(%rip)     # 0x2ac34
movb $0x2, %cl
sbbb $0x0, %cl
movb %cl, 0xa(%rbx)
movzwl %ax, %edi
callq 0xef61
movl %eax, %r13d
leal 0x3(%r13), %ebp
movq (%r14), %rdi
testq %rdi, %rdi
je 0xe754
cmpl $0x0, 0x1c535(%rip)     # 0x2ac34
jne 0xe754
movzwl %bp, %ebp
movb $0x14, 0x7(%rbx,%rbp)
callq 0x5160
movzwl %ax, %edi
callq 0xef6c
movw %ax, 0x8(%rbx,%rbp)
movq (%r14), %rax
movb (%rax), %cl
movb %cl, 0xa(%rbx,%rbp)
testb %cl, %cl
je 0xe743
leaq (%rbx,%rbp), %rcx
addq $0xb, %rcx
xorl %edx, %edx
movb 0x1(%rax,%rdx), %sil
movb %sil, (%rcx,%rdx)
incq %rdx
testb %sil, %sil
jne 0xe732
movzwl 0x8(%rbx,%rbp), %edi
callq 0xef61
leal (%rax,%r13), %ebp
addl $0x6, %ebp
testq %r15, %r15
je 0xe7ae
movzwl %bp, %r13d
movb $0x17, 0x7(%rbx,%r13)
movq %r15, %rdi
callq 0x5160
movzwl %ax, %edi
callq 0xef6c
movw %ax, 0x8(%rbx,%r13)
movb (%r15), %al
movb %al, 0xa(%rbx,%r13)
testb %al, %al
je 0xe79e
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 0xe78f
movzwl 0x8(%rbx,%r13), %edi
callq 0xef61
addl %eax, %ebp
addl $0x3, %ebp
testq %r12, %r12
je 0xe809
movzwl %bp, %r13d
movb $0x18, 0x7(%rbx,%r13)
movq %r12, %rdi
callq 0x5160
movzwl %ax, %edi
callq 0xef6c
movw %ax, 0x8(%rbx,%r13)
movb (%r12), %al
movb %al, 0xa(%rbx,%r13)
testb %al, %al
je 0xe7f9
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 0xe7ea
movzwl 0x8(%rbx,%r13), %edi
callq 0xef61
addl %eax, %ebp
addl $0x3, %ebp
testq %r12, %r12
sete %al
testq %r15, %r15
sete %cl
orb %al, %cl
jne 0xe849
movzwl %bp, %r15d
movb $0x16, 0x7(%rbx,%r15)
movl $0x1, %edi
callq 0xef6c
movw %ax, 0x8(%rbx,%r15)
movl 0x4(%rsp), %ecx
movb %cl, 0xa(%rbx,%r15)
movzwl %ax, %edi
callq 0xef61
addl %eax, %ebp
addl $0x3, %ebp
movzwl %bp, %r15d
movb $0x19, 0x7(%rbx,%r15)
movl $0x1, %edi
callq 0xef6c
movw %ax, 0x8(%rbx,%r15)
movb 0x116(%r14), %cl
movb %cl, 0xa(%rbx,%r15)
movzwl %ax, %edi
callq 0xef61
leal (%rax,%rbp), %r15d
addl $0x3, %r15d
cmpl $0x0, 0x48(%r14)
je 0xe8df
cmpl $0x0, 0x1c3a7(%rip)     # 0x2ac34
jne 0xe8df
movzwl %r15w, %r12d
movl $0x1, %edi
callq 0xef6c
movw %ax, 0x8(%rbx,%r12)
movb $0x31, 0x7(%rbx,%r12)
movb 0x48(%r14), %cl
movb %cl, 0xa(%rbx,%r12)
cmpl $0x0, 0x1c37b(%rip)     # 0x2ac34
je 0xe8d0
cmpl $0x2, 0x1c376(%rip)     # 0x2ac38
jne 0xe8d0
movb $0x32, 0x7(%rbx,%r12)
movb $0x65, 0xa(%rbx,%r12)
movzwl %ax, %edi
callq 0xef61
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