// File: /workspace/github_c_makefile_star2_O1/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:
movl 0x184ca(%rip), %eax     # 0x1965c
testl %eax, %eax
jne 0x11f4
movslq 0x184c3(%rip), %rcx     # 0x19660
leaq 0x1840c(%rip), %rdx     # 0x195b0
movslq (%rdx,%rcx,4), %rdx
leaq 0x183b1(%rip), %rsi     # 0x19560
movslq 0x184ae(%rip), %rcx     # 0x19664
addq (%rsi,%rdx,8), %rcx
xorl %edx, %edx
leaq 0x1841d(%rip), %rsi     # 0x195e0
movsbl (%rcx), %r8d
cmpl $0x20, %r8d
je 0x11e6
movl $0x1, 0xc(%rdx,%rsi)
leal (%r8,%r8,2), %r8d
leal -0x24c(,%r8,4), %r8d
movl %r8d, 0x10(%rdx,%rsi)
addq $0x14, %rdx
addq $0x48, %rcx
cmpq $0x78, %rdx
jne 0x11c3
leaq 0x183f1(%rip), %rcx     # 0x195ec
xorl %edx, %edx
leaq 0x17b9c(%rip), %rsi     # 0x18da0
movl $0x0, -0x18(%rsp,%rdx,4)
movslq -0x4(%rcx), %r8
testq %r8, %r8
js 0x1244
shlq $0x4, %r8
movq (%r8,%rsi), %r10
movslq (%rcx), %r9
movswl (%r10,%r9,2), %r10d
movl %r10d, -0x18(%rsp,%rdx,4)
testq %r9, %r9
je 0x1244
incq %r9
cmpq %r9, 0x8(%r8,%rsi)
movl $0x0, %r8d
cmoval %r9d, %r8d
movl %r8d, (%rcx)
incq %rdx
addq $0x14, %rcx
cmpq $0x6, %rdx
jne 0x1204
xorl %esi, %esi
leaq 0x18386(%rip), %r8      # 0x195e0
xorl %ecx, %ecx
xorl %edx, %edx
movl (%r8), %r9d
movl 0x4(%r8), %r10d
addl 0x10(%r8), %r10d
imull -0x18(%rsp,%rsi,4), %r10d
movl $0x20, %r11d
subl %r9d, %r11d
imull %r10d, %r11d
imull %r9d, %r10d
addl %r10d, %edx
addl %r11d, %ecx
incq %rsi
addq $0x14, %r8
cmpq $0x6, %rsi
jne 0x125e
incl %eax
movl %eax, 0x183c1(%rip)     # 0x1965c
xorl %esi, %esi
cmpb $0x0, 0x183c4(%rip)     # 0x19668
movl $0x3b4, %r8d            # imm = 0x3B4
cmovel %esi, %r8d
cmpl %r8d, %eax
jne 0x12e0
movl $0x0, 0x1839f(%rip)     # 0x1965c
movl 0x183a1(%rip), %eax     # 0x19664
incl %eax
movl %eax, 0x18399(%rip)     # 0x19664
cmpl $0x48, %eax
jne 0x12e0
movl $0x0, 0x1838a(%rip)     # 0x19664
incl 0x18380(%rip)           # 0x19660
movl $0x7fff, %eax           # imm = 0x7FFF
leal (%rdx,%rax), %esi
testl %edx, %edx
cmovnsl %edx, %esi
shrl $0xf, %esi
movw %si, (%rdi)
addl %ecx, %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
shrl $0xf, %eax
movw %ax, 0x2(%rdi)
retq