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

    int i,maxValue;
    int threshold;
    FILE *in,*out;
    RRA *h;

    argIndex = processOptions(argc,argv);

    if (argc-argIndex == 0)
        {
        in = stdin;
        out = stdout;
        }
    else if (argc-argIndex == 1)
        {
        in = OpenFile(argv[argIndex],"r");
        out = stdout;
        }
    else if (argc-argIndex == 2)
        {
        in = OpenFile(argv[argIndex],"r");
        out = OpenFile(argv[argIndex+1],"w");
        }
    else
        {
        printf("usage: %s -fN [<input rra file> [<output rra 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]);

    //printf("%f,%f\n",pow(2,h->bitsPerSample-1)-1,pow(factor,thresholdDb));

    maxValue = pow(2,h->bitsPerSample-1)-1;
    threshold = maxValue * pow(factor,thresholdDb);

    double volumeUpDelta =  1.0 / secondsToSamples(volumeUpRamp);
    double volumeDownDelta =  1.0 / secondsToSamples(volumeDownRamp);
    int volumeUpWaitSamples =  secondsToSamples(volumeUpWait);
    int volumeDownWaitSamples =  secondsToSamples(volumeDownWait);

    window = makeWindow();

    fprintf(stderr,"threshold: %d\n",threshold);
    fprintf(stderr,"amplify; %f\n",amplify);
    fprintf(stderr,"volume down wait; %d\n",volumeDownWaitSamples);
    fprintf(stderr,"volume down delta; %f\n",volumeDownDelta);

    writeRRAHeader(out,h,"modifiedBy: silence",0);

    int reducing = 0;
    double transitionAttenuation = 1;
    int transitionSamples = 0;
    int bottomMessage = 1;
    int topMessage = 1;

    for (i = 0; !feof(in); ++i)
        {
        int amp = readRRAAmplitude(in,out,h->bitsPerSample,outputComment);

        if (rms(i,amp) < threshold && i > windowSize)
            {
            if (!reducing) 
                {
                reducing = 1;
                transitionSamples = 0;
                fprintf(stderr,"waiting to reduce volume at %d samples\n",
                    i);
                fprintf(stderr,"attenuation is %f\n",transitionAttenuation);
                bottomMessage = 0;
                //getchar();
                }
            else if (transitionSamples < volumeDownWaitSamples)
                {
                ++transitionSamples;
                }
            else
                {
                transitionAttenuation -= volumeDownDelta;
                //fprintf(stderr,"reducing volume to %f at %d samples\n",
                    //transitionAttenuation,i);
                }
            }
        else if (i > windowSize)
            {
            if (reducing) 
                {
                reducing = 0;
                transitionSamples = 0;
                fprintf(stderr,"starting to increasing volume at %d samples\n",
                    i);
                fprintf(stderr,"attenuation is %f\n",transitionAttenuation);
                topMessage = 0;
                //getchar();
                }
            else if (transitionSamples < volumeUpWaitSamples)
                {
                ++transitionSamples;
                }
            else
                {
                transitionAttenuation += volumeUpDelta;
                //fprintf(stderr,"increasing volume to %f at %d samples\n",
                    //transitionAttenuation,i);
                }
            }
        if (transitionAttenuation > 1)
            {
            if (!topMessage)
                {
                fprintf(stderr,"at volume top\n");
                topMessage = 1;
                //getchar();
                }
            transitionAttenuation = 1;
            }
        else if (transitionAttenuation < 0)
            {
            if (!bottomMessage)
                {
                fprintf(stderr,"at volume bottom\n");
                bottomMessage = 1;
                //getchar();
                }
            transitionAttenuation = 0;
            }

        fprintf(out,"%d\n",(int) (amplify * amp * transitionAttenuation));
        }

    if (i != h->samples + 1)
        fprintf(stderr,"WARNING: %d samples found, %d expected\n",i,h->samples);

    fclose(in);
    fclose(out);

    return 0;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r12
movl %edi, %ebx
pushq $0x1
popq %r15
movslq %r15d, %r14
cmpl %r15d, %ebx
jle 0x251a
movq (%r12,%r14,8), %rsi
cmpb $0x2d, (%rsi)
jne 0x251a
movb 0x2(%rsi), %r13b
testb %r13b, %r13b
je 0x245e
leaq 0x2(%rsi), %rdi
jmp 0x2463
movq 0x8(%r12,%r14,8), %rdi
movzbl 0x1(%rsi), %eax
cmpl $0x44, %eax
je 0x24d7
cmpl $0x55, %eax
je 0x24c8
cmpl $0x61, %eax
je 0x24f3
cmpl $0x64, %eax
je 0x24b9
cmpl $0x74, %eax
je 0x24e6
cmpl $0x75, %eax
je 0x24aa
cmpl $0x68, %eax
je 0x2a23
cmpl $0x76, %eax
je 0x2bb6
xorl %r14d, %r14d
leaq 0x6d48(%rip), %rdi      # 0x91e9
xorl %eax, %eax
callq 0x6790
jmp 0x2503
callq 0x20b0
movsd %xmm0, 0xbd11(%rip)     # 0xe1c8
jmp 0x2500
callq 0x20b0
movsd %xmm0, 0xbd0a(%rip)     # 0xe1d0
jmp 0x2500
callq 0x20b0
movsd %xmm0, 0xbce3(%rip)     # 0xe1b8
jmp 0x2500
callq 0x20b0
movsd %xmm0, 0xbcdc(%rip)     # 0xe1c0
jmp 0x2500
callq 0x22a0
movl %eax, 0xbcbf(%rip)      # 0xe1b0
jmp 0x2500
callq 0x20b0
movsd %xmm0, 0xbcd8(%rip)     # 0xe1d8
movb $0x1, %r14b
testb %r13b, %r13b
sete %al
andb %r14b, %al
movzbl %al, %eax
addl %eax, %r15d
incl %r15d
jmp 0x2436
subl %r15d, %ebx
jne 0x252b
movq 0xbab2(%rip), %rax      # 0xdfd8
movq (%rax), %r15
jmp 0x254c
cmpl $0x2, %ebx
je 0x2558
cmpl $0x1, %ebx
jne 0x2bd9
movq (%r12,%r14,8), %rdi
leaq 0x70b3(%rip), %rsi      # 0x95f7
callq 0x68c0
movq %rax, %r15
movq 0xba7d(%rip), %rax      # 0xdfd0
movq (%rax), %rax
jmp 0x257c
movq (%r12,%r14,8), %rdi
leaq 0x7094(%rip), %rsi      # 0x95f7
callq 0x68c0
movq %rax, %r15
movq 0x8(%r12,%r14,8), %rdi
leaq 0x6abe(%rip), %rsi      # 0x9035
callq 0x68c0
movq %rax, -0x50(%rbp)
callq 0x6b1c
movq %rax, %rbx
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x6e81
movq %rbx, -0x68(%rbp)
cmpl $0x1, (%rbx)
je 0x25b0
movq (%r12), %rsi
leaq 0x6ac4(%rip), %rdi      # 0x906d
xorl %eax, %eax
callq 0x6790
movq -0x68(%rbp), %rax
movl 0x8(%rax), %edi
decl %edi
movsd 0x6a47(%rip), %xmm0     # 0x9008
callq 0x2070
addsd 0x6a42(%rip), %xmm0     # 0x9010
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
cvtsi2sdl 0xbbce(%rip), %xmm1 # 0xe1b0
movsd 0x6a2e(%rip), %xmm0     # 0x9018
callq 0x20c0
mulsd -0x60(%rbp), %xmm0
movapd %xmm0, -0x60(%rbp)
movsd 0xbbb7(%rip), %xmm0     # 0xe1b8
callq 0x57bc
movsd %xmm0, -0x30(%rbp)
movsd 0xbbad(%rip), %xmm0     # 0xe1c0
callq 0x57bc
movsd %xmm0, -0x40(%rbp)
movsd 0xbba3(%rip), %xmm0     # 0xe1c8
callq 0x57bc
movsd %xmm0, -0x90(%rbp)
movsd 0xbb96(%rip), %xmm0     # 0xe1d0
callq 0x57bc
movsd %xmm0, -0x48(%rbp)
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x6945
xorl %ecx, %ecx
cmpq $0x1f4, %rcx            # imm = 0x1F4
je 0x2662
andl $0x0, (%rax,%rcx,4)
incq %rcx
jmp 0x2650
cvttsd2si -0x60(%rbp), %edx
movsd 0x6999(%rip), %xmm1     # 0x9008
movapd %xmm1, %xmm0
divsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
cvttsd2si -0x90(%rbp), %ecx
movl %ecx, -0x6c(%rbp)
cvttsd2si -0x48(%rbp), %r14d
divsd -0x40(%rbp), %xmm1
movsd %xmm1, -0x78(%rbp)
movq %rax, 0xbdb6(%rip)      # 0xe458
movq 0xb94f(%rip), %rbx      # 0xdff8
movq (%rbx), %rdi
leaq 0x69d9(%rip), %rsi      # 0x908c
xorl %eax, %eax
callq 0x21e0
movq (%rbx), %rdi
movsd 0xbb13(%rip), %xmm0     # 0xe1d8
leaq 0x69cf(%rip), %rsi      # 0x909b
movb $0x1, %al
callq 0x21e0
movq (%rbx), %rdi
leaq 0x69cb(%rip), %rsi      # 0x90a8
movl %r14d, -0x70(%rbp)
movl %r14d, %edx
xorl %eax, %eax
callq 0x21e0
movq (%rbx), %rdi
leaq 0x69c9(%rip), %rsi      # 0x90be
movsd -0x78(%rbp), %xmm0
movb $0x1, %al
callq 0x21e0
leaq 0x69cd(%rip), %rdx      # 0x90d5
movq -0x50(%rbp), %rdi
movq -0x68(%rbp), %rsi
xorl %ecx, %ecx
callq 0x7890
cvttpd2dq -0x60(%rbp), %xmm0
cvtdq2pd %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
pushq $0x1
popq %rax
xorl %r13d, %r13d
movsd 0x68d3(%rip), %xmm0     # 0x9008
movsd %xmm0, -0x60(%rbp)
xorl %ebx, %ebx
movl $0x0, -0x30(%rbp)
movl $0x0, -0x40(%rbp)
movl %eax, -0x34(%rbp)
movl %eax, -0x48(%rbp)
movq %r13, %rax
xorl %edx, %edx
movl $0x1f4, %ecx            # imm = 0x1F4
divq %rcx
movq %rax, %r12
movq %r15, %rdi
callq 0x2200
testl %eax, %eax
jne 0x29d5
imulq $0x7d0, %r12, %rax      # imm = 0x7D0
movq %r15, %rdi
movq %rbx, %r15
subq %rax, %r15
movq -0x68(%rbp), %rax
movl 0x8(%rax), %edx
movq %rdi, %r14
movq -0x50(%rbp), %rsi
leaq 0x5388(%rip), %rcx      # 0x7b1d
callq 0x7356
movl %eax, %r12d
movq 0xbcb4(%rip), %rax      # 0xe458
movl (%rax,%r15), %ecx
movl %r12d, %edx
subl %ecx, %edx
addl %r12d, %ecx
imull %edx, %ecx
cvtsi2sd %ecx, %xmm0
addsd 0xbca1(%rip), %xmm0     # 0xe460
movsd %xmm0, 0xbc99(%rip)     # 0xe460
movl %r12d, (%rax,%r15)
movsd 0x684d(%rip), %xmm1     # 0x9020
callq 0x20c0
movapd -0x90(%rbp), %xmm1
ucomisd %xmm0, %xmm1
seta %al
cmpl $0x1f5, %r13d           # imm = 0x1F5
setae %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x2815
cmpl $0x0, -0x30(%rbp)
movsd -0x60(%rbp), %xmm1
movl -0x40(%rbp), %eax
je 0x2861
cmpl -0x70(%rbp), %eax
movq %r14, %r15
jge 0x2843
incl %eax
movl %eax, -0x40(%rbp)
jmp 0x2848
cmpl $0x1f5, %r13d           # imm = 0x1F5
movsd -0x60(%rbp), %xmm1
jb 0x28ab
cmpl $0x0, -0x30(%rbp)
movq %r14, %r15
movl -0x40(%rbp), %eax
jne 0x2928
cmpl -0x6c(%rbp), %eax
jge 0x2850
incl %eax
movl %eax, -0x40(%rbp)
jmp 0x2858
subsd -0x78(%rbp), %xmm1
pushq $0x1
popq %rax
movl %eax, -0x30(%rbp)
jmp 0x28ae
addsd -0x98(%rbp), %xmm1
movl $0x0, -0x30(%rbp)
jmp 0x28ae
movq 0xb790(%rip), %r15      # 0xdff8
movq (%r15), %rdi
movl $0x0, -0x40(%rbp)
leaq 0x6870(%rip), %rsi      # 0x90e9
movl %r13d, %edx
xorl %eax, %eax
callq 0x21e0
movq (%r15), %rdi
leaq 0x6884(%rip), %rsi      # 0x9111
movsd -0x60(%rbp), %xmm0
movb $0x1, %al
callq 0x21e0
movsd -0x60(%rbp), %xmm1
pushq $0x1
popq %rax
movl %eax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq %r14, %r15
ucomisd 0x6752(%rip), %xmm1     # 0x9008
ja 0x28d8
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x28f0
xorpd %xmm1, %xmm1
cmpl $0x0, -0x34(%rbp)
je 0x29b0
pushq $0x1
popq %rax
movl %eax, -0x34(%rbp)
jmp 0x28f0
movsd 0x6728(%rip), %xmm1     # 0x9008
cmpl $0x0, -0x48(%rbp)
pushq $0x1
popq %rax
movl %eax, -0x48(%rbp)
je 0x2981
cvtsi2sd %r12d, %xmm0
mulsd 0xb8db(%rip), %xmm0     # 0xe1d8
movsd %xmm1, -0x60(%rbp)
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %edx
movq -0x50(%rbp), %rdi
leaq 0x6782(%rip), %rsi      # 0x9097
xorl %eax, %eax
callq 0x21e0
incq %r13
addq $0x4, %rbx
jmp 0x2750
movq 0xb6c9(%rip), %r14      # 0xdff8
movq (%r14), %rdi
movl $0x0, -0x30(%rbp)
leaq 0x67e4(%rip), %rsi      # 0x9124
movl %r13d, %edx
xorl %eax, %eax
callq 0x21e0
movq (%r14), %rdi
leaq 0x67bd(%rip), %rsi      # 0x9111
movsd -0x60(%rbp), %xmm0
movb $0x1, %al
callq 0x21e0
movsd -0x60(%rbp), %xmm1
ucomisd 0x669b(%rip), %xmm1     # 0x9008
movl $0x0, -0x40(%rbp)
movl $0x0, -0x48(%rbp)
jbe 0x28b8
movq 0xb670(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x67bf(%rip), %rdi      # 0x9151
pushq $0xe
popq %rsi
pushq $0x1
popq %rdx
callq 0x22d0
movsd 0x6663(%rip), %xmm1     # 0x9008
pushq $0x1
popq %rax
movl %eax, -0x48(%rbp)
jmp 0x28f0
movq 0xb641(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x679f(%rip), %rdi      # 0x9160
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
callq 0x22d0
xorpd %xmm1, %xmm1
jmp 0x28d0
movq -0x68(%rbp), %rax
movl 0xc(%rax), %ecx
leal 0x1(%rcx), %eax
cmpl %r13d, %eax
jne 0x2a06
movq %r15, %rdi
callq 0x20d0
movq -0x50(%rbp), %rdi
callq 0x20d0
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb5eb(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x675b(%rip), %rsi      # 0x9172
movl %r13d, %edx
xorl %eax, %eax
callq 0x21e0
jmp 0x29e4
leaq 0x67d9(%rip), %rdi      # 0x9203
callq 0x20a0
leaq 0x67d6(%rip), %rdi      # 0x920c
callq 0x20a0
leaq 0x67fb(%rip), %rdi      # 0x923d
callq 0x20a0
leaq 0x681b(%rip), %rdi      # 0x9269
callq 0x20a0
pushq $0xa
popq %rdi
callq 0x2050
leaq 0x6830(%rip), %rdi      # 0x9292
callq 0x20a0
leaq 0x6835(%rip), %rdi      # 0x92a3
callq 0x20a0
leaq 0x6852(%rip), %rdi      # 0x92cc
callq 0x20a0
movsd 0xb751(%rip), %xmm0     # 0xe1d8
leaq 0x671b(%rip), %rbx      # 0x91a9
movq %rbx, %rdi
movb $0x1, %al
callq 0x2100
leaq 0x6860(%rip), %rdi      # 0x92ff
callq 0x20a0
leaq 0x687b(%rip), %rdi      # 0x9326
callq 0x20a0
leaq 0x689f(%rip), %rdi      # 0x9356
callq 0x20a0
movl 0xb6ee(%rip), %esi      # 0xe1b0
leaq 0x6700(%rip), %rdi      # 0x91c9
xorl %eax, %eax
callq 0x2100
leaq 0x68bb(%rip), %rdi      # 0x9392
callq 0x20a0
leaq 0x68df(%rip), %rdi      # 0x93c2
callq 0x20a0
leaq 0x6915(%rip), %rdi      # 0x9404
callq 0x20a0
movsd 0xb6cc(%rip), %xmm0     # 0xe1c8
movq %rbx, %rdi
movb $0x1, %al
callq 0x2100
leaq 0x692b(%rip), %rdi      # 0x9438
callq 0x20a0
leaq 0x694f(%rip), %rdi      # 0x9468
callq 0x20a0
leaq 0x6984(%rip), %rdi      # 0x94a9
callq 0x20a0
leaq 0x69a1(%rip), %rdi      # 0x94d2
callq 0x20a0
movsd 0xb67a(%rip), %xmm0     # 0xe1b8
movq %rbx, %rdi
movb $0x1, %al
callq 0x2100
leaq 0x69b2(%rip), %rdi      # 0x9501
callq 0x20a0
leaq 0x69d8(%rip), %rdi      # 0x9533
callq 0x20a0
movsd 0xb668(%rip), %xmm0     # 0xe1d0
movq %rbx, %rdi
movb $0x1, %al
callq 0x2100
leaq 0x69ee(%rip), %rdi      # 0x9567
callq 0x20a0
leaq 0x6a14(%rip), %rdi      # 0x9599
callq 0x20a0
movsd 0xb62e(%rip), %xmm0     # 0xe1c0
movq %rbx, %rdi
movb $0x1, %al
callq 0x2100
leaq 0x6a25(%rip), %rdi      # 0x95c8
callq 0x20a0
leaq 0x6a4a(%rip), %rdi      # 0x95f9
callq 0x20a0
jmp 0x2bd2
movq 0xb5e3(%rip), %rsi      # 0xe1a0
movq 0xb5e4(%rip), %rdx      # 0xe1a8
leaq 0x65cf(%rip), %rdi      # 0x919a
xorl %eax, %eax
callq 0x2100
xorl %edi, %edi
callq 0x22c0
movq (%r12), %rsi
leaq 0x6453(%rip), %rdi      # 0x9037
xorl %eax, %eax
callq 0x2100
pushq $-0x1
popq %rdi
callq 0x22c0
nop