// File: /workspace/github_cpp_makefile_star2_O2/mikeakohn[P]drumsplusplus/build/../src/Song.cxx
// Function: Song::parse_pattern(Tokens*)
int Song::parse_pattern(Tokens *tokens)
{
  int token_type;
  char token[1024], token1[1024];
  Beat beats[256];
  int ptr;
  char value[1024];
  float low_beat = 0;
  float next_beat = 0;
  int i, count, num_notes;
  int beat_time;
  int time_signature_beats;
  //int time_signature_base;
  int midi_channel;

  beat_time = 60000000 / song_info.bpm;
  time_signature_beats = song_info.time_signature_beats;
  //time_signature_base = song_info.time_signature_base;
  midi_channel = song_info.midi_channel;

  token_type = tokens->get(token);

  if (token_type != TOKEN_ALPHA)
  {
    printf("Error: Pattern is not alphanumeric at %s:%d\n",
      tokens->get_filename(), tokens->get_line());
    return -1;
  }

  if (patterns.find(token) != patterns.end())
  {
    printf("Error: Pattern %s is already defined at %s:%d\n",
      token, tokens->get_filename(), tokens->get_line());
    return -1;
  }

  std::string pattern_name = token;

  const int index = patterns.size();
  Pattern &pattern = patterns[pattern_name];

  pattern_names[index] = token;

  pattern.set_name(pattern_name);
  pattern.set_index(index);

#ifdef DEBUG
printf("parsing pattern: %s %d\n", token, index);
#endif

  token_type = tokens->get(token);

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

  ptr = 0;
  num_notes = 0;

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

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

    // Check if there are some modifications to the global song values.
    if (strcmp(token, "set") == 0)
    {
      token_type = tokens->get(token);

      if (token_type != TOKEN_ALPHA)
      {
        print_error(tokens, "variable to set", token);
        return -1;
      }

      token_type = tokens->get(token1);

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

      token_type = tokens->get(token1);

      if (token_type != TOKEN_NUMBER)
      {
        print_error(tokens, "a number", token1);
        return -1;
      }

      i = atoi(token1);

      if (i == 0)
      {
        print_error(tokens, "a non-zero integer", token1);
        return -1;
      }

      if (strcmp(token, "bpm") == 0)
      {
        beat_time = 60000000 / i;
      }
        else
      if (strcmp(token, "timesignature") == 0)
      {
        time_signature_beats = i;

        token_type = tokens->get(token1);

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

        token_type = tokens->get(token1);

        if (token_type != TOKEN_NUMBER)
        {
          print_error(tokens, "a number", token1);
          return -1;
        }

        i = atoi(token1);

        if (i == 0)
        {
          print_error(tokens, "a non-zero integer", token1);
          return -1;
        }

        //time_signature_base = i;
      }
        else
      if (strcmp(token, "midi_channel") == 0)
      {
        midi_channel = i;

        if (midi_channel > 15)
        {
          printf("Warning: MIDI channel %d is higher than max 15; line %d\n",
            midi_channel, tokens->get_line());
          midi_channel = 9;
        }
      }
        else
      {
        printf("Warning: Cannot set '%s' in pattern.\n", token);
      }

      token_type = tokens->get(token);

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

      continue;
    }

    // Read next line of beat information for an instrument from a pattern.
    if (token_type == TOKEN_NUMBER)
    {
      beats[ptr].channel = midi_channel;

      if (add_beats(tokens, beats, ptr, atoi(token), midi_channel) == -1)
      {
        return -1;
      }
    }
      else
    {
      if (defines.find(token) == defines.end())
      {
        printf("Error: %s is undefined at %s:%d\n",
          token, tokens->get_filename(), tokens->get_line());
        return -1;
      }

      strcpy(value, defines[token].c_str());

      if (Utility::is_number(value) == false)
      {
        printf("Error: %s is not a number at %s:%d\n",
          token, tokens->get_filename(), tokens->get_line());
        return -1;
      }

      beats[ptr].channel = midi_channel;

      if (add_beats(tokens, beats, ptr, atoi(value), midi_channel) == -1)
      {
        return -1;
      }
    }
  }

#ifdef DEBUG
for (int n = 0; n < ptr; n++)
{
  printf("beat: %d %d %f %d\n",
    beats[n].instrument,
    beats[n].channel,
    beats[n].value,
    beats[n].volume);
}
#endif

  // Convert beat information to the pattern format.  This looks like
  // basically a slow sorting algorithm.
  while(1)
  {
    low_beat = time_signature_beats + 1;
    next_beat = time_signature_beats + 1;
    count = 0;

    // Find the next lowest beat value and a count of how many
    // times that beat value exists.
    for (i = 0; i < ptr; i++)
    {
      if (beats[i].value > 0)
      {
        if (beats[i].value < low_beat)
        {
          low_beat = beats[i].value;
          count = 0;
        }

        if (beats[i].value == low_beat) { count++; }
      }
    }

    // If no more beats were found, break from adding to the pattern list.
    if (count == 0)
    {
      if (ptr == 0)
      {
        // If this is the first beat, add an empty pattern?
        int duration = (int)((time_signature_beats) * beat_time);

        pattern.add(0, 0, midi_channel, duration);
      }

      break;
    }

    // Find the next lowest value.  It seems like this whole process
    // could be done without doing this.
    for (i = 0; i < ptr; i++)
    {
      if (beats[i].value > 0)
      {
        if (beats[i].value < next_beat && beats[i].value != low_beat)
        {
          next_beat = beats[i].value;
        }
      }
    }

    // Scan through all the beats, and if it is equal to the current
    // lowest, add it to the pattern.  Then clear it out so it doesn't
    // get counted again.
    for (i = 0; i < ptr; i++)
    {
      if (beats[i].value == low_beat)
      {
        if (num_notes == 0 && low_beat != 1)
        {
          int duration = (int)((low_beat - 1) * beat_time);

          pattern.add(0, 0, midi_channel, duration);
        }

        int duration = 0;


        if (count == 1)
        {
          duration = (int)((next_beat - low_beat) * beat_time);
        }

        pattern.add(beats[i].instrument,
                    beats[i].volume,
                    beats[i].channel,
                    duration);

        beats[i].value = 0;

        count--;
      }
    }

    num_notes++;
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c98, %rsp           # imm = 0x1C98
movq %rdi, %r13
movl $0x3938700, %eax        # imm = 0x3938700
xorl %edx, %edx
idivl 0x20(%rdi)
movl %eax, %ebp
movq %rsi, %r12
movl 0x2c(%rdi), %r14d
movl 0x34(%rdi), %eax
movl %eax, 0x10(%rsp)
leaq 0x90(%rsp), %rsi
movq %r12, %rdi
callq 0x2828
pushq $-0x1
popq %rcx
movl %ecx, 0xc(%rsp)
cmpl $0x1, %eax
jne 0x3cd7
movq %r14, 0x38(%rsp)
movl %ebp, 0x1c(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x507c
leaq 0x78(%r13), %r14
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x5f8a
movq %rax, %r15
leaq 0x80(%r13), %rbp
leaq 0x70(%rsp), %rdi
callq 0x20f0
cmpq %rbp, %r15
je 0x3cf4
movq 0x8(%r12), %rdx
movl 0x2c(%r12), %ecx
leaq 0x3a15(%rip), %rdi      # 0x76d8
leaq 0x90(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
jmp 0x42ba
movq 0x8(%r12), %rsi
movl 0x2c(%r12), %edx
leaq 0x39c3(%rip), %rdi      # 0x76ab
xorl %eax, %eax
callq 0x2050
jmp 0x42ba
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x507c
movl 0xa0(%r13), %eax
movl %eax, 0x2c(%rsp)
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x5016
movq %rax, 0x20(%rsp)
leaq 0xd8(%r13), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x4fc8
leaq 0x90(%rsp), %rsi
movq %rax, %rdi
callq 0x2360
leaq 0x70(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x2110
movl 0x2c(%rsp), %eax
movq 0x20(%rsp), %rcx
movl %eax, 0x38(%rcx)
leaq 0x90(%rsp), %rsi
movq %r12, %rdi
callq 0x2828
cmpw $0x7b, 0x90(%rsp)
jne 0x4067
leaq 0x18(%rsp), %rax
andl $0x0, (%rax)
leaq 0xa8(%r13), %rbp
leaq 0xb0(%r13), %rax
movq %rax, 0x30(%rsp)
leaq 0x90(%rsp), %rsi
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
leaq 0x50(%rsp), %r14
movq %r12, %rdi
movq %rsi, %r15
callq 0x2828
cmpw $0x7d, 0x90(%rsp)
je 0x408a
cmpl $0x746573, 0x90(%rsp) # imm = 0x746573
je 0x3eee
cmpl $0x2, %eax
jne 0x3e21
movslq 0x18(%rsp), %rax
shlq $0x4, %rax
movl 0x10(%rsp), %ebx
movl %ebx, 0x898(%rsp,%rax)
movq %r15, %rdi
callq 0x22f0
movq %r13, %rdi
movq %r12, %rsi
leaq 0x890(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movl %eax, %r8d
movl %ebx, %r9d
callq 0x4a3a
jmp 0x3edd
movq %r14, %rdi
movq %r15, %rsi
leaq 0x17(%rsp), %rdx
callq 0x507c
movq %rbp, %rbx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x5b04
movq %rax, %rbp
movq %r14, %rdi
callq 0x20f0
cmpq 0x30(%rsp), %rbp
je 0x427d
movq %r14, %rdi
movq %r15, %rsi
leaq 0x17(%rsp), %rdx
callq 0x507c
movq %rbx, %rbp
movq %rbx, %rdi
movq %r14, %rsi
callq 0x50b8
movq (%rax), %rsi
leaq 0x1890(%rsp), %rbx
movq %rbx, %rdi
callq 0x2180
movq %r14, %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x662c
testb %al, %al
je 0x4290
movslq 0x18(%rsp), %rax
shlq $0x4, %rax
movl 0x10(%rsp), %ebx
movl %ebx, 0x898(%rsp,%rax)
leaq 0x1890(%rsp), %rdi
callq 0x22f0
movq %r13, %rdi
movq %r12, %rsi
leaq 0x890(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movl %eax, %r8d
movl %ebx, %r9d
callq 0x4a3a
cmpl $-0x1, %eax
movq %r15, %rsi
jne 0x3db2
jmp 0x42b0
movq %r12, %rdi
movq %r15, %rsi
callq 0x2828
cmpl $0x1, %eax
jne 0x42e2
movq %r12, %rdi
leaq 0x490(%rsp), %rsi
callq 0x2828
cmpw $0x3d, 0x490(%rsp)
jne 0x42f3
movq %r12, %rdi
leaq 0x490(%rsp), %rsi
callq 0x2828
cmpl $0x2, %eax
jne 0x42d0
leaq 0x490(%rsp), %rdi
callq 0x22f0
testl %eax, %eax
je 0x42d9
movl %eax, %ebx
cmpl $0x6d7062, 0x90(%rsp) # imm = 0x6D7062
jne 0x3f73
movl $0x3938700, %eax        # imm = 0x3938700
xorl %edx, %edx
idivl %ebx
movl %eax, 0x1c(%rsp)
movq %r15, %rsi
jmp 0x4041
movq %r15, %r14
movq %r15, %rdi
leaq 0x34b8(%rip), %rsi      # 0x7438
pushq $0xe
popq %rdx
callq 0x21d0
testl %eax, %eax
je 0x3fbb
movq %r14, %rdi
leaq 0x3529(%rip), %rsi      # 0x74bf
pushq $0xd
popq %rdx
callq 0x21d0
testl %eax, %eax
je 0x4018
leaq 0x37c5(%rip), %rdi      # 0x776e
movq %r14, %rsi
xorl %eax, %eax
callq 0x2050
movq %r14, %rsi
jmp 0x403c
movq %r12, %rdi
leaq 0x490(%rsp), %rsi
callq 0x2828
leaq 0x50(%rsp), %r14
cmpw $0x2f, 0x490(%rsp)
jne 0x430c
movq %r12, %rdi
leaq 0x490(%rsp), %rsi
callq 0x2828
cmpl $0x2, %eax
jne 0x42d0
leaq 0x490(%rsp), %rdi
callq 0x22f0
movl %ebx, %ecx
movq %rcx, 0x38(%rsp)
testl %eax, %eax
movq %r15, %rsi
jne 0x4041
jmp 0x42d9
cmpl $0x10, %ebx
jl 0x4035
movl 0x2c(%r12), %edx
leaq 0x370c(%rip), %rdi      # 0x7735
movl %ebx, %esi
xorl %eax, %eax
callq 0x2050
pushq $0x9
popq %rbx
movl %ebx, 0x10(%rsp)
movq %r15, %rsi
leaq 0x50(%rsp), %r14
movq %r12, %rdi
callq 0x2828
cmpw $0x3b, 0x90(%rsp)
movq %r15, %rsi
je 0x3db2
leaq 0x34d2(%rip), %rdx      # 0x7534
jmp 0x42e9
leaq 0x3699(%rip), %rdx      # 0x7707
leaq 0x90(%rsp), %rcx
movq %r12, %rsi
callq 0x49e6
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
jmp 0x42b0
movq 0x38(%rsp), %rax
incl %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x30(%rsp)
cvtsi2ssl 0x1c(%rsp), %xmm0
movss %xmm0, 0x48(%rsp)
movl 0x18(%rsp), %eax
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %r13d
movl %eax, 0x44(%rsp)
cmovgl %eax, %r13d
shlq $0x4, %r13
movzbl 0x10(%rsp), %eax
movl %eax, 0x10(%rsp)
xorl %edx, %edx
xorl %eax, %eax
xorl %r14d, %r14d
movss 0x30(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
cmpq %rax, %r13
je 0x4120
movss 0x890(%rsp,%rax), %xmm1
ucomiss 0x3236(%rip), %xmm1     # 0x7328
jbe 0x4114
movss 0xc(%rsp), %xmm2
ucomiss %xmm1, %xmm2
cmoval %ecx, %r14d
movaps %xmm1, %xmm0
minss %xmm2, %xmm0
ucomiss %xmm0, %xmm1
jne 0x411a
jp 0x411a
incl %r14d
jmp 0x411a
movss 0xc(%rsp), %xmm0
addq $0x10, %rax
jmp 0x40d7
testl %r14d, %r14d
je 0x4247
xorl %eax, %eax
movss 0x30(%rsp), %xmm0
cmpq %rax, %r13
je 0x4178
movss 0x890(%rsp,%rax), %xmm1
movaps %xmm1, %xmm2
cmpltps %xmm0, %xmm2
xorps %xmm3, %xmm3
cmpltps %xmm1, %xmm3
andps %xmm2, %xmm3
movd %xmm3, %ecx
testb $0x1, %cl
je 0x4172
movaps %xmm1, %xmm2
cmpneqss 0xc(%rsp), %xmm2
movaps %xmm2, %xmm3
andps %xmm1, %xmm3
andnps %xmm0, %xmm2
orps %xmm3, %xmm2
movaps %xmm2, %xmm0
addq $0x10, %rax
jmp 0x4131
movl %edx, 0x4c(%rsp)
testl %edx, %edx
sete %al
movss 0xc(%rsp), %xmm3
ucomiss 0x3192(%rip), %xmm3     # 0x7320
setp %cl
setne %r12b
orb %cl, %r12b
andb %al, %r12b
movaps %xmm3, %xmm1
addss 0x317e(%rip), %xmm1     # 0x7324
movss 0x48(%rsp), %xmm2
mulss %xmm2, %xmm1
cvttss2si %xmm1, %ebp
subss %xmm3, %xmm0
mulss %xmm2, %xmm0
cvttss2si %xmm0, %ebx
xorl %r15d, %r15d
cmpq %r15, %r13
je 0x423a
movss 0x890(%rsp,%r15), %xmm0
ucomiss 0xc(%rsp), %xmm0
jne 0x4234
jp 0x4234
testb %r12b, %r12b
je 0x41f5
movq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movl 0x10(%rsp), %ecx
movl %ebp, %r8d
callq 0x307c
cmpl $0x1, %r14d
movl $0x0, %r8d
cmovel %ebx, %r8d
movzbl 0x894(%rsp,%r15), %esi
movzbl 0x89c(%rsp,%r15), %edx
movzbl 0x898(%rsp,%r15), %ecx
movq 0x20(%rsp), %rdi
callq 0x307c
andl $0x0, 0x890(%rsp,%r15)
decl %r14d
addq $0x10, %r15
jmp 0x41c3
movl 0x4c(%rsp), %edx
incl %edx
xorl %ecx, %ecx
jmp 0x40cc
movl $0x0, 0xc(%rsp)
cmpl $0x0, 0x44(%rsp)
jne 0x42b0
movq 0x38(%rsp), %r8
imull 0x1c(%rsp), %r8d
movl $0x0, 0xc(%rsp)
movq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movl 0x10(%rsp), %ecx
callq 0x307c
jmp 0x42b0
movq 0x8(%r12), %rdx
movl 0x2c(%r12), %ecx
leaq 0x3506(%rip), %rdi      # 0x7794
jmp 0x42a1
movq 0x8(%r12), %rdx
movl 0x2c(%r12), %ecx
leaq 0x3514(%rip), %rdi      # 0x77b5
leaq 0x90(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
leaq 0x70(%rsp), %rdi
callq 0x20f0
movl 0xc(%rsp), %eax
addq $0x1c98, %rsp           # imm = 0x1C98
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3442(%rip), %rdx      # 0x7719
jmp 0x42fa
leaq 0x3442(%rip), %rdx      # 0x7722
jmp 0x42fa
leaq 0x3420(%rip), %rdx      # 0x7709
leaq 0x90(%rsp), %rcx
jmp 0x4302
leaq 0x309f(%rip), %rdx      # 0x7399
leaq 0x490(%rsp), %rcx
movq %r12, %rsi
callq 0x49e6
jmp 0x42b0
leaq 0x3133(%rip), %rdx      # 0x7446
jmp 0x42fa
jmp 0x433d
jmp 0x433d
jmp 0x4332
jmp 0x431f
jmp 0x433d
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x20f0
jmp 0x4340
jmp 0x433d
jmp 0x433d
movq %rax, %rbx
jmp 0x434a
jmp 0x433d
jmp 0x433d
jmp 0x433d
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x2330