// File: /workspace/github_cpp_makefile_star2_O2/Cicko[P]Ram-Machine/Program.cpp
// Function: Program::parseInstructions(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void  Program::parseInstructions (string fileData) {
  transform(fileData.begin(), fileData.end(), fileData.begin(), ::tolower);   // transform all characters into lower case characters
  vector<string> normalOps = {"add","sub","mul","div","load","store","read","write"};     // Possible no jump operations
  vector<string> jumpOps = {"jump","jgtz","jzero"};  // Possible jump operations
  bool error = false;
  unsigned lineCount = 0;
  char * input = (char*) fileData.c_str(); // Convert string into c char* type
  char * token = std::strtok(input, "\n"); // Divide it into rows
  string actualTag = "main";

  while (token != NULL && !error) {
    bool validInstruction = false;
    bool newTag = false;
    Instruction * dummy = new Instruction;
    string lineStr (token);

    string tag = getTagFromString(lineStr);

    if( tag != "noTag") {
      actualTag = tag;
      tags_.push_back(new Tag(tag, lineCount));
      newTag = true;
    }

    dummy->setTag(actualTag);
    bool skipLine = false;
    fileData = token;
    istringstream iss (fileData);
    string word;
    iss >> word;

    if (newTag) iss >> word;
    if (word == ":") iss >> word;


    if (find (normalOps.begin(), normalOps.end(), word) != normalOps.end()) {   // Is a normal operation
      dummy->setOp(word);
      iss >> word;
      if (word[0] == '*' || word[0] == '=') {                                   // immediate or indirect
        dummy->setType (word[0]);
        if (word.size() > 1) {
          string operand = word.substr (1,word.size()-1);
          dummy->setOperand(operand);
          validInstruction = true;
        }
        else {
           iss >> word;
           if(std::regex_match (word, std::regex("[0-9]") )) {                  // Operand must be a number
             dummy->setOperand(word);
             validInstruction = true;
           }
           else {
             cout << "At line " << lineCount << " is an invalid operad. Expected number and it get \"" << word << "\"." << endl;
             error = true;
           }
        }
      }
      else if (std::regex_match (word, std::regex("[0-9]"))) {
        dummy->setType('d');
        dummy->setOperand(word);
        validInstruction = true;
      }
    }
    else if (find (jumpOps.begin(), jumpOps.end(), word) != jumpOps.end()) {     // Is a jump operation
        dummy->setOp(word);
        iss >> word;
        dummy->setOperand (word);
        validInstruction = true;
    }
    else if (word.find("halt") != string::npos) {                               // Is a halt operation
      dummy->setOp(word);
      validInstruction = true;
    }

    if (validInstruction) {
      dummy->setLine(lineCount++);
      instructions_.push_back(dummy);
    }


    token = std::strtok(NULL, "\n");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3c8, %rsp            # imm = 0x3C8
movq %rsi, %r12
movq %rdi, 0x10(%rsp)
movq (%rsi), %rbx
movq 0x8(%rsi), %r14
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x615b
movsbl (%rbx,%r15), %edi
callq 0x54e0
movb %al, (%rbx,%r15)
incq %r15
jmp 0x6143
leaq 0xf1cb(%rip), %rsi      # 0x1532d
leaq 0x208(%rsp), %r14
leaq 0xa8(%rsp), %rdx
movq %r14, %rdi
callq 0x5150
leaq 0x228(%rsp), %r15
leaq 0xf1a8(%rip), %rsi      # 0x15331
leaq 0xc8(%rsp), %rdx
movq %r15, %rdi
callq 0x5150
leaq 0x248(%rsp), %r15
leaq 0xf18d(%rip), %rsi      # 0x15335
leaq 0xe8(%rsp), %rdx
movq %r15, %rdi
callq 0x5150
leaq 0x268(%rsp), %r15
leaq 0xf172(%rip), %rsi      # 0x15339
leaq 0x28(%rsp), %rdx
movq %r15, %rdi
callq 0x5150
leaq 0x288(%rsp), %r15
leaq 0xf15a(%rip), %rsi      # 0x1533d
leaq 0x70(%rsp), %rdx
movq %r15, %rdi
callq 0x5150
leaq 0x2a8(%rsp), %r15
leaq 0xf143(%rip), %rsi      # 0x15342
leaq 0x48(%rsp), %rdx
movq %r15, %rdi
callq 0x5150
leaq 0x2c8(%rsp), %r15
leaq 0xf12d(%rip), %rsi      # 0x15348
movq %rsp, %rdx
movq %r15, %rdi
callq 0x5150
leaq 0x2e8(%rsp), %r15
leaq 0xf118(%rip), %rsi      # 0x1534d
leaq 0xf(%rsp), %rdx
movq %r15, %rdi
callq 0x5150
leaq 0x90(%rsp), %rdi
leaq 0x208(%rsp), %rsi
pushq $0x8
popq %rdx
leaq 0xe(%rsp), %rcx
callq 0x6e8a
movl $0xe0, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x208, %rdi            # imm = 0x208
callq 0x51a0
addq $-0x20, %rbx
cmpq $-0x20, %rbx
jne 0x6264
leaq 0xf0ce(%rip), %rsi      # 0x15353
leaq 0x208(%rsp), %r14
leaq 0xa8(%rsp), %rdx
movq %r14, %rdi
callq 0x5150
leaq 0x228(%rsp), %r15
leaq 0xf0ac(%rip), %rsi      # 0x15358
leaq 0xc8(%rsp), %rdx
movq %r15, %rdi
callq 0x5150
leaq 0x248(%rsp), %r15
leaq 0xf092(%rip), %rsi      # 0x1535d
leaq 0xe8(%rsp), %rdx
movq %r15, %rdi
callq 0x5150
leaq 0x48(%rsp), %rdi
leaq 0x208(%rsp), %rsi
pushq $0x3
popq %rdx
leaq 0x28(%rsp), %rcx
callq 0x6e8a
pushq $0x40
popq %rbx
leaq (%rsp,%rbx), %rdi
addq $0x208, %rdi            # imm = 0x208
callq 0x51a0
addq $-0x20, %rbx
cmpq $-0x20, %rbx
jne 0x62f8
movq (%r12), %rdi
leaq 0xeeb0(%rip), %rsi      # 0x151cd
callq 0x5540
movq %rax, %r14
leaq 0xf037(%rip), %rsi      # 0x15363
leaq 0xa8(%rsp), %rdi
leaq 0x208(%rsp), %rdx
callq 0x5150
movq 0x10(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x68(%rsp)
xorl %ebx, %ebx
leaq 0xe8(%rsp), %rbp
leaq 0x388(%rsp), %r13
movl $0x0, 0x8(%rsp)
movq %r12, 0x60(%rsp)
movq %r14, 0x20(%rsp)
testq %r14, %r14
je 0x6870
testb $0x1, %bl
jne 0x6870
pushq $0x70
popq %rdi
callq 0x5360
movq %rax, %r15
movq %rax, %rdi
callq 0x6ec4
leaq 0xc8(%rsp), %rbx
movq %r15, (%rsp)
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
leaq 0x208(%rsp), %rdx
callq 0x5150
leaq 0x3a8(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5490
movq %rbp, %rdi
movq %r15, %rdx
callq 0x6acc
movq %r15, %rdi
callq 0x51a0
movq %rbp, %rdi
leaq 0xef7f(%rip), %rsi      # 0x15368
callq 0x6f71
movl %eax, %ebx
testb %al, %al
je 0x6460
leaq 0xa8(%rsp), %rdi
movq %rbp, %rsi
callq 0x5200
pushq $0x28
popq %rdi
callq 0x5360
movq %rax, 0x18(%rsp)
movq %r13, %r14
movq %r13, %rdi
movq %rbp, %rsi
callq 0x5490
movb $0x1, %r12b
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl 0x8(%rsp), %edx
callq 0x6f7c
movq %r15, 0x208(%rsp)
xorl %r12d, %r12d
movq 0x68(%rsp), %rdi
leaq 0x208(%rsp), %rsi
callq 0x7430
movq %r13, %rdi
callq 0x51a0
movq 0x60(%rsp), %r12
movq (%rsp), %r15
leaq 0x1c8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x5490
addq $0x8, %r15
movq %r15, %rdi
leaq 0x1c8(%rsp), %r14
movq %r14, %rsi
callq 0x5200
movq %r14, %rdi
callq 0x51a0
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x5620
leaq 0x28(%rsp), %r15
leaq 0x208(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
pushq $0x8
popq %rdx
callq 0x5570
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
andq $0x0, 0x30(%rsp)
movb $0x0, 0x38(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x5450
testb %bl, %bl
je 0x64ef
movq %r14, %rdi
movq %r15, %rsi
callq 0x5450
movq %r15, %rdi
leaq 0x1000b(%rip), %rsi     # 0x16504
callq 0x6fb5
testb %al, %al
je 0x650d
movq %r14, %rdi
movq %r15, %rsi
callq 0x5450
movq 0x90(%rsp), %rdi
movq 0x98(%rsp), %rsi
movq %r15, %rdx
callq 0x758f
cmpq 0x98(%rsp), %rax
je 0x660f
movq (%rsp), %rbx
leaq 0x1a8(%rsp), %rdi
movq %r15, %rsi
callq 0x5490
addq $0x28, %rbx
movq %rbx, %rdi
leaq 0x1a8(%rsp), %rbx
movq %rbx, %rsi
callq 0x5200
movq %rbx, %rdi
callq 0x51a0
movq %r14, %rdi
movq %r15, %rsi
callq 0x5450
movq 0x28(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x3d, %eax
je 0x6587
cmpl $0x2a, %eax
jne 0x670b
movq (%rsp), %rcx
movb %al, 0x48(%rcx)
movq 0x30(%rsp), %rcx
cmpq $0x2, %rcx
jb 0x669a
movq %r15, %rsi
movq %r13, %r15
movq %rbp, %r13
movq %r12, %rbp
decq %rcx
leaq 0x70(%rsp), %r12
movq %r12, %rdi
pushq $0x1
popq %rdx
callq 0x5390
movq (%rsp), %rbx
leaq 0x1e8(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x5490
addq $0x50, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5200
movq %rbp, %r12
movq %r14, %rdi
callq 0x51a0
leaq 0x70(%rsp), %rdi
callq 0x51a0
movq %r13, %rbp
movq %r15, %r13
leaq 0x28(%rsp), %r15
leaq 0x208(%rsp), %r14
jmp 0x67be
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq %r15, %rdx
callq 0x758f
cmpq 0x50(%rsp), %rax
je 0x6772
movq (%rsp), %rbx
leaq 0x148(%rsp), %rdi
movq %r15, %rsi
callq 0x5490
addq $0x28, %rbx
movq %rbx, %rdi
leaq 0x148(%rsp), %rbx
movq %rbx, %rsi
callq 0x5200
movq %rbx, %rdi
callq 0x51a0
movq %r14, %rdi
movq %r15, %rsi
callq 0x5450
movq (%rsp), %rbx
leaq 0x128(%rsp), %rdi
movq %r15, %rsi
callq 0x5490
addq $0x50, %rbx
movq %rbx, %rdi
leaq 0x128(%rsp), %rbx
movq %rbx, %rsi
callq 0x5200
jmp 0x67b6
movq %r14, %rdi
movq %r15, %rsi
callq 0x5450
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
leaq 0xecba(%rip), %rsi      # 0x1536e
pushq $0x10
popq %rdx
callq 0x6fda
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x6fc2
movq %rbx, %rdi
movl %eax, %ebx
callq 0x7040
testb %bl, %bl
je 0x6819
movq (%rsp), %rbx
leaq 0x188(%rsp), %rdi
movq %r15, %rsi
callq 0x5490
addq $0x50, %rbx
movq %rbx, %rdi
leaq 0x188(%rsp), %rbx
movq %rbx, %rsi
callq 0x5200
jmp 0x67b6
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
leaq 0xec54(%rip), %rsi      # 0x1536e
pushq $0x10
popq %rdx
callq 0x6fda
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x6fc2
movq %rbx, %rdi
movl %eax, %ebx
callq 0x7040
testb %bl, %bl
je 0x67d9
movq (%rsp), %rbx
movb $0x64, 0x48(%rbx)
leaq 0x168(%rsp), %rdi
movq %r15, %rsi
callq 0x5490
addq $0x50, %rbx
movq %rbx, %rdi
leaq 0x168(%rsp), %rbx
movq %rbx, %rsi
callq 0x5200
jmp 0x67b6
xorl %ebx, %ebx
movq %r15, %rdi
leaq 0xec36(%rip), %rsi      # 0x153b4
xorl %edx, %edx
callq 0x5640
cmpq $-0x1, %rax
je 0x67db
movq (%rsp), %rbx
leaq 0x108(%rsp), %rdi
movq %r15, %rsi
callq 0x5490
addq $0x28, %rbx
movq %rbx, %rdi
leaq 0x108(%rsp), %rbx
movq %rbx, %rsi
callq 0x5200
movq %rbx, %rdi
callq 0x51a0
movq (%rsp), %rax
movl 0x8(%rsp), %ecx
movl %ecx, (%rax)
movq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x705a
incl 0x8(%rsp)
xorl %ebx, %ebx
xorl %edi, %edi
leaq 0xe9e9(%rip), %rsi      # 0x151cd
callq 0x5540
movq %r15, %rdi
movq %r14, %r15
movq %rax, %r14
callq 0x51a0
movq %r15, %rdi
callq 0x51b0
movq %rbp, %rdi
callq 0x51a0
leaq 0xc8(%rsp), %rdi
callq 0x51a0
jmp 0x636e
movq 0x1a780(%rip), %rdi     # 0x20fa0
leaq 0xeb4d(%rip), %rsi      # 0x15374
callq 0x5350
movl 0x8(%rsp), %esi
movq %rax, %rdi
callq 0x5280
movq %rax, %rdi
leaq 0xeb3b(%rip), %rsi      # 0x1537d
callq 0x5350
movq %rax, %rdi
movq %r15, %rsi
callq 0x5310
movq %rax, %rdi
leaq 0xeb55(%rip), %rsi      # 0x153b1
callq 0x5350
movb $0x1, %bl
movq %rax, %rdi
callq 0x51c0
jmp 0x67db
leaq 0xa8(%rsp), %rdi
callq 0x51a0
leaq 0x48(%rsp), %rdi
callq 0x7078
leaq 0x90(%rsp), %rdi
callq 0x7078
addq $0x3c8, %rsp            # imm = 0x3C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x6aac
movq %rax, %rbx
pushq $0x40
popq %r14
leaq (%rsp,%r14), %rdi
addq $0x208, %rdi            # imm = 0x208
callq 0x51a0
addq $-0x20, %r14
cmpq $-0x20, %r14
jne 0x68b5
jmp 0x6ab6
jmp 0x68d6
movq %rax, %rbx
addq $-0x20, %r15
movq %r15, %rdi
callq 0x51a0
cmpq %r14, %r15
jne 0x68d9
jmp 0x6ab6
movq %rax, %rbx
jmp 0x6ab6
movq %rax, %rbx
movl $0xe0, %r12d
leaq (%rsp,%r12), %rdi
addq $0x208, %rdi            # imm = 0x208
callq 0x51a0
addq $-0x20, %r12
cmpq $-0x20, %r12
jne 0x6900
movb $0x1, %al
jmp 0x692f
jmp 0x692a
jmp 0x692a
jmp 0x692a
jmp 0x692a
jmp 0x692a
jmp 0x692a
movq %rax, %rbx
xorl %eax, %eax
cmpq %r15, %r14
sete %cl
testb %al, %al
jne 0x6ac3
testb %cl, %cl
jne 0x6ac3
addq $-0x20, %r15
movq %r15, %rdi
callq 0x51a0
cmpq %r14, %r15
jne 0x6945
jmp 0x6ac3
movq %rax, %rbx
jmp 0x6ac3
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
jmp 0x69fd
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
jmp 0x69fd
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
jmp 0x69fd
movq %rax, %rbx
leaq 0x1e8(%rsp), %rdi
callq 0x51a0
jmp 0x69a2
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x69fd
jmp 0x6a6b
jmp 0x69b5
jmp 0x6a6b
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x7040
jmp 0x6a6e
jmp 0x6a6b
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
jmp 0x69fd
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
jmp 0x69fd
jmp 0x6a6b
movq %rax, %rbx
jmp 0x6a36
jmp 0x6a66
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
callq 0x51a0
jmp 0x6a6e
jmp 0x6a42
jmp 0x6a66
movq %rax, %rbx
jmp 0x6a92
jmp 0x6a66
movq %rax, %rbx
leaq 0x3a8(%rsp), %rdi
jmp 0x6a8d
jmp 0x6a6b
movq %rax, %rbx
leaq 0x388(%rsp), %rdi
callq 0x51a0
testb %r12b, %r12b
je 0x6a85
movq 0x18(%rsp), %rdi
callq 0x5340
jmp 0x6a85
movq %rax, %rbx
jmp 0x6a9f
movq %rax, %rbx
movq %r15, %rdi
callq 0x5340
jmp 0x6a9f
movq %rax, %rbx
leaq 0x1c8(%rsp), %rdi
callq 0x51a0
jmp 0x6a85
movq %rax, %rbx
jmp 0x6a85
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x51a0
leaq 0x208(%rsp), %rdi
callq 0x51b0
leaq 0xe8(%rsp), %rdi
callq 0x51a0
leaq 0xc8(%rsp), %rdi
callq 0x51a0
leaq 0xa8(%rsp), %rdi
callq 0x51a0
leaq 0x48(%rsp), %rdi
callq 0x7078
leaq 0x90(%rsp), %rdi
callq 0x7078
movq %rbx, %rdi
callq 0x55c0
nop