// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rsi, %r15
movq %rdi, %rbx
leaq 0xb15(%rip), %rsi       # 0x30d4
movq %r15, %rdi
callq 0x1a48
movq %rax, %r14
movq 0xa0(%rbx), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1ab0
movq %rax, 0xa0(%rbx)
movq (%rbx), %rsi
movq %r14, %rdi
callq 0x257e
movl 0x20(%rbx), %esi
pushq $0x4
popq %rbp
movq %r14, %rdi
movl %ebp, %edx
callq 0x2592
movq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0x257e
movq 0x10(%rbx), %rsi
movq %r14, %rdi
callq 0x257e
movl 0x28(%rbx), %esi
movq %r14, %rdi
movl %ebp, %edx
callq 0x2592
movl 0x30(%rbx), %esi
pushq $0x2
popq %rdx
movq %r14, %rdi
callq 0x2592
cmpl $0x1, 0x30(%rbx)
je 0x2647
leaq 0x1055(%rip), %rdi      # 0x3695
xorl %eax, %eax
callq 0x1918
movl 0x34(%rbx), %esi
pushq $0x2
popq %rbp
movq %r14, %rdi
movl %ebp, %edx
callq 0x2592
movl 0x38(%rbx), %esi
pushq $0x4
popq %r15
movq %r14, %rdi
movl %r15d, %edx
callq 0x2592
movl 0x3c(%rbx), %esi
movq %r14, %rdi
movl %r15d, %edx
callq 0x2592
movl 0x40(%rbx), %esi
movq %r14, %rdi
movl %ebp, %edx
callq 0x2592
movl 0x44(%rbx), %esi
movq %r14, %rdi
movl %ebp, %edx
callq 0x2592
movq 0x18(%rbx), %rsi
movq %r14, %rdi
callq 0x257e
andl $0x0, 0x78(%rbx)
movl 0x34(%rbx), %ecx
xorl %esi, %esi
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
xorl %edx, %edx
cmpq %rdx, %rax
je 0x26cd
movq 0x50(%rbx,%rdx,8), %rdi
movl -0x4(%rdi), %edi
cmpl %edi, %esi
jge 0x26c8
movl %edi, 0x78(%rbx)
movl %edi, %esi
incq %rdx
jmp 0x26b2
movl 0x44(%rbx), %eax
pushq $0x8
popq %rdi
cltd
idivl %edi
imull %ecx, %esi
imull %eax, %esi
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x2592
xorl %r15d, %r15d
movslq 0x78(%rbx), %rax
cmpq %rax, %r15
jge 0x2735
xorl %r12d, %r12d
movslq 0x34(%rbx), %rax
cmpq %rax, %r12
jge 0x2730
movq 0x50(%rbx,%r12,8), %rax
movslq -0x4(%rax), %rcx
xorl %esi, %esi
cmpq %rcx, %r15
jge 0x2720
movl (%rax,%r15,4), %esi
testl %esi, %esi
jns 0x2720
movl 0x80(%rbx), %eax
leal (%rsi,%rax,2), %esi
movl 0x7c(%rbx), %edx
movq %r14, %rdi
callq 0x2592
incq %r12
jmp 0x26f6
incq %r15
jmp 0x26ea
movq %r14, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x10a0