// File: /workspace/github_c_makefile_star2_O3/Carton32[P]quectel-CM/QMIThread.c
// Function: requestGetSIMStatus
static int requestGetSIMStatus(SIM_Status *pSIMStatus) { //RIL_REQUEST_GET_SIM_STATUS
    PQCQMIMSG pRequest;
    PQCQMIMSG pResponse;
    PQMUX_MSG pMUXMsg;
    int err;
    const char * SIM_Status_String[] = {
        "SIM_ABSENT",
        "SIM_NOT_READY",
        "SIM_READY", /* SIM_READY means the radio state is RADIO_STATE_SIM_READY */
        "SIM_PIN",
        "SIM_PUK",
        "SIM_NETWORK_PERSONALIZATION"
    };

    if (s_9x07)
        pRequest = ComposeQMUXMsg(QMUX_TYPE_UIM, QMIUIM_GET_CARD_STATUS_REQ, NULL, NULL);
    else
        pRequest = ComposeQMUXMsg(QMUX_TYPE_DMS, QMIDMS_UIM_GET_STATE_REQ, NULL, NULL);

    err = QmiThreadSendQMI(pRequest, &pResponse);
    qmi_rsp_check_and_return();

    *pSIMStatus = SIM_ABSENT;
    if (s_9x07)
    {
        PQMIUIM_CARD_STATUS pCardStatus = NULL;
        PQMIUIM_PIN_STATE pPINState = NULL;
        UCHAR CardState = 0x01;
        UCHAR PIN1State = QMI_PIN_STATUS_NOT_VERIF;
        //UCHAR PIN1Retries;
        //UCHAR PUK1Retries;
        //UCHAR PIN2State;
        //UCHAR PIN2Retries;
        //UCHAR PUK2Retries;

        pCardStatus = (PQMIUIM_CARD_STATUS)GetTLV(&pResponse->MUXMsg.QMUXMsgHdr, 0x10);
        if (pCardStatus != NULL)
        {
            pPINState = (PQMIUIM_PIN_STATE)((PUCHAR)pCardStatus + sizeof(QMIUIM_CARD_STATUS) + pCardStatus->AIDLength);
            CardState  = pCardStatus->CardState;
            if (CardState == UIM_CARD_STATE_PRESENT) {
                if (pPINState->UnivPIN == 1)
                {
                   PIN1State = pCardStatus->UPINState;
                   //PIN1Retries = pCardStatus->UPINRetries;
                   //PUK1Retries = pCardStatus->UPUKRetries;
                }
                else
                {
                   PIN1State = pPINState->PIN1State;
                   //PIN1Retries = pPINState->PIN1Retries;
                   //PUK1Retries = pPINState->PUK1Retries;
                }
                //PIN2State = pPINState->PIN2State;
                //PIN2Retries = pPINState->PIN2Retries;
                //PUK2Retries = pPINState->PUK2Retries;
            }
        }

        *pSIMStatus = SIM_ABSENT;
        if ((CardState == 0x01) &&  ((PIN1State == QMI_PIN_STATUS_VERIFIED)|| (PIN1State == QMI_PIN_STATUS_DISABLED)))
        {
            *pSIMStatus = SIM_READY;
        }
        else if (CardState == 0x01)
        {
            if (PIN1State == QMI_PIN_STATUS_NOT_VERIF)
            {
                *pSIMStatus = SIM_PIN;
            }
            if ( PIN1State == QMI_PIN_STATUS_BLOCKED)
            {
                *pSIMStatus = SIM_PUK;
            }
            else if (PIN1State == QMI_PIN_STATUS_PERM_BLOCKED)
            {
                *pSIMStatus = SIM_BAD;
            }
            else if (PIN1State == QMI_PIN_STATUS_NOT_INIT || PIN1State == QMI_PIN_STATUS_VERIFIED || PIN1State == QMI_PIN_STATUS_DISABLED)
            {
                *pSIMStatus = SIM_READY;
            }
        }
        else if (CardState == 0x00 || CardState == 0x02)
        {
        }
        else
        {
        }
    }
    else
    {
    //UIM state. Values:
    // 0x00  UIM initialization completed
    // 0x01  UIM is locked or the UIM failed
    // 0x02  UIM is not present
    // 0x03  Reserved
    // 0xFF  UIM state is currently
    //unavailable
        if (pResponse->MUXMsg.UIMGetStateResp.UIMState == 0x00) {
            *pSIMStatus = SIM_READY;
        } else if (pResponse->MUXMsg.UIMGetStateResp.UIMState == 0x01) {
            *pSIMStatus = SIM_ABSENT;
            err = requestGetPINStatus(pSIMStatus);
        } else if ((pResponse->MUXMsg.UIMGetStateResp.UIMState == 0x02) || (pResponse->MUXMsg.UIMGetStateResp.UIMState == 0xFF)) {
            *pSIMStatus = SIM_ABSENT;
        } else {
            *pSIMStatus = SIM_ABSENT;
        }
    }
    dbg_time("%s SIMStatus: %s", __func__, SIM_Status_String[*pSIMStatus]);

    free(pResponse);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
cmpl $0x0, 0x1f180(%rip)     # 0x2a2f8
je 0xb186
movl $0xb, %edi
movl $0x2f, %esi
jmp 0xb190
movl $0x2, %edi
movl $0x44, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xd9a2
leaq 0x157c0(%rip), %rcx     # 0x20960
leaq 0x8(%rsp), %r14
movq %rax, %rdi
movq %r14, %rsi
movl $0x7530, %edx           # imm = 0x7530
callq 0xa273
movl %eax, %ebp
testl %eax, %eax
sets %al
movq (%r14), %r12
testq %r12, %r12
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0xb234
movq 0x1eded(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xea3b
leaq 0x1559f(%rip), %rsi     # 0x20781
leaq 0x15777(%rip), %rcx     # 0x20960
movq %rbx, %rdi
movq %rax, %rdx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x52c0
leaq 0x1faa8(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0xb2f5
callq 0xea3b
leaq 0x15569(%rip), %rsi     # 0x20781
leaq 0x15741(%rip), %rcx     # 0x20960
movq %rbx, %rdi
movq %rax, %rdx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x52c0
jmp 0xb2f5
movzwl 0x10(%r12), %edi
callq 0xee73
testw %ax, %ax
jne 0xb258
movzwl 0x12(%r12), %edi
callq 0xee73
testw %ax, %ax
je 0xb304
movzwl 0x12(%r12), %edi
callq 0xee73
movl %eax, %ebx
movq 0x1ed54(%rip), %rax     # 0x29fc0
movq (%rax), %r14
callq 0xea3b
movq %rax, %r15
movzwl 0x10(%r12), %edi
callq 0xee73
movzwl %ax, %r8d
movzwl %bx, %ebp
leaq 0x15503(%rip), %rsi     # 0x20793
leaq 0x156c9(%rip), %rcx     # 0x20960
movq %r14, %rdi
movq %r15, %rdx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x1f9fa(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0xb2eb
callq 0xea3b
movq %rax, %r14
movzwl 0x10(%r12), %edi
callq 0xee73
movzwl %ax, %r8d
leaq 0x154bf(%rip), %rsi     # 0x20793
leaq 0x15685(%rip), %rcx     # 0x20960
movq %rbx, %rdi
movq %r14, %rdx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52c0
movq 0x8(%rsp), %rdi
callq 0x5040
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, (%rbx)
cmpl $0x0, 0x1efe7(%rip)     # 0x2a2f8
movq 0x8(%rsp), %rdi
je 0xb379
addq $0x9, %rdi
movl $0x10, %esi
callq 0xa1f8
testq %rax, %rax
je 0xb371
cmpb $0x1, 0xc(%rax)
jne 0xb38e
movzbl 0x18(%rax), %ecx
leaq 0xd(%rax), %rdx
cmpb $0x1, 0x19(%rax,%rcx)
leaq 0x1a(%rax,%rcx), %rax
cmoveq %rdx, %rax
movzbl (%rax), %eax
movl $0x0, (%rbx)
movl %eax, %ecx
andb $-0x2, %cl
cmpb $0x2, %cl
je 0xb386
cmpl $0x5, %eax
ja 0xb39e
movl %eax, %eax
leaq 0x1530c(%rip), %rcx     # 0x20674
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x3, (%rbx)
jmp 0xb39e
movzbl 0x17(%rdi), %eax
cmpl $0x1, %eax
je 0xb396
testl %eax, %eax
jne 0xb39e
movl $0x2, (%rbx)
jmp 0xb39e
movl $0x0, (%rbx)
jmp 0xb39e
movq %rbx, %rdi
callq 0xdd81
movq 0x1ec1b(%rip), %rax     # 0x29fc0
movq (%rax), %r14
callq 0xea3b
movl (%rbx), %ecx
leaq 0x1e01a(%rip), %r15     # 0x293d0
movq (%r15,%rcx,8), %r8
leaq 0x155b3(%rip), %rsi     # 0x20974
leaq 0x15598(%rip), %rcx     # 0x20960
xorl %ebp, %ebp
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x1f8ca(%rip), %rax     # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0xb2eb
callq 0xea3b
movl (%rbx), %ecx
movq (%r15,%rcx,8), %r8
leaq 0x15578(%rip), %rsi     # 0x20974
leaq 0x1555d(%rip), %rcx     # 0x20960
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
jmp 0xb2eb
movl $0x4, (%rbx)
jmp 0xb39e
movl $0x6, (%rbx)
jmp 0xb39e