// File: /workspace/github_c_makefile_star2_O2/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 0x16ad
movq %rsi, %r14
movl %edi, %r15d
movl %edi, %eax
addl $-0x2, %r15d
movq -0x8(%rsi,%rax,8), %rsi
leaq 0x2cda(%rip), %rdi      # 0x406b
movq %rsi, -0x50(%rbp)
xorl %eax, %eax
callq 0x10c0
leaq 0x2cd6(%rip), %r13      # 0x4079
leaq 0x2ddc(%rip), %rbx      # 0x4186
xorl %r12d, %r12d
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x40(%rbp)
cmpq %r12, %r15
je 0x148c
movq 0x8(%r14,%r12,8), %rsi
movq %r13, %rdi
xorl %eax, %eax
callq 0x10c0
cmpq $0x14, %r12
jb 0x13fb
movq %rbx, %rdi
callq 0x1080
incq %r12
jmp 0x13d0
movq 0x8(%r14,%r12,8), %rdi
leaq 0x2d73(%rip), %rsi      # 0x417a
callq 0x17f4
movq %rax, -0x48(%rbp)
movq %rax, %rdi
xorl %esi, %esi
callq 0x1cc1
movq %rax, %rbx
movq %rax, -0xf0(%rbp,%r12,8)
movq -0x48(%rbp), %rdi
callq 0x10a0
movl (%rbx), %ecx
movl 0x4(%rbx), %esi
movl 0x8(%rbx), %edx
movl 0xc(%rbx), %r8d
leaq 0x2c43(%rip), %rdi      # 0x4084
xorl %eax, %eax
callq 0x10c0
movl 0xc(%rbx), %eax
movl -0x2c(%rbp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, -0x2c(%rbp)
movl (%rbx), %eax
movl 0x4(%rbx), %ecx
movl %ecx, -0x34(%rbp)
movl -0x40(%rbp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, -0x40(%rbp)
movl 0x10(%rbx), %eax
movl -0x30(%rbp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, -0x30(%rbp)
movl 0x8(%rbx), %eax
movl %eax, -0x48(%rbp)
leaq 0x2d02(%rip), %rbx      # 0x4186
incq %r12
jmp 0x13d0
pushq $0xa
popq %rdi
callq 0x1050
leaq 0x2bf9(%rip), %rdi      # 0x4094
xorl %r14d, %r14d
movl -0x34(%rbp), %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x2bfd(%rip), %rdi      # 0x40ac
movl -0x48(%rbp), %ebx
movl %ebx, %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x2c06(%rip), %rdi      # 0x40c8
movl -0x40(%rbp), %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x2c0a(%rip), %rdi      # 0x40dd
movl -0x2c(%rbp), %r12d
movl %r12d, %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x2c15(%rip), %rdi      # 0x40fd
movl -0x30(%rbp), %r13d
movl %r13d, %esi
xorl %eax, %eax
callq 0x10c0
movl -0x34(%rbp), %edi
movl %ebx, %esi
movl -0x40(%rbp), %edx
movl %r12d, %ecx
callq 0x19bc
movq %rax, %r12
movl %r13d, 0x10(%rax)
xorl %ebx, %ebx
cmpq %r15, %r14
je 0x1574
cmpq $0x13, %r14
jbe 0x151f
incq %r14
jmp 0x150f
movq -0xf0(%rbp,%r14,8), %rax
xorl %ecx, %ecx
movslq (%rax), %rdx
cmpq %rdx, %rcx
jge 0x151a
movq 0x18(%rax), %rdx
movq (%rdx,%rcx,8), %rdx
movq 0x18(%r12), %rsi
movq (%rsi,%rcx,8), %rsi
xorl %edi, %edi
movslq 0xc(%rax), %r8
cmpq %r8, %rdi
jge 0x156f
movl (%rsi,%rdi,4), %r8d
addl (%rdx,%rdi,4), %r8d
movl %r8d, (%rsi,%rdi,4)
movl %r8d, %r9d
negl %r9d
cmovsl %r8d, %r9d
cmpl %ebx, %r9d
cmovgl %r9d, %ebx
incq %rdi
jmp 0x1544
incq %rcx
jmp 0x1529
pushq $0xa
popq %rdi
callq 0x1050
leaq 0x2b8b(%rip), %rdi      # 0x410e
xorl %r14d, %r14d
movl %ebx, %esi
xorl %eax, %eax
callq 0x10c0
movl 0x8(%r12), %edi
decl %edi
movsd 0x2a6a(%rip), %xmm0     # 0x4008
callq 0x1060
addsd 0x2a65(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %esi
leaq 0x2b6b(%rip), %rdi      # 0x4121
xorl %eax, %eax
callq 0x10c0
movl 0x8(%r12), %edi
decl %edi
movsd 0x2a3c(%rip), %xmm0     # 0x4008
callq 0x1060
addsd 0x2a3f(%rip), %xmm0     # 0x4018
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm0
mulsd 0x2a37(%rip), %xmm0     # 0x4020
movsd %xmm0, -0x40(%rbp)
leaq 0x2b49(%rip), %rdi      # 0x413e
movb $0x1, %al
callq 0x10c0
movsd -0x40(%rbp), %xmm1
movslq (%r12), %rax
cmpq %rax, %r14
jge 0x1639
movq 0x18(%r12), %rax
movq (%rax,%r14,8), %rax
xorl %ecx, %ecx
movslq 0xc(%r12), %rdx
cmpq %rdx, %rcx
jge 0x1634
cvtsi2sdl (%rax,%rcx,4), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
jmp 0x1615
incq %r14
jmp 0x1601
pushq $0xa
popq %rdi
callq 0x1050
leaq 0x2b0d(%rip), %rdi      # 0x4155
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x10c0
leaq 0x2b0f(%rip), %rsi      # 0x4168
movq -0x50(%rbp), %rdi
callq 0x11d0
movq %rax, %rbx
cmpl $0x8, -0x48(%rbp)
pushq $0x7f
popq %r8
cmovnel %r14d, %r8d
leaq 0x2af2(%rip), %rdx      # 0x416a
movq %rax, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x2734
leaq 0x2af0(%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 0x2979(%rip), %rdi      # 0x402d
callq 0x1080
pushq $-0x1
popq %rdi
callq 0x1210
nopl (%rax)