// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/src/teksmk.c
// Function: smkplayseq
int
smkplayseq(char *name)
{
     char path[BMAX_PATH], result = SMK_DONE;
     int fh, flen, i, ret = 0;

     unsigned char smkpal[NUMSMKTILES][768], smkchannels[7], smkbitdepth[7], *smkbuf = NULL;
     unsigned char *smkframes = NULL;
     unsigned long smkx, smky, smkaudrate[7];

     unsigned long playframe, decodeframe;
     double usf, clocktime, nowtime;
     int voice = -1, frame;

     snprintf(path, sizeof(path), "smk/%s.smk", name);
     if ((fh = kopen4load(path, 0)) < 0) {
          snprintf(path, sizeof(path), "%s.smk", name);
          fh = kopen4load(path, 0);
     }
     if (fh < 0) {
          debugprintf("smkplayseq(\"%s\") failed\n", name);
          return -1;
     }

     flen = kfilelength(fh);
     smkbuf = (unsigned char *)malloc(flen);
     if (smkbuf == NULL) {
          kclose(fh);
          debugprintf("smkplayseq(\"%s\") malloc of %d bytes failed\n", name, flen);
          return -1;
     }

     kread(fh, smkbuf, flen);
     kclose(fh);

     smkseq = smk_open_memory(smkbuf, flen);
     if (!smkseq) {
          free(smkbuf);
          debugprintf("smk_open_memory() returned null\n");
          return -1;
     }

     smk_enable_all(smkseq, SMK_VIDEO_TRACK | SMK_AUDIO_TRACK_0);
     smk_info_all(smkseq, NULL, NULL, &usf);
     smk_info_video(smkseq, &smkx, &smky, NULL);
     smk_info_audio(smkseq, NULL, smkchannels, smkbitdepth, smkaudrate);
     if (smkbitdepth[0] != 8 || smkchannels[0] > 2) {
          debugprintf("smkplayseq(\"%s\") is not 8-bit mono/stereo audio\n", name);
          ret = -1;
          goto end;
     }

     // Smacker frames are decoded linewise, but BUILD expects its
     // tiles columnwise, so we will treat the tile as though it's
     // rotated 90 degrees and flipped horizontally.

     smkframes = malloc(NUMSMKTILES * smkx*smky);
     if (!smkframes) {
          debugprintf("smkplayseq(\"%s\") malloc of frames failed\n", name);
          ret = -1;
          goto end;
     }
     for (i = 0; i < NUMSMKTILES; i++) {
          tilesizx[SMKPICNUM0+i] = smky;
          tilesizy[SMKPICNUM0+i] = smkx;
          waloff[SMKPICNUM0+i] = (intptr_t)&smkframes[i*smkx*smky];
     }

     smkaudiowrite = smkaudioread = 0;
     decodeframe = playframe = 0;
     result = smk_first(smkseq);

     while (result == SMK_MORE || (playframe < decodeframe && result == SMK_LAST)) {
          while (decodeframe - playframe < NUMSMKTILES && result == SMK_MORE) {
               unsigned long audiolen, slice, downsamp;
               const unsigned char *audiodata, *palptr;

               frame = decodeframe % NUMSMKTILES;
               memcpy(&smkframes[frame*smkx*smky], smk_get_video(smkseq), smkx*smky);
               for (palptr = smk_get_palette(smkseq), i=256*3-1; i>=0; i--)
                   smkpal[frame][i] = palptr[i] >> 2;

               audiolen = smk_get_audio_size(smkseq, 0);
               audiodata = smk_get_audio(smkseq, 0);
               if (smkchannels[0] == 2) audiolen >>= 1;     // Stereo is downmixed for JFAudiolib.
               while (audiolen > 0) {
                    if (smkaudiowrite + audiolen > SMKAUDIOBUFSZ) {
                         slice = SMKAUDIOBUFSZ - smkaudiowrite;
                    } else {
                         slice = audiolen;
                    }
                    if (smkchannels[0] == 2) {
                         downsamp = slice - 1;
                         do { smkaudiobuf[smkaudiowrite+downsamp] =
                              (audiodata[(downsamp<<1)+0] >> 1) +
                              (audiodata[(downsamp<<1)+1] >> 1);
                         } while (downsamp--);
                    } else {
                         memcpy(&smkaudiobuf[smkaudiowrite], audiodata, slice);
                    }
                    audiodata += slice * smkchannels[0];
                    audiolen -= slice;
                    smkaudiowrite = (smkaudiowrite + slice) % SMKAUDIOBUFSZ;
               }

               decodeframe++;
               result = smk_next(smkseq);
               if (result == SMK_ERROR) {
                    ret = -1;
                    goto end;
               }
          }

          nowtime = (double)getusecticks();

          if (voice < 0) {
               voice = FX_StartDemandFeedPlayback(smkplayfeeder, (int)smkaudrate[0],
                    0, 63, 63, 63, 1, (unsigned int)-1);
               if (voice < 0) {
                    debugprintf("smkplayseq(\"%s\") failed to start audio playback\n", name);
                    ret = -1;
                    goto end;
               }

               clocktime = nowtime;
          }

          while (nowtime - clocktime >= usf) clocktime += usf, playframe++;
          frame = playframe % NUMSMKTILES;

          setbrightness(-1, smkpal[frame], 2|4);
#if USE_POLYMOST && USE_OPENGL
          invalidatetile(SMKPICNUM0+frame, 0, -1);
#endif
          clearallviews(0);
          rotatesprite(160<<16, 100<<16, divscale16(200, tilesizx[SMKPICNUM0+frame]),
                      512, SMKPICNUM0+frame, 0, 0, 2+4+64, 0, 0, xdim-1, ydim-1);
          nextpage();

          handleevents();
          if (keystatus[0x1]|keystatus[0x1c]|keystatus[0x39]|keystatus[0x9c]) {
               keystatus[0x1]=keystatus[0x1c]=keystatus[0x39]=keystatus[0x9c]=0;
               ret = 1;
               break;
          }
          if (!FX_SoundActive(voice)) { // If the audio stops for some reason.
               break;
          }
     }

end:
     if (result == SMK_ERROR) debugprintf("smkplayseq(\"%s\") error at decodeframe %lud (%d)\n", name, decodeframe, result);
     if (voice >= 0) FX_StopSound(voice);
     if (smkseq) smk_close(smkseq);
     smkseq = NULL;
     if (smkbuf) free(smkbuf);

     for (i = 0; i < NUMSMKTILES; i++) waloff[SMKPICNUM0+i] = 0;
     if (smkframes) free(smkframes);

     return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xdb8, %rsp            # imm = 0xDB8
movq %rdi, %r14
leaq 0x71936(%rip), %rdx     # 0x8ce59
leaq 0xb0(%rsp), %rbx
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x8710
movq %rbx, %rdi
xorl %esi, %esi
callq 0x32dbc
movl %eax, %ebx
testl %eax, %eax
jns 0x1b57a
leaq 0x71909(%rip), %rdx     # 0x8ce5d
leaq 0xb0(%rsp), %rbx
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x8710
movq %rbx, %rdi
xorl %esi, %esi
callq 0x32dbc
movl %eax, %ebx
testl %ebx, %ebx
js 0x1ba9d
movq %r14, 0x8(%rsp)
movl %ebx, %edi
callq 0x3333e
movl %eax, %ebp
movslq %eax, %r14
movq %r14, %rdi
callq 0x8d50
testq %rax, %rax
je 0x1bab0
movq %rax, %r15
movl %ebx, %edi
movq %rax, %rsi
movl %r14d, %edx
callq 0x3302e
movl %ebx, %edi
callq 0x334dd
movq %r15, %rdi
movq %r14, %rsi
callq 0x2dda8
movq %rax, 0x202c53(%rip)    # 0x21e220
testq %rax, %rax
je 0x1bace
movq %r15, 0x50(%rsp)
movq %rax, %rdi
movl $0x81, %esi
callq 0x2f2be
movq 0x202c31(%rip), %rdi    # 0x21e220
leaq 0x60(%rsp), %rcx
xorl %esi, %esi
xorl %edx, %edx
callq 0x2f128
movq 0x202c1c(%rip), %rdi    # 0x21e220
leaq 0x28(%rsp), %rsi
leaq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0x2f17c
movq 0x202c04(%rip), %rdi    # 0x21e220
leaq 0x39(%rsp), %rdx
leaq 0x69(%rsp), %rbx
leaq 0x70(%rsp), %r8
xorl %esi, %esi
movq %rbx, %rcx
callq 0x2f1c3
cmpb $0x8, (%rbx)
jne 0x1bafd
cmpb $0x3, 0x39(%rsp)
jae 0x1bafd
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r14
movq %r14, %r15
imulq %rbx, %r15
leaq (%r15,%r15,2), %rdi
callq 0x8d50
testq %rax, %rax
movq 0x8(%rsp), %r12
je 0x1bb1c
movq %rax, %rsi
xorl %eax, %eax
leaq 0x5a2803(%rip), %rdi    # 0x5bde80
leaq 0x593fbc(%rip), %rcx    # 0x5af640
leaq 0x52bab5(%rip), %rdx    # 0x547140
movq %rsi, 0x40(%rsp)
movw %r14w, 0x47fa(%rdi,%rax,2)
movw %bx, 0x47fa(%rcx,%rax,2)
movq %rsi, 0x11fe8(%rdx,%rax,8)
incq %rax
addq %r15, %rsi
cmpq $0x3, %rax
jne 0x1b690
xorl %eax, %eax
movq %rax, 0x202b6a(%rip)    # 0x21e228
movq %rax, 0x202b6b(%rip)    # 0x21e230
movq 0x202b54(%rip), %rdi    # 0x21e220
callq 0x2f3c2
movl %eax, %r14d
leaq 0x7af(%rsp), %rax
movabsq $-0x5555555555555555, %rbx # imm = 0xAAAAAAAAAAAAAAAB
leaq 0x202b53(%rip), %rbp    # 0x21e240
xorl %r12d, %r12d
xorl %ecx, %ecx
xorl %r13d, %r13d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq 0x40(%rsp), %r15
cmpb $0x1, %r14b
je 0x1b718
cmpq %r13, %rcx
jae 0x1ba90
cmpb $0x2, %r14b
jne 0x1ba90
movl %edi, 0x4(%rsp)
leaq (,%r13,2), %rsi
addq %r13, %rsi
shlq $0x8, %rsi
addq %rax, %rsi
movq %rcx, 0x10(%rsp)
movq %r13, %rax
mulq %rbx
movq %r13, %rax
subq %rcx, %rax
cmpq $0x2, %rax
ja 0x1b8f2
cmpb $0x1, %r14b
jne 0x1b8f2
andq $-0x2, %rdx
shlq $0x7, %rdx
leaq (%rdx,%rdx,8), %rcx
movq %rsi, 0x58(%rsp)
movq %rsi, %r14
movq %r13, %rax
mulq %rbx
subq %rcx, %r14
shrq %rdx
leaq (%rdx,%rdx,2), %rax
movq %r13, 0x18(%rsp)
subq %rax, %r13
imulq 0x28(%rsp), %r13
imulq 0x20(%rsp), %r13
addq %r15, %r13
movq 0x202a8b(%rip), %rdi    # 0x21e220
callq 0x2f35c
movq 0x20(%rsp), %rdx
imulq 0x28(%rsp), %rdx
movq %r13, %rdi
movq %rax, %rsi
callq 0x80e0
movq 0x202a69(%rip), %rdi    # 0x21e220
callq 0x2f340
xorl %ecx, %ecx
movb 0x2ff(%rax,%rcx), %dl
shrb $0x2, %dl
movb %dl, (%r14,%rcx)
decq %rcx
cmpq $-0x300, %rcx           # imm = 0xFD00
jne 0x1b7be
movq 0x202a41(%rip), %rdi    # 0x21e220
xorl %esi, %esi
callq 0x2f39c
movq %rax, %r14
movq 0x202a30(%rip), %rdi    # 0x21e220
xorl %esi, %esi
callq 0x2f376
movzbl 0x39(%rsp), %r13d
cmpq $0x2, %r13
sete %cl
shrq %cl, %r14
testq %r14, %r14
je 0x1b8aa
movq %rax, %rbx
movl %r12d, 0x34(%rsp)
movq 0x202a11(%rip), %r12    # 0x21e230
leaq (%r12,%r14), %rax
movl $0x8000, %r15d          # imm = 0x8000
subq %r12, %r15
cmpq $0x8001, %rax           # imm = 0x8001
cmovbq %r14, %r15
cmpl $0x2, %r13d
jne 0x1b871
leaq (%r12,%r12), %rcx
notq %rcx
addq %rbx, %rcx
movl $0x8000, %edx           # imm = 0x8000
cmpq %rdx, %rax
cmovaeq %rdx, %rax
movb -0x1(%rcx,%rax,2), %dl
movb (%rcx,%rax,2), %sil
shrb %dl
shrb %sil
addb %dl, %sil
movb %sil, -0x1(%rax,%rbp)
decq %rax
cmpq %rax, %r12
jne 0x1b852
jmp 0x1b880
leaq (%r12,%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x80e0
movq %r15, %rax
imulq %r13, %rax
addq %rax, %rbx
addl %r15d, %r12d
andl $0x7fff, %r12d          # imm = 0x7FFF
subq %r15, %r14
jne 0x1b81f
movq %r12, 0x202990(%rip)    # 0x21e230
movq 0x40(%rsp), %r15
movl 0x34(%rsp), %r12d
movq 0x18(%rsp), %r13
incq %r13
movq 0x202967(%rip), %rdi    # 0x21e220
callq 0x2ff5e
movl %eax, %r14d
movq 0x58(%rsp), %rsi
addq $0x300, %rsi            # imm = 0x300
cmpb $-0x1, %al
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovel %eax, %r12d
movabsq $-0x5555555555555555, %rbx # imm = 0xAAAAAAAAAAAAAAAB
movq 0x10(%rsp), %rcx
jne 0x1b733
jmp 0x1bb0e
callq 0x7f577
movl %eax, %eax
cvtsi2sd %rax, %xmm3
movl 0x4(%rsp), %eax
testl %eax, %eax
movsd 0x48(%rsp), %xmm2
jns 0x1b94f
movsd %xmm3, 0x18(%rsp)
movl 0x70(%rsp), %esi
leaq 0x2e9(%rip), %rdi       # 0x1bc06
xorl %edx, %edx
movl $0x3f, %ecx
movl $0x3f, %r8d
movl $0x3f, %r9d
pushq $-0x1
pushq $0x1
callq 0x843e7
addq $0x10, %rsp
testl %eax, %eax
js 0x1bbe2
movsd 0x18(%rsp), %xmm3
movapd %xmm3, %xmm2
movsd 0x60(%rsp), %xmm0
movapd %xmm3, %xmm1
subsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
movl %eax, 0x4(%rsp)
jae 0x1b96e
movq 0x10(%rsp), %rcx
jmp 0x1b988
movq 0x10(%rsp), %rcx
addsd %xmm0, %xmm2
incq %rcx
movapd %xmm3, %xmm1
subsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jae 0x1b973
movsd %xmm2, 0x48(%rsp)
movq %rcx, %rax
mulq %rbx
shrq %rdx
leaq (%rdx,%rdx,2), %rax
movq %rcx, 0x10(%rsp)
movq %rcx, %rbx
subq %rax, %rbx
leaq (%rbx,%rbx,2), %rax
shlq $0x8, %rax
leaq (%rsp,%rax), %rsi
addq $0x4b0, %rsi            # imm = 0x4B0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0x6, %edx
callq 0x44ae3
addl $0x23fd, %ebx           # imm = 0x23FD
movl %ebx, %edi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4cb39
xorl %edi, %edi
callq 0x44d3d
leaq 0x5a2496(%rip), %rax    # 0x5bde80
movswq (%rax,%rbx,2), %rcx
movl $0xc80000, %eax         # imm = 0xC80000
xorl %edx, %edx
idivq %rcx
leaq 0x59a44c(%rip), %rcx    # 0x5b5e4c
movl (%rcx), %r10d
decl %r10d
leaq 0x59a443(%rip), %rcx    # 0x5b5e50
movl (%rcx), %r11d
decl %r11d
movl $0xa00000, %edi         # imm = 0xA00000
movl $0x640000, %esi         # imm = 0x640000
movl %eax, %edx
movl $0x200, %ecx            # imm = 0x200
movl %ebx, %r8d
xorl %r9d, %r9d
pushq %r11
pushq %r10
xorl %eax, %eax
pushq %rax
pushq %rax
pushq $0x46
pushq %rax
callq 0x4bca3
addq $0x30, %rsp
callq 0x44e75
callq 0x803d6
leaq 0x3106b1(%rip), %rcx    # 0x32c100
movb 0x1c(%rcx), %al
orb 0x1(%rcx), %al
orb 0x39(%rcx), %al
orb 0x9c(%rcx), %al
jne 0x1bbc0
movl 0x4(%rsp), %ebx
movl %ebx, %edi
callq 0x84380
movl %ebx, %edi
testl %eax, %eax
leaq 0x7af(%rsp), %rax
movabsq $-0x5555555555555555, %rbx # imm = 0xAAAAAAAAAAAAAAAB
movq 0x10(%rsp), %rcx
jne 0x1b6ff
movl %r12d, %ebp
movq 0x8(%rsp), %r12
jmp 0x1bb3d
leaq 0x713c0(%rip), %rdi     # 0x8ce64
movq %r14, %rsi
xorl %eax, %eax
callq 0x7f197
jmp 0x1bae4
movl %ebx, %edi
callq 0x334dd
leaq 0x713bf(%rip), %rdi     # 0x8ce7d
movq 0x8(%rsp), %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x7f197
jmp 0x1bae4
movq %r15, %rdi
callq 0x8270
leaq 0x713cc(%rip), %rdi     # 0x8cea9
xorl %eax, %eax
callq 0x7f197
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0xdb8, %rsp            # imm = 0xDB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x713c6(%rip), %rdi     # 0x8ceca
xorl %r15d, %r15d
movq 0x8(%rsp), %r12
jmp 0x1bb26
movl %r12d, %ebp
movq 0x8(%rsp), %r12
movl 0x4(%rsp), %edi
jmp 0x1bb3d
leaq 0x713d8(%rip), %rdi     # 0x8cefb
xorl %r15d, %r15d
movq %r12, %rsi
xorl %eax, %eax
callq 0x7f197
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
cmpb $-0x1, %r14b
jne 0x1bb60
movl %edi, %ebx
leaq 0x7140a(%rip), %rdi     # 0x8cf56
movq %r12, %rsi
movq %r13, %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq 0x7f197
movl %ebx, %edi
testl %edi, %edi
js 0x1bb69
callq 0x8438a
movq 0x2026b0(%rip), %rdi    # 0x21e220
testq %rdi, %rdi
movq 0x50(%rsp), %r14
je 0x1bb7f
callq 0x2eeeb
xorl %ebx, %ebx
movq %rbx, 0x202698(%rip)    # 0x21e220
movq %r14, %rdi
callq 0x8270
leaq 0x52b5a9(%rip), %rax    # 0x547140
movq %rbx, 0x11ff8(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x11fe8(%rax)
testq %r15, %r15
je 0x1bae9
movq %r15, %rdi
callq 0x8270
jmp 0x1bae9
xorl %eax, %eax
leaq 0x310537(%rip), %rcx    # 0x32c100
movb %al, 0x9c(%rcx)
movb %al, 0x39(%rcx)
movb %al, 0x1c(%rcx)
movb %al, 0x1(%rcx)
movl $0x1, %ebp
jmp 0x1bb11
movl %eax, %ebx
leaq 0x7133a(%rip), %rdi     # 0x8cf25
movq 0x8(%rsp), %r12
movq %r12, %rsi
xorl %eax, %eax
callq 0x7f197
movl %ebx, %edi
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x1bb3d