// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, -0x14(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x16d0
movl %eax, -0x14(%rbp)
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x0, %eax
jne 0x1368
movq 0x6c8a(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq 0x6c74(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x1405
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x1, %eax
jne 0x139f
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x42fa(%rip), %rsi      # 0x5680
callq 0x19e0
movq %rax, -0x40(%rbp)
movq 0x6c3a(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x1403
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x2, %eax
jne 0x13e9
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x42c3(%rip), %rsi      # 0x5680
callq 0x19e0
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x3c8f(%rip), %rsi      # 0x506d
callq 0x19e0
movq %rax, -0x48(%rbp)
jmp 0x1401
leaq 0x3c7f(%rip), %rdi      # 0x506f
movb $0x0, %al
callq 0x1090
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x11d0
jmp 0x1403
jmp 0x1405
movq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2350
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x14b9
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x14a5
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jle 0x148a
movl -0x20(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x1498
movl -0x20(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x149a
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1457
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1444
movl -0x28(%rbp), %eax
xorl %ecx, %ecx
subl -0x2c(%rbp), %ecx
cmpl %ecx, %eax
jle 0x14f8
movq -0x50(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x3b2d(%rip), %xmm0     # 0x5008
callq 0x1060
cvtsi2sdl -0x28(%rbp), %xmm1
divsd %xmm1, %xmm0
mulsd 0x6c37(%rip), %xmm0     # 0x8128
movsd %xmm0, -0x38(%rbp)
jmp 0x152d
movq -0x50(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x3afa(%rip), %xmm0     # 0x5008
callq 0x1060
xorl %eax, %eax
subl -0x2c(%rbp), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
mulsd 0x6c00(%rip), %xmm0     # 0x8128
movsd %xmm0, -0x38(%rbp)
movq 0x6ac4(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl -0x28(%rbp), %edx
leaq 0x3b6c(%rip), %rsi      # 0x50ad
movb $0x0, %al
callq 0x1110
movq 0x6aa9(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl -0x2c(%rbp), %edx
leaq 0x3b71(%rip), %rsi      # 0x50cd
movb $0x0, %al
callq 0x1110
movq 0x6a8e(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movsd -0x38(%rbp), %xmm0
leaq 0x3b75(%rip), %rsi      # 0x50ee
movb $0x1, %al
callq 0x1110
cmpl $0x0, 0x6bbd(%rip)      # 0x8144
je 0x1590
xorl %edi, %edi
callq 0x11d0
movl -0x24(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0xc(%rax)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x167e
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x166e
movsd -0x38(%rbp), %xmm1
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %edx
movl %edx, (%rax,%rcx,4)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
cmpl $0x7ffffffe, (%rax,%rcx,4) # imm = 0x7FFFFFFE
jle 0x1627
movq 0x69e4(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
leaq 0x3ae8(%rip), %rsi      # 0x5106
movb $0x0, %al
callq 0x1110
jmp 0x165e
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
cmpl $0x80000002, (%rax,%rcx,4) # imm = 0x80000002
jge 0x165c
movq 0x69ad(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
leaq 0x3ac6(%rip), %rsi      # 0x511b
movb $0x0, %al
callq 0x1110
jmp 0x165e
jmp 0x1660
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x15b8
jmp 0x1670
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x15a1
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x50(%rbp), %rax
movl 0x8(%rax), %ecx
xorl %r8d, %r8d
movl $0x7f, %eax
cmpl $0x8, %ecx
cmovel %eax, %r8d
leaq 0x3a8e(%rip), %rdx      # 0x5131
xorl %eax, %eax
movl %eax, %ecx
callq 0x3280
movq -0x40(%rbp), %rdi
callq 0x1070
movq -0x48(%rbp), %rdi
callq 0x1070
xorl %eax, %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)