// File: /workspace/github_c_makefile_star2_O1/pipde[P]deos-songlib/utils/mixer/mixer.c
// Function: main
int
main(int argc,char **argv)
    {
    FILE *fp;
    int i,j,k;
    int inRRACount;
    int channels,samples,bitsPerSample,sampleRate,skip;
    int max;
    double scale;
    RRA *output,*rras[MAX_RRAS];
    char *outFileName;

    // should have two inputs and one output at least

    if (argc < 4)
	{
	puts("mixer inputFile1 inputFile2 [more input files ...] outputFile");
	exit(-1);
	}

    inRRACount = argc - 2; // 1 program name, 1 output file

    outFileName = argv[argc - 1];

    printf("out file: %s\n", outFileName);

    /* read in rras, keeping track of the highest sampleCount */

    samples = channels = sampleRate = bitsPerSample = skip = 0;

    for (i = 0; i < inRRACount; ++i)
	{
	printf("mixing: %s", argv[i + 1]);

	if (i >= MAX_RRAS)
	    {
	    printf(" skipping (too many input files)\n");
	    continue;
	    }

	fp = OpenFile(argv[i + 1],"r");
	rras[i] = readRRA(fp,0);
	fclose(fp);

	printf(" (%d,%d,%d,%d)\n",rras[i]->sampleRate,
	    rras[i]->bitsPerSample,rras[i]->channels,rras[i]->samples);

	if (rras[i] == NULL)
	    {
	    fprintf(stderr, "file %s does not exist!\n", argv[i + 1]);
	    exit(-1);
	    }
	if (rras[i]->samples > samples)
	    {
	    samples = rras[i]->samples;
	    }
	if (rras[i]->channels > channels)
	    {
	    channels = rras[i]->channels;
	    }
	if (rras[i]->skip > skip)
	    {
	    skip = rras[i]->skip;
	    }
	sampleRate = rras[i]->sampleRate;
	bitsPerSample = rras[i]->bitsPerSample;
	}

    /* create our output wav that is as long as the longest input */

    printf("\n");

    printf("output sample rate: %d\n",sampleRate);
    printf("output bits per sample: %d\n",bitsPerSample);
    printf("output channels: %d\n",channels);
    printf("output samples per channel: %d\n",samples);
    printf("output skip: %d\n",skip);

    output = newRRA(sampleRate,bitsPerSample,channels,samples);
    output->skip = skip;

    /* mix in each input wav and keep track of the highest peek */

    max = 0;
    for (i = 0; i < inRRACount; ++i)
	{
	if (i >= MAX_RRAS) continue;
	for (j = 0; j < rras[i]->channels; ++j)
	    {
	    int *in = rras[i]->data[j];
	    int *out = output->data[j];
	    for (k = 0; k < rras[i]->samples; ++k)
		{
		out[k] += in[k];
		if (abs(out[k]) > max) max = abs(out[k]);
		}
	    }
	}

    printf("\n");
    printf("max amplitude: %d\n", max);
    printf("max allowable amplitude: %d\n",
        (int) (pow(2,output->bitsPerSample - 1) - 0.5));

    /* use the peak to calculate a reasonable scale factor */

    scale = (pow(2,output->bitsPerSample - 1) - 1) / max;

    /* fudge factor? */

    scale *= 0.99;

    printf("scaling samples by %g\n", scale);

    /* scale samples */

    for (i = 0; i < output->channels; ++i)
	{
	int *out = output->data[i];
        for (j = 0; j < output->samples; ++j)
	    out[j] = out[j] * scale;
	}

    printf("\n");
    printf("writing out mix...");

    // write the samples out to the last file specified on command line

    fp = fopen(outFileName,"w");
    writeRRA(fp,output,"modifiedBy: mixer",0, bitsPerSample == 8? 127:0);
    printf("finished.\n");
    fclose(fp);

    return 0;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
cmpl $0x3, %edi
jle 0x191e
movq %rsi, %r14
movl %edi, %r12d
movl %edi, %eax
addl $-0x2, %r12d
movq -0x8(%rsi,%rax,8), %rsi
leaq 0x2a9a(%rip), %rdi      # 0x406b
movq %rsi, -0x48(%rbp)
xorl %eax, %eax
callq 0x10c0
leaq 0x2a96(%rip), %rbx      # 0x4079
xorl %r15d, %r15d
movl $0x0, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x2c(%rbp)
xorl %r13d, %r13d
movl $0x0, -0x30(%rbp)
movq %r14, -0x50(%rbp)
movq 0x8(%r14,%r15,8), %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10c0
cmpq $0x14, %r15
jb 0x162f
leaq 0x2b61(%rip), %rdi      # 0x4186
callq 0x1080
jmp 0x16cd
movq 0x8(%r14,%r15,8), %rdi
leaq 0x2b3f(%rip), %rsi      # 0x417a
callq 0x19ce
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
callq 0x1f3a
movq %rbx, %r14
movq %r12, %rbx
movq %rax, %r12
movq %rax, -0xf0(%rbp,%r15,8)
movq %r13, %rdi
callq 0x10a0
movl (%r12), %ecx
movl 0x4(%r12), %esi
movl 0x8(%r12), %edx
movl 0xc(%r12), %r8d
leaq 0x2a04(%rip), %rdi      # 0x4084
xorl %eax, %eax
callq 0x10c0
movl 0xc(%r12), %eax
movl -0x30(%rbp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, -0x30(%rbp)
movl (%r12), %eax
movl 0x4(%r12), %ecx
movl %ecx, -0x2c(%rbp)
movl -0x40(%rbp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, -0x40(%rbp)
movl 0x10(%r12), %eax
movl -0x34(%rbp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, -0x34(%rbp)
movl 0x8(%r12), %r13d
movq %rbx, %r12
movq %r14, %rbx
movq -0x50(%rbp), %r14
incq %r15
cmpq %r15, %r12
jne 0x1609
movl $0xa, %edi
callq 0x1050
leaq 0x29aa(%rip), %rdi      # 0x4094
xorl %r14d, %r14d
movl -0x2c(%rbp), %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x29ae(%rip), %rdi      # 0x40ac
movl %r13d, %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x29b9(%rip), %rdi      # 0x40c8
movl -0x40(%rbp), %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x29bd(%rip), %rdi      # 0x40dd
movl -0x30(%rbp), %r15d
movl %r15d, %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x29c8(%rip), %rdi      # 0x40fd
movl -0x34(%rbp), %ebx
movl %ebx, %esi
xorl %eax, %eax
callq 0x10c0
movl -0x2c(%rbp), %edi
movl %r13d, -0x2c(%rbp)
movl %r13d, %esi
movl -0x40(%rbp), %edx
movl %r15d, %ecx
callq 0x1bac
movq %rax, %r15
movl %ebx, 0x10(%rax)
cmpl $0x2, %r12d
movl $0x1, %eax
cmovgel %r12d, %eax
xorl %ebx, %ebx
cmpq $0x13, %r14
ja 0x17cd
movq -0xf0(%rbp,%r14,8), %rcx
cmpl $0x0, (%rcx)
jle 0x17cd
movq 0x18(%rcx), %rdx
movq 0x18(%r15), %rsi
xorl %edi, %edi
cmpl $0x0, 0xc(%rcx)
jle 0x17c2
movq (%rdx,%rdi,8), %r8
movq (%rsi,%rdi,8), %r9
xorl %r10d, %r10d
movl (%r9,%r10,4), %r11d
addl (%r8,%r10,4), %r11d
movl %r11d, %r12d
negl %r12d
cmovsl %r11d, %r12d
cmpl %ebx, %r12d
cmovgl %r12d, %ebx
movl %r11d, (%r9,%r10,4)
incq %r10
movslq 0xc(%rcx), %r11
cmpq %r11, %r10
jl 0x1799
incq %rdi
movslq (%rcx), %r8
cmpq %r8, %rdi
jl 0x1788
incq %r14
cmpq %rax, %r14
jne 0x176b
movl $0xa, %edi
callq 0x1050
leaq 0x2928(%rip), %rdi      # 0x410e
xorl %r14d, %r14d
movl %ebx, %esi
xorl %eax, %eax
callq 0x10c0
movl 0x8(%r15), %edi
decl %edi
movsd 0x2808(%rip), %xmm0     # 0x4008
callq 0x1060
addsd 0x2803(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %esi
leaq 0x2909(%rip), %rdi      # 0x4121
xorl %eax, %eax
callq 0x10c0
movl 0x8(%r15), %edi
decl %edi
movsd 0x27db(%rip), %xmm0     # 0x4008
callq 0x1060
addsd 0x27de(%rip), %xmm0     # 0x4018
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm0
mulsd 0x27d6(%rip), %xmm0     # 0x4020
movsd %xmm0, -0x40(%rbp)
leaq 0x28e8(%rip), %rdi      # 0x413e
movb $0x1, %al
callq 0x10c0
movsd -0x40(%rbp), %xmm1
cmpl $0x0, (%r15)
movl -0x2c(%rbp), %r12d
jle 0x18a9
movq 0x18(%r15), %rax
xorl %ecx, %ecx
cmpl $0x0, 0xc(%r15)
jle 0x189e
movq (%rax,%rcx,8), %rdx
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsi2sdl (%rdx,%rsi,4), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %edi
movl %edi, (%rdx,%rsi,4)
incq %rsi
movslq 0xc(%r15), %rdi
cmpq %rdi, %rsi
jl 0x187f
incq %rcx
movslq (%r15), %rdx
cmpq %rdx, %rcx
jl 0x1872
movl $0xa, %edi
callq 0x1050
leaq 0x289b(%rip), %rdi      # 0x4155
xorl %eax, %eax
callq 0x10c0
leaq 0x28a0(%rip), %rsi      # 0x4168
movq -0x48(%rbp), %rdi
callq 0x11d0
movq %rax, %rbx
cmpl $0x8, %r12d
movl $0x7f, %r8d
cmovnel %r14d, %r8d
leaq 0x2881(%rip), %rdx      # 0x416a
movq %rax, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x2a15
leaq 0x287f(%rip), %rdi      # 0x417c
callq 0x1080
movq %rbx, %rdi
callq 0x10a0
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2708(%rip), %rdi      # 0x402d
callq 0x1080
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1210