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

    int i,j,k,wraparounds = 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 wraparound
        i = 0; 
        while (1)
            {
            int prev = 0,amp = 0;
            while (i < r->samples)
                {
                if ((prev > Max * Threshold && amp < 0)
                 || (prev < Min * Threshold && amp > 0))
                    break;
                prev = amp;
                amp = r->data[j][i++];
                }

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

            i -= 2; //went two steps too far

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

            //find the end
            ++i;
                    
            if (r->data[j][start] > 0)
                while (i < r->samples && r->data[j][i] < 0) ++i;
            else
                while (i < r->samples && r->data[j][i] > 0) ++i;

            finish = i+1;

            //for (k = start; k < finish; ++k)
            //    printf("    wrap: %d\n",r->data[j][k]);

            ++wraparounds;

            if (Checking)
                {
                fprintf(stderr,"wraparound from %d (%.4f) to %d (%.4f), "
                    "%d samples\n",
                    start,start / (double) r->sampleRate,
                    finish,finish / (double) r->sampleRate,
                    finish-start
                    );
                //getchar();
                }
            else
                {
                /* invert the region above the clipping threshold */
                if (r->data[j][start] > 0)
                    for (k = start+1; k < finish-1; ++k)
                        r->data[j][k] = Max + r->data[j][k] - Min;
                else
                    for (k = start+1; k < finish-1; ++k)
                        r->data[j][k] = Min + r->data[j][k] - Max;
                //printf("after inversion\n");
                //for (k = start; k < finish; ++k)
                //    printf("    wrap: %d\n",r->data[j][k]);
                //getchar();
                }
            }
        }
                
    if (!Checking)
        writeRRA(out,r,"modifiedBy: fixclip",0,r->bitsPerSample == 8 ? 127:0);

    fclose(out);

    return wraparounds;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movl %edi, %r14d
movl $0x1, %r12d
cmpl $0x2, %edi
jl 0x1697
movl $0x1, %r12d
movslq %r12d, %rax
movq (%rbx,%rax,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x1697
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 $0x73, %eax
jg 0x1641
cmpl $0x63, %eax
jne 0x1658
xorl %eax, %eax
cmpl $0x0, 0x5b39(%rip)      # 0x716c
sete %al
movl %eax, 0x5b30(%rip)      # 0x716c
xorl %r15d, %r15d
jmp 0x167c
cmpl $0x74, %eax
jne 0x1662
callq 0x1080
movsd %xmm0, 0x5af5(%rip)     # 0x7148
movb $0x1, %r15b
jmp 0x167c
cmpl $0x68, %eax
jne 0x166b
jmp 0x197d
cmpl $0x76, %eax
je 0x195f
xorl %r15d, %r15d
leaq 0x2a2b(%rip), %rdi      # 0x40a0
xorl %eax, %eax
callq 0x19f8
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 0x16a8
movq 0x5935(%rip), %rax      # 0x6fd8
movq (%rax), %r12
jmp 0x16ce
cmpl $0x2, %r14d
je 0x16da
cmpl $0x1, %r14d
jne 0x19e2
movslq %r12d, %rax
movq (%rbx,%rax,8), %rdi
leaq 0x2f8f(%rip), %rsi      # 0x4655
callq 0x1a92
movq %rax, %r12
movq 0x58fb(%rip), %rax      # 0x6fd0
movq (%rax), %rax
jmp 0x1701
movslq %r12d, %r14
movq (%rbx,%r14,8), %rdi
leaq 0x2f6d(%rip), %rsi      # 0x4655
callq 0x1a92
movq %rax, %r12
movq 0x8(%rbx,%r14,8), %rdi
leaq 0x2931(%rip), %rsi      # 0x402d
callq 0x1a92
movq %rax, -0x30(%rbp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x1ffe
movq %rax, %r15
movq %r12, %rdi
callq 0x1090
movl 0x8(%r15), %edi
decl %edi
movsd 0x28e0(%rip), %xmm0     # 0x4008
callq 0x1050
xorpd 0x28db(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %eax
movl %eax, 0x5a25(%rip)      # 0x7164
movl 0x8(%r15), %edi
decl %edi
movsd 0x28bb(%rip), %xmm0     # 0x4008
callq 0x1050
xorl %r9d, %r9d
cvttsd2si %xmm0, %eax
decl %eax
movl %eax, 0x5a07(%rip)      # 0x7168
cmpl $0x0, (%r15)
jle 0x1910
movq %r15, %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
xorl %ebx, %ebx
xorl %r9d, %r9d
xorl %r12d, %r12d
movl 0xc(%r15), %r14d
movslq %r14d, %rax
cmpl %eax, %r12d
jge 0x17fb
xorps %xmm0, %xmm0
cvtsi2sdl 0x59d3(%rip), %xmm0 # 0x7168
movsd 0x59ab(%rip), %xmm2     # 0x7148
mulsd %xmm2, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x59b8(%rip), %xmm1 # 0x7164
mulsd %xmm2, %xmm1
movslq %r12d, %r12
xorl %edx, %edx
xorl %ecx, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
ucomisd %xmm0, %xmm2
seta %sil
movl %edx, %ecx
testl %edx, %edx
sets %dl
testb %dl, %sil
jne 0x17fb
ucomisd %xmm2, %xmm1
seta %dl
testl %ecx, %ecx
setg %sil
testb %sil, %dl
jne 0x17fb
movq 0x18(%r15), %rdx
movq (%rdx,%rbx,8), %rdx
movl (%rdx,%r12,4), %edx
incq %r12
cmpq %r12, %rax
jne 0x17b7
movl %r14d, %r12d
cmpl %r14d, %r12d
jne 0x1808
movl %r12d, %r13d
jmp 0x18f5
leal -0x1(%r12), %ecx
movq 0x18(%r15), %rdx
movq (%rdx,%rbx,8), %rdx
movslq %r12d, %rsi
movl -0x8(%rdx,%rsi,4), %esi
movl %ecx, %r13d
testl %esi, %esi
jle 0x183c
cmpl %r14d, %r12d
jg 0x1856
movslq %ecx, %r13
cmpl $0x0, (%rdx,%r13,4)
jns 0x1856
incq %r13
cmpq %rax, %r13
jl 0x182b
jmp 0x1853
cmpl %r14d, %r12d
jg 0x1856
movslq %ecx, %r13
cmpl $0x0, (%rdx,%r13,4)
jle 0x1856
incq %r13
cmpq %rax, %r13
jl 0x1844
movl %r14d, %r13d
incl %r9d
cmpl $0x0, 0x590c(%rip)      # 0x716c
jne 0x18dc
testl %esi, %esi
jle 0x18a3
cmpl %r13d, %r12d
jg 0x18f5
movl 0x58f3(%rip), %eax      # 0x7168
movl 0x58e9(%rip), %esi      # 0x7164
movslq %ecx, %rcx
leaq (%rdx,%rcx,4), %rcx
movl %r13d, %edx
subl %r12d, %edx
incl %edx
xorl %edi, %edi
movl (%rcx,%rdi,4), %r8d
addl %eax, %r8d
subl %esi, %r8d
movl %r8d, (%rcx,%rdi,4)
incq %rdi
cmpl %edi, %edx
jne 0x188c
jmp 0x18f5
cmpl %r13d, %r12d
jg 0x18f5
movl 0x58b6(%rip), %eax      # 0x7164
movl 0x58b4(%rip), %esi      # 0x7168
movslq %ecx, %rcx
leaq (%rdx,%rcx,4), %rcx
movl %r13d, %edx
subl %r12d, %edx
incl %edx
xorl %edi, %edi
movl (%rcx,%rdi,4), %r8d
addl %eax, %r8d
subl %esi, %r8d
movl %r8d, (%rcx,%rdi,4)
incq %rdi
cmpl %edi, %edx
jne 0x18c5
jmp 0x18f5
leal -0x2(%r12), %esi
movl %r13d, %edi
movq -0x38(%rbp), %rdx
movq %r9, -0x40(%rbp)
callq 0x1260
movq -0x40(%rbp), %r9
cmpl %r14d, %r12d
movl %r13d, %r12d
jne 0x177e
incq %rbx
movslq (%r15), %rax
cmpq %rax, %rbx
jl 0x177b
movq %r9, %r14
cmpl $0x0, 0x5852(%rip)      # 0x716c
movq -0x30(%rbp), %rbx
jne 0x1945
xorl %eax, %eax
cmpl $0x8, 0x8(%r15)
movl $0x7f, %r8d
cmovnel %eax, %r8d
leaq 0x272b(%rip), %rdx      # 0x4063
movq %rbx, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x2ad9
movq %rbx, %rdi
callq 0x1090
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x57d2(%rip), %rsi      # 0x7138
movq 0x57d3(%rip), %rdx      # 0x7140
leaq 0x271d(%rip), %rdi      # 0x4091
xorl %eax, %eax
callq 0x10b0
jmp 0x19db
leaq 0x2771(%rip), %rdi      # 0x40f5
callq 0x1070
leaq 0x2776(%rip), %rdi      # 0x4106
callq 0x1070
leaq 0x279b(%rip), %rdi      # 0x4137
callq 0x1070
leaq 0x27b9(%rip), %rdi      # 0x4161
callq 0x1070
leaq 0x27e8(%rip), %rdi      # 0x419c
callq 0x1070
movsd 0x5787(%rip), %xmm0     # 0x7148
leaq 0x26af(%rip), %rdi      # 0x4077
movb $0x1, %al
callq 0x10b0
leaq 0x27f1(%rip), %rdi      # 0x41c7
callq 0x1070
xorl %edi, %edi
callq 0x1200
leaq 0x26d1(%rip), %rdi      # 0x40ba
callq 0x1070
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1200