// File: /workspace/github_c_makefile_star2_O0/pipde[P]deos-songlib/utils/correct/correct.c
// Function: findCycleLength
static int
findCycleLength(void)
    {
    int j,start;
    int bestCycle = 0;
    double error = 0;
    double smallestError = -1;
    int smallestCycle = 0.90 * SampleRate / MaxFreq;
    int largestCycle = 1.10 * SampleRate / MinFreq;
    int maxAmplitude;

    fprintf(stderr,"smallest cycle allowed: %d\n",smallestCycle);
    fprintf(stderr,"largest cycle allowed: %d\n",largestCycle);
    /*
    for (j = 0; j < WindowSize; ++j)
        fprintf(stderr,"%d: %d\n",j,Window[j]);
    */

    start = 0;
    for (j = 0; j < Passes; ++j)
        {
        fprintf(stderr,"Pass %d...\n",j);
        /* skip to first zero (upward) past start */
        int i = start + 1;
        while (i < WindowSize)
            {
            //printf("amp = %d\n",Window[i-1]);
            if (Window[i-1] < 0 && Window[i] >= 0) break;
            ++i;
            }
        start = i;

        //printf("amp = %d\n",Window[i]);
        fprintf(stderr,"found cycle start at location %d!\n",i);
        //getchar();

        while (1)
            {
            /* find the next zero (upward) */
            ++i;
            while (i < WindowSize-CorrelationSize)
                {
                //fprintf(stderr,"amp = %d\n",Window[i-1]);
                if (abs(Window[i-1]) > maxAmplitude)
                    maxAmplitude = abs(Window[i-1]);
                if (Window[i-1] < 0 && Window[i] >= 0) break;
                ++i;
                }
            if (abs(Window[i]) > maxAmplitude)
                maxAmplitude = abs(Window[i]);

            if (i == WindowSize - CorrelationSize) break;
            if (i - start > largestCycle) break;
            if (i - start < smallestCycle) continue;
            if (maxAmplitude < QuietLevel) continue;
            fprintf(stderr,"found cycle end at location %d!\n",i);
            fprintf(stderr,"max amplitude is %d\n",maxAmplitude);

            //printf("amp = %d\n",Window[i]);
            //fprintf(stderr,"found possible cycle end: %d samples!\n",i - start);
            //getchar();
            error = correlate(start,i,CorrelationSize);
            //fprintf(stderr,"for pass %d, cycle length %d, error is %f\n",j,i-start,error);
            if ((smallestError < 0 || error < smallestError))
                {
                bestCycle = i - start;
                fprintf(stderr,"improvement: cycle is %d (%f < %f)\n",
                    bestCycle,error,smallestError);
                smallestError = error;
                }
            //getchar();
            }
        }

    //if (smallestError > MAX_ERROR)
    //    return 0;
    //else
        return bestCycle;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl $0x0, -0xc(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x18(%rbp)
movsd 0xa349(%rip), %xmm0     # 0xd078
movsd %xmm0, -0x20(%rbp)
cvtsi2sdl 0xfe74(%rip), %xmm1 # 0x12bb0
movsd 0xa32c(%rip), %xmm0     # 0xd070
mulsd %xmm1, %xmm0
divsd 0x11678(%rip), %xmm0    # 0x143c8
cvttsd2si %xmm0, %eax
movl %eax, -0x24(%rbp)
cvtsi2sdl 0xfe51(%rip), %xmm1 # 0x12bb0
movsd 0xa301(%rip), %xmm0     # 0xd068
mulsd %xmm1, %xmm0
divsd 0x1164d(%rip), %xmm0    # 0x143c0
cvttsd2si %xmm0, %eax
movl %eax, -0x28(%rbp)
movq 0xf277(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0xa655(%rip), %rsi      # 0xd3e3
movb $0x0, %al
callq 0x21a0
movq 0xf25c(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
movl -0x28(%rbp), %edx
leaq 0xa656(%rip), %rsi      # 0xd3ff
movb $0x0, %al
callq 0x21a0
movl $0x0, -0x8(%rbp)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
cmpl 0xfb4d(%rip), %eax      # 0x12914
jge 0x300d
movq 0xf224(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
movl -0x4(%rbp), %edx
leaq 0xa639(%rip), %rsi      # 0xd41a
movb $0x0, %al
callq 0x21a0
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl 0xfb16(%rip), %eax      # 0x12910
jge 0x2e30
movq 0x115d5(%rip), %rax     # 0x143d8
movl -0x30(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
jge 0x2e25
movq 0x115bf(%rip), %rax     # 0x143d8
movslq -0x30(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jl 0x2e25
jmp 0x2e30
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x2df1
movl -0x30(%rbp), %eax
movl %eax, -0x8(%rbp)
movq 0xf1bb(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
movl -0x30(%rbp), %edx
leaq 0xa5dc(%rip), %rsi      # 0xd426
movb $0x0, %al
callq 0x21a0
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movl 0xfaad(%rip), %ecx      # 0x12910
subl 0xfaaf(%rip), %ecx      # 0x12918
cmpl %ecx, %eax
jge 0x2ee0
movq 0x11564(%rip), %rax     # 0x143d8
movl -0x30(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cmpl -0x2c(%rbp), %eax
jle 0x2ea9
movq 0x11545(%rip), %rax     # 0x143d8
movl -0x30(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl %eax, -0x2c(%rbp)
movq 0x11528(%rip), %rax     # 0x143d8
movl -0x30(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
jge 0x2ed2
movq 0x11512(%rip), %rax     # 0x143d8
movslq -0x30(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jl 0x2ed2
jmp 0x2ee0
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x2e5a
movq 0x114f1(%rip), %rax     # 0x143d8
movslq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cmpl -0x2c(%rbp), %eax
jle 0x2f12
movq 0x114d7(%rip), %rax     # 0x143d8
movslq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
movl 0xf9f5(%rip), %ecx      # 0x12910
subl 0xf9f7(%rip), %ecx      # 0x12918
cmpl %ecx, %eax
jne 0x2f2a
jmp 0x2ffd
movl -0x30(%rbp), %eax
subl -0x8(%rbp), %eax
cmpl -0x28(%rbp), %eax
jle 0x2f3a
jmp 0x2ffd
movl -0x30(%rbp), %eax
subl -0x8(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x2f4a
jmp 0x2e51
movl -0x2c(%rbp), %eax
cmpl 0xf9c9(%rip), %eax      # 0x1291c
jge 0x2f5a
jmp 0x2e51
movq 0xf097(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
movl -0x30(%rbp), %edx
leaq 0xa4db(%rip), %rsi      # 0xd449
movb $0x0, %al
callq 0x21a0
movq 0xf07c(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
movl -0x2c(%rbp), %edx
leaq 0xa4e1(%rip), %rsi      # 0xd46a
movb $0x0, %al
callq 0x21a0
movl -0x8(%rbp), %edi
movl -0x30(%rbp), %esi
movl 0xf97c(%rip), %edx      # 0x12918
callq 0x3410
movsd %xmm0, -0x18(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x20(%rbp), %xmm0
ja 0x2fc0
movsd -0x18(%rbp), %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2ff8
movl -0x30(%rbp), %eax
subl -0x8(%rbp), %eax
movl %eax, -0xc(%rbp)
movq 0xf028(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
movl -0xc(%rbp), %edx
movsd -0x18(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
leaq 0xa498(%rip), %rsi      # 0xd47f
movb $0x2, %al
callq 0x21a0
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0x2e51
jmp 0x2fff
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x2dbe
movl -0xc(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)