// File: /workspace/github_c_makefile_star2_O3/pipde[P]deos-songlib/utils/normalize/normalize.c
// Function: main
int
main(int argc,char **argv)
    {
    int argIndex = 1;

    int i,j,amp,count;
    int largest,smallest;
    double scale;
    FILE *in,*out;
    RRA *h;

    argIndex = processOptions(argc,argv);

    if (argc-argIndex == 0)
        {
        in = stdin;
        out = stdout;
        }
    else if (argc-argIndex == 1)
        {
        in = OpenFile(argv[argIndex],"r");
        out = stdout;
        }
    else if (argc-argIndex == 2)
        {
        in = OpenFile(argv[argIndex],"r");
        out = OpenFile(argv[argIndex+1],"w");
        }
    else
        {
        printf("usage: normalize [-h] [<input rra file> [<output rra file>]]\n");
        exit(-1);
        }

    h = readRRA(in,0);

    largest = h->data[0][0];
    smallest = h->data[0][0];

    count = 0;
    for (i = 0; i < h->samples; ++i)
        {
        for (j = 0; j < h->channels; ++j)
            {
            amp = h->data[j][i];
            if (amp > largest) { largest = amp;}
            if (amp < smallest) { smallest = amp;}
            }
        ++count;
        }

    if (largest > -smallest)
        scale = pow(2,h->bitsPerSample - 1) / largest * amplitude;
    else
        scale = pow(2,h->bitsPerSample - 1) / -smallest * amplitude;

    fprintf(stderr,"normalize: largest value is %d\n",largest);
    fprintf(stderr,"normalize: smallest value is %d\n",smallest);
    fprintf(stderr,"normalize: scale is %f\n",scale);
    if (stats) exit(0);

    h->samples = count;

    for (i = 0; i < h->samples; ++i)
        for (j = 0; j < h->channels; ++j)
            {
            h->data[j][i] *= scale;
            if (h->data[j][i] > INT_MAX - 1)
                fprintf(stderr,"normalize: overflow\n");
            else if (h->data[j][i] < -INT_MAX + 1)
                fprintf(stderr,"normalize: underflow\n");
            }

    writeRRA(out,h,"modifiedBy: normalize",0,h->bitsPerSample == 8 ? 127:0);

    fclose(in);
    fclose(out);

    return 0;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movl %edi, %ebx
movl $0x1, %r12d
cmpl $0x2, %edi
jl 0x16cb
movl $0x1, %r12d
movslq %r12d, %rax
movq (%r14,%rax,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x16cb
movb 0x2(%rsi), %r13b
testb %r13b, %r13b
je 0x1656
leaq 0x2(%rsi), %rdi
jmp 0x165b
movq 0x8(%r14,%rax,8), %rdi
movzbl 0x1(%rsi), %eax
cmpl $0x72, %eax
jg 0x167b
cmpl $0x61, %eax
jne 0x168c
callq 0x1080
movsd %xmm0, 0x5ad2(%rip)     # 0x7148
movb $0x1, %r15b
jmp 0x16b0
cmpl $0x73, %eax
jne 0x1696
movb $0x1, 0x5add(%rip)      # 0x7164
xorl %r15d, %r15d
jmp 0x16b0
cmpl $0x68, %eax
jne 0x169f
jmp 0x18f9
cmpl $0x76, %eax
je 0x18db
xorl %r15d, %r15d
leaq 0x2a17(%rip), %rdi      # 0x40c0
xorl %eax, %eax
callq 0x1954
testb %r13b, %r13b
sete %al
andb %r15b, %al
movzbl %al, %eax
addl %eax, %r12d
incl %r12d
cmpl %ebx, %r12d
jl 0x1637
subl %r12d, %ebx
jne 0x16dc
movq 0x5901(%rip), %rax      # 0x6fd8
movq (%rax), %r15
jmp 0x1700
cmpl $0x2, %ebx
je 0x170c
cmpl $0x1, %ebx
jne 0x193c
movslq %r12d, %rax
movq (%r14,%rax,8), %rdi
leaq 0x2f4a(%rip), %rsi      # 0x4642
callq 0x19ee
movq %rax, %r15
movq 0x58c9(%rip), %rax      # 0x6fd0
movq (%rax), %rax
jmp 0x1733
movslq %r12d, %rbx
movq (%r14,%rbx,8), %rdi
leaq 0x2f28(%rip), %rsi      # 0x4642
callq 0x19ee
movq %rax, %r15
movq 0x8(%r14,%rbx,8), %rdi
leaq 0x28e7(%rip), %rsi      # 0x4015
callq 0x19ee
movq %rax, -0x38(%rbp)
movq %r15, -0x40(%rbp)
movq %r15, %rdi
xorl %esi, %esi
callq 0x1f56
movq %rax, %r15
movq 0x18(%rax), %rax
movq (%rax), %rcx
movl (%rcx), %r12d
movslq 0xc(%r15), %rbx
testq %rbx, %rbx
jle 0x1790
movl (%r15), %ecx
xorl %edx, %edx
movl %r12d, %r13d
testl %ecx, %ecx
jle 0x1786
xorl %esi, %esi
movq (%rax,%rsi,8), %rdi
movl (%rdi,%rdx,4), %edi
cmpl %r13d, %edi
cmovgl %edi, %r13d
cmpl %r12d, %edi
cmovll %edi, %r12d
incq %rsi
cmpq %rsi, %rcx
jne 0x1769
incq %rdx
cmpq %rbx, %rdx
jne 0x1763
jmp 0x1795
xorl %ebx, %ebx
movl %r12d, %r13d
movl %r12d, %r14d
negl %r14d
movl 0x8(%r15), %edi
decl %edi
movsd 0x285f(%rip), %xmm0     # 0x4008
callq 0x1050
cmpl %r14d, %r13d
cmovgl %r13d, %r14d
cvtsi2sd %r14d, %xmm1
divsd %xmm1, %xmm0
mulsd 0x5982(%rip), %xmm0     # 0x7148
movsd %xmm0, -0x30(%rbp)
movq 0x5826(%rip), %r14      # 0x6ff8
movq (%r14), %rdi
leaq 0x283b(%rip), %rsi      # 0x4017
movl %r13d, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0x2847(%rip), %rsi      # 0x4037
movl %r12d, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0x2854(%rip), %rsi      # 0x4058
movsd -0x30(%rbp), %xmm0
movb $0x1, %al
callq 0x1140
cmpb $0x1, 0x594d(%rip)      # 0x7164
je 0x1935
movl %ebx, 0xc(%r15)
testl %ebx, %ebx
movsd -0x30(%rbp), %xmm1
jle 0x1890
movl (%r15), %eax
xorl %r12d, %r12d
testl %eax, %eax
jle 0x1885
xorl %ebx, %ebx
movq 0x18(%r15), %rax
movq (%rax,%rbx,8), %rcx
xorps %xmm0, %xmm0
cvtsi2sdl (%rcx,%r12,4), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, (%rcx,%r12,4)
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x186e
cmpl $0x80000001, %eax       # imm = 0x80000001
jle 0x1875
incq %rbx
movslq (%r15), %rax
cmpq %rax, %rbx
jl 0x1836
jmp 0x1881
callq 0x128f
jmp 0x187a
callq 0x1260
movsd -0x30(%rbp), %xmm1
jmp 0x1861
movl 0xc(%r15), %ebx
incq %r12
movslq %ebx, %rcx
cmpq %rcx, %r12
jl 0x1830
xorl %eax, %eax
cmpl $0x8, 0x8(%r15)
movl $0x7f, %r8d
cmovnel %eax, %r8d
leaq 0x27f3(%rip), %rdx      # 0x409b
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x2a88
movq -0x40(%rbp), %rdi
callq 0x1090
movq %rbx, %rdi
callq 0x1090
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5856(%rip), %rsi      # 0x7138
movq 0x5857(%rip), %rdx      # 0x7140
leaq 0x27c1(%rip), %rdi      # 0x40b1
xorl %eax, %eax
callq 0x10b0
jmp 0x1935
leaq 0x2817(%rip), %rdi      # 0x4117
callq 0x1070
leaq 0x281e(%rip), %rdi      # 0x412a
callq 0x1070
leaq 0x2835(%rip), %rdi      # 0x414d
callq 0x1070
leaq 0x284f(%rip), %rdi      # 0x4173
callq 0x1070
leaq 0x2875(%rip), %rdi      # 0x41a5
callq 0x1070
xorl %edi, %edi
callq 0x1200
leaq 0x2797(%rip), %rdi      # 0x40da
callq 0x1070
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1200
nop