// File: /workspace/github_c_makefile_star2_O1/Carton32[P]quectel-CM/atc.c
// Function: requestGetProfile
static int requestGetProfile(PROFILE_T *profile) {
    int retVal = -1;
    int err;
    ATResponse *p_response = NULL;
    char *cmd = NULL;
    int pdp;
    int old_iptype = 1; // 1 ~ IPV4, 2 ~ IPV6, 3 ~ IPV4V6
    char *old_apn = "", *old_user = "", *old_password = "";
    int old_auth = 0;
    const char *ipStr[] = {"NULL", "IPV4", "IPV6", "IPV4V6"};

    if (profile->enable_ipv4 && profile->enable_ipv6)
        profile->iptype = 3;
    else if (profile->enable_ipv6)
        profile->iptype = 2;
    else
        profile->iptype = 1;

_re_check:
    asprintf(&cmd, "AT+QICSGP=%d", profile->pdp);
    err = at_send_command_singleline(cmd, "+QICSGP:", &p_response);
    safe_free(cmd);
    if (err == AT_ERROR_INVALID_RESPONSE && p_response == NULL) {
        //bug of RG801H
        safe_at_response_free(p_response);
        asprintf(&cmd, "AT+QICSGP=%d,%d,\"\",\"\",\"\",0", profile->pdp, profile->iptype);
        err = at_send_command(cmd, &p_response);
        safe_free(cmd);
        if (!at_response_error(err, p_response)) {
            safe_at_response_free(p_response);
            goto _re_check;
        }
    }

    if (!at_response_error(err, p_response)) {
        QICSGP_C = at_tok_count(p_response->p_intermediates->line);
        if (QICSGP_C > 5)
            QICSGP_C = 5;

        if (QICSGP_C == 5)
            err = at_tok_scanf(p_response->p_intermediates->line,
                "%d%d%s%s%s%d", &pdp, &old_iptype, &old_apn,  &old_user, &old_password, &old_auth);
        else if (QICSGP_C == 4)
            err = at_tok_scanf(p_response->p_intermediates->line,
                "%d%s%s%s%d", &pdp, &old_apn,  &old_user, &old_password, &old_auth);
        else
            goto _error;

        if (err != (QICSGP_C + 1) || pdp != profile->pdp)
            goto _error;
    }
    else {
        ATLine *atLine = NULL;
        char *cgdcont_iptype = NULL;

        safe_at_response_free(p_response);
        err = at_send_command_multiline("AT+CGDCONT?", "+CGDCONT:", &p_response);
        if (at_response_error(err, p_response))
            goto _error;

        atLine = p_response->p_intermediates;
        while (atLine) {
            err = at_tok_scanf(atLine->line, "%d%s%s", &pdp, &cgdcont_iptype, &old_apn);
            if (err == 3 && pdp == profile->pdp) {
                if (!strcasecmp(cgdcont_iptype, ipStr[3]))
                    old_iptype = 3;
                else if (!strcasecmp(cgdcont_iptype, ipStr[2]))
                     old_iptype = 2;
                else
                    old_iptype = 1;
               break;
            }
            old_apn = NULL;
            atLine = atLine->p_next;
        }
    }

    retVal = 0;

_error:
    if (!old_apn) old_apn = "";
    if (!old_user) old_user = "";
    if (!old_password) old_password = "";

    strncpy(profile->old_apn, old_apn, sizeof(profile->old_apn));
    strncpy(profile->old_user, old_user, sizeof(profile->old_user));
    strncpy(profile->old_password, old_password, sizeof(profile->old_password));
    profile->old_auth = old_auth;
    profile->old_iptype = old_iptype; 

    dbg_time("%s[%d] %s/%s/%s/%d/%s", __func__,
        profile->pdp, profile->old_apn, profile->old_user, profile->old_password,
        profile->old_auth, ipStr[profile->old_iptype]);

    safe_at_response_free(p_response);

    return retVal;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
xorl %eax, %eax
movq %rax, (%rsp)
movq %rax, 0x18(%rsp)
movl $0x1, 0x8(%rsp)
leaq 0x5beb(%rip), %rax      # 0x1eea5
movq %rax, 0x10(%rsp)
movq %rax, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
cmpb $0x0, 0x50(%rdi)
je 0x192e6
cmpb $0x0, 0x51(%rbx)
je 0x192e6
movl $0x3, 0x1c(%rbx)
jmp 0x192fc
cmpb $0x0, 0x51(%rbx)
je 0x192f5
movl $0x2, 0x1c(%rbx)
jmp 0x192fc
movl $0x1, 0x1c(%rbx)
leaq 0x18(%rsp), %r15
leaq 0x9e10(%rip), %r12      # 0x23118
movq %rsp, %r13
leaq 0x9e0f(%rip), %rbp      # 0x23121
movl 0x48(%rbx), %edx
movq %r15, %rdi
leaq 0x9dec(%rip), %rsi      # 0x2310b
xorl %eax, %eax
callq 0x53b0
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1b3c6
movl %eax, %r14d
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x19351
callq 0x5040
movq $0x0, 0x18(%rsp)
cmpl $-0x6, %r14d
jne 0x193c8
cmpq $0x0, (%rsp)
jne 0x193c8
movl 0x1c(%rbx), %ecx
movl 0x48(%rbx), %edx
movq %r15, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x53b0
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x1b323
movl %eax, %r14d
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x19399
callq 0x5040
movq $0x0, 0x18(%rsp)
testl %r14d, %r14d
jne 0x193c8
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x193c8
xorl %r14d, %r14d
cmpq $0x0, 0x8(%rdi)
je 0x193c8
cmpl $0x0, (%rdi)
je 0x193c8
callq 0x1b2da
movq $0x0, (%rsp)
jmp 0x19312
testl %r14d, %r14d
setne %al
movq (%rsp), %rdi
testq %rdi, %rdi
sete %cl
orb %al, %cl
jne 0x19448
cmpq $0x0, 0x8(%rdi)
je 0x19448
cmpl $0x0, (%rdi)
je 0x19448
movq 0x10(%rdi), %rax
movq 0x8(%rax), %rdi
callq 0x1be74
movl $0x5, %ecx
cmpl $0x5, %eax
cmovll %eax, %ecx
movl %ecx, 0x12b3a(%rip)     # 0x2bf40
jl 0x19513
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x24(%rsp), %r10
leaq 0x28(%rsp), %r11
leaq 0x9d13(%rip), %rsi      # 0x2313c
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
pushq %r10
pushq %r11
jmp 0x19551
movq $0x0, 0x40(%rsp)
testq %rdi, %rdi
je 0x19463
callq 0x1b2da
movq $0x0, (%rsp)
leaq 0x9cdf(%rip), %rdi      # 0x23149
leaq 0x9ce4(%rip), %rsi      # 0x23155
movq %rsp, %r14
movq %r14, %rdx
callq 0x1b498
testl %eax, %eax
setne %al
movq (%r14), %rcx
testq %rcx, %rcx
sete %dl
orb %al, %dl
movl $0x7, %edx
jne 0x194fb
cmpq $0x0, 0x8(%rcx)
je 0x194fb
cmpl $0x0, (%rcx)
je 0x194fb
movq 0x10(%rcx), %rbp
testq %rbp, %rbp
je 0x194f9
leaq 0x9cb0(%rip), %r14      # 0x2315f
leaq 0xc(%rsp), %r15
leaq 0x40(%rsp), %r12
leaq 0x10(%rsp), %r13
movq 0x8(%rbp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x1be91
cmpl $0x3, %eax
jne 0x194e7
movl 0xc(%rsp), %eax
cmpl 0x48(%rbx), %eax
je 0x196d6
movq $0x0, 0x10(%rsp)
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x194be
xorl %edx, %edx
cmpl $0x7, %edx
je 0x1950c
testl %edx, %edx
jne 0x196c7
xorl %edx, %edx
jmp 0x19574
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
jmp 0x19574
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0x4, %eax
jne 0x19574
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rdi
subq $0x8, %rsp
leaq 0x2c(%rsp), %r10
leaq 0x9c05(%rip), %rsi      # 0x2313e
leaq 0x14(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
pushq %r10
callq 0x1be91
addq $0x10, %rsp
movl 0x129e0(%rip), %ecx     # 0x2bf40
incl %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x19574
movl 0xc(%rsp), %eax
cmpl 0x48(%rbx), %eax
je 0x19508
cmpq $0x0, 0x10(%rsp)
leaq 0x5924(%rip), %rax      # 0x1eea5
jne 0x19588
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x19595
movq %rax, 0x30(%rsp)
movl %edx, 0x3c(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0x195a6
movq %rax, 0x28(%rsp)
leaq 0x256(%rbx), %r14
movq 0x10(%rsp), %rsi
movl $0x40, %edx
movq %r14, %rdi
callq 0x5080
leaq 0x296(%rbx), %r15
movq 0x30(%rsp), %rsi
movl $0x40, %edx
movq %r15, %rdi
callq 0x5080
leaq 0x2d6(%rbx), %r12
movq 0x28(%rsp), %rsi
movl $0x40, %edx
movq %r12, %rdi
callq 0x5080
movl 0x24(%rsp), %eax
movl %eax, 0x316(%rbx)
movl 0x8(%rsp), %eax
movl %eax, 0x31a(%rbx)
movq 0x109b4(%rip), %rax     # 0x29fc0
movq (%rax), %r13
callq 0xeb2f
movl 0x48(%rbx), %r8d
movl 0x316(%rbx), %r10d
movslq 0x31a(%rbx), %r11
leaq 0x10703(%rip), %rbp     # 0x29d30
leaq 0x7398(%rip), %rsi      # 0x209cc
leaq 0x73e2(%rip), %rcx      # 0x20a1d
movq %r13, %rdi
movq %rax, %rdx
movq %r14, %r9
xorl %eax, %eax
pushq (%rbp,%r11,8)
pushq %r10
pushq %r12
pushq %r15
callq 0x52c0
addq $0x20, %rsp
leaq 0x11647(%rip), %rax     # 0x2aca8
movq (%rax), %r13
testq %r13, %r13
je 0x196ad
callq 0xeb2f
movl 0x48(%rbx), %r8d
movl 0x316(%rbx), %r10d
movslq 0x31a(%rbx), %r11
leaq 0x7345(%rip), %rsi      # 0x209cc
leaq 0x738f(%rip), %rcx      # 0x20a1d
movq %r13, %rdi
movq %rax, %rdx
movq %r14, %r9
xorl %eax, %eax
pushq (%rbp,%r11,8)
pushq %r10
pushq %r12
pushq %r15
callq 0x52c0
addq $0x20, %rsp
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x196c3
callq 0x1b2da
movq $0x0, (%rsp)
movl 0x3c(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x40(%rsp), %r14
leaq 0x72e3(%rip), %rsi      # 0x209c5
movq %r14, %rdi
callq 0x5050
testl %eax, %eax
je 0x1970e
leaq 0x72cb(%rip), %rsi      # 0x209c0
movq %r14, %rdi
callq 0x5050
testl %eax, %eax
je 0x1971b
movl $0x1, 0x8(%rsp)
jmp 0x194f9
movl $0x3, 0x8(%rsp)
jmp 0x194f9
movl $0x2, 0x8(%rsp)
jmp 0x194f9