// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %r12
movslq %r12d, %r15
cmpl %r12d, %r14d
jle 0x1425
movq (%rbx,%r15,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x1425
movb 0x2(%rsi), %r13b
testb %r13b, %r13b
je 0x139f
leaq 0x2(%rsi), %rdi
jmp 0x13a4
movq 0x8(%rbx,%r15,8), %rdi
movzbl 0x1(%rsi), %eax
cmpl $0x63, %eax
je 0x13e9
cmpl $0x64, %eax
je 0x13dc
cmpl $0x73, %eax
je 0x1400
cmpl $0x68, %eax
je 0x18ee
cmpl $0x76, %eax
je 0x196b
xorl %r15d, %r15d
leaq 0x2d2f(%rip), %rdi      # 0x4102
xorl %eax, %eax
callq 0x19a4
jmp 0x140e
callq 0x1200
movl %eax, 0x5d6d(%rip)      # 0x7154
jmp 0x140b
xorl %eax, %eax
cmpl $0x0, 0x5d82(%rip)      # 0x7174
sete %al
movl %eax, 0x5d79(%rip)      # 0x7174
xorl %r15d, %r15d
jmp 0x140e
callq 0x1200
movl %eax, 0x5d45(%rip)      # 0x7150
movb $0x1, %r15b
testb %r13b, %r13b
sete %al
andb %r15b, %al
movzbl %al, %eax
addl %eax, %r12d
incl %r12d
jmp 0x1377
subl %r12d, %r14d
jne 0x1436
movq 0x5ba7(%rip), %rax      # 0x6fd8
movq (%rax), %r14
jmp 0x1459
cmpl $0x2, %r14d
je 0x1465
cmpl $0x1, %r14d
jne 0x198e
movq (%rbx,%r15,8), %rdi
leaq 0x3281(%rip), %rsi      # 0x46d2
callq 0x1ad4
movq %rax, %r14
movq 0x5b70(%rip), %rax      # 0x6fd0
movq (%rax), %rax
jmp 0x1489
movq (%rbx,%r15,8), %rdi
leaq 0x3262(%rip), %rsi      # 0x46d2
callq 0x1ad4
movq %rax, %r14
movq 0x8(%rbx,%r15,8), %rdi
leaq 0x2bd7(%rip), %rsi      # 0x405b
callq 0x1ad4
movq %rax, -0x48(%rbp)
movq %r14, %rdi
xorl %esi, %esi
callq 0x1fa1
movq %rax, %r15
movq %r14, %rdi
callq 0x1090
movl 0x8(%r15), %edi
decl %edi
movsd 0x2b58(%rip), %xmm0     # 0x4008
callq 0x1050
xorpd 0x2b53(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %eax
movl %eax, 0x5ca5(%rip)      # 0x716c
movl 0x8(%r15), %edi
decl %edi
movsd 0x2b33(%rip), %xmm0     # 0x4008
callq 0x1050
cvttsd2si %xmm0, %ecx
decl %ecx
movq %rcx, -0x30(%rbp)
movl %ecx, 0x5c86(%rip)      # 0x7170
xorl %edi, %edi
movl $0x0, -0x3c(%rbp)
movq %r15, -0x88(%rbp)
movslq (%r15), %rax
cmpq %rax, %rdi
jge 0x1870
xorl %esi, %esi
movq %rdi, -0x90(%rbp)
movl 0x5c57(%rip), %eax      # 0x716c
movl 0x5c59(%rip), %ecx      # 0x7174
movl %ecx, -0x6c(%rbp)
movq %rax, -0x38(%rbp)
addl %eax, %eax
movl %eax, -0x68(%rbp)
movq -0x30(%rbp), %rax
addl %eax, %eax
movl %eax, -0x64(%rbp)
movl 0x5c1e(%rip), %eax      # 0x7154
movl %eax, -0x7c(%rbp)
movl 0x5c11(%rip), %ecx      # 0x7150
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
movl %ecx, -0x74(%rbp)
movslq 0xc(%r15), %rdx
movslq %esi, %rcx
leal -0x1(%rcx), %r14d
leal -0x2(,%rcx,2), %eax
xorl %esi, %esi
cmpq %rdx, %rcx
jge 0x1585
cmpl -0x30(%rbp), %esi
jge 0x1585
cmpl -0x38(%rbp), %esi
jle 0x1585
movq 0x18(%r15), %rsi
movq (%rsi,%rdi,8), %rsi
movl (%rsi,%rcx,4), %esi
incq %rcx
incl %r14d
addl $0x2, %eax
jmp 0x1560
cmpl %edx, %ecx
je 0x1868
movq 0x18(%r15), %rsi
movq (%rsi,%rdi,8), %r10
movslq %ecx, %rsi
movl -0x4(%r10,%rsi,4), %edi
xorl %ebx, %ebx
cmpl %ecx, %edx
movl %edi, -0x58(%rbp)
cmovll %ebx, %edi
movl $0x0, %r13d
cmpq %rdx, %rcx
jge 0x15b9
movslq %ecx, %rsi
movl (%r10,%rsi,4), %r13d
leal 0x1(%rcx), %esi
cmpl %edx, %esi
jge 0x15c7
movslq %esi, %rsi
movl (%r10,%rsi,4), %ebx
leal 0x2(%rcx), %r8d
xorl %r12d, %r12d
movl $0x0, %esi
cmpl %edx, %r8d
jge 0x15df
movslq %r8d, %rsi
movl (%r10,%rsi,4), %esi
leal 0x3(%rcx), %r8d
cmpl %edx, %r8d
jge 0x15ef
movslq %r8d, %r8
movl (%r10,%r8,4), %r12d
decl %ecx
movq %rcx, -0x50(%rbp)
movslq %r14d, %rcx
movq %rcx, %r15
shlq $0x20, %r15
movq %rcx, -0x60(%rbp)
addq $0x5, %rcx
xorl %r11d, %r11d
xorl %r9d, %r9d
cmpl %edi, -0x30(%rbp)
jle 0x1643
cmpl %edi, -0x38(%rbp)
jge 0x1643
cmpl %r13d, -0x30(%rbp)
jle 0x1643
cmpl %r13d, -0x38(%rbp)
jge 0x1643
cmpl %ebx, -0x30(%rbp)
jle 0x1643
cmpl %ebx, -0x38(%rbp)
jge 0x1643
cmpl %esi, -0x30(%rbp)
jle 0x1643
cmpl %esi, -0x38(%rbp)
jge 0x1643
cmpl %r12d, -0x30(%rbp)
jle 0x1643
cmpl %r12d, -0x38(%rbp)
jl 0x1678
xorl %r8d, %r8d
cmpq %rdx, %rcx
jge 0x164f
movl (%r10,%rcx,4), %r8d
movabsq $0x100000000, %rdi      # imm = 0x100000000
addq %rdi, %r15
decl %r9d
addl -0x7c(%rbp), %r11d
incl %eax
incq %rcx
movl %r13d, %edi
movl %ebx, %r13d
movl %esi, %ebx
movl %r12d, %esi
movl %r8d, %r12d
jmp 0x160d
incl -0x3c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jne 0x17f4
movl %r14d, %r8d
subl %r9d, %r8d
sarq $0x20, %r15
movl -0x68(%rbp), %esi
movl -0x64(%rbp), %edi
movq -0x60(%rbp), %r9
cmpq %r15, %r9
jge 0x16bf
movl (%r10,%r9,4), %ecx
cmpl -0x30(%rbp), %ecx
jle 0x16ad
movl %edi, %edx
subl %ecx, %edx
jmp 0x16b6
cmpl -0x38(%rbp), %ecx
jge 0x16ba
addl %esi, %ecx
movl %ecx, %edx
movl %edx, (%r10,%r9,4)
incq %r9
jmp 0x1699
cltd
pushq $0x2
popq %rcx
idivl %ecx
movl %eax, -0x60(%rbp)
movslq %eax, %rcx
movl (%r10,%rcx,4), %eax
testl %eax, %eax
jle 0x16e4
subl %r11d, %eax
movl %eax, (%r10,%rcx,4)
cmpl -0x30(%rbp), %eax
jle 0x16f4
jmp 0x18b8
addl %r11d, %eax
movl %eax, (%r10,%rcx,4)
cmpl -0x38(%rbp), %eax
jl 0x18d1
movl %r8d, -0x70(%rbp)
movq -0x50(%rbp), %rcx
cmpl $0xa, %ecx
pushq $0x9
popq %rax
cmovll %eax, %ecx
addq $-0x9, %rcx
movq %rcx, -0x50(%rbp)
cmpl $0xa, %r14d
cmovll %eax, %r14d
leaq (%r10,%r14,4), %rcx
addq $-0x24, %rcx
movq %rcx, -0x98(%rbp)
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %ebx
cmovgl %eax, %ebx
cmpl -0x74(%rbp), %ecx
je 0x17de
movl %eax, -0x78(%rbp)
incl %ecx
movq %rcx, -0x58(%rbp)
leal (%rcx,%rcx,8), %edi
movl %edi, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
movslq %eax, %rdx
leaq (%r10,%rax,4), %r12
movq -0x98(%rbp), %r13
movq -0x50(%rbp), %rcx
cmpq %r15, %rcx
jge 0x17cf
movq %r10, %rsi
leaq (%rcx,%rdx), %r9
movl -0x60(%rbp), %r8d
subl %r9d, %r8d
movl %r8d, %r11d
negl %r11d
cmovsl %r8d, %r11d
leal 0x1(%r11), %r10d
xorpd %xmm0, %xmm0
xorl %r8d, %r8d
cmpq %r8, %rbx
je 0x17ae
cmpq %r8, %rax
jne 0x179b
movl (%r12,%rcx,4), %r14d
imull %r10d, %r14d
jmp 0x17a0
movl (%r13,%r8,4), %r14d
cvtsi2sd %r14d, %xmm1
addsd %xmm1, %xmm0
incq %r8
jmp 0x1787
addl %edi, %r11d
cvtsi2sd %r11d, %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %r8d
movq %rsi, %r10
movl %r8d, (%rsi,%r9,4)
incq %rcx
addq $0x4, %r13
jmp 0x175f
movl -0x78(%rbp), %eax
addl $0x9, %eax
movq -0x58(%rbp), %rcx
jmp 0x1726
movq -0x88(%rbp), %r15
movq -0x90(%rbp), %rdi
movl -0x70(%rbp), %esi
jmp 0x154c
movq 0x57fd(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movl -0x58(%rbp), %eax
cmpl -0x38(%rbp), %eax
leaq 0x2852(%rip), %rdx      # 0x405d
leaq 0x2847(%rip), %rax      # 0x4059
cmoveq %rax, %rdx
movq -0x50(%rbp), %rcx
cvtsi2sd %ecx, %xmm0
movq -0x88(%rbp), %r15
cvtsi2sdl 0x4(%r15), %xmm2
divsd %xmm2, %xmm0
subl %r9d, %r14d
cvtsi2sd %r14d, %xmm1
divsd %xmm2, %xmm1
negl %r9d
leaq 0x27e0(%rip), %rsi      # 0x4025
movl %r14d, %r8d
movb $0x2, %al
callq 0x1150
movl 0x591b(%rip), %eax      # 0x7170
movq %rax, -0x30(%rbp)
movl %r14d, %esi
movq -0x90(%rbp), %rdi
jmp 0x150f
incq %rdi
jmp 0x14fa
cmpl $0x0, 0x58fd(%rip)      # 0x7174
jne 0x189d
xorl %eax, %eax
cmpl $0x8, 0x8(%r15)
pushq $0x7f
popq %r8
cmovnel %eax, %r8d
leaq 0x27d3(%rip), %rdx      # 0x4062
movq -0x48(%rbp), %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x2a14
movq -0x48(%rbp), %rdi
callq 0x1090
movl -0x3c(%rbp), %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x27b7(%rip), %rdi      # 0x4076
leaq 0x27cd(%rip), %rsi      # 0x4093
leaq 0x27d0(%rip), %rcx      # 0x409d
pushq $0x21
jmp 0x18e8
leaq 0x27e4(%rip), %rdi      # 0x40bc
leaq 0x27b4(%rip), %rsi      # 0x4093
leaq 0x27b7(%rip), %rcx      # 0x409d
pushq $0x27
popq %rdx
callq 0x10d0
leaq 0x2862(%rip), %rdi      # 0x4157
callq 0x1070
leaq 0x2867(%rip), %rdi      # 0x4168
callq 0x1070
leaq 0x2886(%rip), %rdi      # 0x4193
callq 0x1070
leaq 0x289e(%rip), %rdi      # 0x41b7
callq 0x1070
leaq 0x28c7(%rip), %rdi      # 0x41ec
callq 0x1070
movl 0x5824(%rip), %esi      # 0x7154
leaq 0x27a2(%rip), %rbx      # 0x40d9
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
leaq 0x28c7(%rip), %rdi      # 0x420f
callq 0x1070
leaq 0x28cb(%rip), %rdi      # 0x421f
callq 0x1070
movl 0x57f1(%rip), %esi      # 0x7150
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
jmp 0x1987
movq 0x57ce(%rip), %rsi      # 0x7140
movq 0x57cf(%rip), %rdx      # 0x7148
leaq 0x2773(%rip), %rdi      # 0x40f3
xorl %eax, %eax
callq 0x10b0
xorl %edi, %edi
callq 0x1210
leaq 0x2787(%rip), %rdi      # 0x411c
callq 0x1070
pushq $-0x1
popq %rdi
callq 0x1210
nop