// File: /workspace/github_cpp_makefile_star2_O3/jstaursky[P]hutch/src/Sleigh/pcodecompile.cc
// Function: PcodeCompile::createBitRange(SpecificSymbol*, unsigned int, unsigned int)
ExprTree *PcodeCompile::createBitRange(SpecificSymbol *sym,uint4 bitoffset,uint4 numbits)

{ // Create an expression computing the indicated bitrange of sym
  // The result is truncated to the smallest byte size that can
  // contain the indicated number of bits. The result has the
  // desired bits shifted all the way to the right
  string errmsg;
  if (numbits == 0)
    errmsg = "Size of bitrange is zero";
  VarnodeTpl *vn = sym->getVarnode();
  uint4 finalsize = (numbits+7)/8; // Round up to neareast byte size
  uint4 truncshift = 0;
  bool maskneeded = ((numbits%8)!=0);
  bool truncneeded = true;

  // Special case where we can set the size, without invoking
  // a truncation operator
  if ((errmsg.size()==0)&&(bitoffset==0)&&(!maskneeded)) {
    if ((vn->getSpace().getType()==ConstTpl::handle)&&vn->isZeroSize()) {
      vn->setSize(ConstTpl(ConstTpl::real,finalsize));
      ExprTree *res = new ExprTree(vn);
      //      VarnodeTpl *cruft = buildTemporary();
      //      delete cruft;
      return res;
    }
  }

  if (errmsg.size()==0) {
    VarnodeTpl *truncvn = buildTruncatedVarnode(vn,bitoffset,numbits);
    if (truncvn != (VarnodeTpl *)0) { // If we are able to construct a simple truncated varnode
      ExprTree *res = new ExprTree(truncvn); // Return just the varnode as an expression
      delete vn;
      return res;
    }
  }

  if (vn->getSize().getType()==ConstTpl::real) {
    // If we know the size of the input varnode, we can
    // do some immediate checks, and possibly simplify things
    uint4 insize = vn->getSize().getReal();
    if (insize > 0) {
      truncneeded = (finalsize < insize);
      insize *= 8;		// Convert to number of bits
      if ((bitoffset >= insize)||(bitoffset+numbits > insize))
	errmsg = "Bitrange is bad";
      if (maskneeded && ((bitoffset+numbits)==insize))
	maskneeded = false;
    }
  }

  uintb mask = (uintb)2;
  mask = ((mask<<(numbits-1))-1);
  
  if (truncneeded && ((bitoffset % 8)==0)) {
    truncshift = bitoffset/8;
    bitoffset = 0;
  }

  if ((bitoffset==0)&&(!truncneeded)&&(!maskneeded))
    errmsg = "Superfluous bitrange";

  if (maskneeded && (finalsize > 8))
    errmsg = "Illegal masked bitrange producing varnode larger than 64 bits: " + sym->getName();

  ExprTree *res = new ExprTree(vn);

  if (errmsg.size()>0) {	// Check for error condition
    reportError(errmsg);
    return res;
  }

  if (bitoffset !=0)
    appendOp(CPUI_INT_RIGHT,res,bitoffset,4);
  if (truncneeded)
    appendOp(CPUI_SUBPIECE,res,truncshift,4);
  if (maskneeded)
    appendOp(CPUI_INT_AND,res,mask,finalsize);
  force_size(res->outvn,ConstTpl(ConstTpl::real,finalsize),*res->ops);
  return res;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %ebx
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, 0x18(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
testl %ecx, %ecx
jne 0x53c98
leaq 0x2c334(%rip), %rcx     # 0x7ffb8
leaq 0x28(%rsp), %rdi
movl $0x18, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x417d0
movq (%r12), %rax
movq %r12, %rdi
callq *0x60(%rax)
movq %rax, 0x10(%rsp)
leal 0x7(%rbx), %eax
movl %eax, 0x24(%rsp)
shrl $0x3, %eax
movl %eax, 0xc(%rsp)
movl %ebx, %ebp
andl $0x7, %ebp
setne %r14b
movq 0x30(%rsp), %rax
testq %rax, %rax
setne %cl
movl %ebp, %edx
orl %r15d, %edx
setne %dl
orb %cl, %dl
je 0x53ce0
testq %rax, %rax
jne 0x53d93
jmp 0x53d4b
movq 0x10(%rsp), %rax
cmpl $0x1, (%rax)
jne 0x53d4b
movq 0x10(%rsp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x53d4b
movq 0x10(%rsp), %rax
cmpq $0x0, 0x50(%rax)
jne 0x53d4b
movl 0xc(%rsp), %edx
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x40b20
movq 0x10(%rsp), %rbx
movq %rbx, %rax
addq $0x40, %rax
movups 0x48(%rsp), %xmm0
movups 0x54(%rsp), %xmm1
movups %xmm1, 0xc(%rax)
movups %xmm0, (%rax)
movl $0x10, %edi
callq 0x40810
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3f8f0
jmp 0x53f68
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl %r15d, %edx
movl %ebx, %ecx
callq 0x40850
movq %rax, %r13
testq %rax, %rax
je 0x53d93
movl $0x10, %edi
callq 0x40810
movq %rax, %r12
movq %rax, %rdi
movq %r13, %rsi
callq 0x3f8f0
movq 0x10(%rsp), %rdi
movl $0x68, %esi
callq 0x40880
jmp 0x53f68
movb $0x1, 0xb(%rsp)
movq 0x10(%rsp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x53dfa
movl 0x50(%rax), %r13d
testl %r13d, %r13d
je 0x53dfa
cmpl %r13d, 0xc(%rsp)
setb 0xb(%rsp)
shll $0x3, %r13d
cmpl %r15d, %r13d
seta %al
leal (%rbx,%r15), %r14d
cmpl %r13d, %r14d
setbe %cl
testb %cl, %al
jne 0x53deb
leaq 0x28(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x2c25e(%rip), %rcx     # 0x8003c
movl $0xf, %r8d
xorl %esi, %esi
callq 0x417d0
testl %ebp, %ebp
setne %al
cmpl %r13d, %r14d
setne %r14b
andb %al, %r14b
decb %bl
movl $0x2, %r13d
movl %ebx, %ecx
shlq %cl, %r13
testb $0x7, %r15b
sete %al
movl %r15d, %ebx
shrl $0x3, %ebx
xorl %ecx, %ecx
testb %al, 0xb(%rsp)
cmoveq %rcx, %rbx
cmovnel %ecx, %r15d
testb %r14b, %r14b
jne 0x53e50
cmpb $0x0, 0xb(%rsp)
jne 0x53e50
testl %r15d, %r15d
jne 0x53e50
leaq 0x28(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x2c209(%rip), %rcx     # 0x8004c
movl $0x14, %r8d
xorl %esi, %esi
callq 0x417d0
cmpl $0x48, 0x24(%rsp)
setb %al
movl %r14d, %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x53ea7
addq $0x8, %r12
leaq 0x2c1f4(%rip), %rsi     # 0x80061
leaq 0x48(%rsp), %rdi
movq %r12, %rdx
callq 0x40510
leaq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %r12
movq %r12, %rsi
callq 0x40db0
movq (%r12), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x53ea7
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x40880
movl $0x10, %edi
callq 0x40810
movq %rax, %r12
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x3f8f0
cmpq $0x0, 0x30(%rsp)
je 0x53ede
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
leaq 0x28(%rsp), %rsi
callq *0x20(%rax)
jmp 0x53f68
testl %r15d, %r15d
je 0x53efe
movl %r15d, %ecx
movq 0x18(%rsp), %rdi
movl $0x1e, %esi
movq %r12, %rdx
movl $0x4, %r8d
callq 0x40400
cmpb $0x0, 0xb(%rsp)
je 0x53f20
movq 0x18(%rsp), %rdi
movl $0x3f, %esi
movq %r12, %rdx
movq %rbx, %rcx
movl $0x4, %r8d
callq 0x40400
testb %r14b, %r14b
je 0x53f42
decq %r13
movq 0x18(%rsp), %rdi
movl $0x1b, %esi
movq %r12, %rdx
movq %r13, %rcx
movl 0xc(%rsp), %r8d
callq 0x40400
movq 0x8(%r12), %rbx
movl 0xc(%rsp), %edx
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x40b20
movq (%r12), %rdx
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x3f380
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x53f84
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x40880
movq %r12, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x53fc0
jmp 0x53faa
jmp 0x53fc0
jmp 0x53fc0
jmp 0x53fc0
jmp 0x53fc0
jmp 0x53faa
jmp 0x53fc0
jmp 0x53fc0
jmp 0x53fc0
movq %rax, %rbx
movl $0x10, %esi
movq %r12, %rdi
callq 0x40880
jmp 0x53fc3
jmp 0x53fc0
jmp 0x53fc0
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x53fdf
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x40880
movq %rbx, %rdi
callq 0x41f10
nop