// File: /workspace/github_c_makefile_star2_O3/pipde[P]deos-songlib/lib/wave.c
// Function: writeWave
void
writeWave(WAVE *w,const char *fileName)
    {
    int i,j;
    FILE *out = OpenFile(fileName,"w");

    free(w->outName);
    w->outName = StringDup(fileName);

    writeByteString(out,w->ID,4);

    writeByteInt(out,w->ChunkSize,4);

    writeByteString(out,w->Format,4);

    writeByteString(out,w->Subchunk1ID,4);

    writeByteInt(out,w->Subchunk1Size,4);

    writeByteInt(out,w->AudioFormat,2);

    if (w->AudioFormat != 1)
        Fatal("PCM only (no compression)");

    writeByteInt(out,w->NumChannels,2);

    writeByteInt(out,w->SampleRate,4);

    writeByteInt(out,w->ByteRate,4);

    writeByteInt(out,w->BlockAlign,2);

    writeByteInt(out,w->BitsPerSample,2);

    writeByteString(out,w->Subchunk2ID,4);

    /* find the largest channel and make its length the sample count */

    w->sampleCount = 0;
    for (i = 0; i < w->NumChannels; ++i)
        if (w->sampleCount < LENGTH(w->channels[i]))
            w->sampleCount = LENGTH(w->channels[i]);

    writeByteInt(out,w->sampleCount * (w->BitsPerSample / 8) * w->NumChannels,4);

    for (i = 0; i < w->sampleCount; ++i)
        for (j = 0; j < w->NumChannels; ++j)
            {
            int amp;
            //if (i % 1000 == 0)
            //    printf("%d samples of %d written\n",i,w->sampleCount);
            if (i >= LENGTH(w->channels[j]))
                amp = 0;
            else
                {
                amp = w->channels[j][i];
                if (amp < 0)
                    amp = amp + 2 * w->maxAmp;
                }
            //if (i < 50) printf("write sample %d: %d\n", i, amp);
            writeByteInt(out,amp,w->sampleWidth);
            }

    fclose(out);
    }

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x5d7(%rip), %rsi       # 0x30d4
movq %r15, %rdi
callq 0x212a
movq %rax, %r14
movq 0xa0(%rbx), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x2192
movq %rax, 0xa0(%rbx)
movq (%rbx), %rdi
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rcx
callq 0x11a0
movl 0x20(%rbx), %eax
leaq 0xc(%rsp), %r15
movl %eax, (%r15)
movl $0x1, %esi
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11a0
movq 0x8(%rbx), %rdi
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rcx
callq 0x11a0
movq 0x10(%rbx), %rdi
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rcx
callq 0x11a0
movl 0x28(%rbx), %eax
movl %eax, (%r15)
leaq 0xc(%rsp), %r15
movl $0x1, %esi
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11a0
movl 0x30(%rbx), %eax
movl %eax, (%r15)
leaq 0xc(%rsp), %rdi
movl $0x1, %esi
movl $0x2, %edx
movq %r14, %rcx
callq 0x11a0
cmpl $0x1, 0x30(%rbx)
je 0x2bd5
leaq 0xac7(%rip), %rdi       # 0x3695
xorl %eax, %eax
callq 0x2090
movl 0x34(%rbx), %eax
leaq 0xc(%rsp), %r15
movl %eax, (%r15)
movl $0x1, %esi
movl $0x2, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11a0
movl 0x38(%rbx), %eax
movl %eax, (%r15)
leaq 0xc(%rsp), %r15
movl $0x1, %esi
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11a0
movl 0x3c(%rbx), %eax
movl %eax, (%r15)
leaq 0xc(%rsp), %r15
movl $0x1, %esi
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11a0
movl 0x40(%rbx), %eax
movl %eax, (%r15)
leaq 0xc(%rsp), %r15
movl $0x1, %esi
movl $0x2, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11a0
movl 0x44(%rbx), %eax
movl %eax, (%r15)
leaq 0xc(%rsp), %rdi
movl $0x1, %esi
movl $0x2, %edx
movq %r14, %rcx
callq 0x11a0
movq 0x18(%rbx), %rdi
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rcx
callq 0x11a0
movl $0x0, 0x78(%rbx)
movslq 0x34(%rbx), %rax
testq %rax, %rax
jle 0x2cb7
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x50(%rbx,%rdx,8), %rsi
movl -0x4(%rsi), %esi
cmpl %esi, %ecx
jge 0x2cad
movl %esi, 0x78(%rbx)
movl %esi, %ecx
incq %rdx
cmpq %rdx, %rax
jne 0x2c9c
jmp 0x2cb9
xorl %ecx, %ecx
movl 0x44(%rbx), %edx
leal 0x7(%rdx), %esi
testl %edx, %edx
cmovnsl %edx, %esi
sarl $0x3, %esi
imull %ecx, %eax
imull %esi, %eax
leaq 0xc(%rsp), %rdi
movl %eax, (%rdi)
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rcx
callq 0x11a0
movl 0x78(%rbx), %ecx
testl %ecx, %ecx
jle 0x2d52
movl 0x34(%rbx), %eax
xorl %r12d, %r12d
leaq 0xc(%rsp), %r15
testl %eax, %eax
jle 0x2d47
xorl %r13d, %r13d
movq 0x50(%rbx,%r13,8), %rcx
movslq -0x4(%rcx), %rdx
xorl %eax, %eax
cmpq %rdx, %r12
jge 0x2d20
movl (%rcx,%r12,4), %eax
testl %eax, %eax
jns 0x2d20
movl 0x80(%rbx), %ecx
leal (%rax,%rcx,2), %eax
movslq 0x7c(%rbx), %rdx
movl %eax, 0xc(%rsp)
movl $0x1, %esi
movq %r15, %rdi
movq %r14, %rcx
callq 0x11a0
incq %r13
movslq 0x34(%rbx), %rax
cmpq %rax, %r13
jl 0x2cff
movl 0x78(%rbx), %ecx
incq %r12
movslq %ecx, %rdx
cmpq %rdx, %r12
jl 0x2cf8
movq %r14, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x10a0