// File: /workspace/github_cpp_makefile_star2_O3/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 %rsi, %rbx
movq %rdi, %r14
leaq 0x70(%rsp), %r15
movq %rsi, %rdi
movq %r15, %rsi
callq 0x2884
cmpw $0x7b, (%r15)
jne 0x4a5a
movq 0x38(%r14), %rdi
movq %r14, %rsi
callq 0x2b94
movq 0x38(%r14), %rdi
movq %r14, %rsi
callq 0x2ee0
leaq 0x70(%rsp), %r15
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2884
cmpw $0x7d, (%r15)
jne 0x4ac5
movq 0x38(%r14), %rdi
callq 0x2e64
xorl %eax, %eax
jmp 0x4d55
cmpl $0x1, %eax
jne 0x4d36
movq 0x8(%r14), %r15
leaq 0x70(%rsp), %r12
movq %r12, %rdi
callq 0x20e0
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x22b0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2884
cmpw $0x7b, (%r12)
je 0x4a16
movq 0x8(%rbx), %rcx
movl 0x2c(%rbx), %r8d
leaq 0x2e33(%rip), %rdi      # 0x78e0
leaq 0x2c3b(%rip), %rsi      # 0x76ef
leaq 0x70(%rsp), %rdx
xorl %eax, %eax
callq 0x2050
jmp 0x4d50
leaq 0x48(%r14), %rax
movq %rax, 0x68(%rsp)
leaq 0x50(%r14), %rax
movq %rax, 0x60(%rsp)
leaq 0x78(%r14), %rax
movq %rax, 0x58(%rsp)
leaq 0x80(%r14), %rax
movq %rax, 0x50(%rsp)
movl $0x79616c70, %r15d      # imm = 0x79616C70
leaq 0x70(%rsp), %r13
leaq 0x30(%rsp), %rbp
leaq 0x10(%rsp), %r12
movl 0x70(%rsp), %eax
xorl %r15d, %eax
movzbl 0x74(%rsp), %ecx
orl %eax, %ecx
jne 0x4d18
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2884
cmpw $0x3a, 0x70(%rsp)
jne 0x4d67
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2884
movl $0x1, %r15d
cmpl $0x2, %eax
jne 0x4b5e
movq %r13, %rdi
callq 0x22c0
movl %eax, %r15d
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2884
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq %r13, %rdi
callq 0x20e0
leaq (%rsp,%rax), %rdx
addq $0x70, %rdx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x61de
movq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x6afe
cmpq 0x60(%rsp), %rax
je 0x4bb5
testl %r15d, %r15d
jle 0x4c6d
movq %r14, %rdi
movq %rbp, %rsi
callq 0x52e2
decl %r15d
jne 0x4ba0
jmp 0x4c6d
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r13, %rdi
callq 0x20e0
leaq (%rsp,%rax), %rdx
addq $0x70, %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x61de
movq 0x58(%rsp), %rdi
movq %r12, %rbx
movq %r12, %rsi
callq 0x6586
movq %rax, %r12
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x4c09
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
cmpq 0x50(%rsp), %r12
je 0x4cc1
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r13, %rdi
callq 0x20e0
leaq (%rsp,%rax), %rdx
addq $0x70, %rdx
movq %rbx, %r12
movq %rbx, %rdi
movq %r13, %rsi
callq 0x61de
testl %r15d, %r15d
jle 0x4c51
movq %r14, %rdi
movq %r12, %rsi
callq 0x53dc
decl %r15d
jne 0x4c41
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x4c6d
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2884
movzwl 0x70(%rsp), %eax
cmpl $0x2c, %eax
je 0x4cbc
movl $0x5, %r15d
cmpl $0x3b, %eax
je 0x4ce7
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rcx
movl 0x2c(%rax), %r8d
leaq 0x2c3d(%rip), %rdi      # 0x78e0
leaq 0x2b77(%rip), %rsi      # 0x7821
movq %r13, %rdx
xorl %eax, %eax
callq 0x2050
movl $0x1, %r15d
jmp 0x4ce7
xorl %r15d, %r15d
jmp 0x4ce7
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdx
movl 0x2c(%rax), %ecx
leaq 0x2b89(%rip), %rdi      # 0x785d
movq %r13, %rsi
xorl %eax, %eax
callq 0x2050
movl $0x1, %r15d
movq %rbx, %r12
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x4d03
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
testl %r15d, %r15d
je 0x4b2e
cmpl $0x5, %r15d
movl $0x79616c70, %r15d      # imm = 0x79616C70
jne 0x4d50
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2884
cmpw $0x7d, 0x70(%rsp)
jne 0x4b01
jmp 0x4a4a
movq 0x8(%rbx), %rdx
movl 0x2c(%rbx), %ecx
leaq 0x2adf(%rip), %rdi      # 0x7823
leaq 0x70(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rcx
movl 0x2c(%rax), %r8d
leaq 0x2b65(%rip), %rdi      # 0x78e0
leaq 0x2bbc(%rip), %rsi      # 0x793e
jmp 0x4ab4
jmp 0x4db2
jmp 0x4d91
jmp 0x4db2
jmp 0x4db2
jmp 0x4db2
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x4db5
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x4db5
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x4dd1
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2300
nop