// File: /workspace/github_c_makefile_star2_O1/Carton32[P]quectel-CM/atc.c
// Function: atc_init
static int atc_init(PROFILE_T *profile) {
    int err;
    char *cmd;
    ATResponse *p_response = NULL;


    err = at_handshake();
    if (err) {
        dbg_time("handshake fail, TODO ... ");
        goto exit;
    }
	
    s_pdp = profile->pdp;
    at_send_command_singleline("AT+QCFG=\"usbnet\"", "+QCFG:", NULL);
    at_send_command_multiline("AT+QNETDEVCTL=?", "+QNETDEVCTL:", NULL);
    at_send_command("AT+CGREG=2", NULL); //GPRS Network Registration Status
    at_send_command("AT+CEREG=2", NULL); //EPS Network Registration Status
    at_send_command("AT+C5GREG=2", NULL); //5GS Network Registration Status

    err = at_send_command_singleline("AT+QNETDEVSTATUS=?", "+QNETDEVSTATUS:", &p_response);
    if (at_response_error(err, p_response))
        asr_style_atc = 1; //EC200T/EC100Y do not support this AT, but RG801/RG500U support 
    safe_at_response_free(p_response);

    err = at_send_command_singleline("AT+QCFG=\"NAT\"", "+QCFG:", &p_response);
    if (!at_response_error(err, p_response)) {
        int old_nat, new_nat = asr_style_atc ? 1 : 0;

        err = at_tok_scanf(p_response->p_intermediates->line, "%s%d", NULL, &old_nat);
        if (err == 2 && old_nat != new_nat) {
            safe_at_response_free(p_response);
            asprintf(&cmd, "AT+QCFG=\"NAT\",%d", new_nat);
            err = at_send_command(cmd, &p_response);
            safe_free(cmd);
            if (!at_response_error(err, p_response)) {
                err = at_send_command("at+cfun=1,1",NULL);
                if (!err)
                    g_donot_exit_when_modem_hangup = 1;
                //reboot to take effect
            }
            safe_at_response_free(p_response);
        }
        err = 0;
    }
    safe_at_response_free(p_response);

exit:
    return err;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x20, %rsp
movq %rdi, %r14
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
callq 0x1b4c9
testl %eax, %eax
je 0x18731
movl %eax, %ebx
movq 0x118dc(%rip), %rax     # 0x29fc0
movq (%rax), %r14
callq 0xeb2f
leaq 0xa86f(%rip), %rsi      # 0x22f62
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x125a1(%rip), %rax     # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0x18928
callq 0xeb2f
leaq 0xa843(%rip), %rsi      # 0x22f62
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
jmp 0x18928
movl 0x48(%r14), %eax
movl %eax, 0x137fd(%rip)     # 0x2bf38
leaq 0xa840(%rip), %rdi      # 0x22f82
leaq 0xa84a(%rip), %rsi      # 0x22f93
xorl %edx, %edx
callq 0x1b3c6
leaq 0xa843(%rip), %rdi      # 0x22f9a
leaq 0xa84c(%rip), %rsi      # 0x22faa
xorl %edx, %edx
callq 0x1b498
leaq 0xa84b(%rip), %rdi      # 0x22fb7
xorl %esi, %esi
callq 0x1b323
leaq 0xa848(%rip), %rdi      # 0x22fc2
xorl %esi, %esi
callq 0x1b323
leaq 0xa845(%rip), %rdi      # 0x22fcd
xorl %esi, %esi
callq 0x1b323
leaq 0xa843(%rip), %rdi      # 0x22fd9
leaq 0xa84f(%rip), %rsi      # 0x22fec
leaq 0x8(%rsp), %rbx
movq %rbx, %rdx
callq 0x1b3c6
testl %eax, %eax
setne %al
movq (%rbx), %rdi
testq %rdi, %rdi
sete %cl
orb %al, %cl
jne 0x187c8
cmpq $0x0, 0x8(%rdi)
je 0x187c8
cmpl $0x0, (%rdi)
jne 0x187cf
movb $0x1, 0x1376d(%rip)     # 0x2bf3c
testq %rdi, %rdi
je 0x187e2
callq 0x1b2da
movq $0x0, 0x8(%rsp)
leaq 0xa813(%rip), %rdi      # 0x22ffc
leaq 0xa7a3(%rip), %rsi      # 0x22f93
leaq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0x1b3c6
movl %eax, %ebx
testl %eax, %eax
setne %cl
movq (%r14), %rax
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x18910
xorl %ebx, %ebx
cmpq $0x0, 0x8(%rax)
je 0x18910
cmpl $0x0, (%rax)
je 0x18910
movzbl 0x1370a(%rip), %ebp     # 0x2bf3c
movq 0x10(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0xa965(%rip), %rsi      # 0x231a6
xorl %ebx, %ebx
leaq 0x14(%rsp), %rcx
xorl %edx, %edx
xorl %eax, %eax
callq 0x1be91
cmpl $0x2, %eax
jne 0x18910
cmpl %ebp, 0x14(%rsp)
je 0x18910
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x1887c
callq 0x1b2da
movq $0x0, 0x8(%rsp)
leaq 0xa787(%rip), %rsi      # 0x2300a
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x53b0
movq (%r14), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b323
movl %eax, %ebp
movq (%r14), %rdi
testq %rdi, %rdi
je 0x188b9
callq 0x5040
movq $0x0, 0x18(%rsp)
testl %ebp, %ebp
setne %cl
movq 0x8(%rsp), %rax
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x188f8
cmpq $0x0, 0x8(%rax)
je 0x188f8
cmpl $0x0, (%rax)
je 0x188f8
leaq 0xa73b(%rip), %rdi      # 0x2301b
xorl %esi, %esi
callq 0x1b323
testl %eax, %eax
jne 0x188f8
leaq 0x11ae2(%rip), %rax     # 0x2a3d4
movl $0x1, (%rax)
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x18910
callq 0x1b2da
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x18928
callq 0x1b2da
movq $0x0, 0x8(%rsp)
movl %ebx, %eax
addq $0x20, %rsp
popq %rbx
popq %r14
popq %rbp
retq