// File: /workspace/github_cpp_makefile_star2_O3/hdlguy[P]fft_demo/conv.cpp
// Function: main
int main(int argc, char **argv) {
    const int Nsig  = 256;
    const int Nt    =  32;
    const int Off   =   5;
    const int Nfft  =  (int)pow(2.0, ceil(log2(2.0*Nt-1))); // 2^(ceil(log2(2*Nt-1)));
    cout << "Nfft = " << Nfft << "\n";


    // random signal
    vector<double> s_real(Nsig);
    vector<double> s_imag(Nsig);
    for(int i=0; i<Nsig; i++) {
        s_real[i] = ((double)rand())/((double)RAND_MAX) - 0.5;
        s_imag[i] = ((double)rand())/((double)RAND_MAX) - 0.5;
    }

    // take the reverse order complex conjugate of a portion of the signal to be the convolution template.
    vector<double> h_real(Nt);
    vector<double> h_imag(Nt);
    for(int i=0; i<Nt; i++) {
        h_imag[i] = -s_imag[Nt-1-i+Off];
        h_real[i] = +s_real[Nt-1-i+Off];
    }

    vector<double> h_pad_imag(Nfft, 0.0);
    vector<double> h_pad_real(Nfft, 0.0);
    vector<double> s_pad_imag(Nfft, 0.0);
    vector<double> s_pad_real(Nfft, 0.0);
    for(int i=0; i<Nt; i++) {
        h_pad_imag[i] = h_imag[i];
        h_pad_real[i] = h_real[i];
        s_pad_imag[i] = s_imag[i];
        s_pad_real[i] = s_real[i];
    }
    
    vector<double> H_imag(Nfft); H_imag = h_pad_imag;
    vector<double> H_real(Nfft); H_real = h_pad_real;
    Fft::transformRadix2(H_real, H_imag);

    vector<double> S_imag(Nfft); S_imag = s_pad_imag;
    vector<double> S_real(Nfft); S_real = s_pad_real;
    Fft::transformRadix2(S_real, S_imag);

    vector<double> Y_imag(Nfft);
    vector<double> Y_real(Nfft);
    for(int i=0; i<Nfft; i++) {
        Y_imag[i] = (H_real[i]*S_real[i] - H_imag[i]*S_imag[i]);
        Y_real[i] = (H_real[i]*S_imag[i] + H_imag[i]*S_real[i]);
    }

    vector<double> y_imag(Nfft); y_imag = Y_imag;
    vector<double> y_real(Nfft); y_real = Y_real;
    Fft::transformRadix2(y_imag, y_real);  // real <-> imag swapped to implement inverse fft (without scaling)

    // plot time domain data
    FILE* gnuplotPipe = popen ("gnuplot --persist", "w");
    fprintf(gnuplotPipe, "plot '-' ps 0.1 lt 6\n");
    for (int i=0; i<Nfft; i++) fprintf(gnuplotPipe, "%lf\n", h_pad_real[i]);
    fprintf(gnuplotPipe, "e");
    pclose(gnuplotPipe);


    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq 0x2f5b(%rip), %rbx      # 0x4fd8
leaq 0x106c(%rip), %rsi      # 0x30f0
movl $0x7, %edx
movq %rbx, %rdi
callq 0x10e0
movq %rbx, %rdi
movl $0x40, %esi
callq 0x1150
leaq 0x107b(%rip), %rsi      # 0x3120
movl $0x1, %edx
movq %rax, %rdi
callq 0x10e0
movl $0x800, %edi            # imm = 0x800
callq 0x10d0
movq %rax, %rbx
movl $0x800, %edx            # imm = 0x800
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movl $0x800, %edi            # imm = 0x800
callq 0x10d0
movq %rax, %r14
xorl %r15d, %r15d
movl $0x800, %edx            # imm = 0x800
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
callq 0x1040
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0xfdf(%rip), %xmm1      # 0x30e0
divsd %xmm1, %xmm0
movsd 0xfdb(%rip), %xmm1      # 0x30e8
addsd %xmm1, %xmm0
movsd %xmm0, (%rbx,%r15,8)
callq 0x1040
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0xfb5(%rip), %xmm0      # 0x30e0
addsd 0xfb5(%rip), %xmm0      # 0x30e8
movsd %xmm0, (%r14,%r15,8)
incq %r15
cmpq $0x100, %r15            # imm = 0x100
jne 0x20ed
movl $0x100, %edi            # imm = 0x100
callq 0x10d0
movq %rax, %r12
movl $0x100, %edx            # imm = 0x100
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movl $0x100, %edi            # imm = 0x100
movq %r12, -0x38(%rbp)
callq 0x10d0
xorl %r15d, %r15d
movl $0x100, %edx            # imm = 0x100
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x1090
movq -0x30(%rbp), %rcx
movl $0x24, %eax
movapd 0xf19(%rip), %xmm0      # 0x30b0
movsd (%r14,%rax,8), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, (%rcx,%r15)
movsd (%rbx,%rax,8), %xmm1
movsd %xmm1, (%r12,%r15)
decq %rax
addq $0x8, %r15
cmpq $0x4, %rax
jne 0x2197
xorpd %xmm0, %xmm0
movapd %xmm0, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
movl $0x200, %edi            # imm = 0x200
callq 0x10d0
movq %rax, -0xb0(%rbp)
movq %rax, %r15
addq $0x200, %r15            # imm = 0x200
movq %r15, -0xa0(%rbp)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movq %r15, -0xa8(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x200, %edi            # imm = 0x200
callq 0x10d0
movq %rax, -0x50(%rbp)
movq %rax, %r15
addq $0x200, %r15            # imm = 0x200
movq %r15, -0x40(%rbp)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movq %r15, -0x48(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0x90(%rbp)
movq $0x0, -0x80(%rbp)
movl $0x200, %edi            # imm = 0x200
callq 0x10d0
movq %rax, -0x90(%rbp)
movq %rax, %r15
addq $0x200, %r15            # imm = 0x200
movq %r15, -0x80(%rbp)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movq %r15, -0x88(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0xf0(%rbp)
movq $0x0, -0xe0(%rbp)
movl $0x200, %edi            # imm = 0x200
callq 0x10d0
movq %rax, %r13
movq %rax, -0xf0(%rbp)
movq %rax, %r12
addq $0x200, %r12            # imm = 0x200
movq %r12, -0xe0(%rbp)
xorl %r15d, %r15d
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movq %r12, -0xe8(%rbp)
movq -0xb0(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x90(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdi
movsd (%rdi,%r15), %xmm0
movsd %xmm0, (%rax,%r15)
movsd (%rsi,%r15), %xmm0
movsd %xmm0, (%rcx,%r15)
movsd (%r14,%r15), %xmm0
movsd %xmm0, (%rdx,%r15)
movsd (%rbx,%r15), %xmm0
movsd %xmm0, (%r13,%r15)
addq $0x8, %r15
cmpq $0x100, %r15            # imm = 0x100
jne 0x2306
xorpd %xmm0, %xmm0
movapd %xmm0, -0x170(%rbp)
movq $0x0, -0x160(%rbp)
movl $0x200, %edi            # imm = 0x200
callq 0x10d0
leaq -0x170(%rbp), %r15
movq %rax, (%r15)
movq %rax, %r12
addq $0x200, %r12            # imm = 0x200
movq %r12, 0x10(%r15)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movq %r12, 0x8(%r15)
leaq -0xb0(%rbp), %rsi
movq %r15, %rdi
callq 0x2982
xorpd %xmm0, %xmm0
movapd %xmm0, -0x150(%rbp)
movq $0x0, -0x140(%rbp)
movl $0x200, %edi            # imm = 0x200
callq 0x10d0
leaq -0x150(%rbp), %r15
movq %rax, (%r15)
movq %rax, %r12
addq $0x200, %r12            # imm = 0x200
movq %r12, 0x10(%r15)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movq %r12, 0x8(%r15)
leaq -0x50(%rbp), %rsi
movq %r15, %rdi
callq 0x2982
leaq -0x150(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x134a
xorpd %xmm0, %xmm0
movapd %xmm0, -0x130(%rbp)
movq $0x0, -0x120(%rbp)
movl $0x200, %edi            # imm = 0x200
callq 0x10d0
leaq -0x130(%rbp), %r15
movq %rax, (%r15)
movq %rax, %r12
addq $0x200, %r12            # imm = 0x200
movq %r12, 0x10(%r15)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movq %r12, 0x8(%r15)
leaq -0x90(%rbp), %rsi
movq %r15, %rdi
callq 0x2982
xorpd %xmm0, %xmm0
movapd %xmm0, -0x110(%rbp)
movq $0x0, -0x100(%rbp)
movl $0x200, %edi            # imm = 0x200
callq 0x10d0
leaq -0x110(%rbp), %r15
movq %rax, (%r15)
movq %rax, %r12
addq $0x200, %r12            # imm = 0x200
movq %r12, 0x10(%r15)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movq %r12, 0x8(%r15)
leaq -0xf0(%rbp), %rsi
movq %r15, %rdi
callq 0x2982
leaq -0x110(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x134a
xorpd %xmm0, %xmm0
movapd %xmm0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x200, %edi            # imm = 0x200
callq 0x10d0
movq %rax, -0x70(%rbp)
movq %rax, %r15
addq $0x200, %r15            # imm = 0x200
movq %r15, -0x60(%rbp)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movq %r15, -0x68(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0xd0(%rbp)
movq $0x0, -0xc0(%rbp)
movl $0x200, %edi            # imm = 0x200
callq 0x10d0
movq %rax, %r13
movq %rax, -0xd0(%rbp)
movq %rax, %r12
addq $0x200, %r12            # imm = 0x200
movq %r12, -0xc0(%rbp)
xorl %r15d, %r15d
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movq %r12, -0xc8(%rbp)
movq -0x150(%rbp), %rax
movq -0x110(%rbp), %rcx
movq -0x170(%rbp), %rdx
movq -0x130(%rbp), %rsi
movq -0x70(%rbp), %rdi
movsd (%rax,%r15), %xmm0
movsd (%rsi,%r15), %xmm1
mulsd (%rdx,%r15), %xmm1
mulsd (%rcx,%r15), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rdi,%r15)
movsd (%rax,%r15), %xmm0
movsd (%rdx,%r15), %xmm1
mulsd (%rcx,%r15), %xmm1
mulsd (%rsi,%r15), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r13,%r15)
addq $0x8, %r15
cmpq $0x200, %r15            # imm = 0x200
jne 0x2588
xorpd %xmm0, %xmm0
movapd %xmm0, -0x1b0(%rbp)
movq $0x0, -0x1a0(%rbp)
movl $0x200, %edi            # imm = 0x200
callq 0x10d0
leaq -0x1b0(%rbp), %r15
movq %rax, (%r15)
movq %rax, %r12
addq $0x200, %r12            # imm = 0x200
movq %r12, 0x10(%r15)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movq %r12, 0x8(%r15)
leaq -0x70(%rbp), %rsi
movq %r15, %rdi
callq 0x2982
xorpd %xmm0, %xmm0
movapd %xmm0, -0x190(%rbp)
movq $0x0, -0x180(%rbp)
movl $0x200, %edi            # imm = 0x200
callq 0x10d0
leaq -0x190(%rbp), %r15
movq %rax, (%r15)
movq %rax, %r12
addq $0x200, %r12            # imm = 0x200
movq %r12, 0x10(%r15)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movq %r12, 0x8(%r15)
leaq -0xd0(%rbp), %rsi
movq %r15, %rdi
callq 0x2982
leaq -0x1b0(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x134a
leaq 0xa51(%rip), %rdi       # 0x30f8
leaq 0xa5c(%rip), %rsi       # 0x310a
callq 0x1050
movq %rax, %r13
leaq 0xa4f(%rip), %rdi       # 0x310c
movl $0x15, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x1180
xorl %r12d, %r12d
leaq 0xa49(%rip), %r15       # 0x3122
movq -0x50(%rbp), %rax
movsd (%rax,%r12,8), %xmm0
movq %r13, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x1110
incq %r12
cmpq $0x40, %r12
jne 0x26d9
movl $0x65, %edi
movq %r13, %rsi
callq 0x10f0
movq %r13, %rdi
callq 0x1100
movq -0x190(%rbp), %rdi
testq %rdi, %rdi
je 0x271f
callq 0x10c0
movq -0x1b0(%rbp), %rdi
testq %rdi, %rdi
movq -0x38(%rbp), %r15
je 0x2734
callq 0x10c0
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x2745
callq 0x10c0
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x2753
callq 0x10c0
movq -0x110(%rbp), %rdi
testq %rdi, %rdi
je 0x2764
callq 0x10c0
movq -0x130(%rbp), %rdi
testq %rdi, %rdi
je 0x2775
callq 0x10c0
movq -0x150(%rbp), %rdi
testq %rdi, %rdi
je 0x2786
callq 0x10c0
movq -0x170(%rbp), %rdi
testq %rdi, %rdi
je 0x2797
callq 0x10c0
movq -0xf0(%rbp), %rdi
testq %rdi, %rdi
je 0x27a8
callq 0x10c0
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x27b9
callq 0x10c0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x27c7
callq 0x10c0
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
je 0x27d8
callq 0x10c0
movq -0x30(%rbp), %rdi
callq 0x10c0
movq %r15, %rdi
callq 0x10c0
movq %r14, %rdi
callq 0x10c0
movq %rbx, %rdi
callq 0x10c0
xorl %eax, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x280f
movq %rax, %r13
jmp 0x2899
movq %rax, %r13
jmp 0x28aa
movq %rax, %r13
jmp 0x28bb
jmp 0x28cb
jmp 0x282e
movq %rax, %r13
jmp 0x28df
jmp 0x28f2
jmp 0x283d
movq %rax, %r13
jmp 0x2906
movq %rax, %r13
jmp 0x2917
movq %rax, %r13
jmp 0x2928
movq %rax, %r13
jmp 0x2939
movq %rax, %r13
jmp 0x2947
movq %rax, %r13
jmp 0x2958
movq %rax, %r13
jmp 0x2961
movq %rax, %r13
jmp 0x296a
movq %rax, %r13
jmp 0x2972
movq %rax, %r13
movq -0x190(%rbp), %rdi
testq %rdi, %rdi
je 0x2899
callq 0x10c0
movq -0x1b0(%rbp), %rdi
testq %rdi, %rdi
je 0x28aa
callq 0x10c0
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x28bb
callq 0x10c0
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x28ce
callq 0x10c0
jmp 0x28ce
movq %rax, %r13
movq -0x110(%rbp), %rdi
testq %rdi, %rdi
je 0x28df
callq 0x10c0
movq -0x130(%rbp), %rdi
testq %rdi, %rdi
je 0x28f5
callq 0x10c0
jmp 0x28f5
movq %rax, %r13
movq -0x150(%rbp), %rdi
testq %rdi, %rdi
je 0x2906
callq 0x10c0
movq -0x170(%rbp), %rdi
testq %rdi, %rdi
je 0x2917
callq 0x10c0
movq -0xf0(%rbp), %rdi
testq %rdi, %rdi
je 0x2928
callq 0x10c0
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x2939
callq 0x10c0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x2947
callq 0x10c0
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
je 0x2958
callq 0x10c0
movq -0x30(%rbp), %rdi
callq 0x10c0
movq -0x38(%rbp), %rdi
callq 0x10c0
movq %r14, %rdi
callq 0x10c0
movq %rbx, %rdi
callq 0x10c0
movq %r13, %rdi
callq 0x1160