// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movq %rsi, %rbx
movl %edi, %r14d
pushq $0x1
popq %r12
movslq %r12d, %r15
cmpl %r12d, %r14d
jle 0x1402
movq (%rbx,%r15,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x1402
movb 0x2(%rsi), %r13b
testb %r13b, %r13b
je 0x138c
leaq 0x2(%rsi), %rdi
jmp 0x1391
movq 0x8(%rbx,%r15,8), %rdi
movzbl 0x1(%rsi), %eax
cmpl $0x63, %eax
je 0x13c4
cmpl $0x74, %eax
je 0x13db
cmpl $0x68, %eax
je 0x167c
cmpl $0x76, %eax
je 0x16dc
xorl %r15d, %r15d
leaq 0x2ce5(%rip), %rdi      # 0x40a0
xorl %eax, %eax
callq 0x1714
jmp 0x13eb
xorl %eax, %eax
cmpl $0x0, 0x5d9f(%rip)      # 0x716c
sete %al
movl %eax, 0x5d96(%rip)      # 0x716c
xorl %r15d, %r15d
jmp 0x13eb
callq 0x1080
movsd %xmm0, 0x5d60(%rip)     # 0x7148
movb $0x1, %r15b
testb %r13b, %r13b
sete %al
andb %r15b, %al
movzbl %al, %eax
addl %eax, %r12d
incl %r12d
jmp 0x1364
subl %r12d, %r14d
jne 0x1413
movq 0x5bca(%rip), %rax      # 0x6fd8
movq (%rax), %r12
jmp 0x1436
cmpl $0x2, %r14d
je 0x1442
cmpl $0x1, %r14d
jne 0x16ff
movq (%rbx,%r15,8), %rdi
leaq 0x3224(%rip), %rsi      # 0x4652
callq 0x1844
movq %rax, %r12
movq 0x5b93(%rip), %rax      # 0x6fd0
movq (%rax), %rbx
jmp 0x1469
movq (%rbx,%r15,8), %rdi
leaq 0x3205(%rip), %rsi      # 0x4652
callq 0x1844
movq %rax, %r12
movq 0x8(%rbx,%r15,8), %rdi
leaq 0x2bcc(%rip), %rsi      # 0x402d
callq 0x1844
movq %rax, %rbx
xorl %r13d, %r13d
movq %r12, %rdi
xorl %esi, %esi
callq 0x1d11
movq %rax, %r14
movq %r12, %rdi
callq 0x1090
movl 0x8(%r14), %edi
decl %edi
movsd 0x2b79(%rip), %xmm0     # 0x4008
callq 0x1050
xorpd 0x2b74(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %eax
movl %eax, 0x5cbe(%rip)      # 0x7164
movl 0x8(%r14), %edi
decl %edi
movsd 0x2b54(%rip), %xmm0     # 0x4008
callq 0x1050
cvttsd2si %xmm0, %eax
decl %eax
movl %eax, 0x5ca3(%rip)      # 0x7168
xorl %r15d, %r15d
movslq (%r14), %rcx
cmpq %rcx, %r13
jge 0x1636
xorl %r12d, %r12d
movsd 0x5c69(%rip), %xmm2     # 0x7148
movl 0x5c7f(%rip), %ecx      # 0x7164
cvtsi2sd %eax, %xmm0
mulsd %xmm2, %xmm0
cvtsi2sd %ecx, %xmm1
mulsd %xmm2, %xmm1
movl 0x5c71(%rip), %esi      # 0x716c
movslq 0xc(%r14), %rdi
movslq %r12d, %rdx
cmpl %edi, %edx
movl %edi, %r8d
cmovgl %edx, %r8d
xorl %r10d, %r10d
xorl %r9d, %r9d
cmpq %rdi, %rdx
jge 0x154e
cvtsi2sd %r10d, %xmm2
ucomisd %xmm0, %xmm2
jbe 0x1526
testl %r9d, %r9d
js 0x1551
ucomisd %xmm2, %xmm1
seta %r10b
testl %r9d, %r9d
setg %r11b
testb %r11b, %r10b
jne 0x1551
movq 0x18(%r14), %r10
movq (%r10,%r13,8), %r11
movl %r9d, %r10d
movl (%r11,%rdx,4), %r9d
incq %rdx
jmp 0x1511
movl %r8d, %edx
cmpl %edi, %edx
je 0x162e
leal -0x1(%rdx), %r12d
movq 0x18(%r14), %r8
movq (%r8,%r13,8), %r8
movslq %edx, %r9
movl -0x8(%r8,%r9,4), %r10d
cmpl %r12d, %edi
movslq %r12d, %r9
cmovgl %edi, %r12d
movq %r9, %r11
testl %r10d, %r10d
jle 0x1596
movq %r9, %r11
cmpq %rdi, %r11
jge 0x15a5
cmpl $0x0, (%r8,%r11,4)
jns 0x15a2
incq %r11
jmp 0x1582
incq %r11
cmpq %rdi, %r11
jge 0x15a5
cmpl $0x0, (%r8,%r11,4)
jg 0x1593
movl %r11d, %r12d
incl %r15d
testl %esi, %esi
jne 0x15e7
movslq %r12d, %rdx
testl %r10d, %r10d
jg 0x15dd
cmpq %rdx, %r9
jge 0x14fb
movl (%r8,%r9,4), %edi
addl %ecx, %edi
subl %eax, %edi
movl %edi, (%r8,%r9,4)
incq %r9
jmp 0x15b4
movl (%r8,%r9,4), %edi
addl %eax, %edi
subl %ecx, %edi
movl %edi, (%r8,%r9,4)
incq %r9
cmpq %rdx, %r9
jl 0x15ce
jmp 0x14fb
addl $-0x2, %edx
leal 0x1(%r12), %ecx
movq 0x5a02(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
cvtsi2sd %edx, %xmm0
cvtsi2sdl 0x4(%r14), %xmm2
cvtsi2sd %ecx, %xmm1
divsd %xmm2, %xmm0
divsd %xmm2, %xmm1
movl %ecx, %r8d
subl %edx, %r8d
leaq 0x2a13(%rip), %rsi      # 0x402f
movb $0x2, %al
callq 0x1140
movl 0x5b3f(%rip), %eax      # 0x7168
jmp 0x14d7
incq %r13
jmp 0x14c8
cmpl $0x0, 0x5b2f(%rip)      # 0x716c
jne 0x1662
xorl %eax, %eax
cmpl $0x8, 0x8(%r14)
pushq $0x7f
popq %r8
cmovnel %eax, %r8d
leaq 0x2a0e(%rip), %rdx      # 0x4063
movq %rbx, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2784
movq %rbx, %rdi
callq 0x1090
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2a72(%rip), %rdi      # 0x40f5
callq 0x1070
leaq 0x2a77(%rip), %rdi      # 0x4106
callq 0x1070
leaq 0x2a9c(%rip), %rdi      # 0x4137
callq 0x1070
leaq 0x2aba(%rip), %rdi      # 0x4161
callq 0x1070
leaq 0x2ae9(%rip), %rdi      # 0x419c
callq 0x1070
movsd 0x5a88(%rip), %xmm0     # 0x7148
leaq 0x29b0(%rip), %rdi      # 0x4077
movb $0x1, %al
callq 0x10b0
leaq 0x2af2(%rip), %rdi      # 0x41c7
callq 0x1070
jmp 0x16f8
movq 0x5a55(%rip), %rsi      # 0x7138
movq 0x5a56(%rip), %rdx      # 0x7140
leaq 0x29a0(%rip), %rdi      # 0x4091
xorl %eax, %eax
callq 0x10b0
xorl %edi, %edi
callq 0x1200
leaq 0x29b4(%rip), %rdi      # 0x40ba
callq 0x1070
pushq $-0x1
popq %rdi
callq 0x1200
nop