// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, -0x14(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x17a0
movl %eax, -0x14(%rbp)
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x0, %eax
jne 0x136f
movq 0x6c83(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq 0x6c6d(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x140c
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x1, %eax
jne 0x13a6
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x42fb(%rip), %rsi      # 0x5688
callq 0x1ae0
movq %rax, -0x38(%rbp)
movq 0x6c33(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x140a
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x2, %eax
jne 0x13f0
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x42c4(%rip), %rsi      # 0x5688
callq 0x1ae0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x3c88(%rip), %rsi      # 0x506d
callq 0x1ae0
movq %rax, -0x40(%rbp)
jmp 0x1408
leaq 0x3c78(%rip), %rdi      # 0x506f
movb $0x0, %al
callq 0x1090
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x11d0
jmp 0x140a
jmp 0x140c
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2450
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
callq 0x1070
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x3bcc(%rip), %xmm0     # 0x5008
callq 0x1060
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x6ce2(%rip)      # 0x8144
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x3b90(%rip), %xmm0     # 0x5008
callq 0x1060
cvttsd2si %xmm0, %eax
subl $0x1, %eax
movl %eax, 0x6cbe(%rip)      # 0x8148
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1757
movl $0x0, -0x18(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x18(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x1531
cvtsi2sdl -0x4c(%rbp), %xmm0
cvtsi2sdl 0x6c7a(%rip), %xmm1 # 0x8148
mulsd 0x6c52(%rip), %xmm1     # 0x8128
ucomisd %xmm1, %xmm0
jbe 0x14e2
cmpl $0x0, -0x50(%rbp)
jl 0x1503
cvtsi2sdl -0x4c(%rbp), %xmm1
cvtsi2sdl 0x6c55(%rip), %xmm0 # 0x8144
mulsd 0x6c31(%rip), %xmm0     # 0x8128
ucomisd %xmm1, %xmm0
jbe 0x1505
cmpl $0x0, -0x50(%rbp)
jle 0x1505
jmp 0x1531
movl -0x50(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x18(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x18(%rbp)
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x50(%rbp)
jmp 0x14b5
movl -0x18(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jne 0x1542
jmp 0x1747
movl -0x18(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x28(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0x15bb
jmp 0x1576
movl -0x18(%rbp), %ecx
movq -0x48(%rbp), %rdx
xorl %eax, %eax
cmpl 0xc(%rdx), %ecx
movb %al, -0x51(%rbp)
jge 0x15a5
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
setl %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0x15ae
jmp 0x15b9
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1576
jmp 0x1602
jmp 0x15bd
movl -0x18(%rbp), %ecx
movq -0x48(%rbp), %rdx
xorl %eax, %eax
cmpl 0xc(%rdx), %ecx
movb %al, -0x52(%rbp)
jge 0x15ec
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
setg %al
movb %al, -0x52(%rbp)
movb -0x52(%rbp), %al
testb $0x1, %al
jne 0x15f5
jmp 0x1600
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x15bd
jmp 0x1602
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, 0x6b31(%rip)      # 0x814c
je 0x166c
movq 0x69d4(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl -0x28(%rbp), %edx
cvtsi2sdl -0x28(%rbp), %xmm0
movq -0x48(%rbp), %rax
cvtsi2sdl 0x4(%rax), %xmm1
divsd %xmm1, %xmm0
movl -0x2c(%rbp), %ecx
cvtsi2sdl -0x2c(%rbp), %xmm1
movq -0x48(%rbp), %rax
cvtsi2sdl 0x4(%rax), %xmm2
divsd %xmm2, %xmm1
movl -0x2c(%rbp), %r8d
subl -0x28(%rbp), %r8d
leaq 0x3a4b(%rip), %rsi      # 0x50ab
movb $0x2, %al
callq 0x1110
jmp 0x1742
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x28(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0x16e3
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x16e1
movl 0x6aa6(%rip), %edx      # 0x8148
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
addl (%rax,%rcx,4), %edx
subl 0x6a85(%rip), %edx      # 0x8144
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x168f
jmp 0x1740
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x173e
movl 0x6a45(%rip), %edx      # 0x8144
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
addl (%rax,%rcx,4), %edx
subl 0x6a2c(%rip), %edx      # 0x8148
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x16ec
jmp 0x1740
jmp 0x1742
jmp 0x14a7
jmp 0x1749
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1491
cmpl $0x0, 0x69ee(%rip)      # 0x814c
jne 0x178e
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
xorl %r8d, %r8d
movl $0x7f, %eax
cmpl $0x8, %ecx
cmovel %eax, %r8d
leaq 0x395a(%rip), %rdx      # 0x50df
xorl %eax, %eax
movl %eax, %ecx
callq 0x3380
movq -0x40(%rbp), %rdi
callq 0x1070
movl -0x24(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq