// File: /workspace/github_c_makefile_star2_O2/jonof[P]jfaudiolib/src/test.c
// Function: playsong
void playsong(const char * song, int loopstart, int loopend)
{
    int status;
    int length;
    char * data;
    FILE * fp;

    fp = fopen(song, "rb");
    if (!fp) {
        fprintf(stderr, "Error opening %s\n", song);
        return;
    }

    fseek(fp, 0, SEEK_END);
    length = (int)ftell(fp);
    if (length <= 0) {
        fclose(fp);
        return;
    }

    data = (char *) malloc(length);
    if (!data) {
        fclose(fp);
        return;
    }

    fseek(fp, 0, SEEK_SET);

    if (fread(data, length, 1, fp) != 1) {
        fclose(fp);
        free(data);
        return;
    }

    fclose(fp);

    printf("Playing %s\n", song);

    if (memcmp(data, "MThd", 4) == 0) {
        status = MUSIC_PlaySong(data, length, 0);
        if (status != MUSIC_Ok) {
            fprintf(stderr, "Error playing music: %s\n", MUSIC_ErrorString(MUSIC_ErrorCode));
        } else {
            while (MUSIC_SongPlaying()) {
                ASS_Sleep(500);
            }
        }
    } else {
        if (loopstart >= 0) {
            printf("Looping from %d to %d\n", loopstart, loopend);
            status = FX_PlayLoopedAuto(data, length, loopstart, loopend,
                0, 255, 255, 255, FX_MUSIC_PRIORITY, 1);
        } else {
            status = FX_PlayAuto(data, length, 0, 255, 255, 255, FX_MUSIC_PRIORITY, 1);
        }
        if (status >= FX_Ok) {
            while (FX_SoundActive(status)) {
                ASS_Sleep(500);
            }
        } else {
            fprintf(stderr, "Error playing sound: %s\n", FX_ErrorString(FX_ErrorCode));
        }
    }

    free(data);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebx
movl %esi, %r14d
movq %rdi, %r15
leaq 0x698b(%rip), %rsi      # 0x925e
callq 0x2260
testq %rax, %rax
je 0x2a25
movq %rax, %r13
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x2230
movq %r13, %rdi
callq 0x2030
movq %rax, %rbp
testl %ebp, %ebp
jle 0x29c3
movl %ebx, 0x4(%rsp)
movl %ebp, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %r12, %rdi
callq 0x22a0
testq %rax, %rax
je 0x29c3
movq %rax, %rbx
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2230
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rcx
callq 0x2310
movq %rax, %r12
movq %r13, %rdi
callq 0x21f0
cmpq $0x1, %r12
jne 0x2abd
leaq 0x6914(%rip), %rdi      # 0x9273
movq %r15, %rsi
xorl %eax, %eax
callq 0x2050
cmpl $0x6468544d, (%rbx)     # imm = 0x6468544D
je 0x29d9
testl %r14d, %r14d
js 0x2a4e
leaq 0x691c(%rip), %rdi      # 0x929d
movl %r14d, %esi
movl 0x4(%rsp), %r15d
movl %r15d, %edx
xorl %eax, %eax
callq 0x2050
movl $0xff, %eax
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
movl %r15d, %ecx
xorl %r8d, %r8d
movl $0xff, %r9d
pushq $0x1
pushq $0x7fffffff             # imm = 0x7FFFFFFF
pushq %rax
pushq %rax
callq 0x33e1
addq $0x20, %rsp
jmp 0x2a74
movq %r13, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21f0
movq %rbx, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x6742
testl %eax, %eax
je 0x2a17
movq 0xc5e8(%rip), %rax      # 0xefd8
movq (%rax), %r14
leaq 0x15b3e(%rip), %rax     # 0x18538
movl (%rax), %edi
callq 0x65ac
leaq 0x687c(%rip), %rsi      # 0x9284
jmp 0x2a99
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x8107
callq 0x6721
testl %eax, %eax
jne 0x2a0d
jmp 0x2abd
movq 0xc5ac(%rip), %rax      # 0xefd8
movq (%rax), %rdi
leaq 0x682b(%rip), %rsi      # 0x9261
movq %r15, %rdx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22f0
movq %rbx, %rdi
movl %ebp, %esi
xorl %edx, %edx
movl $0xff, %ecx
movl $0xff, %r8d
movl $0xff, %r9d
pushq $0x1
pushq $0x7fffffff             # imm = 0x7FFFFFFF
callq 0x330a
popq %rcx
popq %rdx
movl %eax, %ebp
testl %eax, %eax
jns 0x2ab2
movq 0xc557(%rip), %rax      # 0xefd8
movq (%rax), %r14
leaq 0xc849(%rip), %rax      # 0xf2d4
movl (%rax), %edi
callq 0x2e3c
leaq 0x681b(%rip), %rsi      # 0x92b4
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22f0
jmp 0x2abd
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x8107
movl %ebp, %edi
callq 0x3271
testl %eax, %eax
jne 0x2aa8
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2280
nop