// File: /workspace/github_cpp_makefile_star2_O2/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, 0x20(%rsp)
leaq 0x198(%rsp), %rdi
pushq $0x18
popq %rdx
callq 0x5570
leaq 0x3a8(%rsp), %rdi
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x5640
leaq 0xd8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq 0x38(%rsp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
xorl %edx, %edx
movb %dl, (%rax)
movb %dl, (%rcx)
leaq 0x218(%rsp), %rdi
callq 0x50e0
testb %al, %al
je 0x1149d
leaq 0x187b9(%rip), %rsi     # 0x2974a
leaq 0xa8(%rsp), %rdi
leaq 0xe8(%rsp), %rdx
callq 0x96e6
leaq 0x1879d(%rip), %rsi     # 0x2974a
leaq 0x88(%rsp), %rdi
leaq 0xe8(%rsp), %rdx
callq 0x96e6
leaq 0x18781(%rip), %rsi     # 0x2974a
leaq 0x68(%rsp), %rdi
leaq 0xe8(%rsp), %rdx
callq 0x96e6
leaq 0xe8(%rsp), %rdi
callq 0x12712
leaq 0x140(%rsp), %rdi
callq 0x12712
leaq 0x198(%rsp), %rbx
leaq 0x28(%rsp), %r14
leaq 0x182aa(%rip), %r15     # 0x292b3
leaq 0x182ab(%rip), %r12     # 0x292bb
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5440
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x110cc
movq %r14, %rdi
movq %r15, %rsi
callq 0x989a
testb %al, %al
jne 0x1104b
movq %r14, %rdi
movq %r12, %rsi
callq 0x989a
testb %al, %al
je 0x11010
leaq 0x18269(%rip), %rsi     # 0x292bb
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x989a
movzbl %al, %r13d
leaq 0x198(%rsp), %r15
leaq 0x1825f(%rip), %r12     # 0x292d1
leaq 0x181fb(%rip), %rbx     # 0x29274
movq %r15, %rdi
movq %r14, %rsi
callq 0x5440
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x110cc
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x5660
cmpq $-0x1, %rax
jne 0x110c7
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x5660
cmpq $-0x1, %rax
je 0x11079
leal -0x1(%r13), %eax
cmpl $0x1, %r13d
movl %eax, %r13d
jg 0x11079
jmp 0x110cc
incl %r13d
jmp 0x11079
movq $0x0, 0x18(%rsp)
leaq 0x198(%rsp), %r15
leaq 0x28(%rsp), %r12
leaq 0x181db(%rip), %r13     # 0x292c4
leaq 0x181db(%rip), %rbp     # 0x292cb
xorl %r14d, %r14d
movq %r15, %rdi
movq %r12, %rsi
callq 0x5440
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x111ec
testl %r14d, %r14d
jne 0x11120
movq %r15, %rdi
callq 0x55b0
movq %rax, %r14
movq %r12, %rdi
movq %r13, %rsi
callq 0x989a
testb %al, %al
jne 0x1113e
movq %r12, %rdi
movq %rbp, %rsi
callq 0x989a
testb %al, %al
je 0x110f3
leaq 0x198(%rsp), %rdi
callq 0x55b0
movq %rax, 0x10(%rsp)
leaq 0x18174(%rip), %rsi     # 0x292cb
leaq 0x28(%rsp), %r13
movq %r13, %rdi
callq 0x989a
movzbl %al, %eax
movq %rax, 0x8(%rsp)
leaq 0x198(%rsp), %rbp
movq $0x0, 0x18(%rsp)
leaq 0x1814f(%rip), %r12     # 0x292d3
leaq 0x1814b(%rip), %rbx     # 0x292d6
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5440
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1120b
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x5660
cmpq $-0x1, %rax
jne 0x111de
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x5660
cmpq $-0x1, %rax
je 0x1118b
movq 0x8(%rsp), %rcx
leal -0x1(%rcx), %eax
cmpl $0x1, %ecx
movq %rax, 0x8(%rsp)
jg 0x1118b
jmp 0x111f7
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
jmp 0x1118b
movq $0x0, 0x8(%rsp)
jmp 0x11215
leaq 0x198(%rsp), %rdi
callq 0x55b0
cltq
movq %rax, 0x18(%rsp)
movslq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x198(%rsp), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x5590
leaq 0x198(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5430
leaq 0x198(%rsp), %r15
leaq 0x28(%rsp), %rbx
leaq 0x1808f(%rip), %r13     # 0x292df
movq %r15, %rdi
movq %rbx, %rsi
callq 0x50a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
setne %r12b
jne 0x112a2
movq %r15, %rdi
callq 0x55b0
cmpq 0x8(%rsp), %rax
jl 0x11250
movq %rbx, %rdi
leaq 0x18053(%rip), %rsi     # 0x292d9
xorl %edx, %edx
callq 0x5660
movq %rax, %rbp
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x5660
cmpq %rax, %rbp
jae 0x11250
movq 0x198(%rsp), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x5590
leaq 0x198(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5430
movslq %r14d, %rax
movq %rax, 0x10(%rsp)
xorl %r15d, %r15d
leaq 0x198(%rsp), %r14
leaq 0x3a8(%rsp), %rbx
leaq 0x48(%rsp), %r13
xorl %ebp, %ebp
notb %r12b
movq %r14, %rdi
leaq 0x28(%rsp), %rsi
callq 0x50a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x11402
movq %r14, %rdi
callq 0x55b0
cmpq 0x10(%rsp), %rax
setl %al
orb %r15b, %al
testb $0x1, %al
jne 0x1137a
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0x189f4
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5310
movq %r13, %rdi
callq 0x51b0
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0x189f4
movq 0x28c50(%rip), %rdi     # 0x39fa8
movq %r13, %rsi
callq 0x5310
movq %rax, %rdi
leaq 0x184b0(%rip), %rsi     # 0x2981a
callq 0x5350
movq %r13, %rdi
callq 0x51b0
movb $0x1, %r15b
movq %r14, %rdi
callq 0x55b0
cmpq 0x18(%rsp), %rax
setl %al
orb %bpl, %al
testb $0x1, %al
jne 0x113af
movq %r13, %rdi
callq 0x18d5c
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5310
movq %r13, %rdi
callq 0x51b0
movb $0x1, %bpl
movq %rbx, %rdi
leaq 0x28(%rsp), %rsi
callq 0x5310
movq %rax, %rdi
callq 0x51d0
movq %r14, %rdi
callq 0x55b0
cmpq 0x8(%rsp), %rax
setl %al
orb %r12b, %al
testb $0x1, %al
jne 0x112ee
movq %r13, %rdi
callq 0x18d7e
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5310
movq %r13, %rdi
callq 0x51b0
xorl %r12d, %r12d
jmp 0x112eb
leaq 0x3a8(%rsp), %rdi
callq 0x54b0
leaq 0x198(%rsp), %rdi
callq 0x5070
leaq 0x140(%rsp), %rdi
callq 0x979a
leaq 0xe8(%rsp), %rdi
callq 0x979a
leaq 0x68(%rsp), %rdi
callq 0x51b0
leaq 0x88(%rsp), %rdi
callq 0x51b0
leaq 0xa8(%rsp), %rdi
callq 0x51b0
leaq 0x28(%rsp), %rdi
callq 0x51b0
leaq 0xc8(%rsp), %rdi
callq 0x51b0
leaq 0x3a8(%rsp), %rdi
callq 0x54e0
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 0x28b44(%rip), %rdi     # 0x39fe8
leaq 0x17d2e(%rip), %rsi     # 0x291d9
callq 0x5350
pushq $-0x2
popq %rdi
callq 0x53f0
jmp 0x11514
jmp 0x114d6
jmp 0x11514
jmp 0x11505
jmp 0x11514
movq %rax, %rbx
jmp 0x11524
movq %rax, %rbx
jmp 0x11531
movq %rax, %rbx
jmp 0x1153b
movq %rax, %rbx
jmp 0x11548
movq %rax, %rbx
jmp 0x11555
movq %rax, %rbx
jmp 0x11579
jmp 0x11514
jmp 0x11514
jmp 0x11514
jmp 0x11514
jmp 0x11514
jmp 0x11505
jmp 0x11514
jmp 0x11514
jmp 0x11514
jmp 0x11505
jmp 0x11514
jmp 0x11514
jmp 0x11514
jmp 0x11514
jmp 0x11514
jmp 0x11514
jmp 0x11514
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x51b0
jmp 0x11517
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x979a
leaq 0xe8(%rsp), %rdi
callq 0x979a
leaq 0x68(%rsp), %rdi
callq 0x51b0
leaq 0x88(%rsp), %rdi
callq 0x51b0
leaq 0xa8(%rsp), %rdi
callq 0x51b0
leaq 0x28(%rsp), %rdi
callq 0x51b0
leaq 0xc8(%rsp), %rdi
callq 0x51b0
leaq 0x3a8(%rsp), %rdi
callq 0x54e0
leaq 0x198(%rsp), %rdi
callq 0x50c0
movq %rbx, %rdi
callq 0x55d0