// File: /workspace/github_c_makefile_star2_O3/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 0x78d8(%rip), %rsi      # 0xa25e
callq 0x2250
testq %rax, %rax
je 0x2ab9
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2220
movq %r13, %rdi
callq 0x2030
movq %rax, %rbp
testl %ebp, %ebp
jle 0x2a7a
movl %ebx, 0x4(%rsp)
movl %ebp, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %r12, %rdi
callq 0x2290
testq %rax, %rax
je 0x2a7a
movq %rax, %rbx
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2220
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rcx
callq 0x2300
movq %rax, %r12
movq %r13, %rdi
callq 0x21f0
cmpq $0x1, %r12
jne 0x2b2c
leaq 0x785d(%rip), %rdi      # 0xa273
movq %r15, %rsi
xorl %eax, %eax
callq 0x2050
cmpl $0x6468544d, (%rbx)     # imm = 0x6468544D
je 0x2a90
testl %r14d, %r14d
js 0x2ae2
leaq 0x7865(%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 0x345f
addq $0x20, %rsp
jmp 0x2b0a
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 0x6cfd
testl %eax, %eax
jne 0x2b42
callq 0x6cd9
testl %eax, %eax
je 0x2b2c
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x8ac4
jmp 0x2aa4
movq 0xd518(%rip), %rax      # 0xffd8
movq (%rax), %rdi
leaq 0x7797(%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 0x22e0
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 0x33dc
addq $0x10, %rsp
movl %eax, %ebp
testl %eax, %eax
jns 0x2b21
callq 0x2441
jmp 0x2b2c
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x8ac4
movl %ebp, %edi
callq 0x3339
testl %eax, %eax
jne 0x2b17
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2270
callq 0x246f
jmp 0x2b2c
nopl (%rax)