// File: /workspace/github_cpp_makefile_star2_O3/joao18araujo[P]pierluigi/src/ly_parser.cpp
// Function: LyParser::add_counterpoint_to_lilypond(Song&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void LyParser::add_counterpoint_to_lilypond(Song & song, string file_path, string output_path){
  std::fstream file(file_path);
  std::ofstream output(output_path);

  string line, s;

  if(!file.is_open()){
    std::cerr << "Error: File not found\n";
    exit(-2);
  }

  string s_scale = "", s_time = "", s_notes = "";
  Note note, prev;
  int score_end_position = 0, layout_end_position = 0, midi_position = 0;

  while(file >> s){
    if(s == "\\layout" || s == "\\layout{") {
      int opened = (s == "\\layout{");
      while(file >> s){
        if(s.find("{") != string::npos){
          opened++;
        } else if(s.find("}") != string::npos){
          opened--;
          if(opened <= 0) break;
        }
      }
      break;
    }
  }

  bool needs_midi_indicator = true;
  while(file >> s){
    if(!layout_end_position) layout_end_position = file.tellg();
    if(s == "\\score" || s == "\\score{") {
      if(!midi_position) midi_position = file.tellg();
      int opened = (s == "\\score{");
      while(file >> s){
        if(s.find("<<") != string::npos){
          opened++;
        } else if(s.find(">>") != string::npos){
          opened--;
          if(opened <= 0){
            score_end_position = file.tellg();
            break;
          }
        }
      }
      break;
    }
  }

  file.clear();
  file.seekg(0);
  while(getline(file, s)){
    if(file.tellg() >= midi_position and s.find("\\midi") < s.find("%")){
      needs_midi_indicator = false;
      break;
    }
  }

  file.clear();
  file.seekg(0);

  bool printed_notes = false;
  bool printed_staff = false;

  while(getline(file, s)){
    if(file.tellg() >= layout_end_position and not printed_notes){
      output << SongReader::song_to_voice_string(song);
      std::cout << SongReader::song_to_voice_string(song) << "\n";
      printed_notes = true;
    }

    if(file.tellg() >= score_end_position and not printed_staff){
      output << SongReader::new_staff_string();
      printed_staff = true;
    }

    output << s << std::endl;

    if(file.tellg() >= midi_position and needs_midi_indicator){
      output << SongReader::midi_indicator();
      needs_midi_indicator = false;
    }
  }

  output.close();
  file.close();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5a8, %rsp            # imm = 0x5A8
movq %rdx, %rbx
movq %rdi, 0x60(%rsp)
leaq 0x198(%rsp), %rdi
movl $0x18, %edx
callq 0x5580
leaq 0x3a8(%rsp), %rdi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x5660
leaq 0xd8(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x48(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x218(%rsp), %rdi
callq 0x50d0
testb %al, %al
je 0x18733
leaq 0xb8(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x236cb(%rip), %rdx     # 0x3b6fa
leaq 0xa8(%rsp), %rdi
movq %rdx, %rsi
callq 0xb5f8
leaq 0x98(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x236a8(%rip), %rdx     # 0x3b6fa
leaq 0x88(%rsp), %rdi
movq %rdx, %rsi
callq 0xb5f8
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x23688(%rip), %rdx     # 0x3b6fa
leaq 0x68(%rsp), %rdi
movq %rdx, %rsi
callq 0xb5f8
leaq 0x140(%rsp), %rdi
callq 0x1aada
leaq 0xe8(%rsp), %rdi
callq 0x1aada
leaq 0x198(%rsp), %rbx
leaq 0x38(%rsp), %r14
leaq 0x231f6(%rip), %r15     # 0x3b2a3
leaq 0x231f7(%rip), %r12     # 0x3b2ab
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5450
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1817f
movq %r14, %rdi
movq %r15, %rsi
callq 0x5100
testl %eax, %eax
je 0x180ef
movq %r14, %rdi
movq %r12, %rsi
callq 0x5100
testl %eax, %eax
jne 0x180b4
leaq 0x231b5(%rip), %rsi     # 0x3b2ab
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x5100
xorl %r13d, %r13d
testl %eax, %eax
sete %r13b
leaq 0x198(%rsp), %r15
leaq 0x231a6(%rip), %r12     # 0x3b2c1
leaq 0x23142(%rip), %rbx     # 0x3b264
movq %r15, %rdi
movq %r14, %rsi
callq 0x5450
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1817f
movl $0x1, %ecx
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x55d0
cmpq $-0x1, %rax
jne 0x1817a
movl $0x1, %ecx
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x55d0
cmpq $-0x1, %rax
je 0x18122
leal -0x1(%r13), %eax
cmpl $0x1, %r13d
movl %eax, %r13d
jg 0x18122
jmp 0x1817f
incl %r13d
jmp 0x18122
movq $0x0, 0x30(%rsp)
leaq 0x198(%rsp), %r14
leaq 0x38(%rsp), %rbx
leaq 0x23118(%rip), %r12     # 0x3b2b4
leaq 0x23118(%rip), %r13     # 0x3b2bb
xorl %r15d, %r15d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5450
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x182ad
testl %r15d, %r15d
jne 0x181d3
movq %r14, %rdi
callq 0x55c0
movq %rax, %r15
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5100
testl %eax, %eax
je 0x181f1
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5100
testl %eax, %eax
jne 0x181a6
leaq 0x198(%rsp), %rdi
callq 0x55c0
movq %rax, 0x58(%rsp)
leaq 0x230b1(%rip), %rsi     # 0x3b2bb
leaq 0x38(%rsp), %r13
movq %r13, %rdi
callq 0x5100
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
movq %rcx, 0x28(%rsp)
leaq 0x198(%rsp), %rbp
movq $0x0, 0x30(%rsp)
leaq 0x23088(%rip), %r12     # 0x3b2c3
leaq 0x23084(%rip), %rbx     # 0x3b2c6
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5450
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x182c5
movl $0x2, %ecx
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x55d0
cmpq $-0x1, %rax
jne 0x1829f
movl $0x2, %ecx
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x55d0
cmpq $-0x1, %rax
je 0x18242
movq 0x28(%rsp), %rcx
leal -0x1(%rcx), %eax
cmpl $0x1, %ecx
movq %rax, 0x28(%rsp)
jg 0x18242
jmp 0x182b1
movq 0x28(%rsp), %rax
incl %eax
movq %rax, 0x28(%rsp)
jmp 0x18242
xorl %ebp, %ebp
jmp 0x182cd
leaq 0x198(%rsp), %rdi
callq 0x55c0
cltq
movq %rax, 0x30(%rsp)
movq 0x58(%rsp), %rax
movslq %eax, %rbp
movq 0x198(%rsp), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x55a0
leaq 0x198(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5440
leaq 0x38(%rsp), %r12
movq 0x198(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5320
movsbl %al, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x5610
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
setne %r13b
jne 0x18376
movq %r14, %rdi
callq 0x55c0
cmpq %rbp, %rax
jl 0x182fc
movl $0x5, %ecx
movq %r12, %rdi
leaq 0x22f78(%rip), %rsi     # 0x3b2c9
xorl %edx, %edx
callq 0x55d0
movq %rax, %rbx
movl $0x1, %ecx
movq %r12, %rdi
leaq 0x22f65(%rip), %rsi     # 0x3b2cf
xorl %edx, %edx
callq 0x55d0
cmpq %rax, %rbx
jae 0x182fc
movq 0x198(%rsp), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x55a0
leaq 0x198(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5440
movslq %r15d, %rax
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
notb %r13b
movq 0x198(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5320
movsbl %al, %edx
movq %r14, %rdi
leaq 0x38(%rsp), %rsi
callq 0x5610
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x18597
movq %r14, %rdi
callq 0x55c0
cmpq 0x28(%rsp), %rax
setl %al
orb %r12b, %al
testb $0x1, %al
jne 0x184a1
leaq 0x8(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x259fe
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x3a8(%rsp), %rdi
callq 0x53d0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x18449
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x8(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x259fe
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x30b47(%rip), %rdi     # 0x48fb0
callq 0x53d0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2334d(%rip), %rsi     # 0x3b7ca
callq 0x53d0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1849e
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5360
movb $0x1, %r12b
movq %r14, %rdi
callq 0x55c0
cmpq 0x30(%rsp), %rax
setl %al
orb %r15b, %al
testb $0x1, %al
jne 0x184f8
leaq 0x8(%rsp), %rdi
callq 0x26308
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x3a8(%rsp), %rdi
callq 0x53d0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x184f5
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5360
movb $0x1, %r15b
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
leaq 0x3a8(%rsp), %rdi
callq 0x53d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x5320
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5250
movq %r14, %rdi
callq 0x55c0
cmpq %rbp, %rax
setl %al
orb %r13b, %al
testb $0x1, %al
jne 0x183b1
leaq 0x8(%rsp), %rdi
callq 0x2632c
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x3a8(%rsp), %rdi
callq 0x53d0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1858f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5360
xorl %r13d, %r13d
jmp 0x183ae
leaq 0x3a8(%rsp), %rdi
callq 0x54b0
leaq 0x198(%rsp), %rdi
callq 0x5080
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x185d2
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x100(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x185f3
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x178(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x18614
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x158(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x18635
movq 0x158(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x18651
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x18676
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x1869b
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x186b7
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x186dc
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x308ad(%rip), %rax     # 0x48f90
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x3b0(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x3a8(%rsp,%rcx)
callq 0x52d0
leaq 0x4a0(%rsp), %rdi
callq 0x5130
leaq 0x198(%rsp), %rdi
callq 0x50c0
addq $0x5a8, %rsp            # imm = 0x5A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x308ae(%rip), %rdi     # 0x48fe8
leaq 0x22a88(%rip), %rsi     # 0x3b1c9
callq 0x5340
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
callq 0x5410
jmp 0x187ca
jmp 0x18777
jmp 0x187ca
jmp 0x187a1
jmp 0x187ca
movq %rax, %rbx
jmp 0x187da
movq %rax, %rbx
jmp 0x187e7
movq %rax, %rbx
jmp 0x18803
movq %rax, %rbx
jmp 0x18828
movq %rax, %rbx
jmp 0x1884d
movq %rax, %rbx
jmp 0x1889b
jmp 0x187ca
jmp 0x187ca
jmp 0x187ca
jmp 0x187ca
jmp 0x187ca
jmp 0x187ca
jmp 0x187a1
jmp 0x187a1
jmp 0x187ca
jmp 0x187ca
jmp 0x187ca
jmp 0x187ca
jmp 0x187ca
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x187cd
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5360
jmp 0x187cd
jmp 0x187ca
jmp 0x187ca
jmp 0x187ca
jmp 0x187ca
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0xb0e0
leaq 0x140(%rsp), %rdi
callq 0xb0e0
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x18803
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x18828
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x1884d
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x18869
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x1888e
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x3a8(%rsp), %rdi
callq 0x54f0
leaq 0x198(%rsp), %rdi
callq 0x50c0
movq %rbx, %rdi
callq 0x55e0