// File: /workspace/github_c_makefile_star2_O3/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 0x1b0c0
testl %eax, %eax
je 0x18509
movl %eax, %ebx
movq 0x11b04(%rip), %rax     # 0x29fc0
movq (%rax), %r14
callq 0xea3b
leaq 0xaab7(%rip), %rsi      # 0x22f82
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x127c9(%rip), %rax     # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0x186fb
callq 0xea3b
leaq 0xaa8b(%rip), %rsi      # 0x22f82
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
jmp 0x186fb
movl 0x48(%r14), %eax
movl %eax, 0x13a25(%rip)     # 0x2bf38
leaq 0xaa88(%rip), %rdi      # 0x22fa2
leaq 0xaa92(%rip), %rsi      # 0x22fb3
xorl %edx, %edx
callq 0x1afa9
leaq 0xaa8b(%rip), %rdi      # 0x22fba
leaq 0xaa94(%rip), %rsi      # 0x22fca
xorl %edx, %edx
callq 0x1b08f
leaq 0xaa93(%rip), %rdi      # 0x22fd7
xorl %esi, %esi
callq 0x1aefe
leaq 0xaa90(%rip), %rdi      # 0x22fe2
xorl %esi, %esi
callq 0x1aefe
leaq 0xaa8d(%rip), %rdi      # 0x22fed
xorl %esi, %esi
callq 0x1aefe
leaq 0xaa8b(%rip), %rdi      # 0x22ff9
leaq 0xaa97(%rip), %rsi      # 0x2300c
leaq 0x8(%rsp), %rbx
movq %rbx, %rdx
callq 0x1afa9
testl %eax, %eax
setne %al
movq (%rbx), %rdi
testq %rdi, %rdi
sete %cl
orb %al, %cl
je 0x185a2
movb $0x1, 0x139a1(%rip)     # 0x2bf3c
testq %rdi, %rdi
jne 0x185b5
jmp 0x185c3
cmpq $0x0, 0x8(%rdi)
je 0x185ae
cmpl $0x0, (%rdi)
jne 0x185b5
movb $0x1, 0x13987(%rip)     # 0x2bf3c
callq 0x1aeb5
movq $0x0, 0x8(%rsp)
leaq 0xaa52(%rip), %rdi      # 0x2301c
leaq 0xa9e2(%rip), %rsi      # 0x22fb3
leaq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0x1afa9
movl %eax, %ebx
testl %eax, %eax
setne %al
movq (%r14), %rdi
testq %rdi, %rdi
sete %cl
orb %al, %cl
je 0x18600
testq %rdi, %rdi
jne 0x186f6
jmp 0x186fb
cmpq $0x0, 0x8(%rdi)
je 0x186f6
cmpl $0x0, (%rdi)
je 0x186f6
movzbl 0x13921(%rip), %ebp     # 0x2bf3c
movq 0x10(%rdi), %rax
movq 0x8(%rax), %rdi
leaq 0xab9c(%rip), %rsi      # 0x231c6
leaq 0x14(%rsp), %rcx
xorl %edx, %edx
xorl %eax, %eax
callq 0x1bb39
movq 0x8(%rsp), %rdi
cmpl $0x2, %eax
jne 0x185f2
cmpl %ebp, 0x14(%rsp)
je 0x185f2
testq %rdi, %rdi
je 0x1865b
callq 0x1aeb5
movq $0x0, 0x8(%rsp)
leaq 0xa9c8(%rip), %rsi      # 0x2302a
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x53b0
movq (%r14), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1aefe
movl %eax, %ebp
movq (%r14), %rdi
testq %rdi, %rdi
je 0x18698
callq 0x5040
movq $0x0, 0x18(%rsp)
testl %ebp, %ebp
setne %al
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
sete %cl
orb %al, %cl
jne 0x186dc
cmpq $0x0, 0x8(%rdi)
je 0x186d7
cmpl $0x0, (%rdi)
je 0x186d7
leaq 0xa97c(%rip), %rdi      # 0x2303b
xorl %esi, %esi
callq 0x1aefe
testl %eax, %eax
jne 0x186d7
leaq 0x11d03(%rip), %rax     # 0x2a3d4
movl $0x1, (%rax)
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x186ef
callq 0x1aeb5
movq $0x0, 0x8(%rsp)
xorl %edi, %edi
jmp 0x185f2
callq 0x1aeb5
movl %ebx, %eax
addq $0x20, %rsp
popq %rbx
popq %r14
popq %rbp
retq