// File: /workspace/github_c_makefile_star2_O3/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
movq %rdi, %rbx
leaq 0x10(%rsp), %r14
movq $0x0, (%r14)
movl $0xffffffff, 0x20(%rsp) # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, 0xc(%rsp)
movl %eax, 0x1c(%rsp)
movl %eax, 0x18(%rsp)
movl %eax, 0x24(%rsp)
movb $0x0, (%rdi)
leaq 0x9cba(%rip), %rdi      # 0x23186
leaq 0x9ce8(%rip), %rsi      # 0x231bb
movq %r14, %rdx
callq 0x1b08f
testl %eax, %eax
setne %al
movq (%r14), %rdi
testq %rdi, %rdi
sete %cl
orb %al, %cl
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jne 0x19717
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpq $0x0, 0x8(%rdi)
je 0x19712
cmpl $0x0, (%rdi)
je 0x19712
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0x1954e
movq 0x8(%rax), %rdi
leaq 0x9c9d(%rip), %rsi      # 0x231c2
xorl %r14d, %r14d
leaq 0x20(%rsp), %r9
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x1bb39
cmpl $0x4, %eax
jne 0x19712
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1955c
callq 0x1aeb5
movq $0x0, 0x10(%rsp)
xorl %r14d, %r14d
movl 0x20(%rsp), %eax
cmpl $0xd, %eax
ja 0x19712
movl $0x74, %ecx
btl %eax, %ecx
jb 0x19590
movl $0x1c00, %ecx           # imm = 0x1C00
btl %eax, %ecx
jae 0x195a0
leaq 0x9c58(%rip), %rdi      # 0x231df
leaq 0x9aeb(%rip), %rsi      # 0x23079
jmp 0x195bc
leaq 0x9c34(%rip), %rdi      # 0x231cb
leaq 0x9acb(%rip), %rsi      # 0x23069
jmp 0x195bc
movl $0x2080, %ecx           # imm = 0x2080
btl %eax, %ecx
jae 0x19712
leaq 0x9c20(%rip), %rdi      # 0x231d5
leaq 0x9ab5(%rip), %rsi      # 0x23071
leaq 0x10(%rsp), %rdx
callq 0x1afa9
testl %eax, %eax
setne %al
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
sete %cl
xorl %r14d, %r14d
orb %al, %cl
jne 0x19717
cmpq $0x0, 0x8(%rdi)
je 0x1970f
cmpl $0x0, (%rdi)
je 0x1970f
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0x1970f
movq 0x8(%rax), %r15
testq %r15, %r15
je 0x1970f
movq %r15, 0x28(%rsp)
movq %r15, %rdi
callq 0x1bb1c
cmpl $0x7, %eax
ja 0x1970f
movl %eax, %eax
leaq 0x9932(%rip), %rcx      # 0x22f60
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x24(%rsp), %rax
movq %rax, (%rsp)
leaq 0x9bac(%rip), %rsi      # 0x231f3
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 0x1bb39
cmpl $0x5, %eax
je 0x19700
jmp 0x19712
leaq 0x9b72(%rip), %rsi      # 0x231ec
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 0x1bb39
cmpl $0x3, %eax
je 0x19700
jmp 0x19712
leaq 0x9b44(%rip), %rsi      # 0x231ea
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 0x1bb39
cmpl $0x4, %eax
je 0x19700
jmp 0x19712
leaq 0x28(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x1b9a4
testl %eax, %eax
jns 0x19700
jmp 0x1970f
leaq 0x9cfb(%rip), %rsi      # 0x233e2
xorl %r14d, %r14d
leaq 0xc(%rsp), %rcx
movq %r15, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0x1bb39
cmpl $0x2, %eax
jne 0x19712
movl 0xc(%rsp), %eax
andl $-0x5, %eax
cmpl $0x1, %eax
jne 0x1970f
movb $0x1, (%rbx)
xorl %r14d, %r14d
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x19721
callq 0x1aeb5
movl %r14d, %eax
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
retq