// File: 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
movq %rsp, %rbp
subq $0x310, %rsp            # imm = 0x310
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0xc9f0(%rip), %rax      # 0x120b0
movq %rax, -0x18(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
movl $0x6, -0x24(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0xc9db(%rip), %rdx      # 0x120bc
callq 0x115b0
movl %eax, -0x28(%rbp)
cmpl $-0x1, %eax
je 0x5769
cmpl $0x69, -0x28(%rbp)
jne 0x5704
leaq 0x15dad(%rip), %rax     # 0x1b4a8
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x5764
cmpl $0x6c, -0x28(%rbp)
jne 0x5720
leaq 0x15d97(%rip), %rax     # 0x1b4a8
movq (%rax), %rdi
callq 0x4490
movsd %xmm0, -0x20(%rbp)
jmp 0x5762
cmpl $0x64, -0x28(%rbp)
jne 0x5754
leaq 0x15d7b(%rip), %rax     # 0x1b4a8
movq (%rax), %rdi
callq 0x4560
movl %eax, -0x24(%rbp)
cmpl $0x1, -0x24(%rbp)
jge 0x5745
movl $0x1, -0x24(%rbp)
cmpl $0x1e, -0x24(%rbp)
jle 0x5752
movl $0x1e, -0x24(%rbp)
jmp 0x5760
movl $0x2, -0x4(%rbp)
jmp 0x5ed8
jmp 0x5762
jmp 0x5764
jmp 0x56d3
movq -0x18(%rbp), %rax
movq %rax, -0x238(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0x230(%rbp)
callq 0x4630
movq -0x238(%rbp), %rsi
movq -0x230(%rbp), %rdx
leaq -0x48(%rbp), %rdi
callq 0x6a20
jmp 0x579d
leaq 0x15c94(%rip), %rsi     # 0x1b438
leaq 0x15ca5(%rip), %rdx     # 0x1b450
leaq -0x48(%rbp), %rdi
callq 0x49b0
movl %eax, -0x23c(%rbp)
jmp 0x57bc
movl -0x23c(%rbp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x23d(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x4220
leaq -0x49(%rbp), %rdi
callq 0x43f0
movb -0x23d(%rbp), %al
testb $0x1, %al
jne 0x57f1
jmp 0x59aa
movq -0x18(%rbp), %rax
movq %rax, -0x250(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0x248(%rbp)
callq 0x4630
movq -0x250(%rbp), %rsi
movq -0x248(%rbp), %rdx
leaq -0x80(%rbp), %rdi
callq 0x6a20
jmp 0x5828
leaq -0x81(%rbp), %rdi
callq 0x43f0
movq 0x15785(%rip), %rdi     # 0x1afc0
leaq 0xc881(%rip), %rsi      # 0x120c3
callq 0x4090
movq %rax, -0x258(%rbp)
jmp 0x5850
leaq -0x80(%rbp), %rdi
movq %rdi, -0x268(%rbp)
callq 0x4320
movq -0x268(%rbp), %rdi
movq %rax, -0xb0(%rbp)
callq 0x41d0
movq %rax, -0xb8(%rbp)
leaq -0xb9(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x4150
movq -0x260(%rbp), %rcx
movq -0xb0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
leaq -0xa8(%rbp), %rdi
callq 0x6ad0
jmp 0x58b0
movq -0x258(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x44e0
movq %rax, -0x270(%rbp)
jmp 0x58cc
movq -0x270(%rbp), %rdi
movq 0x156d6(%rip), %rsi     # 0x1afb0
callq 0x4600
jmp 0x58e1
leaq -0xa8(%rbp), %rdi
callq 0x4738
leaq -0xb9(%rbp), %rdi
callq 0x4640
movl $0x1, -0x4(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x4220
jmp 0x5ed8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x5931
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x4220
leaq -0x49(%rbp), %rdi
callq 0x43f0
jmp 0x5ee4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x81(%rbp), %rdi
callq 0x43f0
jmp 0x5ee4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x599c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x5990
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x4738
leaq -0xb9(%rbp), %rdi
callq 0x4640
leaq -0x80(%rbp), %rdi
callq 0x4220
jmp 0x5ee4
leaq 0x15a9f(%rip), %rsi     # 0x1b450
leaq -0x120(%rbp), %rdi
callq 0xc5d0
leaq -0x150(%rbp), %rdi
callq 0x6b50
leaq -0x169(%rbp), %rdi
movq %rdi, -0x278(%rbp)
callq 0x6b70
movq -0x278(%rbp), %rdx
leaq -0x168(%rbp), %rdi
movl $0x1, %esi
callq 0x6b90
jmp 0x59f6
leaq -0x169(%rbp), %rdi
callq 0x6c00
leaq -0x188(%rbp), %rdi
callq 0x6c20
leaq -0x1a8(%rbp), %rdi
callq 0x4170
movq 0x15577(%rip), %rdi     # 0x1af98
leaq -0x1a8(%rbp), %rsi
callq 0x4380
movq %rax, -0x280(%rbp)
jmp 0x5a36
movq -0x280(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x4690
movb %al, -0x281(%rbp)
jmp 0x5a54
movb -0x281(%rbp), %al
testb $0x1, %al
jne 0x5a63
jmp 0x5e4b
leaq -0x1a8(%rbp), %rdi
movl $0x2c, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x4350
movq %rax, -0x1b0(%rbp)
movq $0x0, -0x1b8(%rbp)
movq -0x1b0(%rbp), %rax
movq %rax, -0x290(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x4270
movq %rax, %rcx
movq -0x290(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x5b5b
movq -0x1b0(%rbp), %rax
incq %rax
movq %rax, -0x298(%rbp)
leaq -0x1a8(%rbp), %rdi
movq %rdi, -0x2a0(%rbp)
callq 0x4270
movq -0x2a0(%rbp), %rsi
movq -0x298(%rbp), %rdx
movq %rax, %rcx
leaq -0x1d8(%rbp), %rdi
callq 0x4520
jmp 0x5afe
leaq -0x1d8(%rbp), %rdi
callq 0x4440
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x4540
movq %rax, -0x1b8(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x4738
jmp 0x5b5b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x169(%rbp), %rdi
callq 0x6c00
jmp 0x5ebe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x5e9a
movq -0x1b0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x1f8(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x4520
jmp 0x5b7b
movq 0x15446(%rip), %rdi     # 0x1afc8
leaq 0xc4af(%rip), %rsi      # 0x12038
callq 0x46a0
movq %rax, -0x2a8(%rbp)
jmp 0x5b97
movq -0x2a8(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0x44e0
movq %rax, -0x2b0(%rbp)
jmp 0x5bb3
movq -0x2b0(%rbp), %rdi
leaq 0xc49f(%rip), %rsi      # 0x12060
callq 0x46a0
movq %rax, -0x2b8(%rbp)
jmp 0x5bcf
movq -0x2b8(%rbp), %rdi
movq -0x1b8(%rbp), %rsi
callq 0x4300
movq %rax, -0x2c0(%rbp)
jmp 0x5beb
movq -0x2c0(%rbp), %rdi
leaq 0xc493(%rip), %rsi      # 0x1208c
callq 0x46a0
movq %rax, -0x2c8(%rbp)
jmp 0x5c07
movl $0x100, %edi            # imm = 0x100
callq 0x6c40
movl %eax, -0x2cc(%rbp)
jmp 0x5c19
movq -0x2c8(%rbp), %rdi
movl -0x2cc(%rbp), %eax
movl %eax, -0x1fc(%rbp)
movl -0x1fc(%rbp), %esi
callq 0x40b0
movq %rax, -0x2d8(%rbp)
jmp 0x5c40
movq -0x2d8(%rbp), %rdi
movsd -0x20(%rbp), %xmm0
callq 0x4210
movq %rax, -0x2e0(%rbp)
jmp 0x5c5a
movl $0x100, %edi            # imm = 0x100
callq 0x6c60
movl %eax, -0x2e4(%rbp)
jmp 0x5c6c
movq -0x2e0(%rbp), %rdi
movl -0x2e4(%rbp), %eax
movl %eax, -0x200(%rbp)
movl -0x200(%rbp), %esi
callq 0x44d0
movq %rax, -0x2f0(%rbp)
jmp 0x5c93
movq -0x2f0(%rbp), %rdi
movq 0x1530f(%rip), %rsi     # 0x1afb0
callq 0x4600
jmp 0x5ca8
leaq -0x150(%rbp), %rdi
movq %rdi, -0x2f8(%rbp)
callq 0x6c80
movq -0x2f8(%rbp), %rdx
leaq -0x120(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0xd290
jmp 0x5cd7
jmp 0x5d91
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x5e3d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x5e3d
movq -0x58(%rbp), %rdi
movq %rdi, -0x300(%rbp)
callq 0x44a0
movq %rax, %rsi
leaq -0x220(%rbp), %rdi
callq 0x6ca0
jmp 0x5d2a
movq -0x300(%rbp), %rdi
callq 0x4110
movq 0x15283(%rip), %rdi     # 0x1afc0
leaq -0x220(%rbp), %rsi
callq 0x44e0
jmp 0x5d4b
movl $0x4, -0x224(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x6ce0
callq 0x45a0
jmp 0x5d87
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x6ce0
callq 0x45a0
jmp 0x5d8c
jmp 0x5e1b
jmp 0x5e3d
leaq -0x168(%rbp), %rdi
movl $0x1, %esi
callq 0x6d00
jmp 0x5da4
leaq -0x168(%rbp), %rdi
movq %rdi, -0x308(%rbp)
callq 0x6d90
movq -0x308(%rbp), %rdi
movq $0x0, (%rax)
callq 0x6d90
movq -0x308(%rbp), %rsi
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, 0x8(%rax)
movsd -0x20(%rbp), %xmm0
movq -0x1b8(%rbp), %rcx
leaq -0x188(%rbp), %rdi
leaq -0x150(%rbp), %rdx
callq 0x5360
jmp 0x5e00
movl -0x24(%rbp), %esi
leaq -0x168(%rbp), %rdi
callq 0x5150
jmp 0x5e11
movl $0x0, -0x224(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0x4738
movl -0x224(%rbp), %eax
testl %eax, %eax
je 0x5e38
jmp 0x5e33
jmp 0x5a1a
jmp 0x5a1a
leaq -0x1f8(%rbp), %rdi
callq 0x4738
jmp 0x5e9a
movl $0x0, -0x4(%rbp)
movl $0x1, -0x224(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x4738
leaq -0x188(%rbp), %rdi
callq 0x6dc0
leaq -0x168(%rbp), %rdi
callq 0x6dc0
leaq -0x150(%rbp), %rdi
callq 0x6e20
leaq -0x120(%rbp), %rdi
callq 0x6e40
jmp 0x5ed8
leaq -0x1a8(%rbp), %rdi
callq 0x4738
leaq -0x188(%rbp), %rdi
callq 0x6dc0
leaq -0x168(%rbp), %rdi
callq 0x6dc0
leaq -0x150(%rbp), %rdi
callq 0x6e20
leaq -0x120(%rbp), %rdi
callq 0x6e40
jmp 0x5ee4
movl -0x4(%rbp), %eax
addq $0x310, %rsp            # imm = 0x310
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x4620
movq %rax, %rdi
callq 0x6cd0
nopw %cs:(%rax,%rax)
nop