// File: /workspace/github_cpp_makefile_star2_O2/princelab[P]emass/emass.cpp
// Function: calculate(std::vector<peak, std::allocator<peak>>&, std::vector<peak, std::allocator<peak>>&, std::map<unsigned long, long, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, long>>>&, double, long)
void calculate(Pattern & tmp, Pattern & result, FormMap & fm, 
	       double limit, long charge)
{
  for(FormMap::iterator i = fm.begin(); i != fm.end(); i++) {
    size_t atom_index = (*i).first;
    SuperAtomList sal = sad[atom_index];
    ulong n = (*i).second;
    ulong j = 0;
    while(n > 0) {
      size_t sz = sal.size();
      if(j == sz) { 
	sal.resize(sz + 1); // Make new superatom from previous
                            // largest superatom. We are trying to
                            // avoid copying on assignment here.
	convolute_basic(sal[j], sal[j - 1], sal[j - 1]);
	prune(sal[j], limit);
      }
      if(n & 1) { // digit is 1, convolute result
	convolute_basic(tmp , result, sal[j]);
	prune(tmp, limit);
	swap(tmp, result);  // Hopefully the swap implementation
                            // will not copy all elements.
      }
      n >>= 1; 
      j++;
    }
  }
  
  // take charge into account
  for(Pattern::iterator i = result.begin(); i != result.end(); ++i) {
    if(charge > 0)
      (*i).mass = (*i).mass / abs(charge) - ELECTRON_MASS;
    else if (charge < 0)
      (*i).mass = (*i).mass / abs(charge) + ELECTRON_MASS;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, 0x20(%rsp)
movsd %xmm0, (%rsp)
movq %rsi, %r14
movq %rdi, %r12
movq 0x18(%rdx), %rbx
addq $0x8, %rdx
movq %rdx, 0x28(%rsp)
pushq $0x18
popq %r15
cmpq 0x28(%rsp), %rbx
je 0x3d46
imulq $0x18, 0x20(%rbx), %rsi
addq 0x767e(%rip), %rsi      # 0xb2f8
leaq 0x8(%rsp), %rdi
callq 0x443c
movq %rbx, 0x30(%rsp)
movq 0x28(%rbx), %r13
xorl %ebp, %ebp
pushq $0x1
popq %rbx
testq %r13, %r13
je 0x3d2a
leaq -0x1(%rbx), %rcx
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
cqto
idivq %r15
cmpq %rax, %rcx
jne 0x3cea
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x4496
movq 0x8(%rsp), %rdi
leaq (%rdi,%rbp), %rdx
addq $-0x18, %rdx
addq %rbp, %rdi
movq %rdx, %rsi
callq 0x3949
movq 0x8(%rsp), %rdi
addq %rbp, %rdi
movsd (%rsp), %xmm0
callq 0x3aae
testb $0x1, %r13b
je 0x3d1b
movq 0x8(%rsp), %rdx
addq %rbp, %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0x3949
movq %r12, %rdi
movsd (%rsp), %xmm0
callq 0x3aae
movq %r12, %rdi
movq %r14, %rsi
callq 0x52aa
shrq %r13
incq %rbx
addq $0x18, %rbp
jmp 0x3c92
leaq 0x8(%rsp), %rdi
callq 0x4354
movq 0x30(%rsp), %rdi
callq 0x3170
movq %rax, %rbx
jmp 0x3c63
movq (%r14), %rax
movq 0x8(%r14), %rcx
xorl %edx, %edx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
setg %dl
leaq 0x32df(%rip), %rsi      # 0x7040
movsd (%rsi,%rdx,8), %xmm0
movq %rdi, %rdx
negq %rdx
cmovsq %rdi, %rdx
cvtsi2sd %rdx, %xmm1
cmpq %rcx, %rax
je 0x3d95
testq %rdi, %rdi
je 0x3d8f
movsd (%rax), %xmm2
divsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, (%rax)
addq $0x10, %rax
jmp 0x3d75
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4354
movq %rbx, %rdi
callq 0x33f0