// File: /workspace/github_cpp_makefile_star2_O3/princelab[P]emass/emass.cpp
// Function: main
int main(int argc, char * argv[])
{
  const char * isotopefn = "ISOTOPE.DAT";
  double limit = 0;
  int digits = 6;
  int opt;
  while((opt = getopt(argc, argv, "i:l:d:")) != -1) {
    if(opt == 'i')
      isotopefn = optarg;
    else if(opt == 'l')
      limit = atof(optarg);
    else if(opt == 'd') {
      digits = atoi(optarg);
      if(digits < 1)
	digits = 1;
      if(digits > 30)
	digits = 30;
    } else
      return 2;
  }

  if(!init_data(isotopefn, sad, em)) {
    string tmp(isotopefn);
    wcerr << "Could not open isotope file: " 
	  << wstring(tmp.begin(), tmp.end()) << endl;
    return 1;
  }

  Parser p(em);

  FormMap fm;

  Pattern result(1);
  Pattern tmp;

  wstring line;
  while(getline(wcin, line)) {

    size_t sep_pos = line.find(L',');
    long charge = 0;
    if(sep_pos < line.size() - 1) {
      wstring charge_str = wstring(line, sep_pos + 1, line.size());
      charge = wcstol(charge_str.c_str(), NULL, 0);
    }

    wstring formula(line, 0, sep_pos);
    wcout << L"formula: " << formula 
	  << L" charge : " << charge 
	  << L" limit: "
	  << std::setiosflags(ios::scientific) 
	  << limit 
	  << std::resetiosflags(ios::scientific)
	  << endl;

    // read a formula
    fm.clear();
    try {
      p.write_compound(formula, fm);
    } catch (Parser::Error e) {
      wcerr << e._message;
      continue; // skip to next compound
    }

    // initialize the result
    result.resize(1);
    result.front().mass = 0.0;
    result.front().rel_area = 1.0;

    calculate(tmp, result, fm, limit, charge);
    print_pattern(result, digits);
    
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rsi, %r14
movl %edi, %ebp
leaq 0x3f9d(%rip), %rbx      # 0x80d8
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0x6, 0x4(%rsp)
leaq 0x3f91(%rip), %r15      # 0x80e4
leaq 0x723e(%rip), %r12      # 0xb398
movl $0x1e, %r13d
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x706c
cmpl $0x69, %eax
jne 0x4178
movq (%r12), %rbx
jmp 0x4160
cmpl $-0x1, %eax
je 0x41bd
cmpl $0x64, %eax
je 0x419c
cmpl $0x6c, %eax
jne 0x457c
movq (%r12), %rdi
callq 0x3360
movsd %xmm0, 0x8(%rsp)
jmp 0x4160
movq (%r12), %rdi
callq 0x33e0
cmpl $0x2, %eax
movl $0x1, %ecx
cmovll %ecx, %eax
cmpl $0x1e, %eax
cmovgel %r13d, %eax
movl %eax, 0x4(%rsp)
jmp 0x4160
leaq 0xe0(%rsp), %rdi
leaq 0x100(%rsp), %rdx
movq %rbx, %rsi
callq 0x4b96
leaq 0x714c(%rip), %rsi      # 0xb328
leaq 0x715d(%rip), %rdx      # 0xb340
leaq 0xe0(%rsp), %rdi
callq 0x376c
movl %eax, %ebp
leaq 0xf0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4213
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x32f0
testl %ebp, %ebp
je 0x463b
leaq 0x711e(%rip), %rsi      # 0xb340
leaq 0x100(%rsp), %rdi
callq 0x5a44
leaq 0xb8(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq %rcx, 0x40(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movl $0x10, %edi
callq 0x32d0
movq %rax, 0x30(%rsp)
movq %rax, %rcx
addq $0x10, %rcx
movq %rcx, 0x40(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rcx, 0x38(%rsp)
xorl %eax, %eax
movq %rax, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
leaq 0x60(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movl $0x0, (%rcx)
movq 0x6cfa(%rip), %r14      # 0xafa8
movq (%r14), %rax
movq -0x18(%rax), %rax
movq 0xf0(%r14,%rax), %rdi
testq %rdi, %rdi
je 0x473f
leaq 0x50(%rsp), %r15
leaq 0xb0(%rsp), %rbp
movhps 0x3d56(%rip), %xmm0     # xmm0 = xmm0[0,1],mem[0,1]
movaps %xmm0, 0x160(%rsp)
leaq 0x30(%rsp), %r13
movq (%rdi), %rax
movl $0xa, %esi
callq *0x50(%rax)
movq %r14, %rdi
movq %r15, %rsi
movl %eax, %edx
callq 0x3310
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4586
movq %r15, %rdi
movl $0x2c, %esi
xorl %edx, %edx
callq 0x3290
movq %rax, %r12
movq 0x58(%rsp), %rcx
leaq -0x1(%rcx), %rax
movl $0x0, %ebx
cmpq %rax, %r12
jae 0x437a
leaq 0x1(%r12), %rdx
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x32a0
movq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x33c0
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x437a
movq 0x20(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x32a0
movl $0x9, %edx
movq 0x6c30(%rip), %rdi      # 0xafc8
leaq 0x3cc1(%rip), %rsi      # 0x8060
callq 0x30e0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x6c13(%rip), %rdi      # 0xafc8
callq 0x30e0
movq %rax, %r12
movl $0xa, %edx
movq %rax, %rdi
leaq 0x3cbc(%rip), %rsi      # 0x8088
callq 0x30e0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3030
movq %rax, %r12
movl $0x8, %edx
movq %rax, %rdi
leaq 0x3cc6(%rip), %rsi      # 0x80b4
callq 0x30e0
movq (%r12), %rax
movq -0x18(%rax), %rax
orl $0x100, 0x18(%r12,%rax) # imm = 0x100
movq %r12, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x3450
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rcx
andl $0xfffffeff, 0x18(%r12,%rcx) # imm = 0xFFFFFEFF
movq -0x18(%rax), %rax
movq 0xf0(%r12,%rax), %rdi
testq %rdi, %rdi
je 0x4744
movq (%rdi), %rax
movl $0xa, %esi
callq *0x50(%rax)
movq %r12, %rdi
movl %eax, %esi
callq 0x3270
movq %rax, %rdi
callq 0x3390
movq %rbp, %rdi
callq 0x58e2
leaq 0x100(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
callq 0x63d2
movl $0x1, %esi
movq %r13, %rdi
callq 0x4bde
movq 0x30(%rsp), %rax
movaps 0x160(%rsp), %xmm0
movups %xmm0, (%rax)
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
movsd 0x8(%rsp), %xmm0
movq %rbx, %rcx
callq 0x3f4f
movq %r13, %rdi
movl 0x4(%rsp), %esi
callq 0x3db7
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x44da
movq 0x20(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
movq (%r14), %rax
movq -0x18(%rax), %rax
movq 0xf0(%r14,%rax), %rdi
testq %rdi, %rdi
jne 0x42e7
jmp 0x473f
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x482b
movq %rbx, %rdi
callq 0x3370
leaq 0x80(%rsp), %rcx
movq %rcx, 0x70(%rsp)
movq (%rax), %rsi
movq 0x8(%rax), %rax
leaq (%rsi,%rax,4), %rdx
leaq 0x70(%rsp), %rdi
callq 0x4cd2
movq %rbx, %rdi
callq 0x3100
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rdx
movq 0x6a7a(%rip), %rdi      # 0xafc0
callq 0x30e0
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x4572
movq 0x80(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
callq 0x3400
jmp 0x44b9
movl $0x2, %eax
jmp 0x472d
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x45a7
movq 0x60(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x45c4
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x45db
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
leaq 0xb0(%rsp), %rdi
callq 0x4d72
leaq 0x150(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x460e
movq 0x150(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4634
movq 0x120(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
xorl %eax, %eax
jmp 0x472d
leaq 0x100(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
movq %rbx, %rsi
callq 0x4b96
movq 0x6966(%rip), %rdi      # 0xafc0
leaq 0x3a8a(%rip), %rsi      # 0x80eb
callq 0x3090
movq %rax, %rbx
movq 0x100(%rsp), %rsi
movq 0x108(%rsp), %rdx
addq %rsi, %rdx
leaq 0xc0(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0xb0(%rsp), %rdi
callq 0x5870
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movq %rbx, %rdi
callq 0x30e0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rax
movq 0xf0(%rbx,%rax), %rdi
testq %rdi, %rdi
je 0x4749
movq (%rdi), %rax
movl $0xa, %esi
callq *0x50(%rax)
movq %rbx, %rdi
movl %eax, %esi
callq 0x3270
movq %rax, %rdi
callq 0x3390
movq 0xb0(%rsp), %rdi
cmpq %r14, %rdi
je 0x4707
movq 0xc0(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4728
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x32f0
movl $0x1, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3380
callq 0x3380
callq 0x3380
jmp 0x4828
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x477d
movq 0x80(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
callq 0x3400
jmp 0x482b
jmp 0x4789
movq %rax, %rdi
callq 0x4bd3
jmp 0x4793
movq %rax, %rbx
jmp 0x47f1
jmp 0x47c4
movq %rax, %rbx
jmp 0x48a1
movq %rax, %rbx
leaq 0xf0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x48bb
movq 0xf0(%rsp), %rsi
jmp 0x480e
movq %rax, %rbx
jmp 0x48bb
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
cmpq %r14, %rdi
je 0x47f1
movq 0xc0(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x48bb
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x32f0
jmp 0x48bb
jmp 0x4823
jmp 0x4828
jmp 0x4823
jmp 0x4823
movq %rax, %rbx
jmp 0x484c
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x484c
movq 0x20(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
leaq 0x60(%rsp), %rax
movq 0x50(%rsp), %rdi
cmpq %rax, %rdi
je 0x486d
movq 0x60(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x488a
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x48a1
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
leaq 0xb0(%rsp), %rdi
callq 0x4d72
leaq 0x100(%rsp), %rdi
callq 0x4c12
movq %rbx, %rdi
callq 0x3470
nop