// File: /workspace/github_c_makefile_star2_O3/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
movl $0x1, %r12d
cmpl $0x2, %edi
jl 0x1684
movl $0x1, %r12d
movslq %r12d, %rax
movq (%rbx,%rax,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x1684
movb 0x2(%rsi), %r13b
testb %r13b, %r13b
je 0x1604
leaq 0x2(%rsi), %rdi
jmp 0x1609
movq 0x8(%rbx,%rax,8), %rdi
movzbl 0x1(%rsi), %eax
cmpl $0x73, %eax
jg 0x162e
cmpl $0x63, %eax
jne 0x1645
xorl %eax, %eax
cmpl $0x0, 0x5b4c(%rip)      # 0x716c
sete %al
movl %eax, 0x5b43(%rip)      # 0x716c
xorl %r15d, %r15d
jmp 0x1669
cmpl $0x74, %eax
jne 0x164f
callq 0x1080
movsd %xmm0, 0x5b08(%rip)     # 0x7148
movb $0x1, %r15b
jmp 0x1669
cmpl $0x68, %eax
jne 0x1658
jmp 0x19a6
cmpl $0x76, %eax
je 0x1988
xorl %r15d, %r15d
leaq 0x2a3e(%rip), %rdi      # 0x40a0
xorl %eax, %eax
callq 0x1a24
testb %r13b, %r13b
sete %al
andb %r15b, %al
movzbl %al, %eax
addl %eax, %r12d
incl %r12d
cmpl %r14d, %r12d
jl 0x15e5
subl %r12d, %r14d
jne 0x1695
movq 0x5948(%rip), %rax      # 0x6fd8
movq (%rax), %r12
jmp 0x16bb
cmpl $0x2, %r14d
je 0x16c7
cmpl $0x1, %r14d
jne 0x1a0b
movslq %r12d, %rax
movq (%rbx,%rax,8), %rdi
leaq 0x2f9f(%rip), %rsi      # 0x4652
callq 0x1abe
movq %rax, %r12
movq 0x590e(%rip), %rax      # 0x6fd0
movq (%rax), %rax
jmp 0x16ee
movslq %r12d, %r14
movq (%rbx,%r14,8), %rdi
leaq 0x2f7d(%rip), %rsi      # 0x4652
callq 0x1abe
movq %rax, %r12
movq 0x8(%rbx,%r14,8), %rdi
leaq 0x2944(%rip), %rsi      # 0x402d
callq 0x1abe
movq %rax, -0x30(%rbp)
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
callq 0x2026
movq %rax, %r15
movq %r12, %rdi
callq 0x1090
movl 0x8(%r15), %edi
decl %edi
movsd 0x28f0(%rip), %xmm0     # 0x4008
callq 0x1050
xorpd 0x28eb(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %eax
movl %eax, 0x5a35(%rip)      # 0x7164
movl 0x8(%r15), %edi
decl %edi
movsd 0x28cb(%rip), %xmm0     # 0x4008
callq 0x1050
cvttsd2si %xmm0, %eax
decl %eax
movl %eax, 0x5a1a(%rip)      # 0x7168
cmpl $0x0, (%r15)
jle 0x193c
xorl %r13d, %r13d
leaq 0x28cd(%rip), %r12      # 0x402f
xorl %r14d, %r14d
xorl %ebx, %ebx
movl 0xc(%r15), %ecx
movslq %ecx, %rdi
cmpl %edi, %ebx
jge 0x17e6
xorps %xmm0, %xmm0
cvtsi2sdl 0x59eb(%rip), %xmm0 # 0x7168
movsd 0x59c3(%rip), %xmm2     # 0x7148
mulsd %xmm2, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x59d0(%rip), %xmm1 # 0x7164
mulsd %xmm2, %xmm1
movslq %ebx, %rdx
xorl %esi, %esi
xorl %eax, %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
ucomisd %xmm0, %xmm2
seta %r8b
movl %esi, %eax
testl %esi, %esi
sets %sil
testb %sil, %r8b
jne 0x17e8
ucomisd %xmm2, %xmm1
seta %sil
testl %eax, %eax
setg %r8b
testb %r8b, %sil
jne 0x17e8
movq 0x18(%r15), %rsi
movq (%rsi,%r13,8), %rsi
movl (%rsi,%rdx,4), %esi
incq %rdx
cmpq %rdx, %rdi
jne 0x179f
jmp 0x192d
movl %ebx, %edx
cmpl %ecx, %edx
je 0x192d
leal -0x1(%rdx), %eax
movq 0x18(%r15), %rsi
movq (%rsi,%r13,8), %rsi
movslq %edx, %r8
movl -0x8(%rsi,%r8,4), %r8d
testl %r8d, %r8d
jle 0x1820
cmpl %ecx, %edx
jle 0x1838
incl %r14d
cmpl $0x0, 0x5956(%rip)      # 0x716c
jne 0x18e7
movl %eax, %ebx
jmp 0x186f
cmpl %ecx, %edx
jle 0x184b
incl %r14d
cmpl $0x0, 0x593e(%rip)      # 0x716c
jne 0x18e7
movl %eax, %ebx
jmp 0x18ab
movslq %eax, %rbx
cmpl $0x0, (%rsi,%rbx,4)
jns 0x185e
incq %rbx
cmpq %rdi, %rbx
jl 0x183b
jmp 0x185c
movslq %eax, %rbx
cmpl $0x0, (%rsi,%rbx,4)
jle 0x185e
incq %rbx
cmpq %rdi, %rbx
jl 0x184e
movl %ecx, %ebx
incl %r14d
cmpl $0x0, 0x5904(%rip)      # 0x716c
jne 0x18e9
testl %r8d, %r8d
jle 0x18ab
cmpl %ebx, %edx
jg 0x1767
movl 0x58eb(%rip), %ecx      # 0x7168
movl 0x58e1(%rip), %edi      # 0x7164
cltq
leaq (%rsi,%rax,4), %rax
movl %ebx, %esi
subl %edx, %esi
incl %esi
xorl %edx, %edx
movl (%rax,%rdx,4), %r8d
addl %ecx, %r8d
subl %edi, %r8d
movl %r8d, (%rax,%rdx,4)
incq %rdx
cmpl %edx, %esi
jne 0x1891
jmp 0x1767
cmpl %ebx, %edx
jg 0x1767
movl 0x58ab(%rip), %ecx      # 0x7164
movl 0x58a9(%rip), %edi      # 0x7168
cltq
leaq (%rsi,%rax,4), %rax
movl %ebx, %esi
subl %edx, %esi
incl %esi
xorl %edx, %edx
movl (%rax,%rdx,4), %r8d
addl %ecx, %r8d
subl %edi, %r8d
movl %r8d, (%rax,%rdx,4)
incq %rdx
cmpl %edx, %esi
jne 0x18cd
jmp 0x1767
movl %eax, %ebx
addl $-0x2, %edx
leal 0x1(%rbx), %ecx
movq 0x5702(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
xorps %xmm2, %xmm2
cvtsi2sdl 0x4(%r15), %xmm2
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm2, %xmm0
divsd %xmm2, %xmm1
movl %ecx, %r8d
subl %edx, %r8d
movq %r12, %rsi
movb $0x2, %al
callq 0x1140
jmp 0x1767
incq %r13
movslq (%r15), %rax
cmpq %rax, %r13
jl 0x1765
cmpl $0x0, 0x5829(%rip)      # 0x716c
movq -0x30(%rbp), %rbx
jne 0x196e
xorl %eax, %eax
cmpl $0x8, 0x8(%r15)
movl $0x7f, %r8d
cmovnel %eax, %r8d
leaq 0x2702(%rip), %rdx      # 0x4063
movq %rbx, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x2b58
movq %rbx, %rdi
callq 0x1090
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x57a9(%rip), %rsi      # 0x7138
movq 0x57aa(%rip), %rdx      # 0x7140
leaq 0x26f4(%rip), %rdi      # 0x4091
xorl %eax, %eax
callq 0x10b0
jmp 0x1a04
leaq 0x2748(%rip), %rdi      # 0x40f5
callq 0x1070
leaq 0x274d(%rip), %rdi      # 0x4106
callq 0x1070
leaq 0x2772(%rip), %rdi      # 0x4137
callq 0x1070
leaq 0x2790(%rip), %rdi      # 0x4161
callq 0x1070
leaq 0x27bf(%rip), %rdi      # 0x419c
callq 0x1070
movsd 0x575e(%rip), %xmm0     # 0x7148
leaq 0x2686(%rip), %rdi      # 0x4077
movb $0x1, %al
callq 0x10b0
leaq 0x27c8(%rip), %rdi      # 0x41c7
callq 0x1070
xorl %edi, %edi
callq 0x1200
leaq 0x26a8(%rip), %rdi      # 0x40ba
callq 0x1070
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1200
nopl (%rax)