// File: /workspace/github_cpp_makefile_star2_O3/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 %r12
pushq %rbx
movl %ecx, %ebx
movl %edx, %r14d
movl %esi, %ebp
movl $0x6, %eax
testl %edi, %edi
je 0x1e7d
xorl %r12d, %r12d
leal 0x1(,%r12,2), %r12d
decl %eax
jne 0x1e6f
jmp 0x1edb
movl $0x5, %eax
btl %eax, %ebp
jae 0x1e8e
addl $-0x1, %eax
jb 0x1e82
jmp 0x1e96
testl %eax, %eax
je 0x1e96
jg 0x1e6c
jmp 0x1ed8
movq 0x813b(%rip), %r15      # 0x9fd8
leaq 0x442f(%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
andl %r12d, %ebp
cmpl %r12d, %ebp
jne 0x1f25
movq 0x80ee(%rip), %r15      # 0x9fd8
leaq 0x43e2(%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 0x1f3e
incl %ebp
xorl %eax, %eax
leaq 0x1(,%rax,2), %rax
decl %ebp
jne 0x1f30
jmp 0x1f40
xorl %eax, %eax
testl %r12d, %r12d
jle 0x1f70
movabsq $0x7fffffff7fffffff, %rcx # imm = 0x7FFFFFFF7FFFFFFF
movq %rax, %rdx
shrq %rdx
cmpl $0x20, %ebx
jne 0x1f64
andq %rcx, %rdx
shll $0x1f, %eax
cltq
jmp 0x1f68
shlq $0x3f, %rax
orq %rdx, %rax
decl %r12d
jne 0x1f4f
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq