// File: /workspace/github_c_makefile_star2_O3/pipde[P]deos-songlib/utils/identify/identifyz.c
// Function: main
int
main(int argc,char **argv)
    {
    int argIndex = 1;

    int i,index;
    FILE *in,*out;
    char *source = 0;
    RRA *h;
    static int skip;

    argIndex = processOptions(argc,argv);

    if (argc-argIndex == 0)
        {
        in = stdin;
        out = stdout;
        }
    else if (argc-argIndex == 1)
        {
        source = argv[argIndex];
        in = OpenFile(source,"r");
        out = stdout;
        }
    else if (argc-argIndex == 2)
        {
        source = argv[argIndex];
        in = OpenFile(source,"r");
        out = OpenFile(argv[argIndex+1],"w");
        }
    else
        {
        printf("usage: %s [<input rra file> [<output wave file>]]\n",argv[0]);
        exit(-1);
        }

    h = newRRAHeader();
    
    readRRAHeader(in,h,0);

    if (h->channels != 1)
        Fatal("%s requires a mono audio file\n",argv[0]);

    /* allocate window and zeros */
    windowSize = (h->sampleRate / MinFrequency) * 10;
    window = malloc(sizeof(int) * windowSize);
    zeros = malloc(sizeof(int) * windowSize);
    if (window == 0 || zeros == 0) Fatal("out of memory\n");

    /* skip over the first part */

    skip = initialOffset * h->sampleRate;
    printf("skip is %d\n",skip);
    for (i = 0; i < skip; ++i)
        (void) readRRAAmplitude(in,out,h->bitsPerSample,outputComment);
        printf("skipping %d\n",readRRAAmplitude(in,out,h->bitsPerSample,outputComment));
    //printf("skipping %d\n",readRRAAmplitude(in,out,h->bitsPerSample,outputComment));

    /* read in a segment */

    printf("windowSize is %d\n",windowSize);
    for (i = 0; i < windowSize && !feof(in); ++i)
        {
        window[i] = readRRAAmplitude(in,out,h->bitsPerSample,outputComment);
        //printf("window[%d] is %d\n",i,window[i]);
        }

    printf("smoothing...\n");
    /* heavily smooth the data */
    for (i = 0; i < smoothings; ++i)
        smooth();

    FILE *fp = fopen("smoothed.rra","w");
    fprintf(fp,"RRAUDIO\n");
    fprintf(fp,"%%%%\n");
    for (i = 0; i < windowSize; ++i)
        fprintf(fp,"%d\n",window[i]);
    fclose(fp);

    printf("finding first upward zero crossing\n");
    /* find the first upward zero crossing */
    for (i = 100; i < windowSize-1; ++i)
        if (window[i] < 0 && window[i+1] >= 0)
            break;

    printf("zero at %d\n",i);
    printf("finding remaining upward zero crossings\n");
    /* find remaining upward zero crossings, recording length and count */
    int count = 0;
    int last = i;
    for (++i; i < windowSize-200; ++i)
        {
        if (window[i] < 0 && window[i+1] >= 0)
            {
            zeros[count++] = i - last;
            printf("zeros at %d = %d\n",count-1,zeros[count-1]);
            last = i;
            }
        }

    printf("computing average and score\n");
    /* compute the average and stddev */
    double total = 0.0;
    for (i = 0; i < count; ++i)
        total += zeros[i];
    printf("count is %d\n",count);
    printf("total is %f\n",total);
    double avg = total / count;
    total = 0.0;
    for (i = 0; i < count; ++i)
        total += pow(zeros[i]-avg,2);
    double stddev = pow(total / count,0.5);
    double freq = h->sampleRate / avg;

    printf("avg is %f\n",avg);

    index = -1;
    for (i = 0; index < 0 && i < 120; ++i)
        {
        if (freq >= freqs[i] && freq < freqs[i+1])
            {
            if (freq - freqs[i] >= freqs[i+1] - freq)
                index = i+1;
            else
                index = i;
            }
        }

    printf("index is %d\n",index);
    if (Pitch && source != 0)
        printf("rratune %f %s %s.repitch\n",
            freqs[index] / freq,source,source);
    else if (Pitch)
        printf("rratune %f\n", freqs[index] / freq);
    else if (Note)
        printf("%s\n", notes[index]);
    else if (Frequency)
        printf("%f\n", freq);
    else
        printf("closest note to %f is %s (freq = %f) -- error is %f\n",
            freq,notes[index],freqs[index],stddev);
            
    fclose(in);
    fclose(out);

    return 0;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rdx
movl %edi, %ebx
movl $0x1, %r15d
cmpl $0x2, %edi
movq %rsi, -0x30(%rbp)
jl 0x26a8
movl $0x1, %r15d
leaq 0x2a1e(%rip), %r12      # 0x5020
movslq %r15d, %rax
movq (%rdx,%rax,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x26a8
movb 0x2(%rsi), %r14b
testb %r14b, %r14b
je 0x2621
leaq 0x2(%rsi), %rdi
jmp 0x2626
movq 0x8(%rdx,%rax,8), %rdi
movzbl 0x1(%rsi), %eax
leal -0x66(%rax), %ecx
cmpl $0xa, %ecx
ja 0x266f
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movb $0x1, 0x62ce(%rip)      # 0x8910
jmp 0x266a
callq 0x2080
movq -0x30(%rbp), %rdx
movsd %xmm0, 0x627b(%rip)     # 0x88d0
movb $0x1, %r13b
jmp 0x268d
movb $0x1, 0x62ab(%rip)      # 0x890c
jmp 0x266a
movb $0x1, 0x629e(%rip)      # 0x8908
xorl %r13d, %r13d
jmp 0x268d
cmpl $0x76, %eax
je 0x2d62
xorl %r13d, %r13d
leaq 0x2d00(%rip), %rdi      # 0x5382
xorl %eax, %eax
callq 0x2da0
movq -0x30(%rbp), %rdx
testb %r14b, %r14b
sete %al
andb %r13b, %al
movzbl %al, %eax
addl %eax, %r15d
incl %r15d
cmpl %ebx, %r15d
jl 0x2602
subl %r15d, %ebx
jne 0x26cb
movq 0x5924(%rip), %rax      # 0x7fd8
movq (%rax), %rbx
movq 0x5912(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq $0x0, -0x38(%rbp)
jmp 0x272d
cmpl $0x2, %ebx
je 0x26ff
cmpl $0x1, %ebx
jne 0x2d85
movslq %r15d, %rax
movq (%rdx,%rax,8), %rdi
leaq 0x2e11(%rip), %rsi      # 0x54f8
movq %rdi, -0x38(%rbp)
callq 0x2e3a
movq %rax, %rbx
movq 0x58d6(%rip), %rax      # 0x7fd0
movq (%rax), %rax
jmp 0x272d
movslq %r15d, %r14
movq (%rdx,%r14,8), %rdi
leaq 0x2deb(%rip), %rsi      # 0x54f8
movq %rdi, -0x38(%rbp)
movq %rdx, %r15
callq 0x2e3a
movq %rax, %rbx
movq 0x8(%r15,%r14,8), %rdi
leaq 0x2aff(%rip), %rsi      # 0x5227
callq 0x2e3a
movq %rax, -0x48(%rbp)
callq 0x30ab
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x34d1
cmpl $0x1, (%r12)
je 0x2762
movq -0x30(%rbp), %rax
movq (%rax), %rsi
leaq 0x2b01(%rip), %rdi      # 0x525c
xorl %eax, %eax
callq 0x2da0
xorps %xmm0, %xmm0
cvtsi2sdl 0x4(%r12), %xmm0
movsd %xmm0, -0x30(%rbp)
divsd 0x288f(%rip), %xmm0     # 0x5008
mulsd 0x288f(%rip), %xmm0     # 0x5010
cvttsd2si %xmm0, %eax
movl %eax, 0x6169(%rip)      # 0x88f4
movslq %eax, %r15
shlq $0x2, %r15
movq %r15, %rdi
callq 0x2190
movq %rax, %r13
movq %rax, 0x6154(%rip)      # 0x88f8
movq %r15, %rdi
callq 0x2190
movsd -0x30(%rbp), %xmm0
movq %rax, 0x6148(%rip)      # 0x8900
testq %r13, %r13
setne %cl
testq %rax, %rax
setne %al
testb %al, %cl
jne 0x27e0
leaq 0x2aac(%rip), %rdi      # 0x527b
xorl %eax, %eax
callq 0x2da0
xorps %xmm0, %xmm0
cvtsi2sdl 0x4(%r12), %xmm0
mulsd 0x60e8(%rip), %xmm0     # 0x88d0
cvttsd2si %xmm0, %esi
movl %esi, 0x60fe(%rip)      # 0x88f0
leaq 0x2a91(%rip), %rdi      # 0x528a
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x20c0
cmpl $0x0, 0x60e6(%rip)      # 0x88f0
movq -0x48(%rbp), %r13
jle 0x2836
leaq 0x1a20(%rip), %r15      # 0x4237
movl 0x8(%r12), %edx
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0x39d4
incl %r14d
cmpl 0x60bc(%rip), %r14d     # 0x88f0
jl 0x2817
movl 0x8(%r12), %edx
leaq 0x19f5(%rip), %rcx      # 0x4237
movq %rbx, %rdi
movq %r13, %rsi
callq 0x39d4
leaq 0x2a42(%rip), %rdi      # 0x5296
xorl %r13d, %r13d
movl %eax, %esi
xorl %eax, %eax
callq 0x20c0
movl 0x608e(%rip), %esi      # 0x88f4
leaq 0x2a36(%rip), %rdi      # 0x52a3
xorl %eax, %eax
callq 0x20c0
cmpl $0x0, 0x6079(%rip)      # 0x88f4
jle 0x28c1
xorl %r14d, %r14d
leaq 0x19b0(%rip), %r15      # 0x4237
movq %rbx, %rdi
callq 0x2170
testl %eax, %eax
jne 0x28c1
movl 0x8(%r12), %edx
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
movq %r15, %rcx
callq 0x39d4
movq 0x604a(%rip), %rcx      # 0x88f8
movl %eax, (%rcx,%r14,4)
incq %r14
movslq 0x6038(%rip), %rax      # 0x88f4
cmpq %rax, %r14
jl 0x2887
leaq 0x2ad4(%rip), %rdi      # 0x539c
callq 0x2070
movl 0x6021(%rip), %eax      # 0x88f4
leal -0x4(%rax), %ecx
movq 0x601b(%rip), %rdx      # 0x88f8
cmpl $0x9, %eax
jl 0x2941
movl 0xc(%rdx), %edi
movl $0x4, %esi
movl -0x10(%rdx,%rsi,4), %r8d
movl -0xc(%rdx,%rsi,4), %r9d
shll $0x5, %r8d
leal (%r9,%r9,2), %r9d
movl -0x8(%rdx,%rsi,4), %r10d
addl 0xc(%rdx,%rsi,4), %edi
leal (%r8,%r9,8), %r8d
leal (%r8,%r10,8), %r8d
addl (%rdx,%rsi,4), %r8d
movl 0x8(%rdx,%rsi,4), %r9d
leal (%r8,%r9,2), %r8d
leal (%r8,%rdi,4), %edi
movslq %edi, %rdi
imulq $0x1b4e81b5, %rdi, %rdi # imm = 0x1B4E81B5
movq %rdi, %r8
shrq $0x3f, %r8
sarq $0x23, %rdi
addl %r8d, %edi
movl %edi, (%rdx,%rsi,4)
incq %rsi
cmpq %rsi, %rcx
jne 0x28ea
incl %r13d
cmpl $0x14, %r13d
jne 0x28dd
leaq 0x2964(%rip), %rdi      # 0x52b5
leaq 0x28cf(%rip), %rsi      # 0x5227
callq 0x21d0
movq %rax, %r15
leaq 0x2d20(%rip), %rdi      # 0x5687
movl $0x8, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2220
leaq 0x2942(%rip), %rsi      # 0x52c2
movq %r15, %rdi
xorl %eax, %eax
callq 0x2150
cmpl $0x0, 0x5f63(%rip)      # 0x88f4
jle 0x29c4
leaq 0x2c9b(%rip), %r13      # 0x5635
xorl %r14d, %r14d
movq 0x5f54(%rip), %rax      # 0x88f8
movl (%rax,%r14,4), %edx
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x2150
incq %r14
movslq 0x5f35(%rip), %rax      # 0x88f4
cmpq %rax, %r14
jl 0x299d
movq %r15, %rdi
callq 0x20a0
leaq 0x29d6(%rip), %rdi      # 0x53a9
callq 0x2070
movl 0x5f16(%rip), %eax      # 0x88f4
movl $0x64, %r14d
cmpl $0x66, %eax
jl 0x2a14
decl %eax
movq 0x5f06(%rip), %rcx      # 0x88f8
movl %eax, %edx
movl $0x64, %r14d
cmpl $0x0, (%rcx,%r14,4)
jns 0x2a09
cmpl $0x0, 0x4(%rcx,%r14,4)
jns 0x2a14
incq %r14
cmpq %r14, %rdx
jne 0x29fa
movl %eax, %r14d
leaq 0x28ad(%rip), %rdi      # 0x52c8
xorl %r13d, %r13d
movl %r14d, %esi
xorl %eax, %eax
callq 0x20c0
leaq 0x299d(%rip), %rdi      # 0x53cc
callq 0x2070
leal 0x1(%r14), %ecx
movl 0x5eb6(%rip), %eax      # 0x88f4
leal -0xc8(%rax), %edx
cmpl %edx, %ecx
jge 0x2aef
movl %ecx, %r15d
movq 0x5ea2(%rip), %rcx      # 0x88f8
xorl %r13d, %r13d
cmpl $0x0, (%rcx,%r15,4)
jns 0x2aa1
leal -0x1(%r15), %edx
movl %edx, %edx
cmpl $0x0, 0x8(%rcx,%rdx,4)
js 0x2aa1
movl %r15d, %edx
subl %r14d, %edx
movq 0x5e86(%rip), %rax      # 0x8900
movslq %r13d, %rsi
incl %r13d
movl %edx, (%rax,%rsi,4)
leaq 0x284a(%rip), %rdi      # 0x52d4
xorl %eax, %eax
callq 0x20c0
movq 0x5e60(%rip), %rcx      # 0x88f8
movl 0x5e56(%rip), %eax      # 0x88f4
movl %r15d, %r14d
incq %r15
leal -0xc8(%rax), %edx
cmpl %r15d, %edx
jg 0x2a59
leaq 0x293e(%rip), %rdi      # 0x53f4
callq 0x2070
testl %r13d, %r13d
setg %r14b
jle 0x2b09
movq 0x5e35(%rip), %rax      # 0x8900
movl %r13d, %ecx
xorpd %xmm1, %xmm1
xorl %edx, %edx
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%rdx,4), %xmm0
addsd %xmm0, %xmm1
incq %rdx
cmpq %rdx, %rcx
jne 0x2ad4
movsd %xmm1, -0x30(%rbp)
jmp 0x2b15
leaq 0x28fe(%rip), %rdi      # 0x53f4
callq 0x2070
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorl %r14d, %r14d
jmp 0x2b15
xorl %r14d, %r14d
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
leaq 0x27ca(%rip), %rdi      # 0x52e6
movl %r13d, %esi
xorl %eax, %eax
callq 0x20c0
leaq 0x27c6(%rip), %rdi      # 0x52f3
movsd -0x30(%rbp), %xmm0
movb $0x1, %al
callq 0x20c0
xorps %xmm1, %xmm1
cvtsi2sd %r13d, %xmm1
movsd -0x30(%rbp), %xmm3
divsd %xmm1, %xmm3
xorpd %xmm0, %xmm0
testb %r14b, %r14b
je 0x2b7b
movq 0x5da6(%rip), %rax      # 0x8900
movl %r13d, %ecx
xorl %edx, %edx
xorps %xmm2, %xmm2
cvtsi2sdl (%rax,%rdx,4), %xmm2
subsd %xmm3, %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
incq %rdx
cmpq %rdx, %rcx
jne 0x2b5f
divsd %xmm1, %xmm0
movsd 0x2491(%rip), %xmm1     # 0x5018
movsd %xmm3, -0x30(%rbp)
callq 0x2090
movsd %xmm0, -0x50(%rbp)
xorps %xmm1, %xmm1
cvtsi2sdl 0x4(%r12), %xmm1
movsd -0x30(%rbp), %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
leaq 0x274b(%rip), %rdi      # 0x5300
movb $0x1, %al
callq 0x20c0
movsd -0x40(%rbp), %xmm3
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %eax, %eax
leaq 0x5580(%rip), %r12      # 0x8150
movq -0x48(%rbp), %r14
movsd (%r12,%rax,8), %xmm0
ucomisd %xmm0, %xmm3
jb 0x2c04
movsd 0x8(%r12,%rax,8), %xmm1
ucomisd %xmm3, %xmm1
jbe 0x2c04
movapd %xmm3, %xmm2
subsd %xmm0, %xmm2
subsd %xmm3, %xmm1
ucomisd %xmm1, %xmm2
movl %eax, %r15d
sbbl $-0x1, %r15d
testl %r15d, %r15d
jns 0x2c16
leaq 0x1(%rax), %rcx
cmpq $0x77, %rax
movq %rcx, %rax
jb 0x2bd4
leaq 0x26ee(%rip), %rdi      # 0x530b
movl %r15d, %esi
xorl %eax, %eax
callq 0x20c0
movb 0x5cdb(%rip), %al       # 0x8908
movl %eax, %ecx
notb %cl
movq -0x38(%rbp), %rsi
testq %rsi, %rsi
sete %dl
orb %cl, %dl
testb $0x1, %dl
jne 0x2c66
movslq %r15d, %rax
movsd (%r12,%rax,8), %xmm0
divsd -0x40(%rbp), %xmm0
leaq 0x26c1(%rip), %rdi      # 0x5318
movq %rsi, %rdx
movb $0x1, %al
callq 0x20c0
jmp 0x2ceb
testb $0x1, %al
movsd -0x40(%rbp), %xmm2
je 0x2c8c
movslq %r15d, %rax
movsd (%r12,%rax,8), %xmm0
divsd %xmm2, %xmm0
leaq 0x26af(%rip), %rdi      # 0x5332
movb $0x1, %al
callq 0x20c0
jmp 0x2ceb
cmpb $0x1, 0x5c79(%rip)      # 0x890c
jne 0x2caa
movslq %r15d, %rax
leaq 0x5811(%rip), %rcx      # 0x84b0
movq (%rcx,%rax,8), %rdi
callq 0x2070
jmp 0x2ceb
cmpb $0x1, 0x5c5f(%rip)      # 0x8910
jne 0x2cc0
leaq 0x2680(%rip), %rdi      # 0x533a
movapd %xmm2, %xmm0
jmp 0x2c83
movslq %r15d, %rax
leaq 0x57e6(%rip), %rcx      # 0x84b0
movq (%rcx,%rax,8), %rsi
movsd (%r12,%rax,8), %xmm1
leaq 0x2663(%rip), %rdi      # 0x533e
movapd %xmm2, %xmm0
movsd -0x50(%rbp), %xmm2
movb $0x3, %al
callq 0x20c0
movq %rbx, %rdi
callq 0x20a0
movq %r14, %rdi
callq 0x20a0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x26fd(%rip), %rdi      # 0x5410
callq 0x2070
leaq 0x2704(%rip), %rdi      # 0x5423
callq 0x2070
leaq 0x2724(%rip), %rdi      # 0x544f
callq 0x2070
leaq 0x2748(%rip), %rdi      # 0x547f
callq 0x2070
leaq 0x2762(%rip), %rdi      # 0x54a5
callq 0x2070
leaq 0x2787(%rip), %rdi      # 0x54d6
callq 0x2070
leaq 0x279f(%rip), %rdi      # 0x54fa
callq 0x2070
jmp 0x2d7e
movq 0x53d7(%rip), %rsi      # 0x8140
movq 0x53d8(%rip), %rdx      # 0x8148
leaq 0x25fc(%rip), %rdi      # 0x5373
xorl %eax, %eax
callq 0x20c0
xorl %edi, %edi
callq 0x2210
movq (%rdx), %rsi
leaq 0x249a(%rip), %rdi      # 0x5229
xorl %eax, %eax
callq 0x20c0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2210