// File: /workspace/github_cpp_makefile_star2_O1/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 0x53951
xorl %r13d, %r13d
jmp 0x53967
movq 0x50(%rbx), %r13
testq %r13, %r13
je 0x53980
leal (%r15,%r12), %eax
cmpq %rax, %r13
jb 0x53a3f
orl %edx, %ecx
testb $0x7, %cl
jne 0x53980
movq %rbx, %rdi
callq 0x401f0
testb %al, %al
jne 0x53980
cmpl $0x1, 0x20(%rbx)
jbe 0x53995
xorl %r14d, %r14d
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, (%rsp)
movq $0x0, 0x10(%rsp)
jne 0x539c6
movl 0x28(%rbx), %edx
movl %r12d, %r8d
leaq 0x28(%rsp), %r14
movq %r14, %rdi
movl $0x1, %esi
movl $0x3, %ecx
callq 0x41890
jmp 0x539f9
cmpl $0x0, 0x40(%rbx)
jne 0x53a93
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 0x40b10
movups (%r14), %xmm0
movups 0xc(%r14), %xmm1
movups %xmm1, 0xc(%rsp)
movaps %xmm0, (%rsp)
movl $0x68, %edi
callq 0x40800
movq %rax, %r14
movl %r15d, %edx
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x40b10
movq %rsp, %rdx
leaq 0x28(%rsp), %rcx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x41aa0
jmp 0x53983
movl $0x20, %edi
callq 0x3fb00
movq %rax, %r14
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2c512(%rip), %rsi     # 0x7ff6e
leaq 0x2c52c(%rip), %rdx     # 0x7ff8f
movq %rsp, %rdi
callq 0x3f8a0
movb $0x1, %bpl
movq %rsp, %rsi
movq %r14, %rdi
callq 0x3f890
xorl %ebp, %ebp
movq 0x4f40e(%rip), %rsi     # 0xa2e90
movq 0x4f1af(%rip), %rdx     # 0xa2c38
movq %r14, %rdi
callq 0x41d60
jmp 0x53ae9
movl $0x20, %edi
callq 0x3fb00
movq %rax, %r14
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2c4e0(%rip), %rsi     # 0x7ff90
leaq 0x2c500(%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 0x4f3b6(%rip), %rsi     # 0xa2e90
movq 0x4f157(%rip), %rdx     # 0xa2c38
movq %r14, %rdi
callq 0x41d60
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x53b20
movq 0x38(%rsp), %rsi
jmp 0x53b10
jmp 0x53b1a
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x53b20
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x40870
jmp 0x53b20
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0x53b3f
movq %r14, %rdi
callq 0x40080
jmp 0x53b3f
movq %rax, %rbx
movl $0x68, %esi
movq %r14, %rdi
callq 0x40870
movq %rbx, %rdi
callq 0x41f00
nop