// File: /workspace/github_c_makefile_star2_O2/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
leaq 0x8(%rsp), %r14
andq $0x0, (%r14)
leaq 0x20(%rsp), %r15
andq $0x0, (%r15)
movq %rdi, %rbx
pushq $0x1
popq %rax
movl %eax, (%rsp)
leaq 0x4ffc(%rip), %rcx      # 0x1cea1
movq %rcx, 0x18(%rsp)
andl $0x0, 0x14(%rsp)
movq %rcx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
cmpb $0x1, 0x50(%rdi)
pushq $0x3
popq %rcx
sbbl $0x0, %ecx
cmpb $0x0, 0x51(%rdi)
cmovel %eax, %ecx
movl %ecx, 0x1c(%rdi)
leaq 0x9284(%rip), %r13      # 0x21158
leaq 0x9286(%rip), %rbp      # 0x21161
movl 0x48(%rbx), %edx
movq %r15, %rdi
leaq 0x9263(%rip), %rsi      # 0x2114b
xorl %eax, %eax
callq 0x53c0
movq 0x20(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x19e06
movl %eax, %r12d
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x17f17
callq 0x5040
andq $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rdi
cmpl $-0x6, %r12d
jne 0x17f8a
testq %rdi, %rdi
jne 0x17f8a
movl 0x1c(%rbx), %ecx
movl 0x48(%rbx), %edx
movq %r15, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x53c0
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x19d64
movl %eax, %r12d
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x17f5f
callq 0x5040
andq $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rdi
testl %r12d, %r12d
jne 0x17f8a
testq %rdi, %rdi
je 0x17f8a
cmpq $0x0, 0x8(%rdi)
je 0x17f92
cmpl $0x0, (%rdi)
je 0x17f92
callq 0x19d1e
andq $0x0, 0x8(%rsp)
jmp 0x17edb
testl %r12d, %r12d
setne %al
jmp 0x17f94
xorl %eax, %eax
testq %rdi, %rdi
sete %cl
orb %al, %cl
je 0x17fab
andq $0x0, 0x38(%rsp)
testq %rdi, %rdi
jne 0x1801d
jmp 0x18028
cmpq $0x0, 0x8(%rdi)
je 0x18017
cmpl $0x0, (%rdi)
je 0x18017
movq 0x10(%rdi), %rax
movq 0x8(%rax), %rdi
callq 0x1a808
pushq $0x5
popq %rcx
pushq $-0x1
popq %rbp
cmpl $0x5, %eax
cmovll %eax, %ecx
movl %ecx, 0x12f6a(%rip)     # 0x2af40
jl 0x1821f
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x14(%rsp), %r10
leaq 0x28(%rsp), %r11
leaq 0x9182(%rip), %rsi      # 0x2117c
leaq 0x4(%rsp), %rdx
movq %rsp, %rcx
leaq 0x18(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
pushq %r10
pushq %r11
jmp 0x1825d
andq $0x0, 0x38(%rsp)
callq 0x19d1e
andq $0x0, 0x8(%rsp)
leaq 0x915a(%rip), %rdi      # 0x21189
leaq 0x915f(%rip), %rsi      # 0x21195
leaq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0x19ed7
testl %eax, %eax
setne %al
movq (%r14), %rbp
testq %rbp, %rbp
sete %cl
orb %al, %cl
jne 0x180ba
cmpq $0x0, 0x8(%rbp)
je 0x180ba
cmpl $0x0, (%rbp)
je 0x180ba
addq $0x10, %rbp
leaq 0x9132(%rip), %r14      # 0x2119f
leaq 0x4(%rsp), %r15
leaq 0x38(%rsp), %r12
leaq 0x18(%rsp), %r13
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x182ce
movq 0x8(%rbp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x1a825
cmpl $0x3, %eax
jne 0x180b2
movl 0x4(%rsp), %eax
cmpl 0x48(%rbx), %eax
je 0x1828a
andq $0x0, 0x18(%rsp)
jmp 0x1807c
pushq $-0x1
popq %rbp
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
jne 0x180d3
leaq 0x4dd3(%rip), %rsi      # 0x1cea1
movq %rsi, 0x18(%rsp)
movq 0x30(%rsp), %r12
testq %r12, %r12
jne 0x180e9
leaq 0x4dbd(%rip), %r12      # 0x1cea1
movq %r12, 0x30(%rsp)
movl %ebp, 0x44(%rsp)
movq 0x28(%rsp), %r13
testq %r13, %r13
jne 0x18103
leaq 0x4da3(%rip), %r13      # 0x1cea1
movq %r13, 0x28(%rsp)
leaq 0x256(%rbx), %r15
pushq $0x40
popq %rbp
movq %r15, %rdi
movq %rbp, %rdx
callq 0x5080
leaq 0x296(%rbx), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x5080
leaq 0x2d6(%rbx), %r12
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x5080
movl 0x14(%rsp), %eax
movl %eax, 0x316(%rbx)
movl (%rsp), %eax
movl %eax, 0x31a(%rbx)
movq 0x10e5c(%rip), %rax     # 0x28fb8
movq (%rax), %r13
callq 0xe343
movl 0x48(%rbx), %r8d
movl 0x316(%rbx), %r10d
movslq 0x31a(%rbx), %r11
leaq 0x10c23(%rip), %rbp     # 0x28da0
leaq 0x6844(%rip), %rsi      # 0x1e9c8
leaq 0x688e(%rip), %rcx      # 0x1ea19
movq %r13, %rdi
movq %rax, %rdx
movq %r15, %r9
xorl %eax, %eax
pushq (%rbp,%r11,8)
pushq %r10
pushq %r12
pushq %r14
callq 0x52c0
addq $0x20, %rsp
leaq 0x11af7(%rip), %rax     # 0x29ca8
movq (%rax), %r13
testq %r13, %r13
je 0x181fd
callq 0xe343
movl 0x48(%rbx), %r8d
movl 0x316(%rbx), %r10d
movslq 0x31a(%rbx), %r11
leaq 0x67f1(%rip), %rsi      # 0x1e9c8
leaq 0x683b(%rip), %rcx      # 0x1ea19
movq %r13, %rdi
movq %rax, %rdx
movq %r15, %r9
xorl %eax, %eax
pushq (%rbp,%r11,8)
pushq %r10
pushq %r12
pushq %r14
callq 0x52c0
addq $0x20, %rsp
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x1820c
callq 0x19d1e
movl 0x44(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x4, %eax
jne 0x180bd
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rdi
subq $0x8, %rsp
leaq 0x1c(%rsp), %r10
leaq 0x8f39(%rip), %rsi      # 0x2117e
leaq 0xc(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
pushq %r10
callq 0x1a825
addq $0x10, %rsp
movl 0x12cd4(%rip), %ecx     # 0x2af40
incl %ecx
cmpl %ecx, %eax
jne 0x180bd
movl 0x4(%rsp), %eax
xorl %ebp, %ebp
cmpl 0x48(%rbx), %eax
setne %bpl
negl %ebp
jmp 0x180bd
movq 0x38(%rsp), %r14
leaq 0x672b(%rip), %rsi      # 0x1e9c1
movq %r14, %rdi
callq 0x5050
testl %eax, %eax
je 0x182be
leaq 0x6713(%rip), %rsi      # 0x1e9bc
movq %r14, %rdi
callq 0x5050
testl %eax, %eax
je 0x182c7
movl $0x1, (%rsp)
jmp 0x182ce
movl $0x3, (%rsp)
jmp 0x182ce
movl $0x2, (%rsp)
xorl %ebp, %ebp
jmp 0x180bd