// File: /workspace/github_c_makefile_star2_O2/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 $0xda8, %rsp            # imm = 0xDA8
movq %rdi, %r12
leaq 0x67fba(%rip), %rdx     # 0x81ecd
leaq 0xa0(%rsp), %rbx
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x8710
movq %rbx, %rdi
xorl %esi, %esi
callq 0x302de
testl %eax, %eax
jns 0x19f6e
leaq 0x67f8f(%rip), %rdx     # 0x81ed1
leaq 0xa0(%rsp), %rbx
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x8710
movq %rbx, %rdi
xorl %esi, %esi
callq 0x302de
testl %eax, %eax
js 0x1a0d1
movl %eax, %ebx
movl %eax, %edi
callq 0x307f4
movl %eax, %ebp
movslq %eax, %r14
movq %r14, %rdi
callq 0x8d50
testq %rax, %rax
je 0x1a091
movq %rax, %r13
movl %ebx, %edi
movq %rax, %rsi
movl %r14d, %edx
callq 0x304e7
movl %ebx, %edi
callq 0x30983
movq %r13, %rdi
movq %r14, %rsi
callq 0x2b1ac
movq %rax, 0x1fd28a(%rip)    # 0x217240
testq %rax, %rax
je 0x1a0ad
movq %rax, %rdi
movl $0x81, %esi
callq 0x2c820
movq 0x1fd26d(%rip), %rdi    # 0x217240
leaq 0x50(%rsp), %rcx
xorl %esi, %esi
xorl %edx, %edx
callq 0x2c640
movq 0x1fd258(%rip), %rdi    # 0x217240
leaq 0x20(%rsp), %rsi
leaq 0x18(%rsp), %rdx
xorl %ecx, %ecx
callq 0x2c6b1
movq 0x1fd240(%rip), %rdi    # 0x217240
leaq 0x29(%rsp), %rdx
leaq 0x59(%rsp), %rbx
leaq 0x60(%rsp), %r8
xorl %esi, %esi
movq %rbx, %rcx
callq 0x2c715
cmpb $0x8, (%rbx)
jne 0x1a0c5
cmpb $0x3, 0x29(%rsp)
jae 0x1a0c5
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r14
movq %r14, %r15
imulq %rbx, %r15
leaq (%r15,%r15,2), %rdi
callq 0x8d50
testq %rax, %rax
je 0x1a48a
xorl %edi, %edi
leaq 0x59ce47(%rip), %r8     # 0x5b6ea0
leaq 0x58e600(%rip), %rcx    # 0x5a8660
leaq 0x5260f9(%rip), %rdx    # 0x540160
movq %rax, %rsi
cmpq $0x3, %rdi
je 0x1a0ea
movw %r14w, 0x47fa(%r8,%rdi,2)
movw %bx, 0x47fa(%rcx,%rdi,2)
movq %rsi, 0x11fe8(%rdx,%rdi,8)
incq %rdi
addq %r15, %rsi
jmp 0x1a06a
movl %ebx, %edi
callq 0x30983
leaq 0x67e52(%rip), %rdi     # 0x81ef1
movq %r12, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x74e7b
jmp 0x1a0e2
movq %r13, %rdi
callq 0x8270
leaq 0x67e61(%rip), %rdi     # 0x81f1d
xorl %eax, %eax
callq 0x74e7b
jmp 0x1a0e2
leaq 0x67e72(%rip), %rdi     # 0x81f3e
jmp 0x1a491
leaq 0x67e00(%rip), %rdi     # 0x81ed8
movq %r12, %rsi
xorl %eax, %eax
callq 0x74e7b
pushq $-0x1
popq %rbx
jmp 0x1a4e7
movq %rax, 0x8(%rsp)
movq %r13, 0x38(%rsp)
movq %r12, 0x30(%rsp)
andq $0x0, 0x1fd147(%rip)    # 0x217248
andq $0x0, 0x1fd147(%rip)    # 0x217250
movq 0x1fd130(%rip), %rdi    # 0x217240
callq 0x2c994
movl %eax, %r13d
xorl %r14d, %r14d
leaq 0x1fd13e(%rip), %rbp    # 0x217260
xorl %r15d, %r15d
pushq $-0x1
popq %rax
movl %eax, 0x4(%rsp)
cmpb $0x1, %r13b
je 0x1a147
xorl %ebx, %ebx
cmpq %r15, %r14
jae 0x1a516
cmpb $0x2, %r13b
jne 0x1a516
movq %r14, 0x48(%rsp)
movq %r15, %rax
subq %r14, %rax
cmpq $0x2, %rax
ja 0x1a2d9
cmpb $0x1, %r13b
jne 0x1a2d9
movq %r15, %rax
xorl %edx, %edx
pushq $0x3
popq %rcx
divq %rcx
movq %rdx, %rbx
movq 0x20(%rsp), %r14
imulq %rdx, %r14
imulq 0x18(%rsp), %r14
addq 0x8(%rsp), %r14
movq 0x1fd0b1(%rip), %rdi    # 0x217240
callq 0x2c901
movq 0x18(%rsp), %rdx
imulq 0x20(%rsp), %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x80e0
movq 0x1fd08f(%rip), %rdi    # 0x217240
callq 0x2c8d4
movl $0x2ff, %ecx            # imm = 0x2FF
imulq $0x300, %rbx, %rdx      # imm = 0x300
addq %rsp, %rdx
addq $0x4a0, %rdx            # imm = 0x4A0
testl %ecx, %ecx
js 0x1a1e2
movl %ecx, %esi
movb (%rax,%rsi), %dil
shrb $0x2, %dil
movb %dil, (%rsi,%rdx)
decl %ecx
jmp 0x1a1cc
movq %r15, 0x10(%rsp)
movq 0x1fd052(%rip), %rdi    # 0x217240
xorl %esi, %esi
callq 0x2c960
movq %rax, %rbx
movq 0x1fd041(%rip), %rdi    # 0x217240
xorl %esi, %esi
callq 0x2c92c
movq %rax, %r13
movzbl 0x29(%rsp), %r12d
cmpq $0x2, %r12
sete %cl
shrq %cl, %rbx
movq 0x1fd030(%rip), %r14    # 0x217250
testq %rbx, %rbx
je 0x1a2b0
leaq (%r14,%rbx), %rax
movl $0x8000, %r15d          # imm = 0x8000
subq %r14, %r15
cmpq $0x8001, %rax           # imm = 0x8001
cmovbq %rbx, %r15
cmpl $0x2, %r12d
jne 0x1a27b
leaq (%r14,%r14), %rcx
notq %rcx
addq %r13, %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, %r14
jne 0x1a25c
jmp 0x1a28a
leaq (%r14,%rbp), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x80e0
movq %r15, %rax
imulq %r12, %rax
addq %rax, %r13
subq %r15, %rbx
addl %r14d, %r15d
andl $0x7fff, %r15d          # imm = 0x7FFF
movq %r15, 0x1fcfa8(%rip)    # 0x217250
movq %r15, %r14
jmp 0x1a220
movq 0x10(%rsp), %r15
incq %r15
movq 0x1fcf81(%rip), %rdi    # 0x217240
callq 0x2d5d4
movl %eax, %r13d
cmpb $-0x1, %al
movq 0x48(%rsp), %r14
jne 0x1a14c
jmp 0x1a453
callq 0x75251
movl %eax, %eax
cvtsi2sd %rax, %xmm3
cmpl $0x0, 0x4(%rsp)
movsd 0x40(%rsp), %xmm2
jns 0x1a331
movl 0x60(%rsp), %esi
leaq 0x256(%rip), %rdi       # 0x1a553
xorl %edx, %edx
pushq $0x3f
popq %rcx
movl %ecx, %r8d
movl %ecx, %r9d
pushq $-0x1
pushq $0x1
movsd %xmm3, 0x20(%rsp)
callq 0x79f7a
movsd 0x20(%rsp), %xmm3
addq $0x10, %rsp
movapd %xmm3, %xmm2
movl %eax, 0x4(%rsp)
testl %eax, %eax
js 0x1a52e
movsd 0x50(%rsp), %xmm0
imulq $0x300, %r14, %rax      # imm = 0x300
leaq (%rsp,%rax), %rsi
addq $0x4a0, %rsi            # imm = 0x4A0
movapd %xmm3, %xmm1
subsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jb 0x1a367
addsd %xmm0, %xmm2
incq %r14
addq $0x300, %rsi            # imm = 0x300
jmp 0x1a349
movsd %xmm2, 0x40(%rsp)
movq %r14, %rax
xorl %edx, %edx
pushq $0x3
popq %rcx
divq %rcx
movq %rax, %rbx
imulq $-0x900, %rax, %rax     # imm = 0xF700
addq %rax, %rsi
pushq $-0x1
popq %rdi
pushq $0x6
popq %rdx
callq 0x41f79
imull $-0x3, %ebx, %eax
movq %r14, %r12
addl %eax, %r14d
addl $0x23fd, %r14d          # imm = 0x23FD
movswl %r14w, %ebx
movl %ebx, %edi
xorl %esi, %esi
pushq $-0x1
popq %rdx
callq 0x49c10
xorl %edi, %edi
callq 0x421ca
leaq 0x59cae2(%rip), %rax    # 0x5b6ea0
movswl (%rax,%r14,2), %ecx
movl $0xc80000, %eax         # imm = 0xC80000
xorl %edx, %edx
idivl %ecx
leaq 0x594a99(%rip), %rcx    # 0x5aee6c
movl (%rcx), %r10d
decl %r10d
leaq 0x594a90(%rip), %rcx    # 0x5aee70
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
pushq $0x0
pushq $0x0
pushq $0x46
pushq $0x0
callq 0x48ebe
addq $0x30, %rsp
callq 0x422ff
callq 0x75fe9
leaq 0x30acfd(%rip), %rcx    # 0x325120
movb 0x1c(%rcx), %al
orb 0x1(%rcx), %al
orb 0x39(%rcx), %al
orb 0x9c(%rcx), %al
jne 0x1a4fb
movl 0x4(%rsp), %edi
callq 0x79f19
testl %eax, %eax
movq %r12, %r14
jne 0x1a12c
xorl %ebx, %ebx
jmp 0x1a516
pushq $-0x1
popq %rbx
movq 0x8(%rsp), %r14
movl 0x4(%rsp), %ebp
leaq 0x67b64(%rip), %rdi     # 0x81fca
pushq $-0x1
popq %rcx
movq 0x30(%rsp), %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x74e7b
testl %ebp, %ebp
movq 0x38(%rsp), %r13
js 0x1a4a1
movl %ebp, %edi
callq 0x79f23
jmp 0x1a4a1
leaq 0x67ade(%rip), %rdi     # 0x81f6f
xorl %r14d, %r14d
movq %r12, %rsi
xorl %eax, %eax
callq 0x74e7b
pushq $-0x1
popq %rbx
movq 0x1fcd98(%rip), %rdi    # 0x217240
testq %rdi, %rdi
je 0x1a4b2
callq 0x2c41d
andq $0x0, 0x1fcd86(%rip)    # 0x217240
movq %r13, %rdi
callq 0x8270
xorl %eax, %eax
leaq 0x525c95(%rip), %rcx    # 0x540160
cmpq $0x3, %rax
je 0x1a4df
andq $0x0, 0x11fe8(%rcx,%rax,8)
incq %rax
jmp 0x1a4cb
movq %r14, %rdi
callq 0x8270
movl %ebx, %eax
addq $0xda8, %rsp            # imm = 0xDA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
leaq 0x30ac1c(%rip), %rcx    # 0x325120
movb %al, 0x9c(%rcx)
movb %al, 0x39(%rcx)
movb %al, 0x1c(%rcx)
movb %al, 0x1(%rcx)
pushq $0x1
popq %rbx
cmpb $-0x1, %r13b
movq 0x8(%rsp), %r14
movl 0x4(%rsp), %ebp
jne 0x1a478
jmp 0x1a45f
leaq 0x67a64(%rip), %rdi     # 0x81f99
movq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0x74e7b
pushq $-0x1
popq %rbx
movq 0x38(%rsp), %r13
movq 0x8(%rsp), %r14
jmp 0x1a4a1