// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %rsi, %rdx
movl %edi, %ebx
movl $0x1, %r15d
cmpl $0x2, %edi
movq %rsi, -0x30(%rbp)
jl 0x2678
movl $0x1, %r15d
leaq 0x2a4e(%rip), %r12      # 0x5020
movslq %r15d, %rax
movq (%rdx,%rax,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x2678
movb 0x2(%rsi), %r14b
testb %r14b, %r14b
je 0x25f1
leaq 0x2(%rsi), %rdi
jmp 0x25f6
movq 0x8(%rdx,%rax,8), %rdi
movzbl 0x1(%rsi), %eax
leal -0x66(%rax), %ecx
cmpl $0xa, %ecx
ja 0x263f
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movb $0x1, 0x62fe(%rip)      # 0x8910
jmp 0x263a
callq 0x2080
movq -0x30(%rbp), %rdx
movsd %xmm0, 0x62ab(%rip)     # 0x88d0
movb $0x1, %r13b
jmp 0x265d
movb $0x1, 0x62db(%rip)      # 0x890c
jmp 0x263a
movb $0x1, 0x62ce(%rip)      # 0x8908
xorl %r13d, %r13d
jmp 0x265d
cmpl $0x76, %eax
je 0x2ce8
xorl %r13d, %r13d
leaq 0x2d30(%rip), %rdi      # 0x5382
xorl %eax, %eax
callq 0x2d28
movq -0x30(%rbp), %rdx
testb %r14b, %r14b
sete %al
andb %r13b, %al
movzbl %al, %eax
addl %eax, %r15d
incl %r15d
cmpl %ebx, %r15d
jl 0x25d2
subl %r15d, %ebx
jne 0x269b
movq 0x5954(%rip), %rax      # 0x7fd8
movq (%rax), %r14
movq 0x5942(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq $0x0, -0x38(%rbp)
jmp 0x26fd
cmpl $0x2, %ebx
je 0x26cf
cmpl $0x1, %ebx
jne 0x2d0b
movslq %r15d, %rax
movq (%rdx,%rax,8), %rdi
leaq 0x2e41(%rip), %rsi      # 0x54f8
movq %rdi, -0x38(%rbp)
callq 0x2dc2
movq %rax, %r14
movq 0x5906(%rip), %rax      # 0x7fd0
movq (%rax), %rax
jmp 0x26fd
movslq %r15d, %rbx
movq (%rdx,%rbx,8), %rdi
leaq 0x2e1b(%rip), %rsi      # 0x54f8
movq %rdi, -0x38(%rbp)
movq %rdx, %r15
callq 0x2dc2
movq %rax, %r14
movq 0x8(%r15,%rbx,8), %rdi
leaq 0x2b2f(%rip), %rsi      # 0x5227
callq 0x2dc2
movq %rax, -0x48(%rbp)
callq 0x303f
movq %rax, %r12
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x344c
cmpl $0x1, (%r12)
je 0x2732
movq -0x30(%rbp), %rax
movq (%rax), %rsi
leaq 0x2b31(%rip), %rdi      # 0x525c
xorl %eax, %eax
callq 0x2d28
xorps %xmm0, %xmm0
cvtsi2sdl 0x4(%r12), %xmm0
divsd 0x28c4(%rip), %xmm0     # 0x5008
mulsd 0x28c4(%rip), %xmm0     # 0x5010
cvttsd2si %xmm0, %eax
movl %eax, 0x619e(%rip)      # 0x88f4
movslq %eax, %r15
shlq $0x2, %r15
movq %r15, %rdi
callq 0x2190
movq %rax, %r13
movq %rax, 0x6189(%rip)      # 0x88f8
movq %r15, %rdi
callq 0x2190
movq %rax, 0x6182(%rip)      # 0x8900
testq %r13, %r13
setne %cl
testq %rax, %rax
setne %al
testb %al, %cl
jne 0x279c
leaq 0x2ae6(%rip), %rdi      # 0x527b
xorl %eax, %eax
callq 0x2d28
xorps %xmm0, %xmm0
cvtsi2sdl 0x4(%r12), %xmm0
mulsd 0x6122(%rip), %xmm0     # 0x88d0
cvttsd2si %xmm0, %esi
movl %esi, 0x6138(%rip)      # 0x88f0
leaq 0x2acb(%rip), %rdi      # 0x528a
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x20c0
cmpl $0x0, 0x6121(%rip)      # 0x88f0
movq -0x48(%rbp), %r13
jle 0x27f9
leaq 0x198d(%rip), %r15      # 0x4169
movl 0x8(%r12), %edx
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0x3938
incl %ebx
cmpl 0x60f9(%rip), %ebx      # 0x88f0
jl 0x27dc
movl 0x8(%r12), %edx
leaq 0x1964(%rip), %rcx      # 0x4169
movq %r14, %rdi
movq %r13, %rsi
callq 0x3938
leaq 0x2a7f(%rip), %rdi      # 0x5296
xorl %r13d, %r13d
movl %eax, %esi
xorl %eax, %eax
callq 0x20c0
movl 0x60cb(%rip), %esi      # 0x88f4
leaq 0x2a73(%rip), %rdi      # 0x52a3
xorl %eax, %eax
callq 0x20c0
cmpl $0x0, 0x60b6(%rip)      # 0x88f4
jle 0x2882
xorl %ebx, %ebx
leaq 0x1920(%rip), %r15      # 0x4169
movq %r14, %rdi
callq 0x2170
testl %eax, %eax
jne 0x2882
movl 0x8(%r12), %edx
movq %r14, %rdi
movq -0x48(%rbp), %rsi
movq %r15, %rcx
callq 0x3938
movq 0x6088(%rip), %rcx      # 0x88f8
movl %eax, (%rcx,%rbx,4)
incq %rbx
movslq 0x6077(%rip), %rax      # 0x88f4
cmpq %rax, %rbx
jl 0x2849
leaq 0x2b13(%rip), %rdi      # 0x539c
callq 0x2070
movl 0x6060(%rip), %eax      # 0x88f4
leal -0x4(%rax), %ecx
movq 0x605a(%rip), %rdx      # 0x88f8
cmpl $0x9, %eax
jl 0x2902
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 0x28ab
incl %r13d
cmpl $0x14, %r13d
jne 0x289e
leaq 0x29a3(%rip), %rdi      # 0x52b5
leaq 0x290e(%rip), %rsi      # 0x5227
callq 0x21d0
movq %rax, %r15
leaq 0x2d5f(%rip), %rdi      # 0x5687
movl $0x8, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2220
leaq 0x2981(%rip), %rsi      # 0x52c2
movq %r15, %rdi
xorl %eax, %eax
callq 0x2150
cmpl $0x0, 0x5fa2(%rip)      # 0x88f4
jle 0x2983
leaq 0x2cda(%rip), %r13      # 0x5635
xorl %ebx, %ebx
movq 0x5f94(%rip), %rax      # 0x88f8
movl (%rax,%rbx,4), %edx
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x2150
incq %rbx
movslq 0x5f76(%rip), %rax      # 0x88f4
cmpq %rax, %rbx
jl 0x295d
movq %r15, %rdi
callq 0x20a0
leaq 0x2a17(%rip), %rdi      # 0x53a9
callq 0x2070
movl 0x5f57(%rip), %eax      # 0x88f4
movl $0x64, %ebx
cmpl $0x66, %eax
jl 0x29ce
decl %eax
movq 0x5f48(%rip), %rcx      # 0x88f8
movl %eax, %edx
movl $0x64, %ebx
cmpl $0x0, (%rcx,%rbx,4)
jns 0x29c4
cmpl $0x0, 0x4(%rcx,%rbx,4)
jns 0x29ce
incq %rbx
cmpq %rbx, %rdx
jne 0x29b7
movl %eax, %ebx
movq %r14, -0x58(%rbp)
leaq 0x28ef(%rip), %rdi      # 0x52c8
xorl %r13d, %r13d
movl %ebx, %esi
xorl %eax, %eax
callq 0x20c0
leaq 0x29e0(%rip), %rdi      # 0x53cc
callq 0x2070
leal 0x1(%rbx), %eax
movl $0xffffff38, %r14d      # imm = 0xFFFFFF38
movl 0x5ef4(%rip), %ecx      # 0x88f4
addl %r14d, %ecx
cmpl %ecx, %eax
jge 0x2a5f
movl %eax, %r15d
xorl %r13d, %r13d
movq 0x5ee4(%rip), %rax      # 0x88f8
cmpl $0x0, (%rax,%r15,4)
jns 0x2a4e
leal -0x1(%r15), %ecx
movl %ecx, %ecx
cmpl $0x0, 0x8(%rax,%rcx,4)
js 0x2a4e
movl %r15d, %edx
subl %ebx, %edx
movq 0x5ecc(%rip), %rax      # 0x8900
movslq %r13d, %rsi
incl %r13d
movl %edx, (%rax,%rsi,4)
leaq 0x2890(%rip), %rdi      # 0x52d4
xorl %eax, %eax
callq 0x20c0
movl %r15d, %ebx
incq %r15
movl 0x5e9d(%rip), %eax      # 0x88f4
addl %r14d, %eax
cmpl %r15d, %eax
jg 0x2a0d
leaq 0x298e(%rip), %rdi      # 0x53f4
callq 0x2070
movl %r13d, %r15d
xorpd %xmm1, %xmm1
testl %r13d, %r13d
jle 0x2a94
movq 0x5e82(%rip), %rax      # 0x8900
xorl %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%rcx,4), %xmm0
addsd %xmm0, %xmm1
incq %rcx
cmpq %rcx, %r15
jne 0x2a80
movsd %xmm1, -0x30(%rbp)
leaq 0x2846(%rip), %rdi      # 0x52e6
movl %r13d, %esi
xorl %eax, %eax
callq 0x20c0
leaq 0x2842(%rip), %rdi      # 0x52f3
movsd -0x30(%rbp), %xmm0
movb $0x1, %al
callq 0x20c0
movsd -0x30(%rbp), %xmm2
cvtsi2sd %r13d, %xmm3
divsd %xmm3, %xmm2
testl %r13d, %r13d
movq -0x58(%rbp), %rbx
xorpd %xmm0, %xmm0
jle 0x2afd
movq 0x5e21(%rip), %rax      # 0x8900
xorl %ecx, %ecx
xorps %xmm1, %xmm1
cvtsi2sdl (%rax,%rcx,4), %xmm1
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rcx
cmpq %rcx, %r15
jne 0x2ae1
divsd %xmm3, %xmm0
movsd 0x250f(%rip), %xmm1     # 0x5018
movsd %xmm2, -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 0x27c9(%rip), %rdi      # 0x5300
movb $0x1, %al
callq 0x20c0
movsd -0x40(%rbp), %xmm3
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %eax, %eax
leaq 0x55fe(%rip), %r12      # 0x8150
movq -0x48(%rbp), %r14
movsd (%r12,%rax,8), %xmm0
ucomisd %xmm0, %xmm3
jb 0x2b8a
movsd 0x8(%r12,%rax,8), %xmm1
ucomisd %xmm3, %xmm1
jbe 0x2b8a
movapd %xmm3, %xmm2
subsd %xmm0, %xmm2
subsd %xmm3, %xmm1
ucomisd %xmm1, %xmm2
jb 0x2b87
leal 0x1(%rax), %r15d
jmp 0x2b8a
movl %eax, %r15d
testl %r15d, %r15d
jns 0x2b9c
leaq 0x1(%rax), %rcx
cmpq $0x77, %rax
movq %rcx, %rax
jb 0x2b56
leaq 0x2768(%rip), %rdi      # 0x530b
movl %r15d, %esi
xorl %eax, %eax
callq 0x20c0
movb 0x5d55(%rip), %al       # 0x8908
movl %eax, %ecx
notb %cl
movq -0x38(%rbp), %rsi
testq %rsi, %rsi
sete %dl
orb %cl, %dl
testb $0x1, %dl
jne 0x2bec
movslq %r15d, %rax
movsd (%r12,%rax,8), %xmm0
divsd -0x40(%rbp), %xmm0
leaq 0x273b(%rip), %rdi      # 0x5318
movq %rsi, %rdx
movb $0x1, %al
callq 0x20c0
jmp 0x2c71
testb $0x1, %al
movsd -0x40(%rbp), %xmm2
je 0x2c12
movslq %r15d, %rax
movsd (%r12,%rax,8), %xmm0
divsd %xmm2, %xmm0
leaq 0x2729(%rip), %rdi      # 0x5332
movb $0x1, %al
callq 0x20c0
jmp 0x2c71
cmpb $0x1, 0x5cf3(%rip)      # 0x890c
jne 0x2c30
movslq %r15d, %rax
leaq 0x588b(%rip), %rcx      # 0x84b0
movq (%rcx,%rax,8), %rdi
callq 0x2070
jmp 0x2c71
cmpb $0x1, 0x5cd9(%rip)      # 0x8910
jne 0x2c46
leaq 0x26fa(%rip), %rdi      # 0x533a
movapd %xmm2, %xmm0
jmp 0x2c09
movslq %r15d, %rax
leaq 0x5860(%rip), %rcx      # 0x84b0
movq (%rcx,%rax,8), %rsi
movsd (%r12,%rax,8), %xmm1
leaq 0x26dd(%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 $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2777(%rip), %rdi      # 0x5410
callq 0x2070
leaq 0x277e(%rip), %rdi      # 0x5423
callq 0x2070
leaq 0x279e(%rip), %rdi      # 0x544f
callq 0x2070
leaq 0x27c2(%rip), %rdi      # 0x547f
callq 0x2070
leaq 0x27dc(%rip), %rdi      # 0x54a5
callq 0x2070
leaq 0x2801(%rip), %rdi      # 0x54d6
callq 0x2070
leaq 0x2819(%rip), %rdi      # 0x54fa
callq 0x2070
jmp 0x2d04
movq 0x5451(%rip), %rsi      # 0x8140
movq 0x5452(%rip), %rdx      # 0x8148
leaq 0x2676(%rip), %rdi      # 0x5373
xorl %eax, %eax
callq 0x20c0
xorl %edi, %edi
callq 0x2210
movq (%rdx), %rsi
leaq 0x2514(%rip), %rdi      # 0x5229
xorl %eax, %eax
callq 0x20c0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2210
nop