// File: /workspace/github_c_makefile_star2_O1/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 0x16ab
movl $0x1, %r12d
movslq %r12d, %rax
movq (%r14,%rax,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x16ab
movb 0x2(%rsi), %r13b
testb %r13b, %r13b
je 0x1636
leaq 0x2(%rsi), %rdi
jmp 0x163b
movq 0x8(%r14,%rax,8), %rdi
movzbl 0x1(%rsi), %eax
cmpl $0x72, %eax
jg 0x165b
cmpl $0x61, %eax
jne 0x166c
callq 0x1080
movsd %xmm0, 0x5af2(%rip)     # 0x7148
movb $0x1, %r15b
jmp 0x1690
cmpl $0x73, %eax
jne 0x1676
movb $0x1, 0x5afd(%rip)      # 0x7164
xorl %r15d, %r15d
jmp 0x1690
cmpl $0x68, %eax
jne 0x167f
jmp 0x18d3
cmpl $0x76, %eax
je 0x18b5
xorl %r15d, %r15d
leaq 0x2a37(%rip), %rdi      # 0x40c0
xorl %eax, %eax
callq 0x192c
testb %r13b, %r13b
sete %al
andb %r15b, %al
movzbl %al, %eax
addl %eax, %r12d
incl %r12d
cmpl %ebx, %r12d
jl 0x1617
subl %r12d, %ebx
jne 0x16bc
movq 0x5921(%rip), %rax      # 0x6fd8
movq (%rax), %r15
jmp 0x16e0
cmpl $0x2, %ebx
je 0x16ec
cmpl $0x1, %ebx
jne 0x1916
movslq %r12d, %rax
movq (%r14,%rax,8), %rdi
leaq 0x2f6d(%rip), %rsi      # 0x4645
callq 0x19c6
movq %rax, %r15
movq 0x58e9(%rip), %rax      # 0x6fd0
movq (%rax), %rax
jmp 0x1713
movslq %r12d, %rbx
movq (%r14,%rbx,8), %rdi
leaq 0x2f4b(%rip), %rsi      # 0x4645
callq 0x19c6
movq %rax, %r15
movq 0x8(%r14,%rbx,8), %rdi
leaq 0x2907(%rip), %rsi      # 0x4015
callq 0x19c6
movq %rax, -0x38(%rbp)
movq %r15, -0x40(%rbp)
movq %r15, %rdi
xorl %esi, %esi
callq 0x1f32
movq %rax, %r15
movq 0x18(%rax), %rax
movq (%rax), %rcx
movl (%rcx), %r12d
movslq 0xc(%r15), %rbx
testq %rbx, %rbx
jle 0x1770
movl (%r15), %ecx
xorl %edx, %edx
movl %r12d, %r13d
testl %ecx, %ecx
jle 0x1766
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 0x1749
incq %rdx
cmpq %rbx, %rdx
jne 0x1743
jmp 0x1775
xorl %ebx, %ebx
movl %r12d, %r13d
movl %r12d, %r14d
negl %r14d
movl 0x8(%r15), %edi
decl %edi
movsd 0x287f(%rip), %xmm0     # 0x4008
callq 0x1050
cmpl %r14d, %r13d
cmovgl %r13d, %r14d
cvtsi2sd %r14d, %xmm1
divsd %xmm1, %xmm0
mulsd 0x59a2(%rip), %xmm0     # 0x7148
movsd %xmm0, -0x30(%rbp)
movq 0x5846(%rip), %r14      # 0x6ff8
movq (%r14), %rdi
leaq 0x285b(%rip), %rsi      # 0x4017
movl %r13d, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0x2867(%rip), %rsi      # 0x4037
movl %r12d, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0x2874(%rip), %rsi      # 0x4058
movsd -0x30(%rbp), %xmm0
movb $0x1, %al
callq 0x1140
cmpb $0x1, 0x596d(%rip)      # 0x7164
je 0x190f
movl %ebx, 0xc(%r15)
testl %ebx, %ebx
movsd -0x30(%rbp), %xmm1
jle 0x186a
xorl %ebx, %ebx
cmpl $0x0, (%r15)
jle 0x185e
xorl %r12d, %r12d
movq 0x18(%r15), %rax
movq (%rax,%r12,8), %rcx
xorps %xmm0, %xmm0
cvtsi2sdl (%rcx,%rbx,4), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, (%rcx,%rbx,4)
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x184b
cmpl $0x80000001, %eax       # imm = 0x80000001
jle 0x1852
incq %r12
movslq (%r15), %rax
cmpq %rax, %r12
jl 0x1815
jmp 0x185e
callq 0x128f
jmp 0x1857
callq 0x1260
movsd -0x30(%rbp), %xmm1
jmp 0x183e
incq %rbx
movslq 0xc(%r15), %rax
cmpq %rax, %rbx
jl 0x180c
xorl %eax, %eax
cmpl $0x8, 0x8(%r15)
movl $0x7f, %r8d
cmovnel %eax, %r8d
leaq 0x2819(%rip), %rdx      # 0x409b
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x2a0d
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 0x587c(%rip), %rsi      # 0x7138
movq 0x587d(%rip), %rdx      # 0x7140
leaq 0x27e7(%rip), %rdi      # 0x40b1
xorl %eax, %eax
callq 0x10b0
jmp 0x190f
leaq 0x283d(%rip), %rdi      # 0x4117
callq 0x1070
leaq 0x2844(%rip), %rdi      # 0x412a
callq 0x1070
leaq 0x285b(%rip), %rdi      # 0x414d
callq 0x1070
leaq 0x2875(%rip), %rdi      # 0x4173
callq 0x1070
leaq 0x289b(%rip), %rdi      # 0x41a5
callq 0x1070
xorl %edi, %edi
callq 0x1200
leaq 0x27bd(%rip), %rdi      # 0x40da
callq 0x1070
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1200