// File: /workspace/github_c_makefile_star2_O3/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 0x193a
movq %rsi, %r14
movl %edi, %r13d
movl %edi, %eax
addl $-0x2, %r13d
movq -0x8(%rsi,%rax,8), %rsi
leaq 0x2a6a(%rip), %rdi      # 0x406b
movq %rsi, -0x40(%rbp)
xorl %eax, %eax
callq 0x10c0
xorl %r12d, %r12d
movl $0x0, -0x38(%rbp)
xorl %ebx, %ebx
movl $0x0, -0x2c(%rbp)
xorl %r15d, %r15d
movl $0x0, -0x30(%rbp)
movq %r14, -0x48(%rbp)
movq 0x8(%r14,%r12,8), %rsi
leaq 0x2a40(%rip), %rdi      # 0x4079
xorl %eax, %eax
callq 0x10c0
cmpq $0x14, %r12
jb 0x1657
leaq 0x2b39(%rip), %rdi      # 0x4186
callq 0x1080
jmp 0x16e6
movq 0x8(%r14,%r12,8), %rdi
leaq 0x2b17(%rip), %rsi      # 0x417a
callq 0x19ea
movl %ebx, %r14d
movq %r13, %rbx
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
callq 0x1f52
movq %rax, %r15
movq %rax, -0xf0(%rbp,%r12,8)
movq %r13, %rdi
movq %rbx, %r13
movl %r14d, %ebx
movq -0x48(%rbp), %r14
callq 0x10a0
movl (%r15), %ecx
movl 0x4(%r15), %esi
movl 0x8(%r15), %edx
movl 0xc(%r15), %r8d
leaq 0x29d6(%rip), %rdi      # 0x4084
xorl %eax, %eax
callq 0x10c0
movl 0xc(%r15), %eax
movl -0x30(%rbp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, -0x30(%rbp)
movl (%r15), %eax
movl 0x4(%r15), %ecx
movl %ecx, -0x2c(%rbp)
movl -0x38(%rbp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, -0x38(%rbp)
movl 0x10(%r15), %eax
cmpl %ebx, %eax
cmovgl %eax, %ebx
movl 0x8(%r15), %r15d
incq %r12
cmpq %r12, %r13
jne 0x162d
movl $0xa, %edi
callq 0x1050
leaq 0x2991(%rip), %rdi      # 0x4094
xorl %r14d, %r14d
movl -0x2c(%rbp), %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x2995(%rip), %rdi      # 0x40ac
movl %r15d, %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x29a0(%rip), %rdi      # 0x40c8
movl -0x38(%rbp), %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x29a4(%rip), %rdi      # 0x40dd
movl -0x30(%rbp), %r12d
movl %r12d, %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x29af(%rip), %rdi      # 0x40fd
movl %ebx, %esi
xorl %eax, %eax
callq 0x10c0
movl -0x2c(%rbp), %edi
movl %r15d, -0x2c(%rbp)
movl %r15d, %esi
movl -0x38(%rbp), %edx
movl %r12d, %ecx
callq 0x1bbc
movq %rax, %r12
movl %ebx, 0x10(%rax)
xorl %ebx, %ebx
cmpq $0x13, %r14
ja 0x17dd
movq -0xf0(%rbp,%r14,8), %rax
movl (%rax), %r8d
testl %r8d, %r8d
jle 0x17dd
movq 0x18(%rax), %rcx
movq 0x18(%r12), %rdx
movl 0xc(%rax), %edi
xorl %esi, %esi
testl %edi, %edi
jle 0x17d2
movq (%rcx,%rsi,8), %r8
movq (%rdx,%rsi,8), %r9
xorl %r10d, %r10d
movl %ebx, %edi
movl (%r9,%r10,4), %r11d
addl (%r8,%r10,4), %r11d
movl %r11d, %ebx
negl %ebx
cmovsl %r11d, %ebx
cmpl %edi, %ebx
cmovlel %edi, %ebx
movl %r11d, (%r9,%r10,4)
incq %r10
movslq 0xc(%rax), %rdi
cmpq %rdi, %r10
jl 0x17a7
movl (%rax), %r8d
incq %rsi
movslq %r8d, %r9
cmpq %r9, %rsi
jl 0x1798
incq %r14
cmpq %r13, %r14
jne 0x1774
movl $0xa, %edi
callq 0x1050
leaq 0x2918(%rip), %rdi      # 0x410e
xorl %r14d, %r14d
movl %ebx, %esi
xorl %eax, %eax
callq 0x10c0
movl 0x8(%r12), %edi
decl %edi
movsd 0x27f7(%rip), %xmm0     # 0x4008
callq 0x1060
addsd 0x27f2(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %esi
leaq 0x28f8(%rip), %rdi      # 0x4121
xorl %eax, %eax
callq 0x10c0
movl 0x8(%r12), %edi
decl %edi
movsd 0x27c9(%rip), %xmm0     # 0x4008
callq 0x1060
addsd 0x27cc(%rip), %xmm0     # 0x4018
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm0
mulsd 0x27c4(%rip), %xmm0     # 0x4020
leaq 0x28db(%rip), %rdi      # 0x413e
movsd %xmm0, -0x38(%rbp)
movb $0x1, %al
callq 0x10c0
movl (%r12), %esi
testl %esi, %esi
movl -0x2c(%rbp), %r15d
jle 0x18c5
movq 0x18(%r12), %rax
movl 0xc(%r12), %edx
xorl %ecx, %ecx
movsd -0x38(%rbp), %xmm1
testl %edx, %edx
jle 0x18ba
movq (%rax,%rcx,8), %rsi
xorl %edi, %edi
xorps %xmm0, %xmm0
cvtsi2sdl (%rsi,%rdi,4), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %edx
movl %edx, (%rsi,%rdi,4)
incq %rdi
movslq 0xc(%r12), %rdx
cmpq %rdx, %rdi
jl 0x1896
movl (%r12), %esi
incq %rcx
movslq %esi, %rdi
cmpq %rdi, %rcx
jl 0x188c
movl $0xa, %edi
callq 0x1050
leaq 0x287f(%rip), %rdi      # 0x4155
xorl %eax, %eax
callq 0x10c0
leaq 0x2884(%rip), %rsi      # 0x4168
movq -0x40(%rbp), %rdi
callq 0x11d0
movq %rax, %rbx
cmpl $0x8, %r15d
movl $0x7f, %r8d
cmovnel %r14d, %r8d
leaq 0x2865(%rip), %rdx      # 0x416a
movq %rax, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x2a84
leaq 0x2863(%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 0x26ec(%rip), %rdi      # 0x402d
callq 0x1080
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1210