// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x18(%rbp)
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rax
movb $0x0, (%rax)
leaq 0xbc2a(%rip), %rdi      # 0x30406
leaq 0xbc58(%rip), %rsi      # 0x3043b
leaq -0x18(%rbp), %rdx
callq 0x27030
movl %eax, -0x10(%rbp)
cmpl $0x0, -0x10(%rbp)
jne 0x24810
cmpq $0x0, -0x18(%rbp)
je 0x24810
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x24810
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x24815
jmp 0x24a96
movl $0x0, -0xc(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x2486b
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xbbfd(%rip), %rsi      # 0x30442
xorl %eax, %eax
movl %eax, %r8d
leaq -0x28(%rbp), %r9
movq %r8, %rdx
movq %r8, %rcx
movb $0x0, %al
callq 0x28860
movl %eax, -0x10(%rbp)
cmpl $0x4, -0x10(%rbp)
je 0x24869
jmp 0x24a96
jmp 0x2486b
cmpq $0x0, -0x18(%rbp)
je 0x24883
movq -0x18(%rbp), %rdi
callq 0x26d20
movq $0x0, -0x18(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x4c(%rbp)
subl $0x2, %eax
je 0x248be
jmp 0x24890
movl -0x4c(%rbp), %eax
addl $-0x4, %eax
subl $0x3, %eax
jb 0x248be
jmp 0x2489d
movl -0x4c(%rbp), %eax
subl $0x7, %eax
je 0x248da
jmp 0x248a7
movl -0x4c(%rbp), %eax
addl $-0xa, %eax
subl $0x3, %eax
jb 0x248f6
jmp 0x248b4
movl -0x4c(%rbp), %eax
subl $0xd, %eax
je 0x248da
jmp 0x24912
leaq 0xbb86(%rip), %rdi      # 0x3044b
leaq 0xba1d(%rip), %rsi      # 0x302e9
leaq -0x18(%rbp), %rdx
callq 0x26e80
movl %eax, -0x10(%rbp)
jmp 0x24917
leaq 0xbb74(%rip), %rdi      # 0x30455
leaq 0xba09(%rip), %rsi      # 0x302f1
leaq -0x18(%rbp), %rdx
callq 0x26e80
movl %eax, -0x10(%rbp)
jmp 0x24917
leaq 0xbb62(%rip), %rdi      # 0x3045f
leaq 0xb9f5(%rip), %rsi      # 0x302f9
leaq -0x18(%rbp), %rdx
callq 0x26e80
movl %eax, -0x10(%rbp)
jmp 0x24917
jmp 0x24a96
cmpl $0x0, -0x10(%rbp)
jne 0x24938
cmpq $0x0, -0x18(%rbp)
je 0x24938
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x24938
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x2493d
jmp 0x24a96
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x24957
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, 0x8(%rax)
jne 0x2495c
jmp 0x24a96
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x287f0
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq %rax, -0x58(%rbp)
subq $0x7, %rax
ja 0x24a7f
movq -0x58(%rbp), %rax
leaq 0xb84c(%rip), %rcx      # 0x301e0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x48(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x28550
movl %eax, -0x10(%rbp)
cmpl $0x0, -0x10(%rbp)
jge 0x249b8
jmp 0x24a96
jmp 0x24a81
movq -0x48(%rbp), %rdi
leaq 0xbd0b(%rip), %rsi      # 0x306d3
xorl %eax, %eax
movl %eax, %edx
leaq -0x2c(%rbp), %rcx
movb $0x0, %al
callq 0x28860
movl %eax, -0x10(%rbp)
cmpl $0x2, -0x10(%rbp)
je 0x249e5
jmp 0x24a96
jmp 0x24a81
movq -0x48(%rbp), %rdi
leaq 0xba77(%rip), %rsi      # 0x3046c
xorl %eax, %eax
movl %eax, %edx
leaq -0x2c(%rbp), %rcx
leaq -0x30(%rbp), %r8
leaq -0x34(%rbp), %r9
movb $0x0, %al
callq 0x28860
movl %eax, -0x10(%rbp)
cmpl $0x3, -0x10(%rbp)
je 0x24a17
jmp 0x24a96
jmp 0x24a81
movq -0x48(%rbp), %rdi
leaq 0xba46(%rip), %rsi      # 0x3046a
xorl %eax, %eax
movl %eax, %edx
leaq -0x2c(%rbp), %rcx
leaq -0x30(%rbp), %r8
leaq -0x34(%rbp), %r9
movb $0x0, %al
callq 0x28860
movl %eax, -0x10(%rbp)
cmpl $0x4, -0x10(%rbp)
je 0x24a46
jmp 0x24a96
jmp 0x24a81
movq -0x48(%rbp), %rdi
leaq 0xba20(%rip), %rsi      # 0x30473
xorl %eax, %eax
movl %eax, %edx
leaq -0x2c(%rbp), %rcx
leaq -0x30(%rbp), %r8
leaq -0x34(%rbp), %r9
leaq -0x38(%rbp), %rax
movq %rax, (%rsp)
movb $0x0, %al
callq 0x28860
movl %eax, -0x10(%rbp)
cmpl $0x5, -0x10(%rbp)
je 0x24a7d
jmp 0x24a96
jmp 0x24a81
jmp 0x24a96
cmpl $0x1, -0x2c(%rbp)
je 0x24a8d
cmpl $0x5, -0x2c(%rbp)
jne 0x24a94
movq -0x8(%rbp), %rax
movb $0x1, (%rax)
jmp 0x24a96
cmpq $0x0, -0x18(%rbp)
je 0x24aae
movq -0x18(%rbp), %rdi
callq 0x26d20
movq $0x0, -0x18(%rbp)
movl -0xc(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)