// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl %edx, %r12d
shrl $0x3, %r12d
movl %ecx, %r15d
shrl $0x3, %r15d
cmpl $0x0, 0x40(%rsi)
je 0x53791
xorl %r13d, %r13d
jmp 0x537a7
movq 0x50(%rbx), %r13
testq %r13, %r13
je 0x537c0
leal (%r15,%r12), %eax
cmpq %rax, %r13
jb 0x5386f
orl %edx, %ecx
testb $0x7, %cl
jne 0x537c0
movq %rbx, %rdi
callq 0x40200
testb %al, %al
jne 0x537c0
cmpl $0x1, 0x20(%rbx)
jbe 0x537d5
xorl %r14d, %r14d
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jne 0x537f6
movl 0x28(%rbx), %edx
movl %r12d, %r8d
leaq 0x28(%rsp), %r14
movq %r14, %rdi
movl $0x1, %esi
movl $0x3, %ecx
callq 0x418a0
jmp 0x53829
cmpl $0x0, 0x40(%rbx)
jne 0x538c3
movq 0x8(%r14), %rax
leal (%r15,%r12), %ecx
subq %rcx, %r13
testb $0x1, 0x24(%rax)
movl %r12d, %edx
cmovneq %r13, %rdx
addq 0x30(%rbx), %rdx
leaq 0x28(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x40b20
movups (%r14), %xmm0
movups 0xc(%r14), %xmm1
movups %xmm1, 0xc(%rsp)
movaps %xmm0, (%rsp)
movl $0x68, %edi
callq 0x40810
movq %rax, %r14
movl %r15d, %edx
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x40b20
movq %rsp, %rdx
leaq 0x28(%rsp), %rcx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x41ab0
jmp 0x537c3
movl $0x20, %edi
callq 0x3fb00
movq %rax, %r14
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2c6e2(%rip), %rsi     # 0x7ff6e
leaq 0x2c6fc(%rip), %rdx     # 0x7ff8f
movq %rsp, %rdi
callq 0x3f8a0
movb $0x1, %bpl
movq %rsp, %rsi
movq %r14, %rdi
callq 0x3f890
xorl %ebp, %ebp
movq 0x4f5de(%rip), %rsi     # 0xa2e90
movq 0x4f37f(%rip), %rdx     # 0xa2c38
movq %r14, %rdi
callq 0x41d70
jmp 0x53919
movl $0x20, %edi
callq 0x3fb00
movq %rax, %r14
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2c6b0(%rip), %rsi     # 0x7ff90
leaq 0x2c6d0(%rip), %rdx     # 0x7ffb7
leaq 0x28(%rsp), %rdi
callq 0x3f8a0
movb $0x1, %bpl
leaq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x3f890
xorl %ebp, %ebp
movq 0x4f586(%rip), %rsi     # 0xa2e90
movq 0x4f327(%rip), %rdx     # 0xa2c38
movq %r14, %rdi
callq 0x41d70
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x53948
movq 0x38(%rsp), %rsi
jmp 0x53940
jmp 0x5394f
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x53948
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x40880
testb %bpl, %bpl
jne 0x53952
jmp 0x5396c
movq %rax, %rbx
movq %r14, %rdi
callq 0x40090
jmp 0x5396c
movq %rax, %rbx
movl $0x68, %esi
movq %r14, %rdi
callq 0x40880
movq %rbx, %rdi
callq 0x41f10