// File: /workspace/github_c_makefile_star2_O1/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 %r14
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
leaq 0x18(%rsp), %r14
movq $0x0, (%r14)
movl $0xffffffff, 0x30(%rsp) # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, 0x14(%rsp)
movl %eax, 0x24(%rsp)
movl %eax, 0x20(%rsp)
movl %eax, 0x34(%rsp)
movb $0x0, (%rdi)
leaq 0x9a04(%rip), %rdi      # 0x23166
leaq 0x9a32(%rip), %rsi      # 0x2319b
movq %r14, %rdx
callq 0x1b498
testl %eax, %eax
setne %cl
movq (%r14), %rax
testq %rax, %rax
sete %dl
orb %cl, %dl
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jne 0x199a1
cmpq $0x0, 0x8(%rax)
je 0x199a1
cmpl $0x0, (%rax)
je 0x199a1
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x197d4
movq 0x8(%rax), %rdi
leaq 0x99ed(%rip), %rsi      # 0x231a2
xorl %r14d, %r14d
leaq 0x30(%rsp), %r9
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x1be91
cmpl $0x4, %eax
jne 0x199a1
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x197ec
callq 0x1b2da
movq $0x0, 0x18(%rsp)
movl 0x30(%rsp), %eax
cmpl $0xd, %eax
ja 0x1999e
movl $0x74, %ecx
btl %eax, %ecx
jb 0x1981d
movl $0x1c00, %ecx           # imm = 0x1C00
btl %eax, %ecx
jae 0x1982d
leaq 0x99ab(%rip), %rdi      # 0x231bf
leaq 0x983e(%rip), %rsi      # 0x23059
jmp 0x19849
leaq 0x9987(%rip), %rdi      # 0x231ab
leaq 0x981e(%rip), %rsi      # 0x23049
jmp 0x19849
movl $0x2080, %ecx           # imm = 0x2080
btl %eax, %ecx
jae 0x1999e
leaq 0x9973(%rip), %rdi      # 0x231b5
leaq 0x9808(%rip), %rsi      # 0x23051
leaq 0x18(%rsp), %rdx
callq 0x1b3c6
testl %eax, %eax
setne %cl
movq 0x18(%rsp), %rax
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x1999e
cmpq $0x0, 0x8(%rax)
je 0x1999e
cmpl $0x0, (%rax)
je 0x1999e
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x1999e
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x1999e
movq %rdi, 0x28(%rsp)
callq 0x1be74
cmpl $0x7, %eax
ja 0x1999e
movl %eax, %eax
leaq 0x968b(%rip), %rcx      # 0x22f40
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x28(%rsp), %rdi
leaq 0x34(%rsp), %rax
movq %rax, (%rsp)
leaq 0x9900(%rip), %rsi      # 0x231d3
xorl %r14d, %r14d
leaq 0x14(%rsp), %rcx
leaq 0x24(%rsp), %r8
leaq 0x20(%rsp), %r9
xorl %edx, %edx
xorl %eax, %eax
callq 0x1be91
cmpl $0x5, %eax
je 0x1998f
jmp 0x199a1
movq 0x28(%rsp), %rdi
leaq 0x98c4(%rip), %rsi      # 0x231cc
xorl %r14d, %r14d
leaq 0x14(%rsp), %rcx
leaq 0x24(%rsp), %r8
leaq 0x20(%rsp), %r9
xorl %edx, %edx
xorl %eax, %eax
callq 0x1be91
cmpl $0x3, %eax
je 0x1998f
jmp 0x199a1
movq 0x28(%rsp), %rdi
leaq 0x9894(%rip), %rsi      # 0x231ca
xorl %r14d, %r14d
leaq 0x14(%rsp), %rcx
leaq 0x24(%rsp), %r8
leaq 0x20(%rsp), %r9
xorl %edx, %edx
xorl %eax, %eax
callq 0x1be91
cmpl $0x4, %eax
je 0x1998f
jmp 0x199a1
leaq 0x28(%rsp), %rdi
leaq 0x14(%rsp), %rsi
callq 0x1bcf4
testl %eax, %eax
jns 0x1998f
jmp 0x1999e
movq 0x28(%rsp), %rdi
leaq 0x9a49(%rip), %rsi      # 0x233c2
xorl %r14d, %r14d
leaq 0x14(%rsp), %rcx
xorl %edx, %edx
xorl %eax, %eax
callq 0x1be91
cmpl $0x2, %eax
jne 0x199a1
movl 0x14(%rsp), %eax
andl $-0x5, %eax
cmpl $0x1, %eax
jne 0x1999e
movb $0x1, (%rbx)
xorl %r14d, %r14d
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x199b9
callq 0x1b2da
movq $0x0, 0x18(%rsp)
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r14
retq