// File: /workspace/github_cpp_makefile_star2_O1/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 0x186a5
leaq 0xb8(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x23759(%rip), %rdx     # 0x3b6fa
leaq 0xa8(%rsp), %rdi
movq %rdx, %rsi
callq 0xb62c
leaq 0x98(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x23736(%rip), %rdx     # 0x3b6fa
leaq 0x88(%rsp), %rdi
movq %rdx, %rsi
callq 0xb62c
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x23716(%rip), %rdx     # 0x3b6fa
leaq 0x68(%rsp), %rdi
movq %rdx, %rsi
callq 0xb62c
leaq 0x140(%rsp), %rdi
callq 0x1aa88
leaq 0xe8(%rsp), %rdi
callq 0x1aa88
leaq 0x198(%rsp), %rbx
leaq 0x38(%rsp), %r14
leaq 0x23284(%rip), %r15     # 0x3b2a3
leaq 0x23285(%rip), %r12     # 0x3b2ab
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5450
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x180f1
movq %r14, %rdi
movq %r15, %rsi
callq 0x5100
testl %eax, %eax
je 0x18061
movq %r14, %rdi
movq %r12, %rsi
callq 0x5100
testl %eax, %eax
jne 0x18026
leaq 0x23243(%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 0x23234(%rip), %r12     # 0x3b2c1
leaq 0x231d0(%rip), %rbx     # 0x3b264
movq %r15, %rdi
movq %r14, %rsi
callq 0x5450
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x180f1
movl $0x1, %ecx
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x55d0
cmpq $-0x1, %rax
jne 0x180ec
movl $0x1, %ecx
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x55d0
cmpq $-0x1, %rax
je 0x18094
leal -0x1(%r13), %eax
cmpl $0x1, %r13d
movl %eax, %r13d
jg 0x18094
jmp 0x180f1
incl %r13d
jmp 0x18094
movq $0x0, 0x30(%rsp)
leaq 0x198(%rsp), %r14
leaq 0x38(%rsp), %rbx
leaq 0x231a6(%rip), %r12     # 0x3b2b4
leaq 0x231a6(%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 0x1821f
testl %r15d, %r15d
jne 0x18145
movq %r14, %rdi
callq 0x55c0
movq %rax, %r15
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5100
testl %eax, %eax
je 0x18163
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5100
testl %eax, %eax
jne 0x18118
leaq 0x198(%rsp), %rdi
callq 0x55c0
movq %rax, 0x58(%rsp)
leaq 0x2313f(%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 0x23116(%rip), %r12     # 0x3b2c3
leaq 0x23112(%rip), %rbx     # 0x3b2c6
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5450
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x18237
movl $0x2, %ecx
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x55d0
cmpq $-0x1, %rax
jne 0x18211
movl $0x2, %ecx
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x55d0
cmpq $-0x1, %rax
je 0x181b4
movq 0x28(%rsp), %rcx
leal -0x1(%rcx), %eax
cmpl $0x1, %ecx
movq %rax, 0x28(%rsp)
jg 0x181b4
jmp 0x18223
movq 0x28(%rsp), %rax
incl %eax
movq %rax, 0x28(%rsp)
jmp 0x181b4
xorl %ebp, %ebp
jmp 0x1823f
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 0x182e8
movq %r14, %rdi
callq 0x55c0
cmpq %rbp, %rax
jl 0x1826e
movl $0x5, %ecx
movq %r12, %rdi
leaq 0x23006(%rip), %rsi     # 0x3b2c9
xorl %edx, %edx
callq 0x55d0
movq %rax, %rbx
movl $0x1, %ecx
movq %r12, %rdi
leaq 0x22ff3(%rip), %rsi     # 0x3b2cf
xorl %edx, %edx
callq 0x55d0
cmpq %rax, %rbx
jae 0x1826e
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 0x18509
movq %r14, %rdi
callq 0x55c0
cmpq 0x28(%rsp), %rax
setl %al
orb %r12b, %al
testb $0x1, %al
jne 0x18413
leaq 0x8(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x259e0
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 0x183bb
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x8(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x259e0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x30bd5(%rip), %rdi     # 0x48fb0
callq 0x53d0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x233db(%rip), %rsi     # 0x3b7ca
callq 0x53d0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x18410
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 0x1846a
leaq 0x8(%rsp), %rdi
callq 0x262ec
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 0x18467
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 0x18323
leaq 0x8(%rsp), %rdi
callq 0x26310
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 0x18501
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5360
xorl %r13d, %r13d
jmp 0x18320
leaq 0x3a8(%rsp), %rdi
callq 0x54b0
leaq 0x198(%rsp), %rdi
callq 0x5080
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x18544
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x100(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x18565
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x178(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x18586
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x5360
leaq 0x158(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x185a7
movq 0x158(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x185c3
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x185e8
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x1860d
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x18629
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x1864e
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x3093b(%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 0x3093c(%rip), %rdi     # 0x48fe8
leaq 0x22b16(%rip), %rsi     # 0x3b1c9
callq 0x5340
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
callq 0x5410
jmp 0x1873c
jmp 0x186e9
jmp 0x1873c
jmp 0x18713
jmp 0x1873c
movq %rax, %rbx
jmp 0x1874c
movq %rax, %rbx
jmp 0x18759
movq %rax, %rbx
jmp 0x18775
movq %rax, %rbx
jmp 0x1879a
movq %rax, %rbx
jmp 0x187bf
movq %rax, %rbx
jmp 0x1880d
jmp 0x1873c
jmp 0x1873c
jmp 0x1873c
jmp 0x1873c
jmp 0x1873c
jmp 0x1873c
jmp 0x18713
jmp 0x18713
jmp 0x1873c
jmp 0x1873c
jmp 0x1873c
jmp 0x1873c
jmp 0x1873c
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1873f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5360
jmp 0x1873f
jmp 0x1873c
jmp 0x1873c
jmp 0x1873c
jmp 0x1873c
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0xb116
leaq 0x140(%rsp), %rdi
callq 0xb116
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x18775
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x1879a
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x187bf
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x187db
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x5360
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x18800
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