// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movl %edi, %r12d
leaq 0x32f1(%rip), %r14      # 0x70c8
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
pushq $0x6
popq %rax
movl %eax, 0xc(%rsp)
leaq 0x32e6(%rip), %r13      # 0x70d4
leaq 0x7573(%rip), %rbx      # 0xb368
pushq $0x1
popq %rbp
movl %r12d, %edi
movq %r15, %rsi
movq %r13, %rdx
callq 0x6b60
cmpl $0x69, %eax
jne 0x3e10
movq (%rbx), %r14
jmp 0x3df8
cmpl $-0x1, %eax
je 0x3e50
cmpl $0x64, %eax
je 0x3e33
cmpl $0x6c, %eax
jne 0x40de
movq (%rbx), %rdi
callq 0x3310
movsd %xmm0, 0x10(%rsp)
jmp 0x3df8
movq (%rbx), %rdi
callq 0x3380
cmpl $0x2, %eax
cmovll %ebp, %eax
cmpl $0x1e, %eax
pushq $0x1e
popq %rcx
cmovgel %ecx, %eax
movl %eax, 0xc(%rsp)
jmp 0x3df8
leaq 0xd8(%rsp), %rdi
leaq 0xf8(%rsp), %rdx
movq %r14, %rsi
callq 0x44cc
leaq 0x7489(%rip), %rsi      # 0xb2f8
leaq 0x749a(%rip), %rdx      # 0xb310
leaq 0xd8(%rsp), %rdi
callq 0x36bc
movl %eax, %ebx
leaq 0xd8(%rsp), %rdi
callq 0x30f0
testl %ebx, %ebx
je 0x4122
leaq 0x746f(%rip), %rsi      # 0xb310
leaq 0xf8(%rsp), %rdi
callq 0x5564
leaq 0x38(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x18(%rsp), %rdi
pushq $0x1
popq %rsi
leaq 0x60(%rsp), %rdx
callq 0x4508
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %rcx
andq $0x0, 0x10(%rcx)
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movaps %xmm0, (%rcx)
andl $0x0, (%rax)
leaq 0x60(%rsp), %r12
leaq 0x158(%rsp), %r13
leaq 0x30(%rsp), %rbp
leaq 0x18(%rsp), %r14
movhps 0x311c(%rip), %xmm0     # xmm0 = xmm0[0,1],mem[0,1]
movaps %xmm0, 0xa0(%rsp)
movq 0x7085(%rip), %rdi      # 0xafb0
movq %r12, %rsi
callq 0x3290
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x40e6
movq %r12, %rdi
pushq $0x2c
popq %rsi
xorl %edx, %edx
callq 0x3240
movq %rax, %rbx
movq 0x68(%rsp), %rcx
leaq -0x1(%rcx), %rax
movl $0x0, %r15d
cmpq %rax, %rbx
jae 0x3f94
leaq 0x1(%rbx), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x3250
movq 0x158(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3360
movq %rax, %r15
movq %r13, %rdi
callq 0x34c0
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x3250
movq 0x7025(%rip), %rdi      # 0xafd0
leaq 0x309e(%rip), %rsi      # 0x7050
callq 0x3430
movq %rax, %rdi
movq %r13, %rsi
callq 0x3330
movq %rax, %rdi
leaq 0x30ac(%rip), %rsi      # 0x7078
callq 0x3430
movq %rax, %rdi
movq %r15, %rsi
callq 0x3030
movq %rax, %rdi
leaq 0x30be(%rip), %rsi      # 0x70a4
callq 0x3430
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
orl $0x100, 0x18(%rax,%rcx) # imm = 0x100
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x33e0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
andl $0xfffffeff, 0x18(%rax,%rcx) # imm = 0xFFFFFEFF
movq %rax, %rdi
callq 0x3130
movq %rbp, %rdi
callq 0x5440
leaq 0xf8(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x5cc0
movq %r14, %rdi
pushq $0x1
popq %rsi
callq 0x4564
movq 0x18(%rsp), %rax
movaps 0xa0(%rsp), %xmm0
movups %xmm0, (%rax)
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movsd 0x10(%rsp), %xmm0
movq %r15, %rcx
callq 0x3c34
movq %r14, %rdi
movl 0xc(%rsp), %esi
callq 0x3b11
movq %r13, %rdi
callq 0x34c0
jmp 0x3f24
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x421e
movq %rbx, %rdi
callq 0x3320
leaq 0xb8(%rsp), %rdi
movq %rax, %rsi
callq 0x3220
movq %rbx, %rdi
callq 0x30d0
movq 0x6f0b(%rip), %rdi      # 0xafc8
leaq 0xb8(%rsp), %rsi
callq 0x3330
leaq 0xb8(%rsp), %rdi
callq 0x34c0
callq 0x33a0
jmp 0x407d
pushq $0x2
popq %rbp
jmp 0x419f
leaq 0x60(%rsp), %rdi
callq 0x34c0
leaq 0x80(%rsp), %rdi
callq 0x50e6
leaq 0x18(%rsp), %rdi
callq 0x50e6
leaq 0x30(%rsp), %rdi
callq 0x45fc
leaq 0x100(%rsp), %rdi
callq 0x4654
xorl %ebp, %ebp
jmp 0x419f
leaq 0xf8(%rsp), %rdi
leaq 0x30(%rsp), %rdx
movq %r14, %rsi
callq 0x44cc
movq 0x6e8a(%rip), %rdi      # 0xafc8
leaq 0x2f96(%rip), %rsi      # 0x70db
callq 0x3080
movq %rax, %rbx
movq 0xf8(%rsp), %rsi
movq 0x100(%rsp), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x30(%rsp), %rdi
callq 0x535e
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x3330
movq %rax, %rdi
callq 0x3130
leaq 0x30(%rsp), %rdi
callq 0x34c0
leaq 0xf8(%rsp), %rdi
callq 0x30f0
movl %ebp, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x421b
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x34c0
callq 0x33a0
jmp 0x421e
jmp 0x41ce
movq %rax, %rdi
callq 0x4559
jmp 0x41d8
movq %rax, %rbx
jmp 0x41f1
jmp 0x420d
movq %rax, %rbx
jmp 0x424c
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x34c0
leaq 0xf8(%rsp), %rdi
jmp 0x4206
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x30f0
jmp 0x4263
movq %rax, %rbx
jmp 0x4263
jmp 0x4216
jmp 0x4216
movq %rax, %rbx
jmp 0x422b
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x34c0
leaq 0x60(%rsp), %rdi
callq 0x34c0
leaq 0x80(%rsp), %rdi
callq 0x50e6
leaq 0x18(%rsp), %rdi
callq 0x50e6
leaq 0x30(%rsp), %rdi
callq 0x45fc
leaq 0x100(%rsp), %rdi
callq 0x4654
movq %rbx, %rdi
callq 0x33f0
nop