// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movl %ecx, %ebp
movl %edx, %ebx
movq %rsi, %r12
movq %rdi, 0x40(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
testl %ecx, %ecx
jne 0x53b98
leaq 0x2c434(%rip), %rcx     # 0x7ffb8
leaq 0x18(%rsp), %rdi
movl $0x18, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x417c0
leal 0x7(%rbp), %r15d
leal -0x1(%rbp), %ecx
movl $0x2, %r13d
shlq %cl, %r13
decq %r13
movq %rbx, 0x38(%rsp)
movl %ebx, %ecx
shlq %cl, %r13
shrl $0x3, %r15d
movb $0x1, 0xf(%rsp)
cmpl $0x0, 0x40(%r12)
movq %r14, %rbx
jne 0x53c24
movl 0x50(%r12), %eax
leal -0x1(%rax), %ecx
cmpl %r15d, %ecx
setae 0xf(%rsp)
shll $0x3, %eax
movq 0x38(%rsp), %rsi
cmpl %esi, %eax
setbe %cl
leal (%rsi,%rbp), %edx
cmpl %eax, %edx
seta %dl
orb %cl, %dl
je 0x53c01
movl $0x18, %r8d
leaq 0x2c3d2(%rip), %rcx     # 0x7ffd1
jmp 0x53c14
xorl %ebp, %eax
orl %esi, %eax
jne 0x53c24
movl $0x24, %r8d
leaq 0x2c3d6(%rip), %rcx     # 0x7ffea
leaq 0x18(%rsp), %rdi
movq 0x8(%rdi), %rdx
xorl %esi, %esi
callq 0x417c0
cmpq $0x0, 0x20(%rsp)
je 0x53c4e
movq 0x40(%rsp), %rdi
movq (%rdi), %rax
leaq 0x18(%rsp), %rsi
callq *0x20(%rax)
movl $0x68, %esi
movq %r12, %rdi
callq 0x40870
jmp 0x53d82
movq %r12, 0x10(%rsp)
movq 0x40(%rsp), %r12
movq 0x8(%rbx), %r14
movl %r15d, %edx
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x40b10
movq (%rbx), %rdx
leaq 0x48(%rsp), %rsi
movq %r14, %rdi
callq 0x3f380
movq %r12, %r14
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq 0x38(%rsp), %r15
movl %r15d, %edx
movl %ebp, %ecx
callq 0x40840
testq %rax, %rax
je 0x53caf
movq %rax, %rsi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x411a0
jmp 0x53d6c
addl %r15d, %ebp
cmpl $0x41, %ebp
jb 0x53cd4
leaq 0x18(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x2c348(%rip), %rcx     # 0x8000f
movl $0x2c, %r8d
xorl %esi, %esi
callq 0x417c0
movl $0x10, %edi
callq 0x40800
movq %rax, %rbp
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x3f8f0
notq %r13
movq %r14, %rdi
movl $0x1b, %esi
movq %rbp, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x403f0
cmpb $0x0, 0xf(%rsp)
je 0x53d1e
movq %r14, %rdi
movl $0x11, %esi
movq %rbx, %rdx
callq 0x419b0
testl %r15d, %r15d
je 0x53d3c
movl %r15d, %ecx
movq %r14, %rdi
movl $0x1d, %esi
movq %rbx, %rdx
movl $0x4, %r8d
callq 0x403f0
movl $0x68, %edi
callq 0x40800
movq %rax, %r12
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x41bf0
movq %r12, %rsi
movl $0x1c, %edx
movq %rbp, %rcx
movq %rbx, %r8
callq 0x41390
movq %rbp, %rbx
cmpq $0x0, 0x20(%rsp)
je 0x53d82
movq (%r14), %rax
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq *0x20(%rax)
movq (%rbx), %r14
movq $0x0, (%rbx)
movq %rbx, %rdi
callq 0x40f50
movl $0x10, %esi
movq %rbx, %rdi
callq 0x40870
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x53dbd
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x40870
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x68, %esi
movq %r12, %rdi
jmp 0x53de7
movq %rax, %rbx
movl $0x10, %esi
movq %rbp, %rdi
callq 0x40870
jmp 0x53df9
jmp 0x53df6
jmp 0x53df6
jmp 0x53df6
jmp 0x53df6
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x53e15
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x40870
movq %rbx, %rdi
callq 0x41f00
nop