// File: /workspace/github_cpp_makefile_star2_O3/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 0x18(%rdx), %rbx
addq $0x8, %rdx
movq %rdx, 0x28(%rsp)
cmpq %rdx, %rbx
je 0x40a5
movq %rdi, %r12
leaq 0x8(%rsp), %r13
movq 0x20(%rbx), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0x738d(%rip), %rsi      # 0xb328
movq %r13, %r15
movq %r13, %rdi
callq 0x4ac0
movq %rbx, 0x30(%rsp)
movq 0x28(%rbx), %rbp
testq %rbp, %rbp
je 0x4082
movl $0x1, %ebx
xorl %r13d, %r13d
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
leaq -0x1(%rbx), %rcx
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rax
cmpq %rax, %rcx
jne 0x401a
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4b5a
movq 0x8(%rsp), %rdi
leaq (%rdi,%r13), %rdx
addq $-0x18, %rdx
addq %r13, %rdi
movq %rdx, %rsi
callq 0x3b71
movq 0x8(%rsp), %rdi
addq %r13, %rdi
movsd (%rsp), %xmm0
callq 0x3d15
testb $0x1, %bpl
je 0x4068
movq 0x8(%rsp), %rdx
addq %r13, %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0x3b71
movq %r12, %rdi
movsd (%rsp), %xmm0
callq 0x3d15
movq 0x10(%r12), %rax
movupd (%r14), %xmm0
movupd (%r12), %xmm1
movupd %xmm0, (%r12)
movq 0x10(%r14), %rcx
movq %rcx, 0x10(%r12)
movupd %xmm1, (%r14)
movq %rax, 0x10(%r14)
movq %rbp, %rax
shrq %rax
incq %rbx
addq $0x18, %r13
cmpq $0x1, %rbp
movq %rax, %rbp
ja 0x3fc0
movq %r15, %r13
movq %r15, %rdi
callq 0x498e
movq 0x30(%rsp), %rdi
callq 0x31b0
movq %rax, %rbx
cmpq 0x28(%rsp), %rax
jne 0x3f88
movq (%r14), %rax
movq 0x8(%r14), %rcx
cmpq %rcx, %rax
movq 0x20(%rsp), %rdi
je 0x40fa
xorl %edx, %edx
testq %rdi, %rdi
setg %dl
leaq 0x3f8b(%rip), %rsi      # 0x8050
movsd (%rsi,%rdx,8), %xmm0
movq %rdi, %rdx
negq %rdx
cmovsq %rdi, %rdx
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
testq %rdi, %rdi
je 0x40f1
movsd (%rax), %xmm2
divsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, (%rax)
addq $0x10, %rax
cmpq %rcx, %rax
jne 0x40dc
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x498e
movq %rbx, %rdi
callq 0x3470