// File: /workspace/github_cpp_makefile_star2_O1/akshayrajput[P]ARMV8_Simulator/./instructions.cpp
// Function: DecodeBitMask(int, int, int, int)
static uint64_t DecodeBitMask(int N, int imms, int immr, int size){
        int len, levels, S, R, diff, esize;

        uint64_t welem = 0;

        /* Finding the highest set bit in N:imms */
        if(N){
                len = 6;
        }
        else{
                imms = ~imms;
                for(int i=5; i>=0; i--){
                        if(imms & (1<<i)){
                                len = i;
                                break;
                        }
                }
                imms = ~imms;
        }
        if(len < 1){
                cout << "Invalid Instruction found" << endl;
        }

        /* caluclating levels value */
        levels = 0;
        for(int i=0; i<len; i++){
                levels = levels << 1;
                levels = (levels | 1) ;
        }

        if((levels & imms) == levels){
                cout << "Invalid Instruction found" << endl;
        }
        S = levels & imms;
        R = levels & immr;
        diff = S - R;
        esize = (1 << len);
        for(int i=0; i<(S+1); i++){
                welem = welem << 1;
                welem = (welem | 1) ;
        }

        for(int i=0; i<R; i++){
                uint64_t lbit = welem & 1;
                welem = welem >> 1;
                if(size == 32){
                        welem = welem & 0xffffffff7fffffff;
                        welem = (welem | (int)((lbit) << 31));
                }
                else{
                        welem = welem & 0x7fffffffffffffff;
                        welem = (welem |(uint64_t) ((lbit) << 63));
                }
        }
        return welem;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %ebx
movl %edx, %r14d
movl %esi, %ebp
movl $0x6, %r13d
testl %edi, %edi
jne 0x1e8b
movl $0x5, %r13d
btl %r13d, %ebp
jae 0x1e8b
addl $-0x1, %r13d
jb 0x1e79
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testl %r13d, %r13d
jg 0x1ed2
movq 0x8141(%rip), %r15      # 0x9fd8
leaq 0x4435(%rip), %rsi      # 0x62d3
movl $0x19, %edx
movq %r15, %rdi
callq 0x10e0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x10c0
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1090
xorl %r12d, %r12d
testl %r13d, %r13d
jle 0x1ee7
leal 0x1(,%r12,2), %r12d
decl %r13d
jne 0x1eda
andl %r12d, %ebp
cmpl %r12d, %ebp
jne 0x1f31
movq 0x80e2(%rip), %r15      # 0x9fd8
leaq 0x43d6(%rip), %rsi      # 0x62d3
movl $0x19, %edx
movq %r15, %rdi
callq 0x10e0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x10c0
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1090
andl %r14d, %r12d
testl %ebp, %ebp
js 0x1f4a
incl %ebp
xorl %eax, %eax
leaq 0x1(,%rax,2), %rax
decl %ebp
jne 0x1f3c
jmp 0x1f4c
xorl %eax, %eax
testl %r12d, %r12d
jle 0x1f7c
movabsq $0x7fffffff7fffffff, %rcx # imm = 0x7FFFFFFF7FFFFFFF
movq %rax, %rdx
shrq %rdx
cmpl $0x20, %ebx
jne 0x1f70
andq %rcx, %rdx
shll $0x1f, %eax
cltq
jmp 0x1f74
shlq $0x3f, %rax
orq %rdx, %rax
decl %r12d
jne 0x1f5b
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq