// File: /workspace/github_c_makefile_star2_O0/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
subq $0x110, %rsp            # imm = 0x110
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x4, -0x8(%rbp)
jge 0x1355
leaq 0x3ce7(%rip), %rdi      # 0x502d
callq 0x1050
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x11e0
movl -0x8(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl -0x8(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rsi
leaq 0x3ce7(%rip), %rdi      # 0x506b
movb $0x0, %al
callq 0x10a0
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x156d
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x3ca0(%rip), %rdi      # 0x5079
movb $0x0, %al
callq 0x10a0
cmpl $0x14, -0x1c(%rbp)
jl 0x13f9
leaq 0x3c97(%rip), %rdi      # 0x5084
movb $0x0, %al
callq 0x10a0
jmp 0x155f
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x3da4(%rip), %rsi      # 0x51b5
callq 0x1a00
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2370
movq %rax, %rcx
movslq -0x1c(%rbp), %rax
movq %rcx, -0xf0(%rbp,%rax,8)
movq -0x18(%rbp), %rdi
callq 0x1080
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movl 0x4(%rax), %esi
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movl 0x8(%rax), %edx
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movl (%rax), %ecx
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movl 0xc(%rax), %r8d
leaq 0x3c24(%rip), %rdi      # 0x50a6
movb $0x0, %al
callq 0x10a0
movslq -0x1c(%rbp), %rax
cmpq $0x0, -0xf0(%rbp,%rax,8)
jne 0x14cb
movq 0x6b59(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x3bfc(%rip), %rsi      # 0x50b6
movb $0x0, %al
callq 0x1120
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x11e0
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movl 0xc(%rax), %eax
cmpl -0x30(%rbp), %eax
jle 0x14f1
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movl 0xc(%rax), %eax
movl %eax, -0x30(%rbp)
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movl (%rax), %eax
cmpl -0x2c(%rbp), %eax
jle 0x1515
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movl 0x10(%rax), %eax
cmpl -0x3c(%rbp), %eax
jle 0x153b
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movl 0x10(%rax), %eax
movl %eax, -0x3c(%rbp)
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movl 0x4(%rax), %eax
movl %eax, -0x38(%rbp)
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movl 0x8(%rax), %eax
movl %eax, -0x34(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x13b5
leaq 0x3ffe(%rip), %rdi      # 0x5572
movb $0x0, %al
callq 0x10a0
movl -0x38(%rbp), %esi
leaq 0x3b4a(%rip), %rdi      # 0x50cf
movb $0x0, %al
callq 0x10a0
movl -0x34(%rbp), %esi
leaq 0x3b51(%rip), %rdi      # 0x50e7
movb $0x0, %al
callq 0x10a0
movl -0x2c(%rbp), %esi
leaq 0x3b5c(%rip), %rdi      # 0x5103
movb $0x0, %al
callq 0x10a0
movl -0x30(%rbp), %esi
leaq 0x3b60(%rip), %rdi      # 0x5118
movb $0x0, %al
callq 0x10a0
movl -0x3c(%rbp), %esi
leaq 0x3b6f(%rip), %rdi      # 0x5138
movb $0x0, %al
callq 0x10a0
movl -0x38(%rbp), %edi
movl -0x34(%rbp), %esi
movl -0x2c(%rbp), %edx
movl -0x30(%rbp), %ecx
callq 0x1dd0
movq %rax, -0x50(%rbp)
movl -0x3c(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x10(%rax)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x1701
cmpl $0x14, -0x1c(%rbp)
jl 0x1614
jmp 0x16f3
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movslq -0x1c(%rbp), %rcx
movq -0xf0(%rbp,%rcx,8), %rcx
cmpl (%rcx), %eax
jge 0x16f1
movslq -0x1c(%rbp), %rax
movq -0xf0(%rbp,%rax,8), %rax
movq 0x18(%rax), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x100(%rbp)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x108(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movslq -0x1c(%rbp), %rcx
movq -0xf0(%rbp,%rcx,8), %rcx
cmpl 0xc(%rcx), %eax
jge 0x16e1
movq -0x100(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x108(%rbp), %rax
movslq -0x24(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movq -0x108(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cmpl -0x40(%rbp), %eax
jle 0x16d4
movq -0x108(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl %eax, -0x40(%rbp)
jmp 0x16d6
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x166f
jmp 0x16e3
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x161b
jmp 0x16f3
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x15fd
leaq 0x3e6a(%rip), %rdi      # 0x5572
movb $0x0, %al
callq 0x10a0
movl -0x40(%rbp), %esi
leaq 0x3a30(%rip), %rdi      # 0x5149
movb $0x0, %al
callq 0x10a0
movq -0x50(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x38e2(%rip), %xmm0     # 0x5018
callq 0x1070
movsd 0x38dd(%rip), %xmm1     # 0x5020
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %esi
leaq 0x3a0a(%rip), %rdi      # 0x515c
movb $0x0, %al
callq 0x10a0
movq -0x50(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x38a9(%rip), %xmm0     # 0x5018
callq 0x1070
movsd 0x3894(%rip), %xmm1     # 0x5010
subsd %xmm1, %xmm0
cvtsi2sdl -0x40(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd 0x3872(%rip), %xmm0     # 0x5008
mulsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
leaq 0x39cd(%rip), %rdi      # 0x5179
movb $0x1, %al
callq 0x10a0
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x182e
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x110(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x1821
movq -0x110(%rbp), %rax
movslq -0x20(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
mulsd -0x48(%rbp), %xmm0
cvttsd2si %xmm0, %edx
movq -0x110(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x17e3
jmp 0x1823
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x17ba
leaq 0x3d3d(%rip), %rdi      # 0x5572
movb $0x0, %al
callq 0x10a0
leaq 0x394d(%rip), %rdi      # 0x5190
movb $0x0, %al
callq 0x10a0
movq -0xf8(%rbp), %rdi
leaq 0x394b(%rip), %rsi      # 0x51a3
callq 0x11a0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl -0x34(%rbp), %ecx
xorl %r8d, %r8d
movl $0x7f, %eax
cmpl $0x8, %ecx
cmovel %eax, %r8d
leaq 0x3923(%rip), %rdx      # 0x51a5
xorl %eax, %eax
movl %eax, %ecx
callq 0x32a0
leaq 0x3925(%rip), %rdi      # 0x51b7
movb $0x0, %al
callq 0x10a0
movq -0x18(%rbp), %rdi
callq 0x1080
xorl %eax, %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopl (%rax)