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

    int i,j,k,clipped = 0;
    int start,finish;
    FILE *in,*out;
    RRA *r;

    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: fixclip [-h] [<input rra file> [<output rra file>]]\n");
        exit(-1);
        }

    r = readRRA(in,0);
    fclose(in);

    Min = (int) -pow(2,r->bitsPerSample-1);
    Max = (int) pow(2,r->bitsPerSample-1) - 1;

    //printf("Min is %d\n",Min);
    //printf("Max is %d\n",Max);

    for (j = 0; j < r->channels; ++j)
        {
        //find start of clip
        i = 0; 
        while (1)
            {
            int amp = 0;
            while (i < r->samples && amp < Max && amp > Min)
                amp = r->data[j][i++];

            if (i == r->samples) break;

            --i; //went one step too far

            start = i;
            //printf("start is %d, amp is %d\n",start,r->data[j][start]);

            //find end of clip
            int extreme = r->data[j][start];
            int amp0 = i+0 < r->samples? r->data[j][i+0] : 0;
            int amp1 = i+1 < r->samples? r->data[j][i+1] : 0;
            int amp2 = i+2 < r->samples? r->data[j][i+2] : 0;
            int amp3 = i+3 < r->samples? r->data[j][i+3] : 0;
            int amp4 = i+4 < r->samples? r->data[j][i+4] : 0;
            while (tooMuch(amp0) || tooMuch(amp1) || tooMuch(amp2)
            || tooMuch(amp3) || tooMuch(amp4))
                {
                //printf("sample %d: %d\n",i,amp0);
                amp0 = amp1;
                amp1 = amp2;
                amp2 = amp3;
                amp3 = amp4;
                ++i;
                amp4 = i+4 < r->samples? r->data[j][i+4] : 0;
                }
            //printf("next sample %d: %d\n",i,r->data[j][i]);

            finish = i;

            ++clipped;

            if (Checking)
                {
                fprintf(stderr,"clipped %s from %d (%.4f) to %d (%.4f), "
                    "%d samples\n",
                    extreme == Min? "low" : "high",
                    start,start / (double) r->sampleRate,
                    finish,finish / (double) r->sampleRate,
                    finish-start
                    );
                }
            else
                {
                /* invert any region above the clipping threshold */
                for (k = start; k < finish; ++k)
                   {
                   if (r->data[j][k] > Max)
                       r->data[j][k] = 2 * Max - r->data[j][k];
                   else if (r->data[j][k] < Min)
                       r->data[j][k] = 2 * Min + r->data[j][k];
                    }
                /* move the mid point of the clipping region down */
                dip(r,j,start,finish);
                /* now smooth out the changes */
                for (k = 0; k < Smoothings; ++k)
                    smooth(r,j,start,finish,k+1);
                }
            }
        }
                
    if (!Checking)
        writeRRA(out,r,"modifiedBy: fixclip",0,r->bitsPerSample == 8 ? 127:0);

    fclose(out);

    return clipped;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, -0x14(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x1970
movl %eax, -0x14(%rbp)
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x0, %eax
jne 0x1382
movq 0x6c70(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq 0x6c5a(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x141f
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x1, %eax
jne 0x13b9
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x4368(%rip), %rsi      # 0x5708
callq 0x2010
movq %rax, -0x38(%rbp)
movq 0x6c20(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x141d
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x2, %eax
jne 0x1403
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x4331(%rip), %rsi      # 0x5708
callq 0x2010
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x3cdf(%rip), %rsi      # 0x50d7
callq 0x2010
movq %rax, -0x40(%rbp)
jmp 0x141b
leaq 0x3c5b(%rip), %rdi      # 0x5065
movb $0x0, %al
callq 0x1090
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x11e0
jmp 0x141d
jmp 0x141f
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2980
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
callq 0x1070
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x3bb9(%rip), %xmm0     # 0x5008
callq 0x1060
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x6cd7(%rip)      # 0x814c
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x3b7d(%rip), %xmm0     # 0x5008
callq 0x1060
cvttsd2si %xmm0, %eax
subl $0x1, %eax
movl %eax, 0x6cb3(%rip)      # 0x8150
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1916
movl $0x0, -0x18(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x18(%rbp), %ecx
movq -0x48(%rbp), %rdx
xorl %eax, %eax
cmpl 0xc(%rdx), %ecx
movb %al, -0x65(%rbp)
jge 0x14f1
movl -0x4c(%rbp), %ecx
xorl %eax, %eax
cmpl 0x6c73(%rip), %ecx      # 0x8150
movb %al, -0x65(%rbp)
jge 0x14f1
movl -0x4c(%rbp), %eax
cmpl 0x6c61(%rip), %eax      # 0x814c
setg %al
movb %al, -0x65(%rbp)
movb -0x65(%rbp), %al
testb $0x1, %al
jne 0x14fa
jmp 0x1520
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x18(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x18(%rbp)
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
jmp 0x14c1
movl -0x18(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jne 0x1531
jmp 0x1906
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x50(%rbp)
movl -0x18(%rbp), %eax
addl $0x0, %eax
movq -0x48(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x158a
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x18(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x6c(%rbp)
jmp 0x1591
xorl %eax, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1591
movl -0x6c(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movq -0x48(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x15c7
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x70(%rbp)
jmp 0x15ce
xorl %eax, %eax
movl %eax, -0x70(%rbp)
jmp 0x15ce
movl -0x70(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x18(%rbp), %eax
addl $0x2, %eax
movq -0x48(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x1604
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x18(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x74(%rbp)
jmp 0x160b
xorl %eax, %eax
movl %eax, -0x74(%rbp)
jmp 0x160b
movl -0x74(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x18(%rbp), %eax
addl $0x3, %eax
movq -0x48(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x1641
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x18(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x78(%rbp)
jmp 0x1648
xorl %eax, %eax
movl %eax, -0x78(%rbp)
jmp 0x1648
movl -0x78(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x18(%rbp), %eax
addl $0x4, %eax
movq -0x48(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x167e
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x18(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x7c(%rbp)
jmp 0x1685
xorl %eax, %eax
movl %eax, -0x7c(%rbp)
jmp 0x1685
movl -0x7c(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x54(%rbp), %edi
callq 0x1b90
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x7d(%rbp)
jne 0x16ec
movl -0x58(%rbp), %edi
callq 0x1b90
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x7d(%rbp)
jne 0x16ec
movl -0x5c(%rbp), %edi
callq 0x1b90
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x7d(%rbp)
jne 0x16ec
movl -0x60(%rbp), %edi
callq 0x1b90
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x7d(%rbp)
jne 0x16ec
movl -0x64(%rbp), %edi
callq 0x1b90
cmpl $0x0, %eax
setne %al
movb %al, -0x7d(%rbp)
movb -0x7d(%rbp), %al
testb $0x1, %al
jne 0x16f5
jmp 0x1761
movl -0x58(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
addl $0x4, %eax
movq -0x48(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x1749
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x18(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x84(%rbp)
jmp 0x1753
xorl %eax, %eax
movl %eax, -0x84(%rbp)
jmp 0x1753
movl -0x84(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x168b
movl -0x18(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, 0x69dd(%rip)      # 0x8154
je 0x17e6
movq 0x6878(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl -0x50(%rbp), %ecx
movl 0x69c0(%rip), %esi      # 0x814c
leaq 0x3946(%rip), %rdx      # 0x50d9
leaq 0x393b(%rip), %rax      # 0x50d5
cmpl %esi, %ecx
cmoveq %rax, %rdx
movl -0x28(%rbp), %ecx
cvtsi2sdl -0x28(%rbp), %xmm0
movq -0x48(%rbp), %rax
cvtsi2sdl 0x4(%rax), %xmm1
divsd %xmm1, %xmm0
movl -0x2c(%rbp), %r8d
cvtsi2sdl -0x2c(%rbp), %xmm1
movq -0x48(%rbp), %rax
cvtsi2sdl 0x4(%rax), %xmm2
divsd %xmm2, %xmm1
movl -0x2c(%rbp), %r9d
subl -0x28(%rbp), %r9d
leaq 0x38c7(%rip), %rsi      # 0x50a1
movb $0x2, %al
callq 0x1120
jmp 0x1901
movl -0x28(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x18b6
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl 0x693b(%rip), %eax      # 0x8150
jle 0x184f
movl 0x6933(%rip), %edx      # 0x8150
shll %edx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
subl (%rax,%rcx,4), %edx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x18a6
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl 0x68e0(%rip), %eax      # 0x814c
jge 0x18a4
movl 0x68d8(%rip), %edx      # 0x814c
shll %edx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x18a6
jmp 0x18a8
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x17ec
movq -0x48(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
callq 0x1bc0
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl 0x6858(%rip), %eax      # 0x8130
jge 0x18ff
movq -0x48(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl -0x20(%rbp), %r8d
addl $0x1, %r8d
callq 0x1d10
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x18cf
jmp 0x1901
jmp 0x14ba
jmp 0x1908
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x14a4
cmpl $0x0, 0x6837(%rip)      # 0x8154
jne 0x194d
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
xorl %r8d, %r8d
movl $0x7f, %eax
cmpl $0x8, %ecx
cmovel %eax, %r8d
leaq 0x379a(%rip), %rdx      # 0x50de
xorl %eax, %eax
movl %eax, %ecx
callq 0x38b0
movq -0x40(%rbp), %rdi
callq 0x1070
movl -0x24(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)