// File: jstaursky[P]hutch/src/Sleigh/pcodecompile.cc
// Function: PcodeCompile::assignBitRange(VarnodeTpl*, unsigned int, unsigned int, ExprTree*)
vector<OpTpl *> *PcodeCompile::assignBitRange(VarnodeTpl *vn,uint4 bitoffset,uint4 numbits,ExprTree *rhs)

{ // Create an expression assigning the rhs to a bitrange within sym
  string errmsg;
  if (numbits == 0)
    errmsg = "Size of bitrange is zero";
  uint4 smallsize = (numbits+7)/8; // Size of input (output of rhs)
  bool shiftneeded = (bitoffset != 0);
  bool zextneeded = true;
  uintb mask = (uintb)2;
  mask = ~(((mask<<(numbits-1))-1) << bitoffset);

  if (vn->getSize().getType()==ConstTpl::real) {
    // If we know the size of the bitranged varnode, we can
    // do some immediate checks, and possibly simplify things
    uint4 symsize = vn->getSize().getReal();
    if (symsize > 0)
      zextneeded = (symsize > smallsize);
    symsize *= 8;		// Convert to number of bits
    if ((bitoffset>=symsize)||(bitoffset+numbits>symsize))
      errmsg = "Assigned bitrange is bad";
    else if ((bitoffset==0)&&(numbits==symsize))
      errmsg = "Assigning to bitrange is superfluous";
  }

  if (errmsg.size()>0) {	// Was there an error condition
    reportError(errmsg);	// Report the error
    delete vn;			// Clean up
    vector<OpTpl *> *resops = rhs->ops; // Passthru old expression
    rhs->ops = (vector<OpTpl *> *)0;
    delete rhs;
    return resops;
  }

  // We know what the size of the input has to be
  force_size(rhs->outvn,ConstTpl(ConstTpl::real,smallsize),*rhs->ops);

  ExprTree *res;
  VarnodeTpl *finalout = buildTruncatedVarnode(vn,bitoffset,numbits);
  if (finalout != (VarnodeTpl *)0) {
    res = createOpOutUnary(finalout,CPUI_COPY,rhs);
  }
  else {
    if (bitoffset + numbits > 64)
      errmsg = "Assigned bitrange extends past first 64 bits";
    res = new ExprTree(vn);
    appendOp(CPUI_INT_AND,res,mask,0);
    if (zextneeded)
      createOp(CPUI_INT_ZEXT,rhs);
    if (shiftneeded)
      appendOp(CPUI_INT_LEFT,rhs,bitoffset,4);
  
    finalout = new VarnodeTpl(*vn);
    res = createOpOut(finalout,CPUI_INT_OR,res,rhs);
  }
  if (errmsg.size() > 0)
    reportError(errmsg);
  vector<OpTpl *> *resops = res->ops;
  res->ops = (vector<OpTpl *> *)0;
  delete res;
  return resops;
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x118(%rsp)
movq %rsi, 0x110(%rsp)
movl %edx, 0x10c(%rsp)
movl %ecx, 0x108(%rsp)
movq %r8, 0x100(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0xdc610
cmpl $0x0, 0x108(%rsp)
jne 0x110242
leaq 0x77d9c(%rip), %rsi     # 0x187fb4
leaq 0xe0(%rsp), %rdi
callq 0xdc010
jmp 0x110227
jmp 0x110242
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
jmp 0x110767
movl 0x108(%rsp), %eax
addl $0x7, %eax
shrl $0x3, %eax
movl %eax, 0xd0(%rsp)
movl 0x10c(%rsp), %eax
testl %eax, %eax
setne 0xcf(%rsp)
movb $0x1, 0xce(%rsp)
movq $0x2, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movb 0x108(%rsp), %cl
decb %cl
shlq %cl, %rax
decq %rax
movb 0x10c(%rsp), %cl
shlq %cl, %rax
notq %rax
movq %rax, 0xc0(%rsp)
movq 0x110(%rsp), %rdi
callq 0xd4930
movq %rax, 0x60(%rsp)
jmp 0x1102bb
movq 0x60(%rsp), %rdi
callq 0xde5d0
movl %eax, 0x5c(%rsp)
jmp 0x1102cb
movl 0x5c(%rsp), %eax
cmpl $0x0, %eax
jne 0x1103b3
movq 0x110(%rsp), %rdi
callq 0xd4930
movq %rax, 0x50(%rsp)
jmp 0x1102ec
movq 0x50(%rsp), %rdi
callq 0xdd890
movq %rax, 0x48(%rsp)
jmp 0x1102fd
movq 0x48(%rsp), %rax
movl %eax, 0xbc(%rsp)
cmpl $0x0, 0xbc(%rsp)
jbe 0x11032d
movl 0xbc(%rsp), %eax
cmpl 0xd0(%rsp), %eax
seta %al
andb $0x1, %al
movb %al, 0xce(%rsp)
movl 0xbc(%rsp), %eax
shll $0x3, %eax
movl %eax, 0xbc(%rsp)
movl 0x10c(%rsp), %eax
cmpl 0xbc(%rsp), %eax
jae 0x110365
movl 0x10c(%rsp), %eax
addl 0x108(%rsp), %eax
cmpl 0xbc(%rsp), %eax
jbe 0x11037d
leaq 0x77c61(%rip), %rsi     # 0x187fcd
leaq 0xe0(%rsp), %rdi
callq 0xdc010
jmp 0x11037b
jmp 0x1103b1
cmpl $0x0, 0x10c(%rsp)
jne 0x1103af
movl 0x108(%rsp), %eax
cmpl 0xbc(%rsp), %eax
jne 0x1103af
leaq 0x77c48(%rip), %rsi     # 0x187fe6
leaq 0xe0(%rsp), %rdi
callq 0xdc010
jmp 0x1103ad
jmp 0x1103af
jmp 0x1103b1
jmp 0x1103b3
leaq 0xe0(%rsp), %rdi
callq 0xd5290
cmpq $0x0, %rax
jbe 0x110472
movq 0x68(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
leaq 0xe0(%rsp), %rsi
callq *%rax
jmp 0x1103e2
movq 0x110(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
je 0x110404
movq 0x40(%rsp), %rdi
movl $0x68, %esi
callq 0xd8620
movq 0x100(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
movq 0x100(%rsp), %rax
movq $0x0, (%rax)
movq 0x100(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x110452
movq 0x38(%rsp), %rdi
callq 0xdb660
movq 0x38(%rsp), %rdi
movl $0x10, %esi
callq 0xd8620
movq 0xb0(%rsp), %rax
movq %rax, 0x120(%rsp)
movl $0x1, 0xac(%rsp)
jmp 0x11074a
movq 0x100(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x30(%rsp)
movl 0xd0(%rsp), %eax
movl %eax, %edx
leaq 0x88(%rsp), %rdi
xorl %esi, %esi
callq 0xd9880
jmp 0x11049d
movq 0x30(%rsp), %rdi
movq 0x100(%rsp), %rax
movq (%rax), %rdx
leaq 0x88(%rsp), %rsi
callq 0xcf5c0
jmp 0x1104bc
movq 0x68(%rsp), %rdi
movq 0x110(%rsp), %rsi
movl 0x10c(%rsp), %edx
movl 0x108(%rsp), %ecx
callq 0xd8550
movq %rax, 0x28(%rsp)
jmp 0x1104e3
movq 0x28(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
je 0x11052a
movq 0x68(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq 0x100(%rsp), %rcx
movl $0x1, %edx
callq 0xdc170
movq %rax, 0x20(%rsp)
jmp 0x110518
movq 0x20(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x1106bd
movl 0x10c(%rsp), %eax
addl 0x108(%rsp), %eax
cmpl $0x40, %eax
jbe 0x110555
leaq 0x77ac7(%rip), %rsi     # 0x18800b
leaq 0xe0(%rsp), %rdi
callq 0xdc010
jmp 0x110553
jmp 0x110555
movl $0x10, %edi
callq 0xd8380
movq %rax, 0x18(%rsp)
jmp 0x110566
movq 0x18(%rsp), %rdi
movq 0x110(%rsp), %rsi
callq 0xd1cc0
jmp 0x11057a
movq 0x68(%rsp), %rdi
movq 0x18(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rdx
movq 0xc0(%rsp), %rcx
movl $0x1b, %esi
xorl %r8d, %r8d
callq 0xd6ab0
jmp 0x1105ab
testb $0x1, 0xce(%rsp)
je 0x1105f8
movq 0x68(%rsp), %rdi
movq 0x100(%rsp), %rdx
movl $0x11, %esi
callq 0xdefa0
jmp 0x1105ce
jmp 0x1105f8
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
movl $0x10, %esi
callq 0xd8620
jmp 0x110767
testb $0x1, 0xcf(%rsp)
je 0x11062c
movq 0x68(%rsp), %rdi
movq 0x100(%rsp), %rdx
movl 0x10c(%rsp), %eax
movl %eax, %ecx
movl $0x1d, %esi
movl $0x4, %r8d
callq 0xd6ab0
jmp 0x11062a
jmp 0x11062c
movl $0x68, %edi
callq 0xd8380
movq %rax, 0x10(%rsp)
jmp 0x11063d
movq 0x10(%rsp), %rdi
movq 0x110(%rsp), %rsi
callq 0xdfaf0
jmp 0x110651
movq 0x68(%rsp), %rdi
movq 0x10(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rcx
movq 0x100(%rsp), %r8
movl $0x1c, %edx
callq 0xdca80
movq %rax, 0x8(%rsp)
jmp 0x110686
movq 0x8(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x1106bd
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
movl $0x68, %esi
callq 0xd8620
jmp 0x110767
leaq 0xe0(%rsp), %rdi
callq 0xd5290
cmpq $0x0, %rax
jbe 0x1106ea
movq 0x68(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
leaq 0xe0(%rsp), %rsi
callq *%rax
jmp 0x1106e8
jmp 0x1106ea
movq 0x80(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x70(%rsp)
movq 0x80(%rsp), %rax
movq $0x0, (%rax)
movq 0x80(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x110732
movq (%rsp), %rdi
callq 0xdb660
movq (%rsp), %rdi
movl $0x10, %esi
callq 0xd8620
movq 0x70(%rsp), %rax
movq %rax, 0x120(%rsp)
movl $0x1, 0xac(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0xd4550
movq 0x120(%rsp), %rax
addq $0x128, %rsp            # imm = 0x128
retq
leaq 0xe0(%rsp), %rdi
callq 0xd4550
movq 0xd8(%rsp), %rdi
callq 0xe0be0
nopw %cs:(%rax,%rax)