// File: /workspace/github_cpp_makefile_star2_O2/mikeakohn[P]drumsplusplus/build/../src/Song.cxx
// Function: Song::parse_song(Tokens*)
int Song::parse_song(Tokens *tokens)
{
  int token_type;
  char token[1024];
  int repeat;
  int x;

  token_type = tokens->get(token);

  if (strcmp(token, "{") != 0)
  {
    if (token_type == TOKEN_ALPHA)
    {
      song_info.set_song_name(token);

      token_type = tokens->get(token);

      if (strcmp(token, "{") != 0)
      {
        print_error(tokens, "{", token);
        return -1;
      }
    }
      else
    {
      printf("Error: Expecting song name or '{' but got '%s' at %s:%d.\n",
        token, tokens->get_filename(), tokens->get_line());
      return -1;
    }
  }

  midi_file->write_header(&song_info);
  midi_file->write_bpm(&song_info);

  while(1)
  {
    token_type = tokens->get(token);

    if (strcmp(token, "}") == 0)
    {
      break;
    }

    if (strcmp(token, "play") == 0)
    {
      token_type = tokens->get(token);

      if (strcmp(token, ":") != 0)
      {
        print_error(tokens, ":", token);
        return -1;
      }

      while(1)
      {
        repeat = 1;

        token_type = tokens->get(token);

        if (token_type == TOKEN_NUMBER)
        {
          repeat = atoi(token);
          token_type = tokens->get(token);
        }

        std::string section_name = token;

        if (sections.find(section_name) != sections.end())
        {
          for (x = 0; x < repeat; x++)
          {
            play_section(section_name);
          }
        }
          else
        {
          if (patterns.find(token) == patterns.end())
          {
            printf("Error:  Undefined pattern '%s' at %s:%d.\n",
              token, tokens->get_filename(), tokens->get_line());
            return -1;
          }

          std::string pattern_name = token;

          for (x = 0; x < repeat; x++)
          {
            play_pattern(pattern_name);
          }
        }

        token_type = tokens->get(token);

        if (strcmp(token, ";") == 0)
        {
          break;
        }
          else
        if (strcmp(token, ",") != 0)
        {
          print_error(tokens, ",", token);
          return -1;
        }
      }
    }
  }

  midi_file->write_footer();

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x478, %rsp            # imm = 0x478
movq %rdi, %r14
leaq 0x70(%rsp), %rbx
movq %rsi, 0x8(%rsp)
movq %rsi, %rdi
movq %rbx, %rsi
callq 0x2828
cmpw $0x7b, (%rbx)
jne 0x47a2
movq 0x38(%r14), %rdi
movq %r14, %rsi
callq 0x2b4e
movq 0x38(%r14), %rdi
movq %r14, %rsi
callq 0x2e5c
leaq 0x48(%r14), %rax
movq %rax, 0x28(%rsp)
leaq 0x50(%r14), %rax
movq %rax, 0x20(%rsp)
leaq 0x78(%r14), %rax
movq %rax, 0x18(%rsp)
leaq 0x80(%r14), %rax
movq %rax, 0x10(%rsp)
leaq 0x70(%rsp), %r13
leaq 0x50(%rsp), %rbp
leaq 0x30(%rsp), %r12
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2828
cmpw $0x7d, 0x70(%rsp)
je 0x485d
movq %r13, %rdi
leaq 0x319d(%rip), %rsi      # 0x780b
pushq $0x5
popq %rdx
callq 0x21d0
testl %eax, %eax
jne 0x464b
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2828
cmpw $0x3a, 0x70(%rsp)
jne 0x486a
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2828
pushq $0x1
popq %r15
cmpl $0x2, %eax
jne 0x46c1
movq %r13, %rdi
callq 0x22f0
movl %eax, %r15d
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2828
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x507c
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x64ec
cmpq 0x20(%rsp), %rax
je 0x4702
testl %r15d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
subl $0x1, %r15d
jb 0x476c
movq %r14, %rdi
movq %rbp, %rsi
callq 0x4d26
jmp 0x46ef
movq %r12, %rdi
movq %r13, %rsi
leaq 0x7(%rsp), %rdx
callq 0x507c
movq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x5f8a
movq %rax, %rbx
movq %r12, %rdi
callq 0x20f0
cmpq 0x10(%rsp), %rbx
je 0x481f
movq %r12, %rdi
movq %r13, %rsi
leaq 0x7(%rsp), %rdx
callq 0x507c
testl %r15d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
subl $0x1, %r15d
jb 0x4764
movq %r14, %rdi
movq %r12, %rsi
callq 0x4dfa
jmp 0x4751
movq %r12, %rdi
callq 0x20f0
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2828
movzwl 0x70(%rsp), %eax
cmpl $0x2c, %eax
jne 0x4790
movq %rbp, %rdi
callq 0x20f0
jmp 0x4693
cmpl $0x3b, %eax
jne 0x47e6
movq %rbp, %rdi
callq 0x20f0
jmp 0x464b
cmpl $0x1, %eax
jne 0x47fe
leaq 0x70(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2360
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x2828
cmpw $0x7b, (%rbx)
je 0x45fd
leaq 0x2f32(%rip), %rdx      # 0x7707
leaq 0x70(%rsp), %rcx
movq 0x8(%rsp), %rsi
callq 0x49e6
jmp 0x4848
leaq 0x304c(%rip), %rdx      # 0x7839
leaq 0x70(%rsp), %rcx
movq 0x8(%rsp), %rsi
callq 0x49e6
jmp 0x483e
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdx
movl 0x2c(%rax), %ecx
leaq 0x302a(%rip), %rdi      # 0x783b
leaq 0x70(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
jmp 0x4848
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdx
movl 0x2c(%rax), %ecx
leaq 0x3043(%rip), %rdi      # 0x7875
leaq 0x70(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
leaq 0x50(%rsp), %rdi
callq 0x20f0
pushq $-0x1
popq %rax
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%r14), %rdi
callq 0x2de2
xorl %eax, %eax
jmp 0x484b
leaq 0x30e5(%rip), %rdx      # 0x7956
jmp 0x47d5
jmp 0x488f
jmp 0x488f
jmp 0x4880
jmp 0x488f
jmp 0x488f
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x20f0
jmp 0x4892
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x2330