// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6710
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6770
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x6740
testb $0x1, %al
jne 0x53b0
jmp 0x5567
leaq -0x30(%rbp), %rdi
callq 0x67a0
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rsi
leaq 0x1606d(%rip), %rdi     # 0x1b438
callq 0x67c0
movq %rax, %rsi
leaq -0x58(%rbp), %rdi
callq 0x67e0
leaq -0x30(%rbp), %rdi
callq 0x67a0
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
cmpq $0x0, -0x60(%rbp)
jbe 0x5547
leaq -0x58(%rbp), %rdi
callq 0x68d0
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
jne 0x54c5
movq -0x70(%rbp), %rsi
incq %rsi
leaq -0x58(%rbp), %rdi
callq 0x6900
jmp 0x542d
movq -0x68(%rbp), %rsi
leaq -0x58(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
callq 0x6360
movq -0xb0(%rbp), %rdi
movq %rax, -0xa8(%rbp)
movq -0x68(%rbp), %rsi
decq %rsi
callq 0x6360
movq -0xb0(%rbp), %rdi
movq %rax, -0xa0(%rbp)
movq -0x68(%rbp), %rsi
decq %rsi
callq 0x6360
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq %rax, %rdx
callq 0x4e50
jmp 0x548d
movq -0x68(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x6360
movq %rax, %rdi
movsd -0x20(%rbp), %xmm0
callq 0x5060
jmp 0x54a9
jmp 0x54c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x61a0
jmp 0x568d
movq -0x60(%rbp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x552b
movq -0x8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x68(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x6360
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq %rax, %rdx
callq 0x4e50
jmp 0x550e
movq -0x8(%rbp), %rdi
movsd -0x20(%rbp), %xmm0
callq 0x5060
jmp 0x551e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x6990
movq -0x60(%rbp), %rax
shrq %rax
movq %rax, -0x60(%rbp)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x53f5
leaq -0x58(%rbp), %rdi
callq 0x61a0
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x69c0
movq %rax, -0x88(%rbp)
jmp 0x538d
movq -0x10(%rbp), %rdi
callq 0x6490
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6500
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x64c0
testb $0x1, %al
jne 0x55a3
jmp 0x5684
cmpq $0x0, -0x28(%rbp)
jle 0x560a
leaq -0x90(%rbp), %rdi
callq 0x6530
movsd (%rax), %xmm0
movsd %xmm0, -0xd0(%rbp)
movq -0x28(%rbp), %rdi
callq 0x6a00
movsd -0xd0(%rbp), %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd 0xca4c(%rip), %xmm1     # 0x12030
subsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x6530
movsd -0xc8(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x5671
cmpq $0x0, -0x28(%rbp)
jge 0x566f
leaq -0x90(%rbp), %rdi
callq 0x6530
movsd (%rax), %xmm0
movsd %xmm0, -0xe0(%rbp)
movq -0x28(%rbp), %rdi
callq 0x6a00
movsd -0xe0(%rbp), %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd 0xc9e5(%rip), %xmm1     # 0x12030
addsd %xmm1, %xmm0
movsd %xmm0, -0xd8(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x6530
movsd -0xd8(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x5671
jmp 0x5673
leaq -0x90(%rbp), %rdi
callq 0x6690
jmp 0x5577
addq $0xe0, %rsp
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x4620
nopw %cs:(%rax,%rax)