// File: /workspace/github_cpp_makefile_star2_O1/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), %r15
addq $0x8, %rdx
movq %rdx, 0x28(%rsp)
cmpq %rdx, %r15
je 0x40c1
movq %rdi, %r12
leaq 0x8(%rsp), %r13
movq 0x20(%r15), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0x736f(%rip), %rsi      # 0xb328
movq %r13, %rbx
movq %r13, %rdi
callq 0x4ad4
movq %r15, 0x30(%rsp)
movq 0x28(%r15), %r15
testq %r15, %r15
je 0x409e
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x10(%rsp), %rsi
subq 0x8(%rsp), %rsi
sarq $0x3, %rsi
movabsq $-0x5555555555555555, %rax # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rsi
cmpq %rsi, %r13
jne 0x4031
incq %rsi
movq %rbx, %rdi
callq 0x4b6e
movq 0x8(%rsp), %rdi
leaq (%rdi,%rbp), %rdx
addq $-0x18, %rdx
addq %rbp, %rdi
movq %rdx, %rsi
callq 0x3ba1
movq 0x8(%rsp), %rdi
addq %rbp, %rdi
movsd (%rsp), %xmm0
callq 0x3d46
testb $0x1, %r15b
je 0x4084
movq 0x8(%rsp), %rdx
addq %rbp, %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0x3ba1
movq %r12, %rdi
movsd (%rsp), %xmm0
callq 0x3d46
movupd (%r12), %xmm0
movq 0x10(%r12), %rax
movq (%r14), %rcx
movq %rcx, (%r12)
movq 0x8(%r14), %rcx
movq %rcx, 0x8(%r12)
movq 0x10(%r14), %rcx
movq %rcx, 0x10(%r12)
movupd %xmm0, (%r14)
movq %rax, 0x10(%r14)
movq %r15, %rax
shrq %rax
incq %r13
addq $0x18, %rbp
cmpq $0x1, %r15
movq %rax, %r15
ja 0x3fdb
movq %rbx, %r13
movq %rbx, %rdi
callq 0x49a0
movq 0x30(%rsp), %rdi
callq 0x31b0
movq %rax, %r15
cmpq 0x28(%rsp), %rax
jne 0x3fa6
movq (%r14), %rax
movq 0x8(%r14), %rcx
cmpq %rcx, %rax
movq 0x20(%rsp), %rdi
je 0x4113
xorl %edx, %edx
testq %rdi, %rdi
setg %dl
leaq 0x3f5f(%rip), %rsi      # 0x8040
movsd (%rsi,%rdx,8), %xmm0
movq %rdi, %rdx
negq %rdx
cmovsq %rdi, %rdx
cvtsi2sd %rdx, %xmm1
testq %rdi, %rdi
je 0x410a
movsd (%rax), %xmm2
divsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, (%rax)
addq $0x10, %rax
cmpq %rcx, %rax
jne 0x40f5
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x49a0
movq %rbx, %rdi
callq 0x3480