// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movq %rsi, %r15
movq %rdi, %rbx
movq %rsi, 0x60(%rsp)
movq (%rsi), %rdi
testq %rdi, %rdi
je 0x2d249
movq %r13, %rsi
callq 0x24416
testl %eax, %eax
js 0x2d249
leaq 0x28(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x34efc(%rip), %rsi     # 0x6211f
leaq 0x34f3f(%rip), %rdx     # 0x62169
leaq 0x18(%rsp), %rdi
callq 0x117f6
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0x2d000
jmp 0x2d501
movq 0x10(%r15), %rax
movq 0x18(%r15), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x2d4aa
xorl %r14d, %r14d
movl $0x0, 0x14(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movq %rbx, 0x58(%rsp)
movq %r15, 0x50(%rsp)
movq %r13, 0x48(%rsp)
movq (%rax,%r14,8), %r12
cmpl $0x1, 0x20(%r12)
jne 0x2d2b9
movl 0x28(%r12), %eax
movl %eax, 0x14(%rsp)
cmpl $0x0, 0x40(%r12)
sete %al
movq %rax, 0x40(%rsp)
movq 0x50(%r12), %rax
movq %rax, 0x38(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x24416
testl %eax, %eax
js 0x2d487
testb $0x1, 0x40(%rsp)
je 0x2d4d0
movl %eax, %ebp
movq 0x18(%rbx), %rbx
movl 0x124(%rbx), %r13d
movl %r13d, %eax
shrl $0x4, %eax
cmpl $0xffffffe, %eax        # imm = 0xFFFFFFE
ja 0x2d2fb
leal 0x10(%r13), %eax
movl %eax, 0x124(%rbx)
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, 0x8(%rsp)
movl $0x68, %edi
callq 0xf370
movq %rax, %r15
movq 0xc8(%rbx), %rsi
leaq 0x18(%rsp), %rdi
callq 0x233a4
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x23374
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movq 0x38(%rsp), %rdx
callq 0x23374
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
leaq 0x68(%rsp), %rdx
movq %rbx, %rcx
callq 0x24330
movq 0x8(%rsp), %rax
movq %r15, (%rax)
movslq 0x14(%rsp), %rax
movq 0x48(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%rax,8), %rbx
movl $0x68, %edi
callq 0xf370
movq %rax, %r13
leaq 0x60(%rbx), %rdx
leaq 0x20(%rbx), %rcx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x24330
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x20118
movl $0x68, %edi
callq 0xf370
movq %rax, %r13
movq 0x58(%rsp), %rbx
movq 0x18(%rbx), %rax
movq 0x98(%rax), %rsi
leaq 0x18(%rsp), %rdi
callq 0x233a4
movl %ebp, %edx
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x23374
movl $0x4, %edx
leaq 0x88(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
callq 0x23374
movq %r13, %rdi
leaq 0x18(%rsp), %rsi
leaq 0x68(%rsp), %rdx
movq %rbp, %rcx
callq 0x24330
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x20118
movq 0x28(%rbx), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x2d44b
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x2d455
leaq 0x8(%rsp), %rdx
callq 0x2044c
movl $0x68, %esi
movq %r12, %rdi
callq 0xf380
movl $0x68, %edi
callq 0xf370
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x24386
movq 0x50(%rsp), %r15
movq 0x10(%r15), %rax
movq %r12, (%rax,%r14,8)
incq %r14
movq 0x10(%r15), %rax
movq 0x18(%r15), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r14
movq 0x48(%rsp), %r13
jl 0x2d28c
movq 0x28(%rbx), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x2d4c2
movq %r15, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x2d4cc
leaq 0x60(%rsp), %rdx
callq 0x2044c
movb $0x1, %al
jmp 0x2d51e
leaq 0x28(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x34c8a(%rip), %rsi     # 0x6216a
leaq 0x34cab(%rip), %rdx     # 0x62192
leaq 0x18(%rsp), %rdi
callq 0x117f6
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0x2d000
movb $0x1, 0x20(%rbx)
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x2d51c
movq 0x28(%rsp), %rsi
incq %rsi
callq 0xf380
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2d534
jmp 0x2d54b
movq %rax, %rbx
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x2d57a
movq 0x28(%rsp), %rsi
incq %rsi
jmp 0x2d575
movq %rax, %rbx
jmp 0x2d57a
jmp 0x2d567
jmp 0x2d562
movq %rax, %rbx
movq %r12, %r15
jmp 0x2d56d
jmp 0x2d567
jmp 0x2d562
jmp 0x2d567
movq %rax, %rbx
jmp 0x2d56d
movq %rax, %rbx
movq %r13, %r15
movl $0x68, %esi
movq %r15, %rdi
callq 0xf380
movq %rbx, %rdi
callq 0xf730