// File: /workspace/github_cpp_makefile_star2_O1/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, 0x14(%rsp)
movq %rsi, %r12
movl 0x2c(%rdi), %eax
movq %rax, 0x50(%rsp)
movl 0x34(%rdi), %eax
movl %eax, 0x8(%rsp)
leaq 0x90(%rsp), %rsi
movq %r12, %rdi
callq 0x2880
cmpl $0x1, %eax
jne 0x3e7a
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
callq 0x20e0
leaq (%rsp,%rax), %rdx
addq $0x90, %rdx
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x6180
leaq 0x78(%r13), %rbx
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x6522
movq %rax, %r14
leaq 0x80(%r13), %r15
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x3e53
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
cmpq %r15, %r14
je 0x3eab
movq 0x8(%r12), %rdx
movl 0x2c(%r12), %ecx
leaq 0x385f(%rip), %rdi      # 0x76c8
leaq 0x90(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
jmp 0x3e92
movq 0x8(%r12), %rsi
movl 0x2c(%r12), %edx
leaq 0x3810(%rip), %rdi      # 0x769b
xorl %eax, %eax
callq 0x2050
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x1c98, %rsp           # imm = 0x1C98
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%rsp), %r14
leaq 0x40(%rsp), %rax
movq %rax, (%r14)
leaq 0x90(%rsp), %r15
movq %r15, %rdi
callq 0x20e0
leaq (%rsp,%rax), %rdx
addq $0x90, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x6180
movl 0xa0(%r13), %eax
movl %eax, 0x2c(%rsp)
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x55ca
movq %rax, 0x20(%rsp)
leaq 0xd8(%r13), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x5566
movq %rax, %rbx
movq 0x8(%rax), %r14
leaq 0x90(%rsp), %rbp
movq %rbp, %rdi
callq 0x20e0
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %rbp, %rcx
movq %rax, %r8
callq 0x22b0
leaq 0x30(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x2100
movl 0x2c(%rsp), %eax
movq 0x20(%rsp), %rcx
movl %eax, 0x38(%rcx)
leaq 0x90(%rsp), %rsi
movq %r12, %rdi
callq 0x2880
cmpw $0x7b, 0x90(%rsp)
jne 0x42cc
leaq 0xc(%rsp), %rax
movl $0x0, (%rax)
leaq 0xa8(%r13), %rax
movq %rax, 0x18(%rsp)
leaq 0x78(%rsp), %rbp
leaq 0xb0(%r13), %rax
movq %rax, 0x58(%rsp)
leaq 0x90(%rsp), %r14
leaq 0x68(%rsp), %r15
movq %r12, %rdi
movq %r14, %rsi
callq 0x2880
cmpw $0x7d, 0x90(%rsp)
je 0x42f8
cmpl $0x746573, 0x90(%rsp) # imm = 0x746573
je 0x4128
cmpl $0x2, %eax
jne 0x4014
movslq 0xc(%rsp), %rax
shlq $0x4, %rax
movl 0x8(%rsp), %ebx
movl %ebx, 0xc98(%rsp,%rax)
movq %r14, %rdi
callq 0x22c0
movq %r13, %rdi
movq %r12, %rsi
leaq 0xc90(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movl %eax, %r8d
movl %ebx, %r9d
callq 0x4f34
jmp 0x411a
movq %rbp, 0x68(%rsp)
movq %r14, %rdi
callq 0x20e0
leaq (%rsp,%rax), %rdx
addq $0x90, %rdx
movq %r15, %rbx
movq %r15, %rdi
movq %r14, %rsi
callq 0x6180
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x60f6
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4061
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21b0
cmpq 0x58(%rsp), %rbx
je 0x44fe
movq %rbp, 0x68(%rsp)
movq %r14, %rdi
callq 0x20e0
leaq (%rsp,%rax), %rdx
addq $0x90, %rdx
movq %r15, %rbx
movq %r15, %rdi
movq %r14, %rsi
callq 0x6180
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x5646
movq (%rax), %rsi
leaq 0x890(%rsp), %rdi
callq 0x2170
movq 0x68(%rsp), %rdi
cmpq %rbp, %rdi
je 0x40c6
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x890(%rsp), %rdi
callq 0x6bd8
testb %al, %al
je 0x4511
movslq 0xc(%rsp), %rax
shlq $0x4, %rax
movl 0x8(%rsp), %ebx
movl %ebx, 0xc98(%rsp,%rax)
leaq 0x890(%rsp), %rdi
callq 0x22c0
movq %r13, %rdi
movq %r12, %rsi
leaq 0xc90(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movl %eax, %r8d
movl %ebx, %r9d
callq 0x4f34
cmpl $-0x1, %eax
jne 0x3fa5
jmp 0x4531
movq %r12, %rdi
movq %r14, %rsi
callq 0x2880
cmpl $0x1, %eax
jne 0x458f
movq %r12, %rdi
leaq 0x490(%rsp), %rsi
callq 0x2880
cmpw $0x3d, 0x490(%rsp)
jne 0x45ac
movq %r12, %rdi
leaq 0x490(%rsp), %rsi
callq 0x2880
cmpl $0x2, %eax
jne 0x455b
leaq 0x490(%rsp), %rdi
callq 0x22c0
testl %eax, %eax
je 0x4575
movl %eax, %ebx
cmpl $0x6d7062, 0x90(%rsp) # imm = 0x6D7062
jne 0x41aa
movl $0x3938700, %eax        # imm = 0x3938700
xorl %edx, %edx
idivl %ebx
movl %eax, 0x14(%rsp)
jmp 0x4298
movq 0x90(%rsp), %rax
movabsq $0x6e676973656d6974, %rcx # imm = 0x6E676973656D6974
xorq %rcx, %rax
movq 0x96(%rsp), %rcx
movabsq $0x65727574616e67, %rdx # imm = 0x65727574616E67
xorq %rdx, %rcx
orq %rax, %rcx
je 0x421b
movq 0x90(%rsp), %rax
movabsq $0x6168635f6964696d, %rcx # imm = 0x6168635F6964696D
xorq %rcx, %rax
movq 0x95(%rsp), %rcx
movabsq $0x6c656e6e616863, %rdx # imm = 0x6C656E6E616863
xorq %rdx, %rcx
orq %rax, %rcx
je 0x4270
leaq 0x354f(%rip), %rdi      # 0x775e
movq %r14, %rsi
xorl %eax, %eax
callq 0x2050
jmp 0x4298
movq %r12, %rdi
leaq 0x490(%rsp), %rsi
callq 0x2880
cmpw $0x2f, 0x490(%rsp)
jne 0x45d1
movq %r12, %rdi
leaq 0x490(%rsp), %rsi
callq 0x2880
cmpl $0x2, %eax
jne 0x455b
leaq 0x490(%rsp), %rdi
callq 0x22c0
movl %ebx, %ecx
movq %rcx, 0x50(%rsp)
testl %eax, %eax
jne 0x4298
jmp 0x4575
cmpl $0x10, %ebx
jl 0x4294
movl 0x2c(%r12), %edx
leaq 0x34a4(%rip), %rdi      # 0x7725
movl %ebx, %esi
xorl %eax, %eax
callq 0x2050
movl $0x9, 0x8(%rsp)
jmp 0x4298
movl %ebx, 0x8(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x2880
cmpw $0x3b, 0x90(%rsp)
je 0x3fa5
movq 0x8(%r12), %rcx
movl 0x2c(%r12), %r8d
leaq 0x3625(%rip), %rdi      # 0x78e8
leaq 0x325a(%rip), %rsi      # 0x7524
jmp 0x42e4
movq 0x8(%r12), %rcx
movl 0x2c(%r12), %r8d
leaq 0x360b(%rip), %rdi      # 0x78e8
leaq 0x3413(%rip), %rsi      # 0x76f7
leaq 0x90(%rsp), %rdx
xorl %eax, %eax
callq 0x2050
jmp 0x4531
movq 0x50(%rsp), %rax
incl %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x10(%rsp)
movl 0xc(%rsp), %edx
xorps %xmm0, %xmm0
cvtsi2ssl 0x14(%rsp), %xmm0
movss %xmm0, 0x64(%rsp)
movq %rdx, %r13
shlq $0x4, %r13
xorl %ecx, %ecx
movzbl 0x8(%rsp), %eax
movl %eax, 0x58(%rsp)
movl $0x0, 0x28(%rsp)
movq %rdx, 0x88(%rsp)
movl $0x0, %r12d
movss 0x10(%rsp), %xmm1
testl %edx, %edx
jle 0x4392
xorl %eax, %eax
movss 0x10(%rsp), %xmm0
xorl %r12d, %r12d
movss 0xc90(%rsp,%rax), %xmm1
ucomiss 0x2faf(%rip), %xmm1     # 0x7318
jbe 0x4386
ucomiss %xmm1, %xmm0
cmoval %ecx, %r12d
movaps %xmm1, %xmm2
minss %xmm0, %xmm2
movaps %xmm2, %xmm0
ucomiss %xmm2, %xmm1
jne 0x4386
jp 0x4386
incl %r12d
movaps %xmm0, %xmm1
addq $0x10, %rax
cmpq %rax, %r13
jne 0x4359
movss %xmm1, 0x18(%rsp)
testl %r12d, %r12d
je 0x44d6
movss 0x10(%rsp), %xmm0
testl %edx, %edx
jle 0x43f8
xorl %eax, %eax
movss 0x10(%rsp), %xmm0
movss 0xc90(%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 0x43ef
movaps %xmm1, %xmm2
cmpneqss 0x18(%rsp), %xmm2
movaps %xmm2, %xmm3
andps %xmm1, %xmm3
andnps %xmm0, %xmm2
orps %xmm3, %xmm2
movaps %xmm2, %xmm0
addq $0x10, %rax
cmpq %rax, %r13
jne 0x43b3
testl %edx, %edx
jle 0x44c3
cmpl $0x0, 0x28(%rsp)
sete %al
movss 0x18(%rsp), %xmm3
ucomiss 0x2efb(%rip), %xmm3     # 0x7310
setp %cl
setne %bpl
orb %cl, %bpl
andb %al, %bpl
movaps %xmm3, %xmm1
addss 0x2ee7(%rip), %xmm1     # 0x7314
movss 0x64(%rsp), %xmm2
mulss %xmm2, %xmm1
cvttss2si %xmm1, %r14d
subss %xmm3, %xmm0
mulss %xmm2, %xmm0
cvttss2si %xmm0, %ebx
xorl %r15d, %r15d
movss 0xc90(%rsp,%r15), %xmm0
ucomiss 0x18(%rsp), %xmm0
jne 0x44ba
jp 0x44ba
testb %bpl, %bpl
je 0x4478
movq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movl 0x58(%rsp), %ecx
movl %r14d, %r8d
callq 0x311e
cmpl $0x1, %r12d
movl $0x0, %r8d
cmovel %ebx, %r8d
movzbl 0xc94(%rsp,%r15), %esi
movzbl 0xc9c(%rsp,%r15), %edx
movzbl 0xc98(%rsp,%r15), %ecx
movq 0x20(%rsp), %rdi
callq 0x311e
movl $0x0, 0xc90(%rsp,%r15)
decl %r12d
addq $0x10, %r15
cmpq %r15, %r13
jne 0x444b
incl 0x28(%rsp)
movq 0x88(%rsp), %rdx
xorl %ecx, %ecx
jmp 0x433e
xorl %ebx, %ebx
testl %edx, %edx
jne 0x4536
movq 0x50(%rsp), %r8
imull 0x14(%rsp), %r8d
xorl %ebx, %ebx
movzbl 0x8(%rsp), %ecx
movq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x311e
jmp 0x4536
movq 0x8(%r12), %rdx
movl 0x2c(%r12), %ecx
leaq 0x3275(%rip), %rdi      # 0x7784
jmp 0x4522
movq 0x8(%r12), %rdx
movl 0x2c(%r12), %ecx
leaq 0x3283(%rip), %rdi      # 0x77a5
leaq 0x90(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x3e97
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x3e97
movq 0x8(%r12), %rcx
movl 0x2c(%r12), %r8d
leaq 0x337c(%rip), %rdi      # 0x78e8
leaq 0x3196(%rip), %rsi      # 0x7709
jmp 0x45c4
movq 0x8(%r12), %rcx
movl 0x2c(%r12), %r8d
leaq 0x3362(%rip), %rdi      # 0x78e8
leaq 0x3185(%rip), %rsi      # 0x7712
jmp 0x45c4
movq 0x8(%r12), %rcx
movl 0x2c(%r12), %r8d
leaq 0x3348(%rip), %rdi      # 0x78e8
leaq 0x3152(%rip), %rsi      # 0x76f9
jmp 0x42e4
movq 0x8(%r12), %rcx
movl 0x2c(%r12), %r8d
leaq 0x332b(%rip), %rdi      # 0x78e8
leaq 0x2dc5(%rip), %rsi      # 0x7389
leaq 0x490(%rsp), %rdx
jmp 0x42ec
movq 0x8(%r12), %rcx
movl 0x2c(%r12), %r8d
leaq 0x3306(%rip), %rdi      # 0x78e8
leaq 0x2e4d(%rip), %rsi      # 0x7436
jmp 0x45c4
jmp 0x4620
jmp 0x4620
jmp 0x4615
jmp 0x45f5
jmp 0x4620
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4623
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x4623
jmp 0x4620
jmp 0x4620
movq %rax, %rbx
jmp 0x463f
jmp 0x4620
jmp 0x4620
jmp 0x4620
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x463f
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2300
nop