// File: /workspace/github_cpp_makefile_star2_O2/atfrank[P]PCASA/lib/PDB.cpp
// Function: PDB::readPDB(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool, bool)
Molecule* PDB::readPDB(const std::string ifile, const int model, const std::string format, const bool hetFlag, const bool remFlag){
  std::ifstream pdbFile;
  std::istream* inp;
  std::string line;
  int currModel=0;
  bool modelFlag=true;
  bool firstModelFlag=false;
  Molecule *mol;
  Chain *chnEntry=new Chain;
  Residue *resEntry=new Residue;
  Atom *atmEntry; //Created in heap by processAtomLine
  Atom *lastAtom;
  PDB pdb;
  std::string PDBID;
  unsigned int year;

  if (format.compare("CHARMM") == 0){
    mol=new MoleculeCHARMM;
  }
  else{
    mol=new Molecule;
  }

  atmEntry=NULL;
  lastAtom=NULL;
  mol->setCopyFlag(false);

  if (ifile.compare("-") == 0){ //Input from pipe
    inp=&std::cin;
  }
  else{ //Input from file
    pdbFile.open((ifile).c_str(), std::ios::in);
    inp=&pdbFile;
    PDBID=Misc::toupper(ifile.substr(0,4));
    mol->setTag(PDBID);
  }

  while (inp->good() && !(inp->eof())){

    getline(*inp,line);
    if (line.compare(0,6,"HEADER") == 0){
      std::stringstream(line.substr(57,2)) >> year;
      if (year < 71){
        mol->setYear(year+2000);
      }
      else{
        mol->setYear(year+1900);
      }
      if (Misc::trim(line.substr(62,4)).length() == 4){
        PDBID=Misc::toupper(Misc::trim(line.substr(62,4)));
      }
    }
    else if (line.compare(0,6,"EXPDTA") == 0){
      mol->setExp(Misc::trim(line.substr(10,60)));
    }
    else if (line.size() > 6 && line.compare(0,6,"MODEL ") == 0){
      std::stringstream(line.substr(10,4)) >> currModel;
      if ((model==0 && firstModelFlag==false) || currModel==model){
        firstModelFlag=true; //Processed first model
        modelFlag=true;
      }
      else{
        modelFlag=false;
      }
    }
    else if (line.size() > 6 && line.compare(0,6,"REMARK") == 0 && remFlag == true){
      mol->addRemark(line);
    }
    else if (modelFlag && line.size() >= 54 && (line.compare(0,4,"ATOM") == 0 || (line.compare(0,6,"HETATM") == 0 && hetFlag == true) || (line.compare(0,5,"HETAT") == 0 && hetFlag == true))){
      //Atom
      atmEntry=pdb.processAtomLine(line, lastAtom);
      atmEntry->setTag(PDBID);
      if (lastAtom != NULL && lastAtom->getResId() == atmEntry->getResId()){
        if (lastAtom->getResName().compare(atmEntry->getResName()) == 0 && (lastAtom->getAlt().compare(0,1," ") == 0 || lastAtom->getAlt().compare(0,1,atmEntry->getAlt(),0,1) == 0)){
          //Eliminate other alternate locations besides first
          mol->addAtom(atmEntry);
        }
        else{
          continue;
        }
      }
      else{
        mol->addAtom(atmEntry);
      }

      //Residue/Chain
      if (lastAtom != NULL && atmEntry->getChainId().compare(lastAtom->getChainId()) != 0) {
        //Store last
        chnEntry->addResidue(resEntry);
        mol->addResidue(resEntry);
        mol->addChain(chnEntry);
        //Create new
        chnEntry=new Chain;
        resEntry=new Residue;
      }
      else if (lastAtom != NULL && lastAtom->getResId() != atmEntry->getResId()){
        chnEntry->addResidue(resEntry);
        mol->addResidue(resEntry);
        resEntry=new Residue;
      }
      else{
        //Do nothing
      }
      
      atmEntry->setResidue(resEntry);
      atmEntry->setChain(chnEntry);
      resEntry->addAtom(atmEntry);
      chnEntry->addAtom(atmEntry);

      //Update for next atom
      lastAtom=atmEntry;
    }
    else{
      continue;
    }
  }
  //Add remaining residues and chains
  if (resEntry->getAtmVecSize() > 0){
    chnEntry->addResidue(resEntry);
    mol->addResidue(resEntry);
    mol->addChain(chnEntry);
  }
  
  if (ifile.compare("-") != 0 && pdbFile.is_open()){
    pdbFile.close();
  }

  if (mol->getAtmVecSize() == 0){
    std::cerr << std::endl << "Error: The PDB file \"" << ifile << "\" ";
    std::cerr << "did not contain any valid atoms" << std::endl << std::endl;
    exit(1);
  }

  mol->setICodeFlag(mol->checkICode());

  return mol;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x548, %rsp            # imm = 0x548
movl %r8d, 0x30(%rsp)
movl %ecx, 0x38(%rsp)
movq %rdx, %rbx
movl %esi, 0x68(%rsp)
movq %rdi, 0x40(%rsp)
leaq 0x340(%rsp), %rdi
callq 0x4360
leaq 0x98(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
andl $0x0, 0x3c(%rsp)
pushq $0x30
popq %rdi
callq 0x4320
movq %rax, 0x20(%rsp)
movq %rax, %rdi
callq 0x17750
pushq $0x18
popq %rdi
callq 0x4320
movq %rax, 0x18(%rsp)
movq %rax, %rdi
callq 0x23990
leaq 0x168(%rsp), %rdi
callq 0x1f88c
leaq 0xb8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0xe59a(%rip), %rsi      # 0x2e502
movq %rbx, %rdi
callq 0x4100
testl %eax, %eax
je 0x1ff8f
movl $0x1a0, %edi            # imm = 0x1A0
callq 0x4320
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rdi
callq 0x19e1c
jmp 0x1ffa8
movl $0x1a0, %edi            # imm = 0x1A0
callq 0x4320
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rdi
callq 0x217f8
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x1a516
leaq 0xe43f(%rip), %rsi      # 0x2e3fa
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x4100
testl %eax, %eax
je 0x20050
movq (%rbx), %rsi
leaq 0x340(%rsp), %rbx
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x41e0
leaq 0x48(%rsp), %rdi
pushq $0x4
popq %rcx
movq 0x40(%rsp), %rsi
xorl %edx, %edx
callq 0x4340
leaq 0x1b8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x18d7e
leaq 0xa8(%rsp), %r14
leaq 0x1b8(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x4390
movq %r15, %rdi
callq 0x4170
leaq 0x48(%rsp), %rdi
callq 0x4170
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1a550
leaq 0x360(%rsp), %rax
jmp 0x2005b
movq 0x3c4f81(%rip), %rbx    # 0x3e4fd8
leaq 0x20(%rbx), %rax
movq %rax, 0x70(%rsp)
movb $0x1, 0xf(%rsp)
xorl %ebp, %ebp
leaq 0x88(%rsp), %r12
movl 0x30(%rsp), %eax
xorb $0x1, %al
movl %eax, 0x30(%rsp)
leaq 0x128(%rsp), %r13
movq $0x0, 0x28(%rsp)
movq %rbp, 0x78(%rsp)
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq 0x70(%rsp), %rcx
cmpl $0x0, (%rcx,%rax)
jne 0x20692
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4080
movq %r12, %rdi
xorl %esi, %esi
pushq $0x6
popq %rdx
leaq 0xe50b(%rip), %rcx      # 0x2e5ca
callq 0x43f0
testl %eax, %eax
je 0x202b4
movq %r12, %rdi
xorl %esi, %esi
pushq $0x6
popq %rdx
leaq 0xe4f6(%rip), %rcx      # 0x2e5d1
callq 0x43f0
testl %eax, %eax
je 0x2032a
cmpq $0x7, 0x90(%rsp)
jb 0x2008f
movq %r12, %rdi
xorl %esi, %esi
pushq $0x6
popq %rdx
leaq 0xe4d6(%rip), %rcx      # 0x2e5d8
callq 0x43f0
testl %eax, %eax
je 0x2060a
cmpq $0x7, 0x90(%rsp)
jb 0x2008f
movq %r12, %rdi
xorl %esi, %esi
pushq $0x6
popq %rdx
leaq 0xe4b2(%rip), %rcx      # 0x2e5df
callq 0x43f0
testl %eax, %eax
setne %al
orb 0x30(%rsp), %al
je 0x204ce
cmpq $0x36, 0x90(%rsp)
setae %al
andb 0xf(%rsp), %al
cmpb $0x1, %al
jne 0x2008f
movq %r12, %rdi
xorl %esi, %esi
pushq $0x4
popq %rdx
leaq 0xd4c8(%rip), %rcx      # 0x2d630
callq 0x43f0
testl %eax, %eax
je 0x201b8
movq %r12, %rdi
xorl %esi, %esi
pushq $0x6
popq %rdx
leaq 0xe466(%rip), %rcx      # 0x2e5e6
callq 0x43f0
testl %eax, %eax
sete %al
testb %al, 0x38(%rsp)
jne 0x201b8
movq %r12, %rdi
xorl %esi, %esi
pushq $0x5
popq %rdx
leaq 0xe44e(%rip), %rcx      # 0x2e5ed
callq 0x43f0
testl %eax, %eax
setne %al
movl 0x38(%rsp), %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x2008f
leaq 0xc8(%rsp), %rdi
movq %r12, %rsi
callq 0x4430
leaq 0x168(%rsp), %rdi
leaq 0xc8(%rsp), %rbp
movq %rbp, %rsi
movq 0x28(%rsp), %rdx
callq 0x20934
movq %rax, %r14
movq %rbp, %rdi
callq 0x4170
movq %r14, %rdi
leaq 0xa8(%rsp), %rsi
callq 0xed10
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x204e0
callq 0xea2c
movl (%rax), %ebp
movq %r14, %rdi
callq 0xea2c
cmpl (%rax), %ebp
movq 0x28(%rsp), %rdi
jne 0x204e0
callq 0xea0c
movq %rax, %rbp
movq %r14, %rdi
callq 0xea0c
movq %rbp, %rdi
movq %rax, %rsi
callq 0x4040
testl %eax, %eax
jne 0x2008f
movq 0x28(%rsp), %rdi
callq 0xe9fc
movq %rax, %rdi
xorl %esi, %esi
pushq $0x1
popq %rdx
leaq 0xe35e(%rip), %rcx      # 0x2e5c4
callq 0x43f0
testl %eax, %eax
je 0x202a2
movq 0x28(%rsp), %rdi
callq 0xe9fc
movq %rax, %rbp
movq %r14, %rdi
callq 0xe9fc
movq %rbp, %rdi
xorl %esi, %esi
pushq $0x1
popq %rdx
movq %rax, %rcx
xorl %r8d, %r8d
movq %rdx, %r9
callq 0x4060
testl %eax, %eax
jne 0x2008f
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1a23a
jmp 0x204f9
leaq 0x48(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
pushq $0x39
popq %rdx
pushq $0x2
popq %rcx
callq 0x4340
leaq 0x1b8(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
pushq $0x18
popq %rdx
callq 0x44b0
movq %rbp, %rdi
leaq 0x6c(%rsp), %rsi
callq 0x4550
movq %rbp, %rdi
callq 0x4260
leaq 0x48(%rsp), %rdi
callq 0x4170
movl 0x6c(%rsp), %eax
cmpl $0x46, %eax
ja 0x203a0
addl $0x7d0, %eax            # imm = 0x7D0
movl %eax, 0x1b8(%rsp)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x1a51c
jmp 0x203b9
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
pushq $0xa
popq %rdx
pushq $0x3c
popq %rcx
callq 0x4340
leaq 0xe8(%rsp), %rbp
movq %rbp, %rdi
leaq 0xe275(%rip), %rsi      # 0x2e5c4
movq %r13, %rdx
callq 0x58d8
leaq 0x1b8(%rsp), %r14
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
movq %rbp, %rdx
callq 0x18be4
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1a52e
movq %r14, %rdi
callq 0x4170
leaq 0xe8(%rsp), %rdi
callq 0x4170
leaq 0x48(%rsp), %rdi
callq 0x4170
jmp 0x2008f
addl $0x76c, %eax            # imm = 0x76C
movl %eax, 0x1b8(%rsp)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x1a51c
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
pushq $0x3e
popq %rdx
pushq $0x4
popq %rcx
callq 0x4340
leaq 0x148(%rsp), %r15
movq %r15, %rdi
leaq 0xe1e6(%rip), %rsi      # 0x2e5c4
movq %r13, %r14
movq %r13, %rdx
callq 0x58d8
movq %r15, %r13
leaq 0x1b8(%rsp), %r15
movq %r15, %rdi
leaq 0x48(%rsp), %rbp
movq %rbp, %rsi
movq %r13, %rdx
callq 0x18be4
movq 0x1c0(%rsp), %rax
movq %rax, 0x80(%rsp)
movq %r15, %rdi
callq 0x4170
movq %r13, %rdi
callq 0x4170
movq %rbp, %rdi
callq 0x4170
cmpq $0x4, 0x80(%rsp)
movq %r14, %r13
jne 0x2008f
movq %r13, %rdi
movq %r12, %rsi
pushq $0x3e
popq %rdx
pushq $0x4
popq %rcx
callq 0x4340
leaq 0x108(%rsp), %rbp
movq %rbp, %rdi
leaq 0xe160(%rip), %rsi      # 0x2e5c4
leaq 0x37(%rsp), %rdx
callq 0x58d8
leaq 0x48(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x18be4
leaq 0x1b8(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x18d7e
leaq 0xa8(%rsp), %rdi
movq %rbp, %rsi
callq 0x4390
movq %rbp, %rdi
callq 0x4170
movq %r14, %rdi
callq 0x4170
leaq 0x108(%rsp), %rdi
callq 0x4170
movq %r13, %rdi
callq 0x4170
jmp 0x2008f
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x1c80e
jmp 0x2008f
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1a23a
cmpq $0x0, 0x28(%rsp)
je 0x205c7
movq %r14, %rdi
callq 0xea1c
movq %rax, %rbp
movq 0x28(%rsp), %rdi
callq 0xea1c
movq %rbp, %rdi
movq %rax, %rsi
callq 0x4040
testl %eax, %eax
je 0x2057a
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1777c
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1a572
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x1a5a2
pushq $0x30
popq %rdi
callq 0x4320
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rdi
callq 0x17750
pushq $0x18
popq %rdi
callq 0x4320
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
callq 0x23990
jmp 0x205c7
movq 0x28(%rsp), %rdi
callq 0xea2c
movl (%rax), %ebp
movq %r14, %rdi
callq 0xea2c
cmpl (%rax), %ebp
je 0x205c7
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1777c
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1a572
pushq $0x18
popq %rdi
callq 0x4320
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
callq 0x23990
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0xed7a
movq %r14, %rdi
movq 0x20(%rsp), %rsi
callq 0xeda2
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x239aa
movb $0x1, 0xf(%rsp)
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x177a8
movq %r14, 0x28(%rsp)
jmp 0x2008f
leaq 0x48(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
pushq $0xa
popq %rdx
pushq $0x4
popq %rcx
callq 0x4340
leaq 0x1b8(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
pushq $0x18
popq %rdx
callq 0x44b0
movq %rbp, %r15
movq %rbp, %rdi
leaq 0x3c(%rsp), %rsi
callq 0x41d0
movq 0x78(%rsp), %rbp
movl 0x68(%rsp), %r14d
testl %r14d, %r14d
sete 0xf(%rsp)
movq %r15, %rdi
callq 0x4260
leaq 0x48(%rsp), %rdi
callq 0x4170
testb $0x1, %bpl
sete %al
andb 0xf(%rsp), %al
cmpl %r14d, 0x3c(%rsp)
sete %cl
orb %al, %cl
movb %cl, 0xf(%rsp)
movzbl %bpl, %ebp
pushq $0x1
popq %rax
cmovnel %eax, %ebp
jmp 0x2008a
movq 0x18(%rsp), %rdi
callq 0x23aee
testl %eax, %eax
je 0x206cd
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1777c
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1a572
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x1a5a2
leaq 0xdd26(%rip), %rsi      # 0x2e3fa
movq 0x40(%rsp), %rdi
callq 0x4100
testl %eax, %eax
je 0x20700
leaq 0x3b8(%rsp), %rdi
callq 0x40b0
testb %al, %al
je 0x20700
leaq 0x340(%rsp), %rdi
callq 0x4090
movq 0x10(%rsp), %rdi
callq 0x1ac18
testl %eax, %eax
je 0x20770
movq 0x10(%rsp), %rdi
callq 0x1a5f4
movzbl %al, %esi
movq 0x10(%rsp), %rdi
callq 0x1a5ec
leaq 0xa8(%rsp), %rdi
callq 0x4170
leaq 0x168(%rsp), %rdi
callq 0x21812
leaq 0x88(%rsp), %rdi
callq 0x4170
leaq 0x340(%rsp), %rdi
callq 0x40e0
movq 0x10(%rsp), %rax
addq $0x548, %rsp            # imm = 0x548
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3c4879(%rip), %rdi    # 0x3e4ff0
callq 0x4180
leaq 0xde70(%rip), %rsi      # 0x2e5f3
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
movq 0x40(%rsp), %rsi
callq 0x42c0
leaq 0xde6a(%rip), %rsi      # 0x2e609
movq %rax, %rdi
callq 0x4300
movq 0x3c4842(%rip), %rdi    # 0x3e4ff0
leaq 0xde57(%rip), %rsi      # 0x2e60c
callq 0x4300
movq %rax, %rdi
callq 0x4180
movq %rax, %rdi
callq 0x4180
pushq $0x1
popq %rdi
callq 0x4380
jmp 0x207e1
movq %rax, %rbx
pushq $0x30
popq %rsi
movq 0x20(%rsp), %rdi
jmp 0x20858
movq %rax, %rbx
pushq $0x18
popq %rsi
movq 0x18(%rsp), %rdi
jmp 0x20858
jmp 0x208f5
jmp 0x2084b
jmp 0x208af
jmp 0x208f5
jmp 0x20885
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4170
jmp 0x20816
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x4170
jmp 0x20828
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
jmp 0x208ec
jmp 0x208f5
jmp 0x208f5
jmp 0x208f5
jmp 0x208af
jmp 0x208f5
movq %rax, %rbx
movl $0x1a0, %esi            # imm = 0x1A0
movq 0x10(%rsp), %rdi
callq 0x4330
jmp 0x208f8
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
callq 0x4170
jmp 0x20877
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
jmp 0x208a4
jmp 0x208f5
jmp 0x208af
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
callq 0x4260
jmp 0x208b2
jmp 0x208f5
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0x4170
jmp 0x208b2
jmp 0x208af
jmp 0x208f5
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
jmp 0x208ec
movq %rax, %rbx
pushq $0x18
popq %rsi
movq 0x18(%rsp), %rdi
jmp 0x208d3
jmp 0x208da
movq %rax, %rbx
pushq $0x30
popq %rsi
movq 0x20(%rsp), %rdi
callq 0x4330
jmp 0x20912
movq %rax, %rbx
jmp 0x20912
jmp 0x208f5
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x4170
jmp 0x208f8
jmp 0x208f5
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x4170
leaq 0x168(%rsp), %rdi
callq 0x21812
leaq 0x88(%rsp), %rdi
callq 0x4170
leaq 0x340(%rsp), %rdi
callq 0x40e0
movq %rbx, %rdi
callq 0x4560