// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/src/teksnd.c
// Function: transmutehmp
static int transmutehmp(char *filedata)
{
     const char ndmfident[] = "HMIMIDIP013195";
     const int commandlengths[8] = { 2, 2, 2, 2, 1, 1, 2, -1 };
     const int syscomlengths[16] =  { -1, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1 };

     if (memcmp(ndmfident, filedata, sizeof ndmfident)) return -1;

     // Extract the important values from the NDMF header.
     struct ndmfheader *ndmfhead = (struct ndmfheader *)filedata;
     int numtracks = B_LITTLE32(ndmfhead->numtracks);
     int ticksperqunote = B_LITTLE32(ndmfhead->ticksperqunote);
     int tempo = 120000000 / B_LITTLE32(ndmfhead->tempo);
     ndmfhead = NULL;

     // Construct a new MIDI header.
     struct smfheader *smfhead = (struct smfheader *)filedata;
     memcpy(smfhead->ident, "MThd", 4);
     smfhead->headsize = B_BIG32(6);
     smfhead->format = B_BIG16(1);
     smfhead->numtracks = B_BIG16(numtracks);
     smfhead->ticksperqunote = B_BIG16(ticksperqunote);

     // Transcribe tracks.
     int ndmffileofs = sizeof(struct ndmfheader);
     int smffileofs = sizeof(struct smfheader);
     for (int trk = 0; trk < numtracks; trk++) {
          struct ndmftrackheader *ndmftrack = (struct ndmftrackheader *)&filedata[ndmffileofs];
          struct smftrackheader *smftrack = (struct smftrackheader *)&filedata[smffileofs];

          int ndmfdatalen = B_LITTLE32(ndmftrack->tracklen) - 12;
          int smfdatalen = ndmfdatalen;
          if (trk == 0) {
               // We need to add a tempo event to the first MIDI track.
               smfdatalen += 7;
          }

          memcpy(smftrack->ident, "MTrk", 4);
          smftrack->tracklen = B_BIG32(smfdatalen);

          uint8_t *ndmfdata = (uint8_t *)&ndmftrack->data[0];
          uint8_t *smfdata = (uint8_t *)&smftrack->data[0];

          if (trk == 0) {
               // Insert a tempo event.
               *(smfdata++) = 0;
               *(smfdata++) = 0xff;
               *(smfdata++) = 0x51;
               *(smfdata++) = 3;
               *(smfdata++) = (tempo>>16)&0xff;
               *(smfdata++) = (tempo>>8)&0xff;
               *(smfdata++) = tempo&0xff;
          }

          // Process events.
          uint8_t status = 0;
          for (int i = 0; i < ndmfdatalen; ) {
               uint8_t b;
               int copylen = 0;

               // Re-encode the offset.
               uint8_t vlenbytes[4], vlencnt = 0;
               do {
                    b = ndmfdata[i++];
                    vlenbytes[vlencnt++] = b & 0x7f;
               } while (!(b & 0x80));
               do {
                    b = vlenbytes[--vlencnt];
                    if (vlencnt) b |= 0x80;
                    *(smfdata++) = b;
               } while (vlencnt > 0);

               b = ndmfdata[i];
               if (b&0x80) {
                    // A new status byte.
                    *(smfdata++) = b;
                    i++;

                    status = b;    // Keep for running status.
                    copylen = commandlengths[(status & 0x7f)>>4];

                    if ((b&0xf0) == 0xf0) {
                         switch (b&0x0f) {
                              case 0x0: // Sysex.
                                   do *(smfdata++) = (b = ndmfdata[i++]);
                                   while (!(b&0x80) && b != 0xf7);
                                   break;
                              case 0xf: // Meta.
                                   *(smfdata++) = ndmfdata[i++];  // Type.
                                   copylen = (*(smfdata++) = ndmfdata[i++]);     // Length.
                                   break;
                              default:  // Sys common.
                                   copylen = syscomlengths[b&0x0f];
                                   break;
                         }
                    } else if ((b&0xf0) == 0xb0) {     // Controller change.
                         // SOS/EMIDI custom controller range. For whatever reason SOS
                         // controller values have their high bit set.
                         if (ndmfdata[i] >= 102 && ndmfdata[i] <= 119) {
                              if (trk == 1 && ndmfdata[i] == 110) {        // Global loop start
                                   *(smfdata++) = 118;
                                   *(smfdata++) = (ndmfdata[i+1] & 0x7f);
                              } else if (trk == 1 && ndmfdata[i] == 111) { // Global loop end.
                                   *(smfdata++) = 119;
                                   *(smfdata++) = 127;
                              } else {
                                   *(smfdata++) = 102; // Neuter all other controllers.
                                   *(smfdata++) = 0;
                              }
                              i += 2;
                              copylen = 0;
                         }
                    }
               } else {
                    copylen = commandlengths[(status & 0x7f)>>4];
               }

               for (; copylen>0; copylen--) {     // Copy data bytes.
                    *(smfdata++) = ndmfdata[i++];
               }
          }

          ndmffileofs += ndmfdatalen + 12;
          smffileofs += smfdatalen + 8;
     }

     return smffileofs;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movq 0xb27fd(%rip), %rax     # 0xda0c3
movq %rax, -0x1f(%rbp)
movl 0xb27fb(%rip), %eax     # 0xda0cb
movl %eax, -0x17(%rbp)
movw 0xb27f5(%rip), %ax      # 0xda0cf
movw %ax, -0x13(%rbp)
movb 0xb27ed(%rip), %al      # 0xda0d1
movb %al, -0x11(%rbp)
movq 0xb27f2(%rip), %rax     # 0xda0e0
movq %rax, -0x40(%rbp)
movq 0xb27ef(%rip), %rax     # 0xda0e8
movq %rax, -0x38(%rbp)
movq 0xb27ec(%rip), %rax     # 0xda0f0
movq %rax, -0x30(%rbp)
movq 0xb27e9(%rip), %rax     # 0xda0f8
movq %rax, -0x28(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x8410
movl $0xffffffff, -0x80(%rbp) # imm = 0xFFFFFFFF
movl $0x2, -0x78(%rbp)
movl $0x1, -0x74(%rbp)
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
leaq -0x1f(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0xf, %edx
callq 0x8f20
cmpl $0x0, %eax
je 0x27962
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x280c1
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x8c(%rbp)
movq -0x88(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x90(%rbp)
movq -0x88(%rbp), %rcx
movl $0x7270e00, %eax        # imm = 0x7270E00
xorl %edx, %edx
divl 0x38(%rcx)
movl %eax, -0x94(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movl 0xb270b(%rip), %ecx     # 0xda0d2
movl %ecx, (%rax)
movl $0x6, %edi
callq 0x282c0
movl %eax, %ecx
movq -0xa0(%rbp), %rax
movl %ecx, 0x4(%rax)
movl $0x1, %edi
callq 0x28300
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, 0x8(%rax)
movl -0x8c(%rbp), %eax
movzwl %ax, %edi
callq 0x28300
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, 0xa(%rax)
movl -0x90(%rbp), %eax
movzwl %ax, %edi
callq 0x28300
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, 0xc(%rax)
movl $0x388, -0xa4(%rbp)     # imm = 0x388
movl $0xe, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jge 0x280b8
movq -0x10(%rbp), %rax
movslq -0xa4(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0x10(%rbp), %rax
movslq -0xa8(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rax
movl 0x4(%rax), %eax
subl $0xc, %eax
movl %eax, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
movl %eax, -0xc8(%rbp)
cmpl $0x0, -0xac(%rbp)
jne 0x27ac0
movl -0xc8(%rbp), %eax
addl $0x7, %eax
movl %eax, -0xc8(%rbp)
movq -0xc0(%rbp), %rax
movl 0xb260a(%rip), %ecx     # 0xda0d7
movl %ecx, (%rax)
movl -0xc8(%rbp), %edi
callq 0x282c0
movl %eax, %ecx
movq -0xc0(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0xb8(%rbp), %rax
addq $0xc, %rax
movq %rax, -0xd0(%rbp)
movq -0xc0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xd8(%rbp)
cmpl $0x0, -0xac(%rbp)
jne 0x27be9
movq -0xd8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0xd8(%rbp)
movb $0x0, (%rax)
movq -0xd8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0xd8(%rbp)
movb $-0x1, (%rax)
movq -0xd8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0xd8(%rbp)
movb $0x51, (%rax)
movq -0xd8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0xd8(%rbp)
movb $0x3, (%rax)
movl -0x94(%rbp), %eax
sarl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0xd8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xd8(%rbp)
movb %cl, (%rax)
movl -0x94(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0xd8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xd8(%rbp)
movb %cl, (%rax)
movl -0x94(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0xd8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xd8(%rbp)
movb %cl, (%rax)
movb $0x0, -0xd9(%rbp)
movl $0x0, -0xe0(%rbp)
movl -0xe0(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jge 0x2807a
movl $0x0, -0xe8(%rbp)
movb $0x0, -0xed(%rbp)
movq -0xd0(%rbp), %rax
movl -0xe0(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0xe0(%rbp)
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0xe1(%rbp)
movzbl -0xe1(%rbp), %eax
andl $0x7f, %eax
movb %al, %cl
movb -0xed(%rbp), %al
movb %al, %dl
addb $0x1, %dl
movb %dl, -0xed(%rbp)
movzbl %al, %eax
movb %cl, -0xec(%rbp,%rax)
movzbl -0xe1(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x27c1d
jmp 0x27c82
movb -0xed(%rbp), %al
addb $-0x1, %al
movb %al, -0xed(%rbp)
movzbl %al, %eax
movb -0xec(%rbp,%rax), %al
movb %al, -0xe1(%rbp)
cmpb $0x0, -0xed(%rbp)
je 0x27cbb
movzbl -0xe1(%rbp), %eax
orl $0x80, %eax
movb %al, -0xe1(%rbp)
movb -0xe1(%rbp), %cl
movq -0xd8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xd8(%rbp)
movb %cl, (%rax)
movzbl -0xed(%rbp), %eax
cmpl $0x0, %eax
jg 0x27c82
movq -0xd0(%rbp), %rax
movslq -0xe0(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0xe1(%rbp)
movzbl -0xe1(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x2800b
movb -0xe1(%rbp), %cl
movq -0xd8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xd8(%rbp)
movb %cl, (%rax)
movl -0xe0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe0(%rbp)
movb -0xe1(%rbp), %al
movb %al, -0xd9(%rbp)
movzbl -0xd9(%rbp), %eax
andl $0x7f, %eax
sarl $0x4, %eax
cltq
movl -0x40(%rbp,%rax,4), %eax
movl %eax, -0xe8(%rbp)
movzbl -0xe1(%rbp), %eax
andl $0xf0, %eax
cmpl $0xf0, %eax
jne 0x27eab
movzbl -0xe1(%rbp), %eax
andl $0xf, %eax
movl %eax, -0xf4(%rbp)
je 0x27da0
jmp 0x27d8c
movl -0xf4(%rbp), %eax
subl $0xf, %eax
je 0x27e1b
jmp 0x27e90
jmp 0x27da2
movq -0xd0(%rbp), %rax
movl -0xe0(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0xe0(%rbp)
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movb %cl, -0xe1(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xd8(%rbp)
movb %cl, (%rax)
movzbl -0xe1(%rbp), %ecx
andl $0x80, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0xf5(%rbp)
jne 0x27e0c
movzbl -0xe1(%rbp), %eax
cmpl $0xf7, %eax
setne %al
movb %al, -0xf5(%rbp)
movb -0xf5(%rbp), %al
testb $0x1, %al
jne 0x27da2
jmp 0x27ea6
movq -0xd0(%rbp), %rax
movl -0xe0(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0xe0(%rbp)
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movq -0xd8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xd8(%rbp)
movb %cl, (%rax)
movq -0xd0(%rbp), %rax
movl -0xe0(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0xe0(%rbp)
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movq -0xd8(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0xd8(%rbp)
movb %al, (%rcx)
movzbl %al, %eax
movl %eax, -0xe8(%rbp)
jmp 0x27ea6
movzbl -0xe1(%rbp), %eax
andl $0xf, %eax
cltq
movl -0x80(%rbp,%rax,4), %eax
movl %eax, -0xe8(%rbp)
jmp 0x28009
movzbl -0xe1(%rbp), %eax
andl $0xf0, %eax
cmpl $0xb0, %eax
jne 0x28007
movq -0xd0(%rbp), %rax
movslq -0xe0(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x66, %eax
jl 0x28005
movq -0xd0(%rbp), %rax
movslq -0xe0(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x77, %eax
jg 0x28005
cmpl $0x1, -0xac(%rbp)
jne 0x27f68
movq -0xd0(%rbp), %rax
movslq -0xe0(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x6e, %eax
jne 0x27f68
movq -0xd8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0xd8(%rbp)
movb $0x76, (%rax)
movq -0xd0(%rbp), %rax
movl -0xe0(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
andl $0x7f, %eax
movb %al, %cl
movq -0xd8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xd8(%rbp)
movb %cl, (%rax)
jmp 0x27fec
cmpl $0x1, -0xac(%rbp)
jne 0x27fba
movq -0xd0(%rbp), %rax
movslq -0xe0(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x6f, %eax
jne 0x27fba
movq -0xd8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0xd8(%rbp)
movb $0x77, (%rax)
movq -0xd8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0xd8(%rbp)
movb $0x7f, (%rax)
jmp 0x27fea
movq -0xd8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0xd8(%rbp)
movb $0x66, (%rax)
movq -0xd8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0xd8(%rbp)
movb $0x0, (%rax)
jmp 0x27fec
movl -0xe0(%rbp), %eax
addl $0x2, %eax
movl %eax, -0xe0(%rbp)
movl $0x0, -0xe8(%rbp)
jmp 0x28007
jmp 0x28009
jmp 0x28024
movzbl -0xd9(%rbp), %eax
andl $0x7f, %eax
sarl $0x4, %eax
cltq
movl -0x40(%rbp,%rax,4), %eax
movl %eax, -0xe8(%rbp)
jmp 0x28026
cmpl $0x0, -0xe8(%rbp)
jle 0x28075
movq -0xd0(%rbp), %rax
movl -0xe0(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0xe0(%rbp)
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movq -0xd8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xd8(%rbp)
movb %cl, (%rax)
movl -0xe8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xe8(%rbp)
jmp 0x28026
jmp 0x27bfa
movl -0xc4(%rbp), %eax
addl $0xc, %eax
addl -0xa4(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xc8(%rbp), %eax
addl $0x8, %eax
addl -0xa8(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x27a4d
movl -0xa8(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax)