// File: /workspace/github_cpp_makefile_star2_O2/jstaursky[P]hutch/src/Sleigh/pcodecompile.cc
// Function: PcodeCompile::buildTruncatedVarnode(VarnodeTpl*, unsigned int, unsigned int)
VarnodeTpl *PcodeCompile::buildTruncatedVarnode(VarnodeTpl *basevn,uint4 bitoffset,uint4 numbits)

{ // Build a truncated form -basevn- that matches the bitrange [ -bitoffset-, -numbits- ] if possible
  // using just ConstTpl mechanics, otherwise return null
  uint4 byteoffset = bitoffset / 8; // Convert to byte units
  uint4 numbytes = numbits / 8;
  uintb fullsz = 0;
  if (basevn->getSize().getType() == ConstTpl::real) {
    // If we know the size of base, make sure the bit range is in bounds
    fullsz = basevn->getSize().getReal();
    if (fullsz == 0) return (VarnodeTpl *)0;
    if (byteoffset + numbytes > fullsz)
      throw SleighError("Requested bit range out of bounds");
  }

  if ((bitoffset % 8) != 0) return (VarnodeTpl *)0;
  if ((numbits % 8) != 0) return (VarnodeTpl *)0;

  if (basevn->getSpace().isUniqueSpace()) // Do we really want to prevent truncated uniques??
    return (VarnodeTpl *)0;

  ConstTpl::const_type offset_type = basevn->getOffset().getType();
  if ((offset_type != ConstTpl::real)&&(offset_type != ConstTpl::handle))
    return (VarnodeTpl *)0;

  ConstTpl specialoff;
  if (offset_type == ConstTpl::handle) {
    // We put in the correct adjustment to offset assuming things are little endian
    // We defer the correct big endian calculation until after the consistency check
    // because we need to know the subtable export sizes
    specialoff = ConstTpl(ConstTpl::handle,basevn->getOffset().getHandleIndex(),
			  ConstTpl::v_offset_plus,byteoffset);
  }
  else { 
    if (basevn->getSize().getType() != ConstTpl::real)
      throw SleighError("Could not construct requested bit range");
    uintb plus;
    if (defaultspace->isBigEndian())
      plus = fullsz - (byteoffset + numbytes);
    else
      plus = byteoffset;
    specialoff = ConstTpl(ConstTpl::real,basevn->getOffset().getReal() + plus);
  }
  VarnodeTpl *res = new VarnodeTpl(basevn->getSpace(),specialoff,ConstTpl(ConstTpl::real,numbytes));
  return res;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl %edx, %r15d
shrl $0x3, %r15d
movl %ecx, %r12d
shrl $0x3, %r12d
cmpl $0x0, 0x40(%rsi)
je 0x60043
xorl %r13d, %r13d
jmp 0x60059
movq 0x50(%rbx), %r13
testq %r13, %r13
je 0x60072
leal (%r12,%r15), %eax
cmpq %rax, %r13
jb 0x6011e
orl %edx, %ecx
testb $0x7, %cl
jne 0x60072
movq %rbx, %rdi
callq 0x4eca0
testb %al, %al
jne 0x60072
cmpl $0x1, 0x20(%rbx)
jbe 0x60087
xorl %r15d, %r15d
movq %r15, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jne 0x600a4
movl 0x28(%rbx), %edx
movl %r15d, %r8d
leaq 0x38(%rsp), %r14
pushq $0x1
popq %rsi
pushq $0x3
popq %rcx
movq %r14, %rdi
callq 0x50f90
jmp 0x600d7
cmpl $0x0, 0x40(%rbx)
jne 0x60169
movq 0x8(%r14), %rax
leal (%r12,%r15), %ecx
subq %rcx, %r13
testb $0x1, 0x24(%rax)
movl %r15d, %edx
cmovneq %r13, %rdx
addq 0x30(%rbx), %rdx
leaq 0x38(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x4fa20
movups (%r14), %xmm0
movups 0xc(%r14), %xmm1
movups %xmm1, 0x1c(%rsp)
movaps %xmm0, 0x10(%rsp)
pushq $0x68
popq %rdi
callq 0x4f5d0
movq %rax, %r15
movl %r12d, %edx
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x4fa20
leaq 0x10(%rsp), %rdx
leaq 0x38(%rsp), %rcx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x51230
jmp 0x60075
pushq $0x20
popq %rdi
callq 0x4e180
movq %rax, %rbx
leaq 0x26e1e(%rip), %rsi     # 0x86f4e
leaq 0x10(%rsp), %rdi
leaq 0x38(%rsp), %rdx
callq 0x515e0
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x4ddf0
xorl %ebp, %ebp
movq 0x4ed30(%rip), %rsi     # 0xaee88
movq 0x4ead9(%rip), %rdx     # 0xaec38
movq %rbx, %rdi
callq 0x51630
jmp 0x601b2
pushq $0x20
popq %rdi
callq 0x4e180
movq %rax, %rbx
leaq 0x26df5(%rip), %rsi     # 0x86f70
leaq 0x38(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x515e0
movb $0x1, %bpl
leaq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x4ddf0
xorl %ebp, %ebp
movq 0x4ece5(%rip), %rsi     # 0xaee88
movq 0x4ea8e(%rip), %rdx     # 0xaec38
movq %rbx, %rdi
callq 0x51630
movq %rax, %r14
leaq 0x38(%rsp), %rdi
jmp 0x601c6
jmp 0x601d2
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x4e020
testb %bpl, %bpl
jne 0x601d5
jmp 0x601ed
movq %rax, %r14
movq %rbx, %rdi
callq 0x4ea60
jmp 0x601ed
movq %rax, %r14
pushq $0x68
popq %rsi
movq %r15, %rdi
callq 0x4f670
movq %r14, %rdi
callq 0x518c0
nop