// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %rbx
movl %edi, %r14d
movl $0x1, %r12d
cmpl $0x2, %edi
jl 0x16a6
movl $0x1, %r12d
movslq %r12d, %rax
movq (%rbx,%rax,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x16a6
movb 0x2(%rsi), %r13b
testb %r13b, %r13b
je 0x1617
leaq 0x2(%rsi), %rdi
jmp 0x161c
movq 0x8(%rbx,%rax,8), %rdi
movzbl 0x1(%rsi), %eax
cmpl $0x67, %eax
jg 0x163c
cmpl $0x63, %eax
je 0x1651
cmpl $0x64, %eax
jne 0x167a
callq 0x1200
movl %eax, 0x5b1a(%rip)      # 0x7154
jmp 0x164c
cmpl $0x73, %eax
jne 0x1668
callq 0x1200
movl %eax, 0x5b04(%rip)      # 0x7150
movb $0x1, %r15b
jmp 0x168b
xorl %eax, %eax
cmpl $0x0, 0x5b1a(%rip)      # 0x7174
sete %al
movl %eax, 0x5b11(%rip)      # 0x7174
xorl %r15d, %r15d
jmp 0x168b
cmpl $0x68, %eax
je 0x1bde
cmpl $0x76, %eax
je 0x1c5b
xorl %r15d, %r15d
leaq 0x2a7e(%rip), %rdi      # 0x4102
xorl %eax, %eax
callq 0x1c94
testb %r13b, %r13b
sete %al
andb %r15b, %al
movzbl %al, %eax
addl %eax, %r12d
incl %r12d
cmpl %r14d, %r12d
jl 0x15f8
subl %r12d, %r14d
jne 0x16b7
movq 0x5926(%rip), %rax      # 0x6fd8
movq (%rax), %r12
jmp 0x16dd
cmpl $0x2, %r14d
je 0x16e9
cmpl $0x1, %r14d
jne 0x1c7e
movslq %r12d, %rax
movq (%rbx,%rax,8), %rdi
leaq 0x2ffd(%rip), %rsi      # 0x46d2
callq 0x1d2e
movq %rax, %r12
movq 0x58ec(%rip), %rax      # 0x6fd0
movq (%rax), %rax
jmp 0x1710
movslq %r12d, %r14
movq (%rbx,%r14,8), %rdi
leaq 0x2fdb(%rip), %rsi      # 0x46d2
callq 0x1d2e
movq %rax, %r12
movq 0x8(%rbx,%r14,8), %rdi
leaq 0x2950(%rip), %rsi      # 0x405b
callq 0x1d2e
movq %rax, -0x48(%rbp)
xorl %r15d, %r15d
movq %r12, %rdi
xorl %esi, %esi
callq 0x2296
movq %rax, %r14
movq %r12, %rdi
callq 0x1090
movl 0x8(%r14), %edi
decl %edi
movsd 0x28ce(%rip), %xmm0     # 0x4008
callq 0x1050
xorpd 0x28c9(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %eax
movl %eax, 0x5a1b(%rip)      # 0x716c
movl 0x8(%r14), %edi
decl %edi
movsd 0x28a9(%rip), %xmm0     # 0x4008
callq 0x1050
cvttsd2si %xmm0, %eax
decl %eax
movl %eax, 0x5a00(%rip)      # 0x7170
cmpl $0x0, (%r14)
jle 0x1b56
movq $0x0, -0x50(%rbp)
movl 0x59e4(%rip), %ecx      # 0x716c
movq %rcx, -0x30(%rbp)
xorl %r15d, %r15d
movq %r14, -0x58(%rbp)
xorl %ebx, %ebx
movl 0x59d9(%rip), %ecx      # 0x7174
movl %ecx, -0x6c(%rbp)
movq -0x30(%rbp), %rcx
addl %ecx, %ecx
movl %ecx, -0x68(%rbp)
leal (%rax,%rax), %ecx
movl %ecx, -0x64(%rbp)
movl 0x59a1(%rip), %ecx      # 0x7154
movl %ecx, -0x78(%rbp)
movl 0x5994(%rip), %ecx      # 0x7150
movl %ecx, -0x40(%rbp)
cmpl $0x0, -0x30(%rbp)
sets %dl
testl %eax, %eax
setg %dil
movslq 0xc(%r14), %r10
cmpl %r10d, %ebx
setl %cl
andb %dl, %dil
andb %cl, %dil
cmpb $0x1, %dil
jne 0x1811
movq 0x18(%r14), %rcx
movq -0x50(%rbp), %rdx
movq (%rcx,%rdx,8), %rdx
movslq %ebx, %rdi
incq %rdi
incl %ebx
cmpq %r10, %rdi
setl %cl
jge 0x1811
movl -0x4(%rdx,%rdi,4), %r8d
cmpl %eax, %r8d
jge 0x1811
incq %rdi
cmpl -0x30(%rbp), %r8d
jg 0x17f4
cmpl %r10d, %ebx
je 0x1b3f
movslq %ebx, %r8
movq 0x18(%r14), %rdx
movq -0x50(%rbp), %rdi
movq (%rdx,%rdi,8), %rsi
movq %r8, %rdx
movq %rsi, -0x38(%rbp)
movl -0x4(%rsi,%r8,4), %edi
xorl %r11d, %r11d
cmpl %r10d, %ebx
movl %edi, -0x3c(%rbp)
cmovgl %r11d, %edi
movl $0x0, %r8d
testb %cl, %cl
je 0x1854
movq -0x38(%rbp), %rcx
movl (%rcx,%rdx,4), %r8d
movq %r15, -0x60(%rbp)
leal 0x1(%rbx), %ecx
cmpl %r10d, %ecx
jge 0x186b
movslq %ecx, %rcx
movq -0x38(%rbp), %r9
movl (%r9,%rcx,4), %r11d
leal 0x2(%rbx), %ecx
xorl %r15d, %r15d
movl $0x0, %r14d
cmpl %r10d, %ecx
jge 0x1887
movslq %ecx, %rcx
movq -0x38(%rbp), %r9
movl (%r9,%rcx,4), %r14d
movq %rbx, %rsi
leal 0x3(%rbx), %ecx
cmpl %r10d, %ecx
jge 0x189d
movslq %ecx, %rcx
movq -0x38(%rbp), %r9
movl (%r9,%rcx,4), %r15d
decq %rdx
movq %rdx, %rbx
movq -0x38(%rbp), %rcx
leaq (%rcx,%rdx,4), %r13
addq $0x14, %r13
xorl %r12d, %r12d
xorl %r9d, %r9d
cmpl %edi, %eax
jle 0x18ea
cmpl %edi, -0x30(%rbp)
jge 0x18ea
cmpl %r8d, %eax
jle 0x18ea
cmpl %r8d, -0x30(%rbp)
jge 0x18ea
cmpl %r11d, %eax
jle 0x18ea
cmpl %r11d, -0x30(%rbp)
jge 0x18ea
cmpl %r14d, %eax
jle 0x18ea
cmpl %r14d, -0x30(%rbp)
jge 0x18ea
cmpl %r15d, %eax
jle 0x18ea
cmpl %r15d, -0x30(%rbp)
jl 0x1916
leaq (%rbx,%r9), %rdx
addq $0x5, %rdx
xorl %ecx, %ecx
cmpq %r10, %rdx
jge 0x18fe
movl (%r13,%r9,4), %ecx
incq %r9
addl -0x78(%rbp), %r12d
movl %r8d, %edi
movl %r11d, %r8d
movl %r14d, %r11d
movl %r15d, %r14d
movl %ecx, %r15d
jmp 0x18b5
movq -0x60(%rbp), %r15
incl %r15d
cmpl $0x0, -0x6c(%rbp)
jne 0x1ad2
movl %ebx, %ecx
addq %r9, %rcx
movq %rsi, %r11
movq %rcx, -0x80(%rbp)
cmpl %ecx, %r11d
movl -0x68(%rbp), %r8d
movl -0x64(%rbp), %r10d
jg 0x196d
movq -0x38(%rbp), %rcx
leaq (%rcx,%rbx,4), %rcx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edx
cmpl %eax, %edx
jle 0x1958
movl %r10d, %edi
subl %edx, %edi
jmp 0x1962
cmpl -0x30(%rbp), %edx
jge 0x1965
addl %r8d, %edx
movl %edx, %edi
movl %edi, (%rcx,%rsi,4)
incq %rsi
cmpl %esi, %r9d
jne 0x194a
leal (%r9,%r11,2), %ecx
leal (%r9,%r11,2), %edx
addl $-0x2, %edx
shrl $0x1f, %edx
addl %edx, %ecx
addl $-0x2, %ecx
sarl %ecx
movl %ecx, -0x3c(%rbp)
movslq %ecx, %rdx
movq -0x38(%rbp), %rdi
movl (%rdi,%rdx,4), %ecx
testl %ecx, %ecx
jle 0x19a2
subl %r12d, %ecx
movl %ecx, (%rdi,%rdx,4)
cmpl %eax, %ecx
jle 0x19b1
jmp 0x1ba0
addl %r12d, %ecx
movl %ecx, (%rdi,%rdx,4)
cmpl -0x30(%rbp), %ecx
jl 0x1bbf
movq %r15, -0x60(%rbp)
cmpl $0x0, -0x40(%rbp)
jle 0x1ac1
addq %rbx, %r9
cmpl $0xa, %ebx
movl $0x9, %ecx
cmovll %ecx, %ebx
leal -0x9(%rbx), %edx
movl %edx, -0x70(%rbp)
movl %r9d, %r9d
leaq (%rdi,%rbx,4), %rdx
addq $-0x24, %rdx
movq %rdx, -0x90(%rbp)
addq $-0x9, %rbx
movq %rbx, -0x98(%rbp)
xorl %edx, %edx
cmpl $0x2, %ecx
movl $0x1, %r13d
movl %ecx, -0x74(%rbp)
cmovgel %ecx, %r13d
incl %edx
movq %rdx, -0x88(%rbp)
movl -0x70(%rbp), %ecx
cmpl -0x80(%rbp), %ecx
jge 0x1aab
movq -0x88(%rbp), %rcx
leal (%rcx,%rcx,8), %r11d
movl %r11d, %r8d
shrl $0x1f, %r8d
addl %r11d, %r8d
sarl %r8d
movslq %r8d, %rsi
leaq (%rdi,%r8,4), %r14
movq -0x90(%rbp), %r12
movq -0x98(%rbp), %rcx
leaq (%rcx,%rsi), %r10
movl -0x3c(%rbp), %edx
subl %r10d, %edx
movl %edx, %edi
negl %edi
cmovsl %edx, %edi
leal 0x1(%rdi), %ebx
xorpd %xmm0, %xmm0
xorl %edx, %edx
cmpq %rdx, %r8
jne 0x1a6d
movl (%r14,%rcx,4), %r15d
imull %ebx, %r15d
jmp 0x1a71
movl (%r12,%rdx,4), %r15d
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
addsd %xmm1, %xmm0
incq %rdx
cmpq %rdx, %r13
jne 0x1a5e
addl %r11d, %edi
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %edx
movq -0x38(%rbp), %rdi
movl %edx, (%rdi,%r10,4)
incq %rcx
addq $0x4, %r12
cmpq %r9, %rcx
jne 0x1a44
movl -0x74(%rbp), %ecx
addl $0x9, %ecx
movq -0x88(%rbp), %rdx
cmpl -0x40(%rbp), %edx
jne 0x19f2
movq -0x80(%rbp), %rbx
movq -0x60(%rbp), %r15
movq -0x58(%rbp), %r14
jmp 0x17bf
movl -0x3c(%rbp), %eax
cmpl -0x30(%rbp), %eax
leaq 0x257e(%rip), %rdx      # 0x405d
leaq 0x2573(%rip), %rax      # 0x4059
cmoveq %rax, %rdx
movq %rbx, %rcx
cvtsi2sd %ecx, %xmm0
movq 0x5500(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x58(%rbp), %rax
cvtsi2sdl 0x4(%rax), %xmm2
divsd %xmm2, %xmm0
leal -0x1(%rsi,%r9), %ebx
cvtsi2sd %ebx, %xmm1
divsd %xmm2, %xmm1
leaq 0x2509(%rip), %rsi      # 0x4025
movl %ebx, %r8d
movb $0x2, %al
callq 0x1150
movq -0x58(%rbp), %r14
movl 0x5640(%rip), %eax      # 0x7170
movl 0x5636(%rip), %ecx      # 0x716c
movq %rcx, -0x30(%rbp)
jmp 0x1795
movq -0x50(%rbp), %rdi
incq %rdi
movslq (%r14), %rcx
movq %rdi, -0x50(%rbp)
cmpq %rcx, %rdi
jl 0x1793
cmpl $0x0, 0x5617(%rip)      # 0x7174
jne 0x1b85
xorl %eax, %eax
cmpl $0x8, 0x8(%r14)
movl $0x7f, %r8d
cmovnel %eax, %r8d
leaq 0x24eb(%rip), %rdx      # 0x4062
movq -0x48(%rbp), %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2dc8
movq -0x48(%rbp), %rdi
callq 0x1090
movl %r15d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x24cf(%rip), %rdi      # 0x4076
leaq 0x24e5(%rip), %rsi      # 0x4093
leaq 0x24e8(%rip), %rcx      # 0x409d
movl $0x21, %edx
callq 0x10d0
leaq 0x24f6(%rip), %rdi      # 0x40bc
leaq 0x24c6(%rip), %rsi      # 0x4093
leaq 0x24c9(%rip), %rcx      # 0x409d
movl $0x27, %edx
callq 0x10d0
leaq 0x2572(%rip), %rdi      # 0x4157
callq 0x1070
leaq 0x2577(%rip), %rdi      # 0x4168
callq 0x1070
leaq 0x2596(%rip), %rdi      # 0x4193
callq 0x1070
leaq 0x25ae(%rip), %rdi      # 0x41b7
callq 0x1070
leaq 0x25d7(%rip), %rdi      # 0x41ec
callq 0x1070
movl 0x5534(%rip), %esi      # 0x7154
leaq 0x24b2(%rip), %rbx      # 0x40d9
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
leaq 0x25d7(%rip), %rdi      # 0x420f
callq 0x1070
leaq 0x25db(%rip), %rdi      # 0x421f
callq 0x1070
movl 0x5501(%rip), %esi      # 0x7150
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
jmp 0x1c77
movq 0x54de(%rip), %rsi      # 0x7140
movq 0x54df(%rip), %rdx      # 0x7148
leaq 0x2483(%rip), %rdi      # 0x40f3
xorl %eax, %eax
callq 0x10b0
xorl %edi, %edi
callq 0x1210
leaq 0x2497(%rip), %rdi      # 0x411c
callq 0x1070
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1210