// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rsi, %rbx
movl %edi, %r14d
movl $0x1, %r12d
cmpl $0x2, %edi
jl 0x1676
movl $0x1, %r12d
movslq %r12d, %rax
movq (%rbx,%rax,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x1676
movb 0x2(%rsi), %r13b
testb %r13b, %r13b
je 0x15e7
leaq 0x2(%rsi), %rdi
jmp 0x15ec
movq 0x8(%rbx,%rax,8), %rdi
movzbl 0x1(%rsi), %eax
cmpl $0x67, %eax
jg 0x160c
cmpl $0x63, %eax
je 0x1621
cmpl $0x64, %eax
jne 0x164a
callq 0x1200
movl %eax, 0x5b4a(%rip)      # 0x7154
jmp 0x161c
cmpl $0x73, %eax
jne 0x1638
callq 0x1200
movl %eax, 0x5b34(%rip)      # 0x7150
movb $0x1, %r15b
jmp 0x165b
xorl %eax, %eax
cmpl $0x0, 0x5b4a(%rip)      # 0x7174
sete %al
movl %eax, 0x5b41(%rip)      # 0x7174
xorl %r15d, %r15d
jmp 0x165b
cmpl $0x68, %eax
je 0x1bc8
cmpl $0x76, %eax
je 0x1c45
xorl %r15d, %r15d
leaq 0x2aae(%rip), %rdi      # 0x4102
xorl %eax, %eax
callq 0x1c80
testb %r13b, %r13b
sete %al
andb %r15b, %al
movzbl %al, %eax
addl %eax, %r12d
incl %r12d
cmpl %r14d, %r12d
jl 0x15c8
subl %r12d, %r14d
jne 0x1687
movq 0x5956(%rip), %rax      # 0x6fd8
movq (%rax), %r12
jmp 0x16ad
cmpl $0x2, %r14d
je 0x16b9
cmpl $0x1, %r14d
jne 0x1c68
movslq %r12d, %rax
movq (%rbx,%rax,8), %rdi
leaq 0x3030(%rip), %rsi      # 0x46d5
callq 0x1d1a
movq %rax, %r12
movq 0x591c(%rip), %rax      # 0x6fd0
movq (%rax), %rax
jmp 0x16e0
movslq %r12d, %r14
movq (%rbx,%r14,8), %rdi
leaq 0x300e(%rip), %rsi      # 0x46d5
callq 0x1d1a
movq %rax, %r12
movq 0x8(%rbx,%r14,8), %rdi
leaq 0x2980(%rip), %rsi      # 0x405b
callq 0x1d1a
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x2286
movq %rax, %rbx
movq %r12, %rdi
callq 0x1090
movl 0x8(%rbx), %edi
decl %edi
movsd 0x28fa(%rip), %xmm0     # 0x4008
callq 0x1050
xorpd 0x28f5(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %eax
movl %eax, 0x5a47(%rip)      # 0x716c
movl 0x8(%rbx), %edi
decl %edi
movsd 0x28d6(%rip), %xmm0     # 0x4008
callq 0x1050
movq %rbx, %rsi
cvttsd2si %xmm0, %eax
decl %eax
movl %eax, 0x5a2a(%rip)      # 0x7170
cmpl $0x0, (%rbx)
jle 0x1b43
movq $0x0, -0x38(%rbp)
movq $0x0, -0x50(%rbp)
movq %rsi, -0x68(%rbp)
xorl %ebx, %ebx
movslq 0xc(%rsi), %r14
movl 0x5a01(%rip), %edx      # 0x7170
movl 0x59f7(%rip), %eax      # 0x716c
cmpl %r14d, %ebx
setl %cl
testl %edx, %edx
setg %r8b
testl %eax, %eax
sets %dil
andb %cl, %dil
andb %r8b, %dil
cmpb $0x1, %dil
jne 0x17c1
movq 0x18(%rsi), %rcx
movq -0x38(%rbp), %rdi
movq (%rcx,%rdi,8), %r9
movslq %ebx, %rdi
incq %rdi
incl %ebx
cmpq %r14, %rdi
setl %cl
jge 0x17c1
movl -0x4(%r9,%rdi,4), %r8d
cmpl %edx, %r8d
jge 0x17c1
incq %rdi
cmpl %eax, %r8d
jg 0x17a5
cmpl %r14d, %ebx
jne 0x17ce
movl %ebx, %r12d
jmp 0x1ac8
movq 0x18(%rsi), %r8
movq -0x38(%rbp), %rdi
movq %r8, -0x58(%rbp)
movq (%r8,%rdi,8), %r8
movslq %ebx, %r9
movl -0x4(%r8,%r9,4), %r13d
xorl %r12d, %r12d
cmpl %r14d, %ebx
movl %r13d, -0x2c(%rbp)
cmovgl %r12d, %r13d
movl $0x0, %edi
testb %cl, %cl
je 0x1801
movl (%r8,%r9,4), %edi
leal 0x1(%rbx), %ecx
cmpl %r14d, %ecx
jge 0x1810
movslq %ecx, %rcx
movl (%r8,%rcx,4), %r12d
leal 0x2(%rbx), %ecx
xorl %r10d, %r10d
movl $0x0, %r11d
cmpl %r14d, %ecx
jge 0x1828
movslq %ecx, %rcx
movl (%r8,%rcx,4), %r11d
leal 0x3(%rbx), %ecx
movq %r14, -0x60(%rbp)
cmpl %r14d, %ecx
jge 0x183b
movslq %ecx, %rcx
movl (%r8,%rcx,4), %r10d
movq %rbx, -0x70(%rbp)
leal -0x1(%rbx), %ecx
movq %rcx, -0x40(%rbp)
movslq %ecx, %rcx
movq %rcx, %rsi
shlq $0x20, %rsi
movq %rcx, -0x78(%rbp)
leaq 0x5(%rcx), %r14
xorl %r15d, %r15d
xorl %r9d, %r9d
cmpl %r13d, %edx
setg %cl
cmpl %r13d, %eax
setl %bl
andb %cl, %bl
cmpb $0x1, %bl
jne 0x18b8
cmpl %edi, %edx
setg %cl
cmpl %edi, %eax
setl %bl
andb %cl, %bl
cmpb $0x1, %bl
jne 0x18b8
cmpl %r12d, %edx
setg %cl
cmpl %r12d, %eax
setl %bl
andb %cl, %bl
cmpb $0x1, %bl
jne 0x18b8
cmpl %r11d, %edx
setg %cl
cmpl %r11d, %eax
setl %bl
andb %cl, %bl
cmpb $0x1, %bl
jne 0x18b8
cmpl %r10d, %edx
setg %cl
cmpl %r10d, %eax
setl %bl
testb %bl, %cl
jne 0x18ee
xorl %ecx, %ecx
cmpq -0x60(%rbp), %r14
jge 0x18c4
movl (%r8,%r14,4), %ecx
decl %r9d
movabsq $0x100000000, %rbx      # imm = 0x100000000
addq %rbx, %rsi
incq %r14
incl %r15d
movl %edi, %r13d
movl %r12d, %edi
movl %r11d, %r12d
movl %r10d, %r11d
movl %ecx, %r10d
jmp 0x185e
movq -0x40(%rbp), %rcx
movl %ecx, %r12d
subl %r9d, %r12d
movq -0x50(%rbp), %rcx
incl %ecx
cmpl $0x0, 0x586f(%rip)      # 0x7174
movq %rcx, -0x50(%rbp)
jne 0x1ad6
movq -0x70(%rbp), %rbx
cmpl %r12d, %ebx
movq -0x60(%rbp), %r14
jg 0x1951
leal (%rax,%rax), %ecx
leal (%rdx,%rdx), %edi
movq -0x78(%rbp), %r10
leaq (%r8,%r10,4), %r8
movl (%r8), %r10d
cmpl %edx, %r10d
jle 0x193a
movl %edi, %r11d
subl %r10d, %r11d
jmp 0x1945
cmpl %eax, %r10d
jge 0x1948
addl %ecx, %r10d
movl %r10d, %r11d
movl %r11d, (%r8)
addq $0x4, %r8
decl %r15d
jne 0x192a
leal (%rbx,%rbx), %ecx
subl %r9d, %ecx
movl %ecx, %edi
addl $-0x2, %edi
shrl $0x1f, %edi
leal (%rcx,%rdi), %r8d
addl $-0x2, %r8d
sarl %r8d
movq -0x38(%rbp), %rcx
movq -0x58(%rbp), %rdi
movq (%rdi,%rcx,8), %rdi
movslq %r8d, %rcx
movl (%rdi,%rcx,4), %r10d
movl 0x57d0(%rip), %r11d     # 0x7154
testl %r10d, %r10d
jle 0x199e
imull %r9d, %r11d
addl %r10d, %r11d
movl %r11d, (%rdi,%rcx,4)
cmpl %edx, %r11d
jle 0x19b2
jmp 0x1b8a
imull %r11d, %r9d
subl %r9d, %r10d
movl %r10d, (%rdi,%rcx,4)
cmpl %eax, %r10d
jl 0x1ba9
movl 0x5798(%rip), %eax      # 0x7150
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jle 0x1ac4
movq -0x40(%rbp), %rax
cmpl $0xa, %eax
movl $0x9, %ecx
cmovll %ecx, %eax
leal -0x9(%rax), %edx
movl %edx, -0x7c(%rbp)
sarq $0x20, %rsi
leaq (%rdi,%rax,4), %rdx
addq $-0x24, %rdx
movq %rdx, -0x88(%rbp)
addq $-0x9, %rax
movq %rax, -0x40(%rbp)
xorl %eax, %eax
movl %r12d, -0x80(%rbp)
cmpl $0x2, %ecx
movl $0x1, %r14d
movl %ecx, -0x78(%rbp)
cmovgel %ecx, %r14d
incl %eax
movq %rax, -0x58(%rbp)
cmpl %r12d, -0x7c(%rbp)
jge 0x1a9f
movq -0x58(%rbp), %rax
leal (%rax,%rax,8), %r15d
movl %r15d, %r12d
shrl $0x1f, %r12d
addl %r15d, %r12d
sarl %r12d
movslq %r12d, %r10
movq -0x88(%rbp), %rcx
movq -0x40(%rbp), %r13
leaq (%r10,%r13), %rax
movl %r8d, %edx
subl %eax, %edx
movl %edx, %r9d
negl %r9d
cmovsl %edx, %r9d
leal 0x1(%r9), %r11d
xorpd %xmm0, %xmm0
xorl %ebx, %ebx
cmpq %rbx, %r12
jne 0x1a67
movl (%rcx,%rbx,4), %edx
imull %r11d, %edx
jmp 0x1a6a
movl (%rcx,%rbx,4), %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
addsd %xmm1, %xmm0
incq %rbx
cmpq %rbx, %r14
jne 0x1a59
addl %r15d, %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9d, %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %edx
movl %edx, (%rdi,%rax,4)
incq %r13
addq $0x4, %rcx
cmpq %rsi, %r13
jne 0x1a3c
movl -0x78(%rbp), %ecx
addl $0x9, %ecx
movq -0x58(%rbp), %rax
cmpl -0x2c(%rbp), %eax
movl -0x80(%rbp), %r12d
jne 0x19f9
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rbx
movq -0x60(%rbp), %r14
jmp 0x1ac8
movq -0x68(%rbp), %rsi
cmpl %r14d, %ebx
movl %r12d, %ebx
jne 0x1765
jmp 0x1b2c
cmpl %eax, -0x2c(%rbp)
leaq 0x257d(%rip), %rdx      # 0x405d
leaq 0x2572(%rip), %rax      # 0x4059
cmoveq %rax, %rdx
movq -0x40(%rbp), %rcx
cvtsi2sd %ecx, %xmm0
movq 0x54fe(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x68(%rbp), %rbx
cvtsi2sdl 0x4(%rbx), %xmm2
divsd %xmm2, %xmm0
cvtsi2sd %r12d, %xmm1
divsd %xmm2, %xmm1
negl %r9d
leaq 0x2508(%rip), %rsi      # 0x4025
movl %r12d, %r8d
movb $0x2, %al
callq 0x1150
movq %rbx, %rsi
jmp 0x1aba
movq -0x38(%rbp), %rdx
incq %rdx
movslq (%rsi), %rax
movq %rdx, -0x38(%rbp)
cmpq %rax, %rdx
jl 0x1763
cmpl $0x0, 0x562a(%rip)      # 0x7174
jne 0x1b6e
xorl %eax, %eax
cmpl $0x8, 0x8(%rsi)
movl $0x7f, %r8d
cmovnel %eax, %r8d
leaq 0x24ff(%rip), %rdx      # 0x4062
movq -0x48(%rbp), %rdi
xorl %ecx, %ecx
callq 0x2d61
movq -0x48(%rbp), %rdi
callq 0x1090
movq -0x50(%rbp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x24e5(%rip), %rdi      # 0x4076
leaq 0x24fb(%rip), %rsi      # 0x4093
leaq 0x24fe(%rip), %rcx      # 0x409d
movl $0x21, %edx
callq 0x10d0
leaq 0x250c(%rip), %rdi      # 0x40bc
leaq 0x24dc(%rip), %rsi      # 0x4093
leaq 0x24df(%rip), %rcx      # 0x409d
movl $0x27, %edx
callq 0x10d0
leaq 0x2588(%rip), %rdi      # 0x4157
callq 0x1070
leaq 0x258d(%rip), %rdi      # 0x4168
callq 0x1070
leaq 0x25ac(%rip), %rdi      # 0x4193
callq 0x1070
leaq 0x25c4(%rip), %rdi      # 0x41b7
callq 0x1070
leaq 0x25ed(%rip), %rdi      # 0x41ec
callq 0x1070
movl 0x554a(%rip), %esi      # 0x7154
leaq 0x24c8(%rip), %rbx      # 0x40d9
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
leaq 0x25ed(%rip), %rdi      # 0x420f
callq 0x1070
leaq 0x25f1(%rip), %rdi      # 0x421f
callq 0x1070
movl 0x5517(%rip), %esi      # 0x7150
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
jmp 0x1c61
movq 0x54f4(%rip), %rsi      # 0x7140
movq 0x54f5(%rip), %rdx      # 0x7148
leaq 0x2499(%rip), %rdi      # 0x40f3
xorl %eax, %eax
callq 0x10b0
xorl %edi, %edi
callq 0x1210
leaq 0x24ad(%rip), %rdi      # 0x411c
callq 0x1070
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1210
nop