// File: /workspace/github_c_makefile_star2_O2/Carton32[P]quectel-CM/atc.c
// Function: requestRegistrationState
static int requestRegistrationState(UCHAR *pPSAttachedState) {
    int retVal = -1;
    int err;
    ATResponse *p_response = NULL;
    ATLine *p_cur;
    int i;
    int cops_act = -1;
    int state = 0, lac = 0, cid = 0, act = 0;
    int commas;
    char *line;

    *pPSAttachedState = 0;

    err = at_send_command_multiline(
              "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
              "+COPS:", &p_response);
    if (at_response_error(err, p_response))
        goto error;

/*
AT< +COPS: 0,0,"CHINA MOBILE",13
AT< +COPS: 0,1,"CMCC",13
AT< +COPS: 0,2,"46000",13
AT< OK
*/
    retVal = 0;
    for (i = 0, p_cur = p_response->p_intermediates; p_cur != NULL; p_cur = p_cur->p_next, i++) {
        err = at_tok_scanf(p_cur->line, "%d%d%s%d", NULL, NULL, NULL, &cops_act);
        if (err != 4) goto error;

        break;
    }

    safe_at_response_free(p_response);
    switch (cops_act) {
        case 2: //UTRAN
        case 4: //UTRAN W/HSDPA
        case 5: //UTRAN W/HSUPA
        case 6: //UTRAN W/HSDPA and HSUPA
            //AT+CGREG  GPRS Network Registration Status
            err = at_send_command_singleline("AT+CGREG?", "+CGREG:", &p_response);
        break;

        case 7: //E-UTRAN
        case 13: //E-UTRAN-NR dual connectivity
            //AT+CEREG  EPS Network Registration Status
            err = at_send_command_singleline("AT+CEREG?", "+CEREG:", &p_response);
        break;

        case 10: //E-UTRAN connected to a 5GCN
        case 11: //NR connected to a 5GCN
        case 12: //NG-RAN
            //AT+C5GREG  5GS Network Registration Status
            err = at_send_command_singleline("AT+C5GREG?", "+C5GREG:", &p_response);
        break;

        default:
            goto error;
        break;
    }

    if (at_response_error(err, p_response))
        goto error;
    if (!p_response->p_intermediates || !p_response->p_intermediates->line) goto error;

    line = p_response->p_intermediates->line;
    commas = at_tok_count(line);
     switch (commas) {
        case 0: /* +CREG: <stat> */
            err = at_tok_nextint(&line, &state);
            if (err < 0) goto error;
            break;

        case 1: /* +CREG: <n>, <stat> */
            err = at_tok_scanf(line, "%d%d", NULL, &state);
            if (err != 2) goto error;
            break;

        case 2: /* +CREG: <stat>, <lac>, <cid> */
            err = at_tok_scanf(line, "%d%x%x", NULL, &state, &lac, &cid);
            if (err != 3) goto error;
            break;

        case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
            err = at_tok_scanf(line, "%d%d%x%x", NULL, &state, &lac, &cid);
            if (err != 4) goto error;
            break;

        case 4: //<n>, <stat>, <lac>, <cid>, <networkType> */
        case 5:
        case 6: 
        case 7:
            err = at_tok_scanf(line, "%d%d%x%x%d", NULL, &state, &lac, &cid, &act);
            if (err != 5) goto error;
        break;

        default:
            goto error;
    }

    //dbg_time("state=%d", state);

    if (state == 1 || state == 5) { //Registered, home network  / roaming
        *pPSAttachedState = 1;
    }

error:
    safe_at_response_free(p_response);
    return retVal;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x30, %rsp
leaq 0x10(%rsp), %r14
andq $0x0, (%r14)
orl $-0x1, 0x20(%rsp)
andl $0x0, 0xc(%rsp)
andl $0x0, 0x1c(%rsp)
movq %rdi, %rbx
andl $0x0, 0x18(%rsp)
andl $0x0, 0x24(%rsp)
movb $0x0, (%rdi)
leaq 0x8e99(%rip), %rdi      # 0x211a6
leaq 0x8ec7(%rip), %rsi      # 0x211db
movq %r14, %rdx
callq 0x19ed7
testl %eax, %eax
setne %al
movq (%r14), %rdi
testq %rdi, %rdi
sete %cl
pushq $-0x1
popq %r14
orb %al, %cl
jne 0x1854b
cmpq $0x0, 0x8(%rdi)
je 0x18546
cmpl $0x0, (%rdi)
je 0x18546
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0x18387
movq 0x8(%rax), %rdi
leaq 0x8e84(%rip), %rsi      # 0x211e2
xorl %r14d, %r14d
leaq 0x20(%rsp), %r9
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x1a825
cmpl $0x4, %eax
jne 0x18546
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x18392
callq 0x19d1e
andq $0x0, 0x10(%rsp)
xorl %r14d, %r14d
movl 0x20(%rsp), %eax
cmpl $0xd, %eax
ja 0x18546
pushq $0x74
popq %rcx
btl %eax, %ecx
jb 0x183c4
movl $0x1c00, %ecx           # imm = 0x1C00
btl %eax, %ecx
jae 0x183d4
leaq 0x8e44(%rip), %rdi      # 0x211ff
leaq 0x8cd7(%rip), %rsi      # 0x21099
jmp 0x183f0
leaq 0x8e20(%rip), %rdi      # 0x211eb
leaq 0x8cb7(%rip), %rsi      # 0x21089
jmp 0x183f0
movl $0x2080, %ecx           # imm = 0x2080
btl %eax, %ecx
jae 0x18546
leaq 0x8e0c(%rip), %rdi      # 0x211f5
leaq 0x8ca1(%rip), %rsi      # 0x21091
leaq 0x10(%rsp), %rdx
callq 0x19e06
testl %eax, %eax
setne %al
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
sete %cl
xorl %r14d, %r14d
orb %al, %cl
jne 0x1854b
cmpq $0x0, 0x8(%rdi)
je 0x18543
cmpl $0x0, (%rdi)
je 0x18543
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0x18543
movq 0x8(%rax), %r15
testq %r15, %r15
je 0x18543
movq %r15, 0x28(%rsp)
movq %r15, %rdi
callq 0x1a808
cmpl $0x7, %eax
ja 0x18543
movl %eax, %eax
leaq 0x8b1e(%rip), %rcx      # 0x20f80
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x24(%rsp), %rax
movq %rax, (%rsp)
leaq 0x8d98(%rip), %rsi      # 0x21213
xorl %r14d, %r14d
leaq 0xc(%rsp), %rcx
leaq 0x1c(%rsp), %r8
leaq 0x18(%rsp), %r9
movq %r15, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0x1a825
cmpl $0x5, %eax
je 0x18534
jmp 0x18546
leaq 0x8d5e(%rip), %rsi      # 0x2120c
xorl %r14d, %r14d
leaq 0xc(%rsp), %rcx
leaq 0x1c(%rsp), %r8
leaq 0x18(%rsp), %r9
movq %r15, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0x1a825
cmpl $0x3, %eax
je 0x18534
jmp 0x18546
leaq 0x8d30(%rip), %rsi      # 0x2120a
xorl %r14d, %r14d
leaq 0xc(%rsp), %rcx
leaq 0x1c(%rsp), %r8
leaq 0x18(%rsp), %r9
movq %r15, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0x1a825
cmpl $0x4, %eax
je 0x18534
jmp 0x18546
leaq 0x28(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x1a6ac
testl %eax, %eax
jns 0x18534
jmp 0x18543
leaq 0x8ee7(%rip), %rsi      # 0x21402
xorl %r14d, %r14d
leaq 0xc(%rsp), %rcx
movq %r15, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0x1a825
cmpl $0x2, %eax
jne 0x18546
movl 0xc(%rsp), %eax
andl $-0x5, %eax
cmpl $0x1, %eax
jne 0x18543
movb $0x1, (%rbx)
xorl %r14d, %r14d
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x18555
callq 0x19d1e
movl %r14d, %eax
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
retq