// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x8(%rsp)
xorl %eax, %eax
callq 0x19f04
testl %eax, %eax
je 0x173e6
movl %eax, %ebx
movq 0x11c1f(%rip), %rax     # 0x28fb8
movq (%rax), %r14
callq 0xe343
leaq 0x9bfa(%rip), %rsi      # 0x20fa2
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x128ec(%rip), %rax     # 0x29ca8
movq (%rax), %r14
testq %r14, %r14
je 0x175cc
callq 0xe343
leaq 0x9bce(%rip), %rsi      # 0x20fa2
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
jmp 0x175cc
movl 0x48(%r14), %eax
movl %eax, 0x13b48(%rip)     # 0x2af38
leaq 0x9bcb(%rip), %rdi      # 0x20fc2
leaq 0x9bd5(%rip), %rsi      # 0x20fd3
xorl %edx, %edx
callq 0x19e06
leaq 0x9bce(%rip), %rdi      # 0x20fda
leaq 0x9bd7(%rip), %rsi      # 0x20fea
xorl %edx, %edx
callq 0x19ed7
leaq 0x9bd6(%rip), %rdi      # 0x20ff7
xorl %esi, %esi
callq 0x19d64
leaq 0x9bd3(%rip), %rdi      # 0x21002
xorl %esi, %esi
callq 0x19d64
leaq 0x9bd0(%rip), %rdi      # 0x2100d
xorl %esi, %esi
callq 0x19d64
leaq 0x9bce(%rip), %rdi      # 0x21019
leaq 0x9bda(%rip), %rsi      # 0x2102c
leaq 0x8(%rsp), %rbx
movq %rbx, %rdx
callq 0x19e06
testl %eax, %eax
setne %al
movq (%rbx), %rdi
testq %rdi, %rdi
sete %cl
orb %al, %cl
je 0x1747f
movb $0x1, 0x13ac4(%rip)     # 0x2af3c
testq %rdi, %rdi
jne 0x17492
jmp 0x1749d
cmpq $0x0, 0x8(%rdi)
je 0x1748b
cmpl $0x0, (%rdi)
jne 0x17492
movb $0x1, 0x13aaa(%rip)     # 0x2af3c
callq 0x19d1e
andq $0x0, 0x8(%rsp)
leaq 0x9b98(%rip), %rdi      # 0x2103c
leaq 0x9b28(%rip), %rsi      # 0x20fd3
leaq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0x19e06
movl %eax, %ebx
testl %eax, %eax
setne %al
movq (%r14), %rdi
testq %rdi, %rdi
sete %cl
orb %al, %cl
je 0x174da
testq %rdi, %rdi
jne 0x175c7
jmp 0x175cc
cmpq $0x0, 0x8(%rdi)
je 0x175c7
cmpl $0x0, (%rdi)
je 0x175c7
movzbl 0x13a47(%rip), %ebp     # 0x2af3c
movq 0x10(%rdi), %rax
movq 0x8(%rax), %rdi
leaq 0x9ce2(%rip), %rsi      # 0x211e6
leaq 0x14(%rsp), %rcx
xorl %edx, %edx
xorl %eax, %eax
callq 0x1a825
movq 0x8(%rsp), %rdi
cmpl $0x2, %eax
jne 0x174cc
cmpl %ebp, 0x14(%rsp)
je 0x174cc
testq %rdi, %rdi
je 0x17532
callq 0x19d1e
andq $0x0, 0x8(%rsp)
leaq 0x9b11(%rip), %rsi      # 0x2104a
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x53c0
movq (%r14), %rdi
leaq 0x8(%rsp), %rsi
callq 0x19d64
movl %eax, %ebp
movq (%r14), %rdi
testq %rdi, %rdi
je 0x1756c
callq 0x5040
andq $0x0, 0x18(%rsp)
testl %ebp, %ebp
setne %al
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
sete %cl
orb %al, %cl
jne 0x175b0
cmpq $0x0, 0x8(%rdi)
je 0x175ab
cmpl $0x0, (%rdi)
je 0x175ab
leaq 0x9ac8(%rip), %rdi      # 0x2105b
xorl %esi, %esi
callq 0x19d64
testl %eax, %eax
jne 0x175ab
leaq 0x11e2f(%rip), %rax     # 0x293d4
movl $0x1, (%rax)
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x175c0
callq 0x19d1e
andq $0x0, 0x8(%rsp)
xorl %edi, %edi
jmp 0x174cc
callq 0x19d1e
movl %ebx, %eax
addq $0x20, %rsp
popq %rbx
popq %r14
popq %rbp
retq