// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
leaq 0xedd(%rip), %rsi       # 0x4108
callq 0x1df0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rdi
callq 0x1030
movq -0x10(%rbp), %rdi
callq 0x1eb0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rsi
movl $0x4, %edx
callq 0x31b0
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %esi
movl $0x4, %edx
callq 0x31e0
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0x4, %edx
callq 0x31b0
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rsi
movl $0x4, %edx
callq 0x31b0
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %esi
movl $0x4, %edx
callq 0x31e0
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x30(%rax), %esi
movl $0x2, %edx
callq 0x31e0
movq -0x8(%rbp), %rax
cmpl $0x1, 0x30(%rax)
je 0x32f9
leaq 0x13ea(%rip), %rdi      # 0x46dc
movb $0x0, %al
callq 0x1ca0
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x34(%rax), %esi
movl $0x2, %edx
callq 0x31e0
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x38(%rax), %esi
movl $0x4, %edx
callq 0x31e0
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x3c(%rax), %esi
movl $0x4, %edx
callq 0x31e0
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x40(%rax), %esi
movl $0x2, %edx
callq 0x31e0
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x44(%rax), %esi
movl $0x2, %edx
callq 0x31e0
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movl $0x4, %edx
callq 0x31b0
movq -0x8(%rbp), %rax
movl $0x0, 0x78(%rax)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x34(%rcx), %eax
jge 0x33d3
movq -0x8(%rbp), %rax
movl 0x78(%rax), %eax
movq -0x8(%rbp), %rcx
movslq -0x14(%rbp), %rdx
movq 0x50(%rcx,%rdx,8), %rcx
cmpl -0x4(%rcx), %eax
jge 0x33c6
movq -0x8(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq 0x50(%rax,%rcx,8), %rax
movl -0x4(%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x78(%rax)
jmp 0x33c8
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x338a
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x78(%rax), %esi
movq -0x8(%rbp), %rax
movl 0x44(%rax), %eax
movl $0x8, %ecx
cltd
idivl %ecx
imull %eax, %esi
movq -0x8(%rbp), %rax
imull 0x34(%rax), %esi
movl $0x4, %edx
callq 0x31e0
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x78(%rcx), %eax
jge 0x34ab
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x34(%rcx), %eax
jge 0x349b
movl -0x14(%rbp), %eax
movq -0x8(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movq 0x50(%rcx,%rdx,8), %rcx
cmpl -0x4(%rcx), %eax
jl 0x344a
movl $0x0, -0x24(%rbp)
jmp 0x347d
movq -0x8(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq 0x50(%rax,%rcx,8), %rax
movslq -0x14(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x347b
movl -0x24(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0x80(%rcx), %ecx
shll %ecx
addl %ecx, %eax
movl %eax, -0x24(%rbp)
jmp 0x347d
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
movq -0x8(%rbp), %rax
movl 0x7c(%rax), %edx
callq 0x31e0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x3420
jmp 0x349d
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x3409
movq -0x20(%rbp), %rdi
callq 0x1080
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)