// File: /workspace/github_c_makefile_star2_O1/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, 0x1f0de(%rip)     # 0x2a2f8
je 0xb228
movl $0xb, %edi
movl $0x2f, %esi
jmp 0xb232
movl $0x2, %edi
movl $0x44, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xda86
leaq 0x15722(%rip), %rcx     # 0x20964
leaq 0x8(%rsp), %r14
movq %rax, %rdi
movq %r14, %rsi
movl $0x7530, %edx           # imm = 0x7530
callq 0xa317
movl %eax, %ebp
testl %eax, %eax
sets %al
movq (%r14), %r12
testq %r12, %r12
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0xb2d6
movq 0x1ed4b(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xeb2f
leaq 0x15501(%rip), %rsi     # 0x20785
leaq 0x156d9(%rip), %rcx     # 0x20964
movq %rbx, %rdi
movq %rax, %rdx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x52c0
leaq 0x1fa06(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0xb397
callq 0xeb2f
leaq 0x154cb(%rip), %rsi     # 0x20785
leaq 0x156a3(%rip), %rcx     # 0x20964
movq %rbx, %rdi
movq %rax, %rdx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x52c0
jmp 0xb397
movzwl 0x10(%r12), %edi
callq 0xef61
testw %ax, %ax
jne 0xb2fa
movzwl 0x12(%r12), %edi
callq 0xef61
testw %ax, %ax
je 0xb3a6
movzwl 0x12(%r12), %edi
callq 0xef61
movl %eax, %ebx
movq 0x1ecb2(%rip), %rax     # 0x29fc0
movq (%rax), %r14
callq 0xeb2f
movq %rax, %r15
movzwl 0x10(%r12), %edi
callq 0xef61
movzwl %ax, %r8d
movzwl %bx, %ebp
leaq 0x15465(%rip), %rsi     # 0x20797
leaq 0x1562b(%rip), %rcx     # 0x20964
movq %r14, %rdi
movq %r15, %rdx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x1f958(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0xb38d
callq 0xeb2f
movq %rax, %r14
movzwl 0x10(%r12), %edi
callq 0xef61
movzwl %ax, %r8d
leaq 0x15421(%rip), %rsi     # 0x20797
leaq 0x155e7(%rip), %rcx     # 0x20964
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, 0x1ef45(%rip)     # 0x2a2f8
movq 0x8(%rsp), %rdi
je 0xb3f1
addq $0x9, %rdi
movl $0x10, %esi
callq 0xa29c
movb $0x1, %cl
testq %rax, %rax
je 0xb3ed
cmpb $0x1, 0xc(%rax)
jne 0xb413
movzbl 0x18(%rax), %ecx
leaq 0xd(%rax), %rdx
cmpb $0x1, 0x19(%rax,%rcx)
leaq 0x1a(%rax,%rcx), %rax
cmoveq %rdx, %rax
movb (%rax), %cl
movb $0x1, %al
jmp 0xb415
movzbl 0x17(%rdi), %eax
cmpl $0x1, %eax
jg 0xb455
testl %eax, %eax
je 0xb429
cmpl $0x1, %eax
jne 0xb45f
movl $0x0, (%rbx)
movq %rbx, %rdi
callq 0xde78
jmp 0xb465
xorl %eax, %eax
movl $0x0, (%rbx)
testb %al, %al
je 0xb431
movl %ecx, %edx
andb $-0x2, %dl
cmpb $0x2, %dl
jne 0xb431
movl $0x2, (%rbx)
jmp 0xb465
testb %al, %al
je 0xb465
cmpb $0x5, %cl
ja 0xb465
movzbl %cl, %eax
leaq 0x15234(%rip), %rcx     # 0x20678
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x3, (%rbx)
jmp 0xb465
cmpl $0x2, %eax
je 0xb45f
cmpl $0xff, %eax
movl $0x0, (%rbx)
movq 0x1eb54(%rip), %rax     # 0x29fc0
movq (%rax), %r14
callq 0xeb2f
movl (%rbx), %ecx
leaq 0x1df53(%rip), %r15     # 0x293d0
movq (%r15,%rcx,8), %r8
leaq 0x154f0(%rip), %rsi     # 0x20978
leaq 0x154d5(%rip), %rcx     # 0x20964
xorl %ebp, %ebp
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x1f803(%rip), %rax     # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0xb38d
callq 0xeb2f
movl (%rbx), %ecx
movq (%r15,%rcx,8), %r8
leaq 0x154b5(%rip), %rsi     # 0x20978
leaq 0x1549a(%rip), %rcx     # 0x20964
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
jmp 0xb38d
movl $0x6, (%rbx)
jmp 0xb465
movl $0x4, (%rbx)
jmp 0xb465