// File: /workspace/github_c_makefile_star2_O3/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, %r12
leaq 0x74842(%rip), %rdx     # 0x8ff39
leaq 0xb0(%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 0x34174
testl %eax, %eax
jns 0x1b752
leaq 0x74817(%rip), %rdx     # 0x8ff3d
leaq 0xb0(%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 0x34174
testl %eax, %eax
js 0x1bc59
movl %eax, %ebx
movl %eax, %edi
callq 0x346d9
movl %eax, %ebp
movslq %eax, %r14
movq %r14, %rdi
callq 0x8d50
testq %rax, %rax
je 0x1bc1c
movq %rax, %r13
movl %ebx, %edi
movq %rax, %rsi
movl %r14d, %edx
callq 0x343bc
movl %ebx, %edi
callq 0x34878
movq %r13, %rdi
movq %r14, %rsi
callq 0x2f0c8
movq %rax, 0x205aa6(%rip)    # 0x221240
testq %rax, %rax
je 0x1bc38
movq %rax, %rdi
movl $0x81, %esi
callq 0x3059e
movq 0x205a89(%rip), %rdi    # 0x221240
leaq 0x60(%rsp), %rcx
xorl %esi, %esi
xorl %edx, %edx
callq 0x30408
movq 0x205a74(%rip), %rdi    # 0x221240
leaq 0x30(%rsp), %rsi
leaq 0x28(%rsp), %rdx
xorl %ecx, %ecx
callq 0x3045c
movq 0x205a5c(%rip), %rdi    # 0x221240
leaq 0x39(%rsp), %rdx
leaq 0x69(%rsp), %rbx
leaq 0x70(%rsp), %r8
xorl %esi, %esi
movq %rbx, %rcx
callq 0x304a3
cmpb $0x8, (%rbx)
jne 0x1bc50
cmpb $0x3, 0x39(%rsp)
jae 0x1bc50
movq 0x30(%rsp), %rbx
movq 0x28(%rsp), %r14
movq %r14, %r15
imulq %rbx, %r15
leaq (%r15,%r15,2), %rdi
callq 0x8d50
testq %rax, %rax
je 0x1bcab
movq %r13, 0x10(%rsp)
movq %r12, 0x18(%rsp)
xorl %edi, %edi
leaq 0x5a5659(%rip), %r8     # 0x5c0ea0
leaq 0x596e12(%rip), %rcx    # 0x5b2660
leaq 0x52e90b(%rip), %rdx    # 0x54a160
movq %rax, %rsi
movq %rax, %r12
movw %r14w, 0x47fa(%r8,%rdi,2)
movw %bx, 0x47fa(%rcx,%rdi,2)
movq %rsi, 0x11fe8(%rdx,%rdi,8)
incq %rdi
addq %r15, %rsi
cmpq $0x3, %rdi
jne 0x1b85b
xorl %eax, %eax
movq %rax, 0x2059bf(%rip)    # 0x221248
movq %rax, 0x2059c0(%rip)    # 0x221250
movq 0x2059a9(%rip), %rdi    # 0x221240
callq 0x306a2
movl %eax, %ebp
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
leaq 0x7af(%rsp), %rax
leaq 0x2059ab(%rip), %r13    # 0x221260
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %r12, 0x48(%rsp)
cmpb $0x1, %bpl
je 0x1b8db
xorl %ebx, %ebx
cmpq %r15, %r14
jae 0x1bd3f
cmpb $0x2, %bpl
jne 0x1bd3f
leaq (%r15,%r15,2), %rsi
shlq $0x8, %rsi
addq %rax, %rsi
movq %r14, 0x50(%rsp)
movq %r15, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
movq %r15, %rax
subq %r14, %rax
cmpq $0x2, %rax
ja 0x1baa0
cmpb $0x1, %bpl
jne 0x1baa0
andq $-0x2, %rdx
shlq $0x7, %rdx
leaq (%rdx,%rdx,8), %rcx
movq %rsi, 0x58(%rsp)
movq %rsi, %r14
movq %r15, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rdx
subq %rcx, %r14
shrq %rdx
leaq (%rdx,%rdx,2), %rax
movq %r15, 0x20(%rsp)
subq %rax, %r15
imulq 0x30(%rsp), %r15
imulq 0x28(%rsp), %r15
addq %r12, %r15
movq 0x2058df(%rip), %rdi    # 0x221240
callq 0x3063c
movq 0x28(%rsp), %rdx
imulq 0x30(%rsp), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x80e0
movq 0x2058bd(%rip), %rdi    # 0x221240
callq 0x30620
xorl %ecx, %ecx
movb 0x2ff(%rax,%rcx), %dl
shrb $0x2, %dl
movb %dl, (%r14,%rcx)
decq %rcx
cmpq $-0x300, %rcx           # imm = 0xFD00
jne 0x1b98a
movq 0x205895(%rip), %rdi    # 0x221240
xorl %esi, %esi
callq 0x3067c
movq %rax, %rbx
movq 0x205884(%rip), %rdi    # 0x221240
xorl %esi, %esi
callq 0x30656
movzbl 0x39(%rsp), %r15d
cmpq $0x2, %r15
sete %cl
shrq %cl, %rbx
testq %rbx, %rbx
je 0x1ba6c
movq %rax, %rbp
movq 0x20586a(%rip), %r12    # 0x221250
leaq (%r12,%rbx), %rax
movl $0x8000, %r14d          # imm = 0x8000
subq %r12, %r14
cmpq $0x8001, %rax           # imm = 0x8001
cmovbq %rbx, %r14
cmpl $0x2, %r15d
jne 0x1ba38
leaq (%r12,%r12), %rcx
notq %rcx
addq %rbp, %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,%r13)
decq %rax
cmpq %rax, %r12
jne 0x1ba19
jmp 0x1ba47
leaq (%r12,%r13), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x80e0
movq %r14, %rax
imulq %r15, %rax
addq %rax, %rbp
addl %r14d, %r12d
andl $0x7fff, %r12d          # imm = 0x7FFF
subq %r14, %rbx
jne 0x1b9e6
movq %r12, 0x2057e9(%rip)    # 0x221250
movq 0x48(%rsp), %r12
movq 0x20(%rsp), %r15
incq %r15
movq 0x2057c5(%rip), %rdi    # 0x221240
callq 0x312a0
movl %eax, %ebp
movq 0x58(%rsp), %rsi
addq $0x300, %rsi            # imm = 0x300
cmpb $-0x1, %al
movq 0x50(%rsp), %r14
jne 0x1b8eb
jmp 0x1bc74
callq 0x82179
movl %eax, %eax
cvtsi2sd %rax, %xmm3
cmpl $0x0, 0xc(%rsp)
movsd 0x40(%rsp), %xmm2
jns 0x1bb00
movl 0x70(%rsp), %esi
leaq 0x2b9(%rip), %rdi       # 0x1bd7d
xorl %edx, %edx
movl $0x3f, %ecx
movl $0x3f, %r8d
movl $0x3f, %r9d
pushq $-0x1
pushq $0x1
movsd %xmm3, 0x30(%rsp)
callq 0x86f7c
movsd 0x30(%rsp), %xmm3
addq $0x10, %rsp
movapd %xmm3, %xmm2
movl %eax, 0xc(%rsp)
testl %eax, %eax
js 0x1bd58
movsd 0x60(%rsp), %xmm0
movapd %xmm3, %xmm1
subsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jb 0x1bb1d
addsd %xmm0, %xmm2
incq %r14
jmp 0x1bb06
movsd %xmm2, 0x40(%rsp)
movq %r14, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq %rdx
leaq (%rdx,%rdx,2), %rax
movq %r14, %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 0x46954
addl $0x23fd, %ebx           # imm = 0x23FD
movl %ebx, %edi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4ed07
xorl %edi, %edi
callq 0x46ba3
leaq 0x5a531c(%rip), %rax    # 0x5c0ea0
movswl (%rax,%rbx,2), %ecx
movl $0xc80000, %eax         # imm = 0xC80000
xorl %edx, %edx
idivl %ecx
leaq 0x59d2d4(%rip), %rcx    # 0x5b8e6c
movl (%rcx), %r10d
decl %r10d
leaq 0x59d2cb(%rip), %rcx    # 0x5b8e70
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 0x4dcbd
addq $0x30, %rsp
callq 0x46cdb
callq 0x83076
leaq 0x313539(%rip), %rcx    # 0x32f120
movb 0x1c(%rcx), %al
orb 0x1(%rcx), %al
orb 0x39(%rcx), %al
orb 0x9c(%rcx), %al
jne 0x1bd22
movl 0xc(%rsp), %edi
callq 0x86f15
testl %eax, %eax
leaq 0x7af(%rsp), %rax
jne 0x1b8c0
xorl %ebx, %ebx
jmp 0x1bd3f
movl %ebx, %edi
callq 0x34878
leaq 0x74333(%rip), %rdi     # 0x8ff5d
movq %r12, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x81da7
jmp 0x1bc6a
movq %r13, %rdi
callq 0x8270
leaq 0x74342(%rip), %rdi     # 0x8ff89
xorl %eax, %eax
callq 0x81da7
jmp 0x1bc6a
leaq 0x74353(%rip), %rdi     # 0x8ffaa
jmp 0x1bcb2
leaq 0x742e4(%rip), %rdi     # 0x8ff44
movq %r12, %rsi
xorl %eax, %eax
callq 0x81da7
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x1bd0e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r13
leaq 0x743ac(%rip), %rdi     # 0x90036
movq %r15, %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq 0x81da7
movq %r12, %r15
movl 0xc(%rsp), %edi
testl %edi, %edi
js 0x1bcc4
callq 0x86f1f
jmp 0x1bcc4
leaq 0x74329(%rip), %rdi     # 0x8ffdb
xorl %r15d, %r15d
movq %r12, %rsi
xorl %eax, %eax
callq 0x81da7
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x205575(%rip), %rdi    # 0x221240
testq %rdi, %rdi
je 0x1bcd5
callq 0x301d3
xorl %r14d, %r14d
movq %r14, 0x205561(%rip)    # 0x221240
movq %r13, %rdi
callq 0x8270
leaq 0x52e472(%rip), %rax    # 0x54a160
movq %r14, 0x11ff8(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x11fe8(%rax)
testq %r15, %r15
je 0x1bd0e
movq %r15, %rdi
callq 0x8270
movl %ebx, %eax
addq $0xdb8, %rsp            # imm = 0xDB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
leaq 0x3133f5(%rip), %rcx    # 0x32f120
movb %al, 0x9c(%rcx)
movb %al, 0x39(%rcx)
movb %al, 0x1c(%rcx)
movb %al, 0x1(%rcx)
movl $0x1, %ebx
cmpb $-0x1, %bpl
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r13
jne 0x1bc99
jmp 0x1bc83
movq %r12, %r15
leaq 0x742a3(%rip), %rdi     # 0x90005
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x81da7
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x10(%rsp), %r13
jmp 0x1bcc4