// File: /workspace/github_c_makefile_star2_O1/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 0x78dd(%rip), %rsi      # 0xa25e
callq 0x2240
testq %rax, %rax
je 0x2ab4
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2210
movq %r13, %rdi
callq 0x2030
movq %rax, %rbp
testl %ebp, %ebp
jle 0x2a75
movl %ebx, 0x4(%rsp)
movl %ebp, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %r12, %rdi
callq 0x2280
testq %rax, %rax
je 0x2a75
movq %rax, %rbx
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2210
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rcx
callq 0x22f0
movq %rax, %r12
movq %r13, %rdi
callq 0x21e0
cmpq $0x1, %r12
jne 0x2b27
leaq 0x7862(%rip), %rdi      # 0xa273
movq %r15, %rsi
xorl %eax, %eax
callq 0x2050
cmpl $0x6468544d, (%rbx)     # imm = 0x6468544D
je 0x2a8b
testl %r14d, %r14d
js 0x2add
leaq 0x786a(%rip), %rdi      # 0xa29d
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 0x3460
addq $0x20, %rsp
jmp 0x2b05
movq %r13, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21e0
movq %rbx, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x6c72
testl %eax, %eax
jne 0x2b3d
callq 0x6c4e
testl %eax, %eax
je 0x2b27
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x8a5c
jmp 0x2a9f
movq 0xd51d(%rip), %rax      # 0xffd8
movq (%rax), %rdi
leaq 0x779c(%rip), %rsi      # 0xa261
movq %r15, %rdx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22d0
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 0x33d7
addq $0x10, %rsp
movl %eax, %ebp
testl %eax, %eax
jns 0x2b1c
callq 0x2431
jmp 0x2b27
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x8a5c
movl %ebp, %edi
callq 0x3334
testl %eax, %eax
jne 0x2b12
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2260
callq 0x245f
jmp 0x2b27