// File: /workspace/github_c_makefile_star2_O2/hamsternz[P]drummer/drummer.c
// Function: generate_sample
void generate_sample(int16_t o[2]) {
   int32_t samples[N_VOICES];
   int32_t output[2];

   // Start of new tick?
   if(sampleOfTick == 0) {
      for(int i = 0; i < N_VOICES; i++) {
         if(patterns[bars[bar]]->pattern[i][tickOfBar] != ' ') {
            voices[i].pos  = 1;
            voices[i].emph = (patterns[bars[bar]]->pattern[i][tickOfBar]-'1') * 12;
         }
      }
   }

   for(int i = 0; i < N_VOICES; i++) {
      samples[i] = 0;
      if(voices[i].sample >= 0) {
         samples[i] = sounds[voices[i].sample].samples[voices[i].pos ];
         if(voices[i].pos != 0) {
            voices[i].pos++;
            if(voices[i].pos  >= sounds[voices[i].sample].len) {
               voices[i].pos  = 0;
            } 
         }
      }
   }

   output[0] = 0;
   output[1] = 0;
   for(int i = 0; i < N_VOICES; i++) {
      int vol = voices[i].emph + voices[i].volume;
      output[0] += vol * samples[i]*(voices[i].pan);
      output[1] += vol * samples[i]*(32-voices[i].pan);
   }

   sampleOfTick++;
   if(sampleOfTick == samplesPerTick) {
      sampleOfTick = 0;
      tickOfBar++;
      if(tickOfBar == BAR_LEN) {
         tickOfBar = 0;
         bar++;
      }
   }

   // Convert back to 16 bit
   o[0] = output[0] / (32 *4 * 256);
   o[1] = output[1] / (32 *4 * 256);
}

// Assembly:
pushq %rbx
movl 0x184e9(%rip), %edx     # 0x1965c
testl %edx, %edx
je 0x127a
leaq 0x1846a(%rip), %rax     # 0x195ec
xorl %ecx, %ecx
leaq 0x17c15(%rip), %rsi     # 0x18da0
cmpq $0x6, %rcx
je 0x11d7
andl $0x0, -0x20(%rsp,%rcx,4)
movslq -0x4(%rax), %r8
testq %r8, %r8
js 0x11ce
shlq $0x4, %r8
movq (%r8,%rsi), %r10
movslq (%rax), %r9
movswl (%r10,%r9,2), %r10d
movl %r10d, -0x20(%rsp,%rcx,4)
testq %r9, %r9
je 0x11ce
incq %r9
cmpq %r9, 0x8(%r8,%rsi)
movl $0x0, %r8d
cmoval %r9d, %r8d
movl %r8d, (%rax)
incq %rcx
addq $0x14, %rax
jmp 0x118b
xorl %esi, %esi
leaq 0x18400(%rip), %r8      # 0x195e0
movl $0x20, %r9d
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x6, %rsi
je 0x121d
movl 0x4(%r8), %r10d
addl 0x10(%r8), %r10d
movl (%r8), %r11d
imull -0x20(%rsp,%rsi,4), %r10d
movl %r9d, %ebx
subl %r11d, %ebx
imull %r10d, %ebx
imull %r11d, %r10d
addl %r10d, %eax
addl %ebx, %ecx
incq %rsi
addq $0x14, %r8
jmp 0x11ea
incl %edx
movl %edx, 0x18437(%rip)     # 0x1965c
xorl %esi, %esi
cmpb $0x0, 0x1843a(%rip)     # 0x19668
movl $0x3b4, %r8d            # imm = 0x3B4
cmovel %esi, %r8d
cmpl %r8d, %edx
jne 0x1264
andl $0x0, 0x18418(%rip)     # 0x1965c
movl 0x1841a(%rip), %edx     # 0x19664
incl %edx
movl %edx, 0x18412(%rip)     # 0x19664
cmpl $0x48, %edx
jne 0x1264
andl $0x0, 0x18406(%rip)     # 0x19664
incl 0x183fc(%rip)           # 0x19660
movl $0x8000, %esi           # imm = 0x8000
cltd
idivl %esi
movw %ax, (%rdi)
movl %ecx, %eax
cltd
idivl %esi
movw %ax, 0x2(%rdi)
popq %rbx
retq
movslq 0x183df(%rip), %rax     # 0x19660
leaq 0x18328(%rip), %rcx     # 0x195b0
movslq (%rcx,%rax,4), %rcx
leaq 0x182cd(%rip), %rsi     # 0x19560
movslq 0x183ca(%rip), %rax     # 0x19664
addq (%rsi,%rcx,8), %rax
xorl %ecx, %ecx
leaq 0x18339(%rip), %rsi     # 0x195e0
cmpq $0x78, %rcx
je 0x117b
movsbl (%rax), %r8d
cmpl $0x20, %r8d
je 0x12d3
movl $0x1, 0xc(%rcx,%rsi)
imull $0xc, %r8d, %r8d
addl $0xfffffdb4, %r8d       # imm = 0xFFFFFDB4
movl %r8d, 0x10(%rcx,%rsi)
addq $0x14, %rcx
addq $0x48, %rax
jmp 0x12a7