// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, -0x14(%rbp)
movq $0x0, -0x38(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x2ac0
movl %eax, -0x14(%rbp)
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x0, %eax
jne 0x2373
movq 0x6c7f(%rip), %rax      # 0x8fd8
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq 0x6c69(%rip), %rax      # 0x8fd0
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x2427
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x1, %eax
jne 0x23b2
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq 0x45ef(%rip), %rsi      # 0x6988
callq 0x2ee0
movq %rax, -0x28(%rbp)
movq 0x6c27(%rip), %rax      # 0x8fd0
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x2425
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x2, %eax
jne 0x2404
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq 0x45b0(%rip), %rsi      # 0x6988
callq 0x2ee0
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x3e46(%rip), %rsi      # 0x623f
callq 0x2ee0
movq %rax, -0x30(%rbp)
jmp 0x2423
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x3e2f(%rip), %rdi      # 0x6241
movb $0x0, %al
callq 0x2090
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21d0
jmp 0x2425
jmp 0x2427
callq 0x33c0
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x38c0
movq -0x40(%rbp), %rax
cmpl $0x1, (%rax)
je 0x245f
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x3e1c(%rip), %rdi      # 0x6274
movb $0x0, %al
callq 0x2d90
movq -0x40(%rbp), %rax
cvtsi2sdl 0x4(%rax), %xmm0
divsd 0x7440(%rip), %xmm0     # 0x98b0
movsd 0x3b90(%rip), %xmm1     # 0x6008
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x7456(%rip)      # 0x98dc
movslq 0x744f(%rip), %rdi      # 0x98dc
shlq $0x2, %rdi
callq 0x2150
movq %rax, 0x7443(%rip)      # 0x98e0
movslq 0x7438(%rip), %rdi      # 0x98dc
shlq $0x2, %rdi
callq 0x2150
movq %rax, 0x7434(%rip)      # 0x98e8
cmpq $0x0, 0x7424(%rip)      # 0x98e0
je 0x24c8
cmpq $0x0, 0x7422(%rip)      # 0x98e8
jne 0x24d6
leaq 0x3dc4(%rip), %rdi      # 0x6293
movb $0x0, %al
callq 0x2d90
movsd 0x73da(%rip), %xmm0     # 0x98b8
movq -0x40(%rbp), %rax
cvtsi2sdl 0x4(%rax), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x73e3(%rip)      # 0x98d8
movl 0x73dd(%rip), %esi      # 0x98d8
leaq 0x3da0(%rip), %rdi      # 0x62a2
movb $0x0, %al
callq 0x2090
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl 0x73bf(%rip), %eax      # 0x98d8
jge 0x2541
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rax
movl 0x8(%rax), %edx
leaq 0x276f(%rip), %rcx      # 0x4ca0
callq 0x3ff0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x2510
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rax
movl 0x8(%rax), %edx
leaq 0x2749(%rip), %rcx      # 0x4ca0
callq 0x3ff0
movl %eax, %esi
leaq 0x3d49(%rip), %rdi      # 0x62ae
movb $0x0, %al
callq 0x2090
movl 0x736a(%rip), %esi      # 0x98dc
leaq 0x3d42(%rip), %rdi      # 0x62bb
movb $0x0, %al
callq 0x2090
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %ecx
xorl %eax, %eax
cmpl 0x734a(%rip), %ecx      # 0x98dc
movb %al, -0x71(%rbp)
jge 0x25ab
movq -0x28(%rbp), %rdi
callq 0x2130
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0x25b4
jmp 0x25ea
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rax
movl 0x8(%rax), %edx
leaq 0x26d6(%rip), %rcx      # 0x4ca0
callq 0x3ff0
movl %eax, %edx
movq 0x7308(%rip), %rax      # 0x98e0
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x2587
leaq 0x3cdc(%rip), %rdi      # 0x62cd
movb $0x0, %al
callq 0x2090
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl 0x72b8(%rip), %eax      # 0x98c0
jge 0x261a
callq 0x2cb0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x25ff
leaq 0x3cba(%rip), %rdi      # 0x62db
leaq 0x3c17(%rip), %rsi      # 0x623f
callq 0x2190
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
leaq 0x403b(%rip), %rsi      # 0x6677
movb $0x0, %al
callq 0x2110
movq -0x48(%rbp), %rdi
leaq 0x3c9a(%rip), %rsi      # 0x62e8
movb $0x0, %al
callq 0x2110
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl 0x7277(%rip), %eax      # 0x98dc
jge 0x2692
movq -0x48(%rbp), %rdi
movq 0x726e(%rip), %rax      # 0x98e0
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x3fa5(%rip), %rsi      # 0x6625
movb $0x0, %al
callq 0x2110
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x265c
movq -0x48(%rbp), %rdi
callq 0x2070
leaq 0x3c4c(%rip), %rdi      # 0x62ee
movb $0x0, %al
callq 0x2090
movl $0x64, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl 0x7223(%rip), %ecx      # 0x98dc
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x26f6
movq 0x7219(%rip), %rax      # 0x98e0
movslq -0x18(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jge 0x26e9
movq 0x7208(%rip), %rax      # 0x98e0
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
jl 0x26e9
jmp 0x26f6
jmp 0x26eb
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x26b0
movl -0x18(%rbp), %esi
leaq 0x3c12(%rip), %rdi      # 0x6312
movb $0x0, %al
callq 0x2090
leaq 0x3c10(%rip), %rdi      # 0x631e
movb $0x0, %al
callq 0x2090
movl $0x0, -0x4c(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl 0x71a8(%rip), %ecx      # 0x98dc
subl $0xc8, %ecx
cmpl %ecx, %eax
jge 0x27c4
movq 0x7197(%rip), %rax      # 0x98e0
movslq -0x18(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jge 0x27b4
movq 0x7186(%rip), %rax      # 0x98e0
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
jl 0x27b4
movl -0x18(%rbp), %edx
subl -0x50(%rbp), %edx
movq 0x7172(%rip), %rax      # 0x98e8
movl -0x4c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x4c(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x4c(%rbp), %esi
subl $0x1, %esi
movq 0x7154(%rip), %rax      # 0x98e8
movl -0x4c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
leaq 0x3ba0(%rip), %rdi      # 0x6347
movb $0x0, %al
callq 0x2090
movl -0x18(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0x27b6
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x272b
leaq 0x3b8e(%rip), %rdi      # 0x6359
movb $0x0, %al
callq 0x2090
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x280e
movq 0x70f8(%rip), %rax      # 0x98e8
movslq -0x18(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x27e1
movl -0x4c(%rbp), %esi
leaq 0x3b5e(%rip), %rdi      # 0x6376
movb $0x0, %al
callq 0x2090
movsd -0x58(%rbp), %xmm0
leaq 0x3b58(%rip), %rdi      # 0x6383
movb $0x1, %al
callq 0x2090
movsd -0x58(%rbp), %xmm0
cvtsi2sdl -0x4c(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x2893
movq 0x7085(%rip), %rax      # 0x98e8
movslq -0x18(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
subsd -0x60(%rbp), %xmm0
movsd 0x379f(%rip), %xmm1     # 0x6018
callq 0x2060
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x2854
movsd -0x58(%rbp), %xmm0
cvtsi2sdl -0x4c(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x3767(%rip), %xmm1     # 0x6010
callq 0x2060
movsd %xmm0, -0x68(%rbp)
movq -0x40(%rbp), %rax
cvtsi2sdl 0x4(%rax), %xmm0
divsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x60(%rbp), %xmm0
leaq 0x3abe(%rip), %rdi      # 0x6390
movb $0x1, %al
callq 0x2090
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x18(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x1c(%rbp)
movb %al, -0x72(%rbp)
jge 0x28fc
cmpl $0x78, -0x18(%rbp)
setl %al
movb %al, -0x72(%rbp)
movb -0x72(%rbp), %al
testb $0x1, %al
jne 0x2908
jmp 0x2997
movsd -0x70(%rbp), %xmm0
movslq -0x18(%rbp), %rcx
leaq 0x6818(%rip), %rax      # 0x9130
ucomisd (%rax,%rcx,8), %xmm0
jb 0x2987
movsd -0x70(%rbp), %xmm1
movl -0x18(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x67fc(%rip), %rax      # 0x9130
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2987
movsd -0x70(%rbp), %xmm0
movslq -0x18(%rbp), %rcx
leaq 0x67e1(%rip), %rax      # 0x9130
subsd (%rax,%rcx,8), %xmm0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x67cc(%rip), %rax      # 0x9130
movsd (%rax,%rcx,8), %xmm1
subsd -0x70(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x297f
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2985
movl -0x18(%rbp), %eax
movl %eax, -0x1c(%rbp)
jmp 0x2987
jmp 0x2989
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x28e7
movl -0x1c(%rbp), %esi
leaq 0x39fa(%rip), %rdi      # 0x639b
movb $0x0, %al
callq 0x2090
cmpl $0x0, 0x6f41(%rip)      # 0x98f0
je 0x29e8
cmpq $0x0, -0x38(%rbp)
je 0x29e8
movslq -0x1c(%rbp), %rcx
leaq 0x676d(%rip), %rax      # 0x9130
movsd (%rax,%rcx,8), %xmm0
divsd -0x70(%rbp), %xmm0
movq -0x38(%rbp), %rsi
movq -0x38(%rbp), %rdx
leaq 0x39cc(%rip), %rdi      # 0x63a8
movb $0x1, %al
callq 0x2090
jmp 0x2a9c
cmpl $0x0, 0x6f01(%rip)      # 0x98f0
je 0x2a19
movslq -0x1c(%rbp), %rcx
leaq 0x6734(%rip), %rax      # 0x9130
movsd (%rax,%rcx,8), %xmm0
divsd -0x70(%rbp), %xmm0
leaq 0x39b5(%rip), %rdi      # 0x63c2
movb $0x1, %al
callq 0x2090
jmp 0x2a9a
cmpl $0x0, 0x6ed4(%rip)      # 0x98f4
je 0x2a41
movslq -0x1c(%rbp), %rcx
leaq 0x6a63(%rip), %rax      # 0x9490
movq (%rax,%rcx,8), %rsi
leaq 0x3ea2(%rip), %rdi      # 0x68da
movb $0x0, %al
callq 0x2090
jmp 0x2a98
cmpl $0x0, 0x6eb0(%rip)      # 0x98f8
je 0x2a5f
movsd -0x70(%rbp), %xmm0
leaq 0x3974(%rip), %rdi      # 0x63ca
movb $0x1, %al
callq 0x2090
jmp 0x2a96
movsd -0x70(%rbp), %xmm0
movslq -0x1c(%rbp), %rcx
leaq 0x6a21(%rip), %rax      # 0x9490
movq (%rax,%rcx,8), %rsi
movslq -0x1c(%rbp), %rcx
leaq 0x66b2(%rip), %rax      # 0x9130
movsd (%rax,%rcx,8), %xmm1
movsd -0x68(%rbp), %xmm2
leaq 0x393f(%rip), %rdi      # 0x63ce
movb $0x3, %al
callq 0x2090
jmp 0x2a98
jmp 0x2a9a
jmp 0x2a9c
movq -0x28(%rbp), %rdi
callq 0x2070
movq -0x30(%rbp), %rdi
callq 0x2070
xorl %eax, %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)