// File: /workspace/github_c_makefile_star2_O1/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 0x534(%rip), %rsi       # 0x30d4
movq %r15, %rdi
callq 0x2136
movq %rax, %r14
movq 0xa0(%rbx), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x21ac
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 0x2c78
leaq 0xa14(%rip), %rdi       # 0x3685
xorl %eax, %eax
callq 0x209c
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 0x2d59
movl 0x78(%rbx), %edx
xorl %ecx, %ecx
movq 0x50(%rbx,%rcx,8), %rsi
movl -0x4(%rsi), %esi
cmpl %esi, %edx
jge 0x2d51
movl %esi, 0x78(%rbx)
movl %esi, %edx
incq %rcx
cmpq %rcx, %rax
jne 0x2d40
movl 0x44(%rbx), %ecx
leal 0x7(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
imull 0x78(%rbx), %eax
sarl $0x3, %edx
imull %edx, %eax
leaq 0xc(%rsp), %rdi
movl %eax, (%rdi)
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rcx
callq 0x11a0
cmpl $0x0, 0x78(%rbx)
jle 0x2def
xorl %r12d, %r12d
leaq 0xc(%rsp), %r15
cmpl $0x0, 0x34(%rbx)
jle 0x2de3
xorl %r13d, %r13d
movq 0x50(%rbx,%r13,8), %rcx
movslq -0x4(%rcx), %rdx
xorl %eax, %eax
cmpq %rdx, %r12
jge 0x2dbf
movl (%rcx,%r12,4), %eax
testl %eax, %eax
jns 0x2dbf
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 0x2d9e
incq %r12
movslq 0x78(%rbx), %rax
cmpq %rax, %r12
jl 0x2d95
movq %r14, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x10a0