// File: /workspace/github_cpp_makefile_star2_O1/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 $0x158, %rsp            # imm = 0x158
movq %rsi, %r14
movl %edi, %ebp
leaq 0x3f74(%rip), %rbx      # 0x80c8
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
movl $0x6, 0xc(%rsp)
leaq 0x3f68(%rip), %r15      # 0x80d4
leaq 0x7225(%rip), %r12      # 0xb398
movl $0x1e, %r13d
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6f8c
cmpl $0x69, %eax
jne 0x4191
movq (%r12), %rbx
jmp 0x4179
cmpl $-0x1, %eax
je 0x41d6
cmpl $0x64, %eax
je 0x41b5
cmpl $0x6c, %eax
jne 0x4595
movq (%r12), %rdi
callq 0x3360
movsd %xmm0, 0x28(%rsp)
jmp 0x4179
movq (%r12), %rdi
callq 0x33f0
cmpl $0x2, %eax
movl $0x1, %ecx
cmovll %ecx, %eax
cmpl $0x1e, %eax
cmovgel %r13d, %eax
movl %eax, 0xc(%rsp)
jmp 0x4179
leaq 0xd8(%rsp), %rdi
leaq 0xf8(%rsp), %rdx
movq %rbx, %rsi
callq 0x4baa
leaq 0x7133(%rip), %rsi      # 0xb328
leaq 0x7144(%rip), %rdx      # 0xb340
leaq 0xd8(%rsp), %rdi
callq 0x377c
movl %eax, %ebp
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x422c
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x32f0
testl %ebp, %ebp
je 0x4654
leaq 0x7105(%rip), %rsi      # 0xb340
leaq 0xf8(%rsp), %rdi
callq 0x5a60
leaq 0xb0(%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, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movl $0x10, %edi
callq 0x32d0
movq %rax, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq %rax, %rcx
addq $0x10, %rcx
movq %rcx, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rcx, 0x18(%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 0x6cdc(%rip), %r14      # 0xafa8
movq (%r14), %rax
movq -0x18(%rax), %rax
movq 0xf0(%r14,%rax), %rdi
testq %rdi, %rdi
je 0x4758
leaq 0x50(%rsp), %r15
leaq 0xa8(%rsp), %rbp
leaq 0x10(%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 0x459f
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 0x4389
leaq 0x1(%r12), %rdx
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x32a0
movq 0x30(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x33d0
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x4389
movq 0x40(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x32a0
movl $0x9, %edx
movq 0x6c21(%rip), %rdi      # 0xafc8
leaq 0x3ca2(%rip), %rsi      # 0x8050
callq 0x30e0
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x6c04(%rip), %rdi      # 0xafc8
callq 0x30e0
movq %rax, %r12
movl $0xa, %edx
movq %rax, %rdi
leaq 0x3c9d(%rip), %rsi      # 0x8078
callq 0x30e0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3030
movq %rax, %r12
movl $0x8, %edx
movq %rax, %rdi
leaq 0x3ca7(%rip), %rsi      # 0x80a4
callq 0x30e0
movq (%r12), %rax
movq -0x18(%rax), %rax
orl $0x100, 0x18(%r12,%rax) # imm = 0x100
movq %r12, %rdi
movsd 0x28(%rsp), %xmm0
callq 0x3460
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 0x475d
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 0x5908
leaq 0xf8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq %rbp, %rdx
callq 0x631e
movl $0x1, %esi
movq %r13, %rdi
callq 0x4bf2
movq 0x10(%rsp), %rax
movq $0x0, (%rax)
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, 0x8(%rax)
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
movsd 0x28(%rsp), %xmm0
movq %rbx, %rcx
callq 0x3f6d
movq %r13, %rdi
movl 0xc(%rsp), %esi
callq 0x3ddb
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x44f3
movq 0x40(%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 0x42f6
jmp 0x4758
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x4844
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 0x4ce6
movq %rbx, %rdi
callq 0x3100
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rdx
movq 0x6a61(%rip), %rdi      # 0xafc0
callq 0x30e0
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x458b
movq 0x80(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
callq 0x3410
jmp 0x44d2
movl $0x2, %eax
jmp 0x4746
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x45c0
movq 0x60(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x45dd
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x45f4
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
leaq 0xa8(%rsp), %rdi
callq 0x4d7e
leaq 0x148(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4627
movq 0x148(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
leaq 0x118(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x464d
movq 0x118(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
xorl %eax, %eax
jmp 0x4746
leaq 0xf8(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
movq %rbx, %rsi
callq 0x4baa
movq 0x694d(%rip), %rdi      # 0xafc0
leaq 0x3a61(%rip), %rsi      # 0x80db
callq 0x3090
movq %rax, %rbx
movq 0xf8(%rsp), %rsi
movq 0x100(%rsp), %rdx
addq %rsi, %rdx
leaq 0xb8(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0xa8(%rsp), %rdi
callq 0x5894
movq 0xa8(%rsp), %rsi
movq 0xb0(%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 0x4762
movq (%rdi), %rax
movl $0xa, %esi
callq *0x50(%rax)
movq %rbx, %rdi
movl %eax, %esi
callq 0x3270
movq %rax, %rdi
callq 0x3390
movq 0xa8(%rsp), %rdi
cmpq %r14, %rdi
je 0x4720
movq 0xb8(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4741
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x32f0
movl $0x1, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3380
callq 0x3380
callq 0x3380
jmp 0x4841
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x4796
movq 0x80(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
callq 0x3410
jmp 0x4844
jmp 0x47a2
movq %rax, %rdi
callq 0x4be7
jmp 0x47ac
movq %rax, %rbx
jmp 0x480a
jmp 0x47dd
movq %rax, %rbx
jmp 0x48ba
movq %rax, %rbx
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x48d4
movq 0xe8(%rsp), %rsi
jmp 0x4827
movq %rax, %rbx
jmp 0x48d4
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
cmpq %r14, %rdi
je 0x480a
movq 0xb8(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x48d4
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x32f0
jmp 0x48d4
jmp 0x483c
jmp 0x4841
jmp 0x483c
jmp 0x483c
movq %rax, %rbx
jmp 0x4865
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x4865
movq 0x40(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
leaq 0x60(%rsp), %rax
movq 0x50(%rsp), %rdi
cmpq %rax, %rdi
je 0x4886
movq 0x60(%rsp), %rax
leaq 0x4(,%rax,4), %rsi
callq 0x32f0
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x48a3
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x48ba
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
leaq 0xa8(%rsp), %rdi
callq 0x4d7e
leaq 0xf8(%rsp), %rdi
callq 0x4c26
movq %rbx, %rdi
callq 0x3480