// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %r12
movslq %r12d, %r15
cmpl %r12d, %ebx
jle 0x13f5
movq (%r14,%r15,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x13f5
movb 0x2(%rsi), %r13b
testb %r13b, %r13b
je 0x138a
leaq 0x2(%rsi), %rdi
jmp 0x138f
movq 0x8(%r14,%r15,8), %rdi
movzbl 0x1(%rsi), %eax
cmpl $0x61, %eax
je 0x13c2
cmpl $0x73, %eax
je 0x13d4
cmpl $0x68, %eax
je 0x1611
cmpl $0x76, %eax
je 0x164f
xorl %r15d, %r15d
leaq 0x2d07(%rip), %rdi      # 0x40c0
xorl %eax, %eax
callq 0x1688
jmp 0x13de
callq 0x1080
movsd %xmm0, 0x5d79(%rip)     # 0x7148
movb $0x1, %r15b
jmp 0x13de
movb $0x1, 0x5d89(%rip)      # 0x7164
xorl %r15d, %r15d
testb %r13b, %r13b
sete %al
andb %r15b, %al
movzbl %al, %eax
addl %eax, %r12d
incl %r12d
jmp 0x1366
subl %r12d, %ebx
jne 0x1406
movq 0x5bd7(%rip), %rax      # 0x6fd8
movq (%rax), %r12
jmp 0x1427
cmpl $0x2, %ebx
je 0x1433
cmpl $0x1, %ebx
jne 0x1672
movq (%r14,%r15,8), %rdi
leaq 0x3223(%rip), %rsi      # 0x4642
callq 0x17b8
movq %rax, %r12
movq 0x5ba2(%rip), %rax      # 0x6fd0
movq (%rax), %rax
jmp 0x1457
movq (%r14,%r15,8), %rdi
leaq 0x3204(%rip), %rsi      # 0x4642
callq 0x17b8
movq %rax, %r12
movq 0x8(%r14,%r15,8), %rdi
leaq 0x2bc3(%rip), %rsi      # 0x4015
callq 0x17b8
movq %rax, -0x38(%rbp)
xorl %ebx, %ebx
movq %r12, -0x40(%rbp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x1c85
movq %rax, %r15
movq 0x18(%rax), %rax
movq (%rax), %rcx
movl (%rcx), %r13d
movl 0xc(%r15), %r14d
testl %r14d, %r14d
cmovlel %ebx, %r14d
xorl %ecx, %ecx
movl %r13d, %r12d
cmpq %r14, %rcx
je 0x14bb
movl (%r15), %edx
testl %edx, %edx
cmovlel %ebx, %edx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x14b6
movq (%rax,%rsi,8), %rdi
movl (%rdi,%rcx,4), %edi
cmpl %r13d, %edi
cmovgl %edi, %r13d
cmpl %r12d, %edi
cmovll %edi, %r12d
incq %rsi
jmp 0x1497
incq %rcx
jmp 0x1488
movl %r12d, %ebx
negl %ebx
movl 0x8(%r15), %edi
decl %edi
movsd 0x2b3a(%rip), %xmm0     # 0x4008
callq 0x1050
cmpl %ebx, %r13d
cmovgl %r13d, %ebx
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm0
mulsd 0x5c5e(%rip), %xmm0     # 0x7148
movsd %xmm0, -0x30(%rbp)
movq 0x5b02(%rip), %rbx      # 0x6ff8
movq (%rbx), %rdi
leaq 0x2b17(%rip), %rsi      # 0x4017
movl %r13d, %edx
xorl %eax, %eax
callq 0x1140
movq (%rbx), %rdi
leaq 0x2b23(%rip), %rsi      # 0x4037
movl %r12d, %edx
xorl %eax, %eax
callq 0x1140
movq %rbx, %r13
movq (%rbx), %rdi
leaq 0x2b2d(%rip), %rsi      # 0x4058
movsd -0x30(%rbp), %xmm0
movb $0x1, %al
callq 0x1140
cmpb $0x1, 0x5c26(%rip)      # 0x7164
je 0x166b
movl %r14d, 0xc(%r15)
xorl %r12d, %r12d
pushq $0x1
popq %rbx
movsd -0x30(%rbp), %xmm1
movslq %r14d, %rax
cmpq %rax, %r12
jge 0x15c8
xorl %r14d, %r14d
movslq (%r15), %rax
cmpq %rax, %r14
jge 0x15bf
movq 0x18(%r15), %rax
movq (%rax,%r14,8), %rcx
cvtsi2sdl (%rcx,%r12,4), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, (%rcx,%r12,4)
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x1593
cmpl $0x80000001, %eax       # imm = 0x80000001
jle 0x15a2
incq %r14
jmp 0x155e
movq (%r13), %rcx
leaq 0x2ad2(%rip), %rdi      # 0x4070
pushq $0x14
jmp 0x15af
movq (%r13), %rcx
leaq 0x2ad8(%rip), %rdi      # 0x4085
pushq $0x15
popq %rsi
movq %rbx, %rdx
callq 0x1210
movsd -0x30(%rbp), %xmm1
jmp 0x158e
incq %r12
movl 0xc(%r15), %r14d
jmp 0x1553
xorl %eax, %eax
cmpl $0x8, 0x8(%r15)
pushq $0x7f
popq %r8
cmovnel %eax, %r8d
leaq 0x2abd(%rip), %rdx      # 0x409b
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x26f8
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
leaq 0x2aff(%rip), %rdi      # 0x4117
callq 0x1070
leaq 0x2b06(%rip), %rdi      # 0x412a
callq 0x1070
leaq 0x2b1d(%rip), %rdi      # 0x414d
callq 0x1070
leaq 0x2b37(%rip), %rdi      # 0x4173
callq 0x1070
leaq 0x2b5d(%rip), %rdi      # 0x41a5
callq 0x1070
jmp 0x166b
movq 0x5ae2(%rip), %rsi      # 0x7138
movq 0x5ae3(%rip), %rdx      # 0x7140
leaq 0x2a4d(%rip), %rdi      # 0x40b1
xorl %eax, %eax
callq 0x10b0
xorl %edi, %edi
callq 0x1200
leaq 0x2a61(%rip), %rdi      # 0x40da
callq 0x1070
pushq $-0x1
popq %rdi
callq 0x1200
nop