// File: /workspace/github_c_makefile_star2_O3/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
leaq 0x8(%rsp), %r14
movq %rax, (%r14)
leaq 0x20(%rsp), %r15
movq %rax, (%r15)
movl $0x1, %eax
movl %eax, (%rsp)
leaq 0x5e6c(%rip), %rcx      # 0x1eea1
movq %rcx, 0x18(%rsp)
movq %rcx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movl $0x0, 0x14(%rsp)
cmpb $0x1, 0x50(%rdi)
movl $0x3, %ecx
sbbl $0x0, %ecx
cmpb $0x0, 0x51(%rdi)
cmovel %eax, %ecx
movl %ecx, 0x1c(%rdi)
leaq 0xa0c2(%rip), %r12      # 0x2312b
leaq 0xa0c8(%rip), %r13      # 0x23138
movl 0x48(%rbx), %edx
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x53b0
movq 0x20(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x1afa9
movl %eax, %ebp
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x190aa
callq 0x5040
movq $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rdi
cmpl $-0x6, %ebp
jne 0x19124
testq %rdi, %rdi
jne 0x19124
movl 0x1c(%rbx), %ecx
movl 0x48(%rbx), %edx
movq %r15, %rdi
leaq 0xa078(%rip), %rsi      # 0x23141
xorl %eax, %eax
callq 0x53b0
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x1aefe
movl %eax, %ebp
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x190f7
callq 0x5040
movq $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rdi
testl %ebp, %ebp
jne 0x19124
testq %rdi, %rdi
je 0x19124
cmpq $0x0, 0x8(%rdi)
je 0x1912b
cmpl $0x0, (%rdi)
je 0x1912b
callq 0x1aeb5
movq $0x0, 0x8(%rsp)
jmp 0x19070
testl %ebp, %ebp
setne %al
jmp 0x1912d
xorl %eax, %eax
testq %rdi, %rdi
sete %cl
orb %al, %cl
je 0x1914a
movq $0x0, 0x38(%rsp)
testq %rdi, %rdi
jne 0x191be
jmp 0x191cc
cmpq $0x0, 0x8(%rdi)
je 0x191b5
cmpl $0x0, (%rdi)
je 0x191b5
movq 0x10(%rdi), %rax
movq 0x8(%rax), %rdi
callq 0x1bb1c
movl $0x5, %ecx
cmpl $0x5, %eax
cmovll %eax, %ecx
movl %ecx, 0x12dcc(%rip)     # 0x2bf40
jl 0x193cd
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x14(%rsp), %r10
leaq 0x28(%rsp), %r11
leaq 0x9fc4(%rip), %rsi      # 0x2315c
leaq 0x4(%rsp), %rdx
movq %rsp, %rcx
leaq 0x18(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
pushq %r10
pushq %r11
jmp 0x19410
movq $0x0, 0x38(%rsp)
callq 0x1aeb5
movq $0x0, 0x8(%rsp)
leaq 0x9f96(%rip), %rdi      # 0x23169
leaq 0x9f9b(%rip), %rsi      # 0x23175
leaq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0x1b08f
testl %eax, %eax
setne %cl
movq (%r14), %rax
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x19263
cmpq $0x0, 0x8(%rax)
je 0x19263
cmpl $0x0, (%rax)
je 0x19263
movq 0x10(%rax), %rbp
testq %rbp, %rbp
je 0x1925f
leaq 0x9f6a(%rip), %r14      # 0x2317f
leaq 0x4(%rsp), %r15
leaq 0x38(%rsp), %r12
leaq 0x18(%rsp), %r13
movq 0x8(%rbp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x1bb39
cmpl $0x3, %eax
jne 0x1924d
movl 0x4(%rsp), %eax
cmpl 0x48(%rbx), %eax
je 0x19441
movq $0x0, 0x18(%rsp)
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x19224
xorl %edx, %edx
jmp 0x19268
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
jne 0x1927e
leaq 0x5c28(%rip), %rsi      # 0x1eea1
movq %rsi, 0x18(%rsp)
movq 0x30(%rsp), %r12
testq %r12, %r12
jne 0x19294
leaq 0x5c12(%rip), %r12      # 0x1eea1
movq %r12, 0x30(%rsp)
movl %edx, 0x44(%rsp)
movq 0x28(%rsp), %r13
testq %r13, %r13
jne 0x192ae
leaq 0x5bf8(%rip), %r13      # 0x1eea1
movq %r13, 0x28(%rsp)
leaq 0x256(%rbx), %r14
movl $0x40, %edx
movq %r14, %rdi
callq 0x5080
leaq 0x296(%rbx), %r15
movl $0x40, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x5080
leaq 0x2d6(%rbx), %r12
movl $0x40, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x5080
movl 0x14(%rsp), %eax
movl %eax, 0x316(%rbx)
movl (%rsp), %eax
movl %eax, 0x31a(%rbx)
movq 0x10cb6(%rip), %rax     # 0x29fc0
movq (%rax), %r13
callq 0xea3b
movl 0x48(%rbx), %r8d
movl 0x316(%rbx), %r10d
movslq 0x31a(%rbx), %r11
leaq 0x10a05(%rip), %rbp     # 0x29d30
leaq 0x7696(%rip), %rsi      # 0x209c8
leaq 0x76e0(%rip), %rcx      # 0x20a19
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 0x11949(%rip), %rax     # 0x2aca8
movq (%rax), %r13
testq %r13, %r13
je 0x193ab
callq 0xea3b
movl 0x48(%rbx), %r8d
movl 0x316(%rbx), %r10d
movslq 0x31a(%rbx), %r11
leaq 0x7643(%rip), %rsi      # 0x209c8
leaq 0x768d(%rip), %rcx      # 0x20a19
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 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x193ba
callq 0x1aeb5
movl 0x44(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0x4, %eax
jne 0x19268
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rdi
subq $0x8, %rsp
leaq 0x1c(%rsp), %r10
leaq 0x9d66(%rip), %rsi      # 0x2315e
leaq 0xc(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
pushq %r10
callq 0x1bb39
addq $0x10, %rsp
movl 0x12b21(%rip), %ecx     # 0x2bf40
incl %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x19268
movl 0x4(%rsp), %eax
xorl %edx, %edx
cmpl 0x48(%rbx), %eax
setne %dl
negl %edx
jmp 0x19268
movq 0x38(%rsp), %r14
leaq 0x7574(%rip), %rsi      # 0x209c1
movq %r14, %rdi
callq 0x5050
testl %eax, %eax
je 0x19478
leaq 0x755c(%rip), %rsi      # 0x209bc
movq %r14, %rdi
callq 0x5050
testl %eax, %eax
je 0x19484
movl $0x1, (%rsp)
jmp 0x1925f
movl $0x3, (%rsp)
jmp 0x1925f
movl $0x2, (%rsp)
jmp 0x1925f