// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %rdx, %r15
movq %rdi, %r14
movq %rsi, 0x8(%rsp)
movq (%rsi), %rdi
testq %rdi, %rdi
movq %r14, 0x18(%rsp)
je 0x2d4c6
movq %r15, %rsi
callq 0x24534
testl %eax, %eax
js 0x2d4c6
leaq 0x30(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x34c34(%rip), %rsi     # 0x620cf
leaq 0x34c77(%rip), %rdx     # 0x62119
leaq 0x20(%rsp), %rdi
callq 0x1183c
movq 0x18(%rsp), %rbx
movq 0x18(%rbx), %rdi
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x2d276
jmp 0x2d77b
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x18(%rcx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x2d71a
xorl %ebx, %ebx
movl $0x0, 0x14(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
movq %r15, 0x50(%rsp)
movq (%rax,%rbx,8), %rbp
cmpl $0x1, 0x20(%rbp)
jne 0x2d529
movl 0x28(%rbp), %eax
movl %eax, 0x14(%rsp)
cmpl $0x0, 0x40(%rbp)
sete %al
movq %rax, 0x48(%rsp)
movq 0x50(%rbp), %rax
movq %rax, 0x40(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x24534
testl %eax, %eax
js 0x2d6f7
testb $0x1, 0x48(%rsp)
je 0x2d745
movl %eax, %r12d
movq 0x18(%r14), %r14
movl 0x124(%r14), %r15d
movl %r15d, %eax
shrl $0x4, %eax
cmpl $0xffffffe, %eax        # imm = 0xFFFFFFE
ja 0x2d56d
leal 0x10(%r15), %eax
movl %eax, 0x124(%r14)
movl $0x28, %edi
callq 0xf370
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
xorl %ecx, %ecx
movq %rcx, 0x20(%rax)
movl $0x3f, 0x8(%rax)
movq %rcx, (%rax)
movq %rax, (%rsp)
movl $0x68, %edi
callq 0xf370
movq %rax, %r13
movq 0xc8(%r14), %rsi
leaq 0x20(%rsp), %rdi
callq 0x2349c
leaq 0x58(%rsp), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2346c
leaq 0x78(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
movq 0x40(%rsp), %rdx
callq 0x2346c
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x58(%rsp), %rdx
movq %r14, %rcx
callq 0x2444e
movq (%rsp), %rax
movq %r13, (%rax)
movslq 0x14(%rsp), %rax
movq 0x50(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%rax,8), %r14
movl $0x68, %edi
callq 0xf370
movq %rax, %r15
leaq 0x60(%r14), %rdx
leaq 0x20(%r14), %rcx
movq %rax, %rdi
movq %r14, %rsi
callq 0x2444e
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x200b0
movl $0x68, %edi
callq 0xf370
movq %rax, %r15
movq 0x18(%rsp), %r14
movq 0x18(%r14), %rax
movq 0x98(%rax), %rsi
leaq 0x20(%rsp), %rdi
callq 0x2349c
movl %r12d, %edx
leaq 0x58(%rsp), %rdi
xorl %esi, %esi
callq 0x2346c
movl $0x4, %edx
leaq 0x78(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2346c
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x58(%rsp), %rdx
movq %r12, %rcx
callq 0x2444e
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x200b0
movq 0x28(%r14), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x2d6b3
movq (%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x2d6bb
movq %rsp, %rdx
callq 0x203d6
movl $0x68, %esi
movq %rbp, %rdi
callq 0xf380
movq 0x8(%rsp), %r14
movl $0x68, %edi
callq 0xf370
movq %rax, %r15
movq %rax, %rdi
movq %r13, %rsi
callq 0x244a4
movq 0x10(%r14), %rax
movq %r15, (%rax,%rbx,8)
movq 0x18(%rsp), %r14
movq 0x50(%rsp), %r15
incq %rbx
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x18(%rcx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbx
jl 0x2d503
movq 0x28(%r14), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x2d737
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x2d741
leaq 0x8(%rsp), %rdx
callq 0x203d6
movb $0x1, %al
jmp 0x2d798
leaq 0x30(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x349c5(%rip), %rsi     # 0x6211a
leaq 0x349e6(%rip), %rdx     # 0x62142
leaq 0x20(%rsp), %rdi
callq 0x1183c
movq 0x18(%rsp), %rbx
movq 0x18(%rbx), %rdi
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x2d276
movb $0x1, 0x20(%rbx)
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x2d796
movq 0x30(%rsp), %rsi
incq %rsi
callq 0xf380
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2d7ae
jmp 0x2d7c5
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x2d7ee
movq 0x30(%rsp), %rsi
incq %rsi
jmp 0x2d7e9
movq %rax, %rbx
jmp 0x2d7ee
jmp 0x2d7db
jmp 0x2d7d6
jmp 0x2d7db
jmp 0x2d7db
jmp 0x2d7d6
jmp 0x2d7db
movq %rax, %rbx
jmp 0x2d7e1
movq %rax, %rbx
movq %r15, %r13
movl $0x68, %esi
movq %r13, %rdi
callq 0xf380
movq %rbx, %rdi
callq 0xf730