// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, 0x10(%rsp)
movl %ecx, %ebp
movl %edx, %ebx
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rsi, %r12
movq %rdi, %r14
movb $0x0, (%rax)
testl %ecx, %ecx
jne 0x60239
leaq 0x26d69(%rip), %rsi     # 0x86f98
leaq 0x20(%rsp), %rdi
callq 0x51eb0
leal 0x7(%rbp), %r15d
leal -0x1(%rbp), %ecx
pushq $0x2
popq %r13
shlq %cl, %r13
decq %r13
movq %rbx, 0x18(%rsp)
movl %ebx, %ecx
shlq %cl, %r13
shrl $0x3, %r15d
movb $0x1, 0xf(%rsp)
movq %r12, %rbx
cmpl $0x0, 0x40(%r12)
jne 0x602af
movl 0x50(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl %r15d, %ecx
setae 0xf(%rsp)
shll $0x3, %eax
movq 0x18(%rsp), %rsi
cmpl %esi, %eax
setbe %cl
leal (%rsi,%rbp), %edx
cmpl %eax, %edx
seta %dl
orb %cl, %dl
je 0x60298
leaq 0x26d1b(%rip), %rsi     # 0x86fb1
jmp 0x602a5
xorl %ebp, %eax
orl %esi, %eax
jne 0x602af
leaq 0x26d25(%rip), %rsi     # 0x86fca
leaq 0x20(%rsp), %rdi
callq 0x51eb0
cmpq $0x0, 0x28(%rsp)
je 0x602d5
movq (%r14), %rax
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq *0x20(%rax)
pushq $0x68
popq %rsi
movq %rbx, %rdi
callq 0x4f670
jmp 0x603f4
movq %r14, 0x40(%rsp)
movq 0x10(%rsp), %r12
movq 0x8(%r12), %r14
movl %r15d, %edx
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x4fa20
movq (%r12), %rdx
leaq 0x48(%rsp), %rsi
movq %r14, %rdi
callq 0x4d520
movq 0x40(%rsp), %r14
movq %r14, %rdi
movq %rbx, %r15
movq %rbx, %rsi
movq 0x18(%rsp), %rbx
movl %ebx, %edx
movl %ebp, %ecx
callq 0x4f640
testq %rax, %rax
je 0x6033a
pushq $0x1
popq %rdx
movq %rax, %rsi
movq 0x10(%rsp), %rcx
callq 0x50550
jmp 0x603de
addl %ebx, %ebp
cmpl $0x41, %ebp
jb 0x60352
leaq 0x26ca7(%rip), %rsi     # 0x86fef
leaq 0x20(%rsp), %rdi
callq 0x51eb0
pushq $0x10
popq %rdi
callq 0x4f5d0
movq %rax, %rbp
movq %rax, %rdi
movq %r15, %rsi
callq 0x4de50
notq %r13
pushq $0x1b
popq %rsi
movq %r14, %rdi
movq %rbp, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x4efc0
cmpb $0x0, 0xf(%rsp)
je 0x60396
pushq $0x11
popq %rsi
movq %r14, %rdi
movq 0x10(%rsp), %rdx
callq 0x51130
testl %ebx, %ebx
je 0x603b0
movl %ebx, %ecx
pushq $0x1d
popq %rsi
pushq $0x4
popq %r8
movq %r14, %rdi
movq 0x10(%rsp), %rdx
callq 0x4efc0
pushq $0x68
popq %rdi
callq 0x4f5d0
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x51460
pushq $0x1c
popq %rdx
movq %r12, %rsi
movq %rbp, %rcx
movq 0x10(%rsp), %r8
callq 0x50840
movq %rbp, 0x10(%rsp)
cmpq $0x0, 0x28(%rsp)
je 0x603f4
movq (%r14), %rax
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq *0x20(%rax)
movq 0x10(%rsp), %rbx
movq (%rbx), %r14
andq $0x0, (%rbx)
movq %rbx, %rdi
callq 0x501f0
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0x4f670
leaq 0x20(%rsp), %rdi
callq 0x4e020
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x68
popq %rsi
movq %r12, %rdi
jmp 0x60443
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %rbp, %rdi
callq 0x4f670
jmp 0x60455
jmp 0x60452
jmp 0x60452
jmp 0x60452
jmp 0x60452
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x4e020
movq %rbx, %rdi
callq 0x518c0
nop