// File: /workspace/github_c_makefile_star2_O0/pipde[P]deos-songlib/utils/fixpitch/fixpitch.c
// Function: findCycleLength
static int
findCycleLength(double *factor,int *n)
    {
    int end;
    int peak = 0;
    int bestCycle = 0;
    int bestNote = 0;
    double bestFactor = 0;
    double error;
    double smallestError = -1;

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

    end = ShortestCycle;
    if (Report) fprintf(ReportFP,"shortest Cycle is %d samples\n",end);
    
    end = 0;
    peak = 0;
    while (1)
        {
        end = crossing(end+1,&peak);
        //fprintf(ReportFP,"found a crossing, %d samples, peak is %d\n",end,peak);
        if (peak < AbsoluteThreshold || end == 0 || end >= LongestCycle) break;
        if (Report)
            fprintf(ReportFP,"found a cycle, %d samples\n",end);

        *factor = findFactor(end,n);
        if (*factor == 0) continue;
        error = correlate(end);
        if (*n == LastNote) error /= LastNoteBias;
        if (Report)
            fprintf(ReportFP,"    for cycle length %d, error is %f\n",end,error);
        if (smallestError < 0 || error < smallestError)
            {
            bestFactor = *factor;
            bestNote = *n;
            bestCycle = end;
            if (Report)
                fprintf(ReportFP,"        improvement: cycle is %d (%f < %f)\n",
                    bestCycle,error,smallestError);
            smallestError = error;
            }
        //else
            //printf("not an improvement\n");
        //getchar();
        }

    if (Report)
        fprintf(ReportFP,"best cycle is %d\n",bestCycle);
    *n = bestNote;
    *factor = bestFactor;
    return bestCycle;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
movsd 0x4013(%rip), %xmm0     # 0x7038
movsd %xmm0, -0x38(%rbp)
movl 0x78f4(%rip), %eax      # 0xa924
movl %eax, -0x14(%rbp)
cmpl $0x0, 0x78da(%rip)      # 0xa914
je 0x3054
movq 0x78d5(%rip), %rdi      # 0xa918
movl -0x14(%rbp), %edx
leaq 0x449f(%rip), %rsi      # 0x74ec
movb $0x0, %al
callq 0x2130
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x14(%rbp), %edi
addl $0x1, %edi
leaq -0x18(%rbp), %rsi
callq 0x3620
movl %eax, -0x14(%rbp)
movl -0x18(%rbp), %eax
cmpl 0x78bf(%rip), %eax      # 0xa93c
jl 0x3090
cmpl $0x0, -0x14(%rbp)
je 0x3090
movl -0x14(%rbp), %eax
cmpl 0x7892(%rip), %eax      # 0xa920
jl 0x3095
jmp 0x31a6
cmpl $0x0, 0x7878(%rip)      # 0xa914
je 0x30b6
movq 0x7873(%rip), %rdi      # 0xa918
movl -0x14(%rbp), %edx
leaq 0x445b(%rip), %rsi      # 0x750a
movb $0x0, %al
callq 0x2130
movl -0x14(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x36c0
movq -0x8(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x8(%rbp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x30df
jp 0x30df
jmp 0x3062
movl -0x14(%rbp), %edi
callq 0x3710
movsd %xmm0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
cmpl 0x77e4(%rip), %eax      # 0xa8dc
jne 0x3110
movsd 0x77de(%rip), %xmm1     # 0xa8e0
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
cmpl $0x0, 0x77fd(%rip)      # 0xa914
je 0x3136
movq 0x77f8(%rip), %rdi      # 0xa918
movl -0x14(%rbp), %edx
movsd -0x30(%rbp), %xmm0
leaq 0x43f6(%rip), %rsi      # 0x7525
movb $0x1, %al
callq 0x2130
xorps %xmm0, %xmm0
ucomisd -0x38(%rbp), %xmm0
ja 0x3150
movsd -0x30(%rbp), %xmm1
movsd -0x38(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x31a1
movq -0x8(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x20(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, 0x77a1(%rip)      # 0xa914
je 0x3197
movq 0x779c(%rip), %rdi      # 0xa918
movl -0x1c(%rbp), %edx
movsd -0x30(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
leaq 0x43bb(%rip), %rsi      # 0x754b
movb $0x2, %al
callq 0x2130
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x3062
cmpl $0x0, 0x7767(%rip)      # 0xa914
je 0x31c7
movq 0x7762(%rip), %rdi      # 0xa918
movl -0x1c(%rbp), %edx
leaq 0x43b7(%rip), %rsi      # 0x7577
movb $0x0, %al
callq 0x2130
movl -0x20(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movsd -0x28(%rbp), %xmm0
movq -0x8(%rbp), %rax
movsd %xmm0, (%rax)
movl -0x1c(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)