// File: /workspace/github_cpp_makefile_star2_O2/jstaursky[P]hutch/src/Sleigh/slgh_compile.cc
// Function: MacroBuilder::transferOp(OpTpl*, std::vector<HandleTpl*, std::allocator<HandleTpl*>>&)
bool MacroBuilder::transferOp(OpTpl *op,vector<HandleTpl *> &params)

{ // Fix handle details of a macro generated OpTpl relative to its specific invocation
  // and transfer it into the output stream
  VarnodeTpl *outvn = op->getOut();
  int4 handleIndex = 0;
  int4 plus;
  bool hasrealsize = false;
  uintb realsize = 0;

  if (outvn != (VarnodeTpl *)0) {
    plus = outvn->transfer(params);
    if (plus >= 0) {
      reportError("Cannot currently assign to bitrange of macro parameter that is a temporary");
      return false;
    }
  }
  for(int4 i=0;i<op->numInput();++i) {
    VarnodeTpl *vn = op->getIn(i);
    if (vn->getOffset().getType() == ConstTpl::handle) {
      handleIndex = vn->getOffset().getHandleIndex();
      hasrealsize = (vn->getSize().getType() == ConstTpl::real);
      realsize = vn->getSize().getReal();
    }
    plus = vn->transfer(params);
    if (plus >= 0) {
      if (!hasrealsize) {
	reportError("Problem with bit range operator in macro");
	return false;
      }
      uintb newtemp = slgh->getUniqueAddr(); // Generate a new temporary location

      // Generate a SUBPIECE op that implements the offset_plus
      OpTpl *subpieceop = new OpTpl(CPUI_SUBPIECE);
      VarnodeTpl *newvn = new VarnodeTpl(ConstTpl(slgh->getUniqueSpace()),ConstTpl(ConstTpl::real,newtemp),
					 ConstTpl(ConstTpl::real,realsize));
      subpieceop->setOutput(newvn);
      HandleTpl *hand = params[handleIndex];
      VarnodeTpl *origvn = new VarnodeTpl( hand->getSpace(), hand->getPtrOffset(), hand->getSize() );
      subpieceop->addInput(origvn);
      VarnodeTpl *plusvn = new VarnodeTpl( ConstTpl(slgh->getConstantSpace()), ConstTpl(ConstTpl::real,plus),
					   ConstTpl(ConstTpl::real, 4) );
      subpieceop->addInput(plusvn);
      outvec.push_back(subpieceop);

      delete vn;		// Replace original varnode
      op->setInput(new VarnodeTpl( *newvn ), i); // with output of subpiece
    }
  }
  outvec.push_back(op);
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %r14
movq %rsi, %rbp
movq %rdi, %rbx
movq %rsi, 0x60(%rsp)
movq (%rsi), %rdi
testq %rdi, %rdi
je 0x294b7
movq %r14, %rsi
callq 0x21bd0
testl %eax, %eax
js 0x294b7
leaq 0x2dc59(%rip), %rsi     # 0x570ef
leaq 0x28(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x10c3a
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x292da
jmp 0x29725
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x0, 0x4(%rsp)
movq %rbx, 0x50(%rsp)
movq %r14, 0x20(%rsp)
movq %rbp, 0x48(%rsp)
movq 0x10(%rbp), %rax
movq 0x18(%rbp), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r15
jge 0x296f0
movq (%rax,%r15,8), %r12
cmpl $0x1, 0x20(%r12)
jne 0x2952b
movl 0x28(%r12), %eax
movl %eax, 0x4(%rsp)
cmpl $0x0, 0x40(%r12)
sete %al
movq %rax, 0x18(%rsp)
movq 0x50(%r12), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x21bd0
testl %eax, %eax
js 0x296e8
testb $0x1, 0x18(%rsp)
je 0x29702
movl %eax, %r13d
movq 0x18(%rbx), %rbx
movl 0x124(%rbx), %ebp
movl %ebp, %eax
shrl $0x4, %eax
cmpl $0xffffffe, %eax        # imm = 0xFFFFFFE
ja 0x2956b
leal 0x10(%rbp), %eax
movl %eax, 0x124(%rbx)
pushq $0x28
popq %rdi
callq 0xf370
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
andq $0x0, 0x20(%rax)
movl $0x3f, 0x8(%rax)
andq $0x0, (%rax)
movq %rax, 0x8(%rsp)
pushq $0x68
popq %rdi
callq 0xf370
movq %rax, %r14
movq 0xc8(%rbx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x20e14
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x20de4
movq %r15, 0x58(%rsp)
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
callq 0x20de4
movq %r14, %rdi
leaq 0x28(%rsp), %rsi
leaq 0x68(%rsp), %rdx
movq %rbx, %rcx
callq 0x21aea
movq 0x8(%rsp), %rax
movq %r14, (%rax)
movslq 0x4(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%rax,8), %rbx
pushq $0x68
popq %r15
movq %r15, %rdi
callq 0xf370
movq %rax, %rbp
leaq 0x60(%rbx), %rdx
leaq 0x20(%rbx), %rcx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21aea
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x1deb2
movq %r15, %rdi
callq 0xf370
movq %rax, %rbp
movq 0x50(%rsp), %rbx
movq 0x18(%rbx), %rax
movq 0x98(%rax), %rsi
leaq 0x28(%rsp), %rdi
callq 0x20e14
movl %r13d, %edx
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x20de4
leaq 0x88(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
pushq $0x4
popq %rdx
callq 0x20de4
movq %rbp, %rdi
leaq 0x28(%rsp), %rsi
leaq 0x68(%rsp), %rdx
movq %r15, %rcx
callq 0x21aea
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x1deb2
movq 0x28(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1de74
movq %r12, %rdi
pushq $0x68
popq %r15
movq %r15, %rsi
callq 0xf380
movq %r15, %rdi
callq 0xf370
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x21b40
movq 0x48(%rsp), %rbp
movq 0x10(%rbp), %rax
movq 0x58(%rsp), %r15
movq %r12, (%rax,%r15,8)
movq 0x20(%rsp), %r14
incq %r15
jmp 0x294e3
movq 0x28(%rbx), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1de74
movb $0x1, %al
jmp 0x29731
leaq 0x2da31(%rip), %rsi     # 0x5713a
leaq 0x28(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x10c3a
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x292da
leaq 0x28(%rsp), %rdi
callq 0xf1a0
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x29747
jmp 0x29756
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0xf1a0
jmp 0x29783
movq %rax, %rbx
jmp 0x29783
jmp 0x29775
jmp 0x2976d
jmp 0x2976d
movq %rax, %rbx
movq %r12, %r14
jmp 0x29778
jmp 0x29775
jmp 0x2976d
movq %rax, %rbx
movq %rbp, %r14
jmp 0x29778
movq %rax, %rbx
pushq $0x68
popq %rsi
movq %r14, %rdi
callq 0xf380
movq %rbx, %rdi
callq 0xf6e0
nop