// File: /workspace/github_c_makefile_star2_O2/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, 0x1e5ca(%rip)     # 0x292f8
je 0xad37
pushq $0xb
popq %rdi
pushq $0x2f
jmp 0xad3c
pushq $0x2
popq %rdi
pushq $0x44
popq %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xd53c
leaq 0x13c13(%rip), %rcx     # 0x1e960
movq %rsp, %r14
movq %rax, %rdi
movq %r14, %rsi
movl $0x7530, %edx           # imm = 0x7530
callq 0x9e8b
movl %eax, %ebp
testl %eax, %eax
sets %al
movq (%r14), %r12
testq %r12, %r12
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0xaddf
movq 0x1e23a(%rip), %rax     # 0x28fb8
movq (%rax), %rbx
callq 0xe343
leaq 0x139f4(%rip), %rsi     # 0x1e781
leaq 0x13bcc(%rip), %rcx     # 0x1e960
movq %rbx, %rdi
movq %rax, %rdx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x52c0
leaq 0x1eefd(%rip), %rax     # 0x29ca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0xae9f
callq 0xe343
leaq 0x139be(%rip), %rsi     # 0x1e781
leaq 0x13b96(%rip), %rcx     # 0x1e960
movq %rbx, %rdi
movq %rax, %rdx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x52c0
jmp 0xae9f
movzwl 0x10(%r12), %edi
callq 0xe73e
testw %ax, %ax
jne 0xae03
movzwl 0x12(%r12), %edi
callq 0xe73e
testw %ax, %ax
je 0xaeae
movzwl 0x12(%r12), %edi
callq 0xe73e
movl %eax, %ebx
movq 0x1e1a1(%rip), %rax     # 0x28fb8
movq (%rax), %r14
callq 0xe343
movq %rax, %r15
movzwl 0x10(%r12), %edi
callq 0xe73e
movzwl %ax, %r8d
movzwl %bx, %ebp
leaq 0x13958(%rip), %rsi     # 0x1e793
leaq 0x13b1e(%rip), %rcx     # 0x1e960
movq %r14, %rdi
movq %r15, %rdx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x1ee4f(%rip), %rax     # 0x29ca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0xae96
callq 0xe343
movq %rax, %r14
movzwl 0x10(%r12), %edi
callq 0xe73e
movzwl %ax, %r8d
leaq 0x13914(%rip), %rsi     # 0x1e793
leaq 0x13ada(%rip), %rcx     # 0x1e960
movq %rbx, %rdi
movq %r14, %rdx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52c0
movq (%rsp), %rdi
callq 0x5040
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
andl $0x0, (%rbx)
cmpl $0x0, 0x1e440(%rip)     # 0x292f8
movq (%rsp), %rdi
je 0xaef3
addq $0x9, %rdi
pushq $0x10
popq %rsi
callq 0x9e14
movb $0x1, %cl
testq %rax, %rax
je 0xaeef
cmpb $0x1, 0xc(%rax)
jne 0xaf05
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 0xaf07
movzbl 0x17(%rdi), %eax
cmpl $0x1, %eax
je 0xaf52
testl %eax, %eax
je 0xaf18
jmp 0xb0cd
xorl %eax, %eax
andl $0x0, (%rbx)
testb %al, %al
je 0xaf23
movl %ecx, %edx
andb $-0x2, %dl
cmpb $0x2, %dl
jne 0xaf23
movl $0x2, (%rbx)
jmp 0xb0cd
testb %al, %al
je 0xb0cd
cmpb $0x5, %cl
ja 0xb0cd
movzbl %cl, %eax
leaq 0x13736(%rip), %rcx     # 0x1e674
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x3, (%rbx)
jmp 0xb0cd
pushq $0x2
popq %rdi
pushq $0x2b
popq %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xd53c
leaq 0x13a23(%rip), %rcx     # 0x1e98b
leaq 0x8(%rsp), %r14
movq %rax, %rdi
movq %r14, %rsi
movl $0x7530, %edx           # imm = 0x7530
callq 0x9e8b
movl %eax, %ebp
testl %eax, %eax
sets %al
movq (%r14), %r12
testq %r12, %r12
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0xaffc
movq 0x1e01d(%rip), %rax     # 0x28fb8
movq (%rax), %r14
callq 0xe343
leaq 0x137d7(%rip), %rsi     # 0x1e781
leaq 0x139da(%rip), %rcx     # 0x1e98b
movq %r14, %rdi
movq %rax, %rdx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x52c0
leaq 0x1ece0(%rip), %rax     # 0x29ca8
movq (%rax), %r14
testq %r14, %r14
je 0xb0cd
callq 0xe343
leaq 0x137a1(%rip), %rsi     # 0x1e781
leaq 0x139a4(%rip), %rcx     # 0x1e98b
movq %r14, %rdi
movq %rax, %rdx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x52c0
jmp 0xb0cd
movzwl 0x10(%r12), %edi
callq 0xe73e
testw %ax, %ax
jne 0xb020
movzwl 0x12(%r12), %edi
callq 0xe73e
testw %ax, %ax
je 0xb144
movzwl 0x12(%r12), %edi
callq 0xe73e
movl %eax, %ebp
movq 0x1df84(%rip), %rax     # 0x28fb8
movq (%rax), %r14
callq 0xe343
movq %rax, %r15
movzwl 0x10(%r12), %edi
callq 0xe73e
movzwl %ax, %r8d
movzwl %bp, %ebp
leaq 0x1373b(%rip), %rsi     # 0x1e793
leaq 0x1392c(%rip), %rcx     # 0x1e98b
movq %r14, %rdi
movq %r15, %rdx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x1ec32(%rip), %rax     # 0x29ca8
movq (%rax), %r14
testq %r14, %r14
je 0xb0b3
callq 0xe343
movq %rax, %r15
movzwl 0x10(%r12), %edi
callq 0xe73e
movzwl %ax, %r8d
leaq 0x136f7(%rip), %rsi     # 0x1e793
leaq 0x138e8(%rip), %rcx     # 0x1e98b
movq %r14, %rdi
movq %r15, %rdx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52c0
movq 0x8(%rsp), %rdi
callq 0x5040
jmp 0xb0cd
movl $0x6, (%rbx)
jmp 0xb0cd
movl $0x4, (%rbx)
movq 0x1dee4(%rip), %rax     # 0x28fb8
movq (%rax), %r14
callq 0xe343
movl (%rbx), %ecx
leaq 0x1d24b(%rip), %r15     # 0x28330
movq (%r15,%rcx,8), %r8
leaq 0x13884(%rip), %rsi     # 0x1e974
leaq 0x13869(%rip), %rcx     # 0x1e960
xorl %ebp, %ebp
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x1eb9b(%rip), %rax     # 0x29ca8
movq (%rax), %r14
testq %r14, %r14
je 0xae96
callq 0xe343
movl (%rbx), %ecx
movq (%r15,%rcx,8), %r8
leaq 0x13849(%rip), %rsi     # 0x1e974
leaq 0x1382e(%rip), %rcx     # 0x1e960
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
jmp 0xae96
movq 0x8(%rsp), %rdi
addq $0x9, %rdi
pushq $0x11
popq %rsi
callq 0x9e14
testq %rax, %rax
je 0xb0b3
movzbl 0x3(%rax), %eax
cmpl $0x1, %eax
je 0xb17d
cmpl $0x5, %eax
je 0xb179
cmpl $0x4, %eax
jne 0xb0b3
pushq $0x4
jmp 0xb17f
pushq $0x6
jmp 0xb17f
pushq $0x3
popq %rax
movl %eax, (%rbx)
jmp 0xb0b3