// File: /workspace/github_cpp_makefile_star2_O1/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 0x2880
cmpw $0x7b, (%r15)
jne 0x49fa
movq 0x38(%r14), %rdi
movq %r14, %rsi
callq 0x2b94
movq 0x38(%r14), %rdi
movq %r14, %rsi
callq 0x2eca
leaq 0x70(%rsp), %r15
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2880
cmpw $0x7d, (%r15)
jne 0x4a65
movq 0x38(%r14), %rdi
callq 0x2e4e
xorl %eax, %eax
jmp 0x4cf5
cmpl $0x1, %eax
jne 0x4cd6
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 0x2880
cmpw $0x7b, (%r12)
je 0x49b6
movq 0x8(%rbx), %rcx
movl 0x2c(%rbx), %r8d
leaq 0x2e9b(%rip), %rdi      # 0x78e8
leaq 0x2ca3(%rip), %rsi      # 0x76f7
leaq 0x70(%rsp), %rdx
xorl %eax, %eax
callq 0x2050
jmp 0x4cf0
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 0x4cb8
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2880
cmpw $0x3a, 0x70(%rsp)
jne 0x4d07
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2880
movl $0x1, %r15d
cmpl $0x2, %eax
jne 0x4afe
movq %r13, %rdi
callq 0x22c0
movl %eax, %r15d
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2880
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 0x6180
movq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x6a9a
cmpq 0x60(%rsp), %rax
je 0x4b55
testl %r15d, %r15d
jle 0x4c0d
movq %r14, %rdi
movq %rbp, %rsi
callq 0x5274
decl %r15d
jne 0x4b40
jmp 0x4c0d
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 0x6180
movq 0x58(%rsp), %rdi
movq %r12, %rbx
movq %r12, %rsi
callq 0x6522
movq %rax, %r12
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x4ba9
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
cmpq 0x50(%rsp), %r12
je 0x4c61
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 0x6180
testl %r15d, %r15d
jle 0x4bf1
movq %r14, %rdi
movq %r12, %rsi
callq 0x5392
decl %r15d
jne 0x4be1
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x4c0d
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2880
movzwl 0x70(%rsp), %eax
cmpl $0x2c, %eax
je 0x4c5c
movl $0x5, %r15d
cmpl $0x3b, %eax
je 0x4c87
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rcx
movl 0x2c(%rax), %r8d
leaq 0x2ca5(%rip), %rdi      # 0x78e8
leaq 0x2bdf(%rip), %rsi      # 0x7829
movq %r13, %rdx
xorl %eax, %eax
callq 0x2050
movl $0x1, %r15d
jmp 0x4c87
xorl %r15d, %r15d
jmp 0x4c87
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdx
movl 0x2c(%rax), %ecx
leaq 0x2bf1(%rip), %rdi      # 0x7865
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 0x4ca3
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
testl %r15d, %r15d
je 0x4ace
cmpl $0x5, %r15d
movl $0x79616c70, %r15d      # imm = 0x79616C70
jne 0x4cf0
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2880
cmpw $0x7d, 0x70(%rsp)
jne 0x4aa1
jmp 0x49ea
movq 0x8(%rbx), %rdx
movl 0x2c(%rbx), %ecx
leaq 0x2b47(%rip), %rdi      # 0x782b
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 0x2bcd(%rip), %rdi      # 0x78e8
leaq 0x2c24(%rip), %rsi      # 0x7946
jmp 0x4a54
jmp 0x4d52
jmp 0x4d31
jmp 0x4d52
jmp 0x4d52
jmp 0x4d52
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x4d55
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x4d55
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x4d71
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2300
nop