// File: matteotiziano[P]simple-wavelet/main.cpp
// Function: main
int main(int argc, const char *argv[]) {

  // Read arguments from command line
  if(argc < 3) {
    std::cout << "usage: " << std::string(argv[0]) << " num_rows num_cols num_levels" << std::endl;
    return 1;
  }

  // TODO: handle invalid inputs

  // Size of the matrix
  const size_t num_rows = static_cast<size_t>(std::stoi(argv[1]));
  const size_t num_cols = static_cast<size_t>(std::stoi(argv[2]));
  // Number of decomposition levels
  const size_t num_levels = static_cast<size_t>(std::stoi(argv[3]));

  // Initialise a random 2D matrix using seed equal to 0
  // Note: use `matrix.Rand();` without argument to generate
  // different values at each execution
  Matrix<double> matrix(num_rows, num_cols);
  matrix.Rand(0);

  // Filters definition of Discrete Haar transform
  // https://en.wikipedia.org/wiki/Haar_wavelet#Haar_transform
  const double inv_sqrt2 = 1.0 / std::sqrt(2.0);
  const std::vector<double> Lo_D = {inv_sqrt2, inv_sqrt2};
  const std::vector<double> Hi_D = {-inv_sqrt2, inv_sqrt2};
  const std::vector<double> Lo_R = {inv_sqrt2, inv_sqrt2};
  const std::vector<double> Hi_R = {inv_sqrt2, -inv_sqrt2};

  // Instantiate a Wavelet object with Haar filters
  const Wavelet<double> haar(Lo_D, Hi_D, Lo_R, Hi_R);


  // Example of 1D decomposition and reconstruction using the first column of
  // the above matrix as input
  const std::vector<double> vector = matrix.GetColumn(0);
  const Decomposition1D<double> dec1D = haar.Wavedec(vector, num_levels);
  const std::vector<double> vector_rec = haar.Waverec(dec1D, vector.size());
  const double norm1D = Norm(vector - vector_rec);

  // Example of 2D decomposition and reconstruction
  const Decomposition2D<double> dec2D = haar.Wavedec(matrix, num_levels);
  const Matrix<double> matrix_rec = haar.Waverec(dec2D, matrix.Size());
  const double norm2D = Norm((matrix - matrix_rec).GetData());

  std::cout << "Error (l2-norm) of 1D reconstruction: " << norm1D << std::endl;
  std::cout << "Error (l2-norm) of 2D reconstruction: " << norm2D << std::endl;

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x490, %rsp            # imm = 0x490
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
jge 0x267d
movq 0x17a0e(%rip), %rdi     # 0x19fa8
leaq 0xfa8f(%rip), %rsi      # 0x12030
callq 0x2200
movq %rax, -0x3b0(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x3a8(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x3a0(%rbp)
callq 0x2360
movq -0x3a8(%rbp), %rsi
movq -0x3a0(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x3030
jmp 0x25e4
movq -0x3b0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x21e0
movq %rax, -0x3b8(%rbp)
jmp 0x25fd
movq -0x3b8(%rbp), %rdi
leaq 0xfa2d(%rip), %rsi      # 0x12038
callq 0x2200
movq %rax, -0x3c0(%rbp)
jmp 0x2619
movq -0x3c0(%rbp), %rdi
movq 0x17969(%rip), %rsi     # 0x19f90
callq 0x2230
jmp 0x262e
leaq -0x30(%rbp), %rdi
callq 0x2130
leaq -0x31(%rbp), %rdi
callq 0x2250
movl $0x1, -0x4(%rbp)
jmp 0x301b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x266f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x2130
leaq -0x31(%rbp), %rdi
callq 0x2250
jmp 0x3027
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x3d0(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0x3c8(%rbp)
callq 0x2360
movq -0x3d0(%rbp), %rsi
movq -0x3c8(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0x3030
jmp 0x26b5
xorl %eax, %eax
movl %eax, %esi
leaq -0x70(%rbp), %rdi
movl $0xa, %edx
callq 0x30e0
movl %eax, -0x3d4(%rbp)
jmp 0x26cf
movl -0x3d4(%rbp), %eax
cltq
movq %rax, -0x3f0(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x2130
leaq -0x71(%rbp), %rdi
callq 0x2250
movq -0x3f0(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x3e8(%rbp)
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x3e0(%rbp)
callq 0x2360
movq -0x3e8(%rbp), %rsi
movq -0x3e0(%rbp), %rdx
leaq -0xa0(%rbp), %rdi
callq 0x3030
jmp 0x2739
xorl %eax, %eax
movl %eax, %esi
leaq -0xa0(%rbp), %rdi
movl $0xa, %edx
callq 0x30e0
movl %eax, -0x3f4(%rbp)
jmp 0x2756
movl -0x3f4(%rbp), %eax
cltq
movq %rax, -0x410(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x2130
leaq -0xa1(%rbp), %rdi
callq 0x2250
movq -0x410(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x408(%rbp)
leaq -0xd1(%rbp), %rdi
movq %rdi, -0x400(%rbp)
callq 0x2360
movq -0x408(%rbp), %rsi
movq -0x400(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
callq 0x3030
jmp 0x27c6
xorl %eax, %eax
movl %eax, %esi
leaq -0xd0(%rbp), %rdi
movl $0xa, %edx
callq 0x30e0
movl %eax, -0x414(%rbp)
jmp 0x27e3
movl -0x414(%rbp), %eax
cltq
movq %rax, -0x428(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x2130
leaq -0xd1(%rbp), %rdi
callq 0x2250
movq -0x428(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x50(%rbp), %rsi
movq -0x80(%rbp), %rdx
leaq -0x100(%rbp), %rdi
movq %rdi, -0x420(%rbp)
xorps %xmm0, %xmm0
callq 0xc7e0
movq -0x420(%rbp), %rdi
xorl %esi, %esi
callq 0xd920
jmp 0x2846
movsd 0xf7ba(%rip), %xmm0     # 0x12008
callq 0x21a0
movaps %xmm0, %xmm1
movsd 0xf7b2(%rip), %xmm0     # 0x12010
divsd %xmm1, %xmm0
movsd %xmm0, -0x108(%rbp)
movsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x140(%rbp)
movsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x138(%rbp)
leaq -0x140(%rbp), %rax
movq %rax, -0x130(%rbp)
movq $0x2, -0x128(%rbp)
leaq -0x141(%rbp), %rdi
movq %rdi, -0x430(%rbp)
callq 0x3120
movq -0x430(%rbp), %rcx
movq -0x130(%rbp), %rsi
movq -0x128(%rbp), %rdx
leaq -0x120(%rbp), %rdi
callq 0x3140
jmp 0x28d9
leaq -0x141(%rbp), %rdi
callq 0x31c0
movaps 0xf734(%rip), %xmm1     # 0x12020
movsd -0x108(%rbp), %xmm0
pxor %xmm1, %xmm0
movlpd %xmm0, -0x180(%rbp)
movsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x178(%rbp)
leaq -0x180(%rbp), %rax
movq %rax, -0x170(%rbp)
movq $0x2, -0x168(%rbp)
leaq -0x181(%rbp), %rdi
movq %rdi, -0x438(%rbp)
callq 0x3120
movq -0x438(%rbp), %rcx
movq -0x170(%rbp), %rsi
movq -0x168(%rbp), %rdx
leaq -0x160(%rbp), %rdi
callq 0x3140
jmp 0x295f
leaq -0x181(%rbp), %rdi
callq 0x31c0
movsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x1c0(%rbp)
movsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x1b8(%rbp)
leaq -0x1c0(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq $0x2, -0x1a8(%rbp)
leaq -0x1c1(%rbp), %rdi
movq %rdi, -0x440(%rbp)
callq 0x3120
movq -0x440(%rbp), %rcx
movq -0x1b0(%rbp), %rsi
movq -0x1a8(%rbp), %rdx
leaq -0x1a0(%rbp), %rdi
callq 0x3140
jmp 0x29da
leaq -0x1c1(%rbp), %rdi
callq 0x31c0
movsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x200(%rbp)
movaps 0xf623(%rip), %xmm1     # 0x12020
movsd -0x108(%rbp), %xmm0
pxor %xmm1, %xmm0
movlpd %xmm0, -0x1f8(%rbp)
leaq -0x200(%rbp), %rax
movq %rax, -0x1f0(%rbp)
movq $0x2, -0x1e8(%rbp)
leaq -0x201(%rbp), %rdi
movq %rdi, -0x448(%rbp)
callq 0x3120
movq -0x448(%rbp), %rcx
movq -0x1f0(%rbp), %rsi
movq -0x1e8(%rbp), %rdx
leaq -0x1e0(%rbp), %rdi
callq 0x3140
jmp 0x2a60
leaq -0x201(%rbp), %rdi
callq 0x31c0
leaq -0x270(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0x160(%rbp), %rdx
leaq -0x1a0(%rbp), %rcx
leaq -0x1e0(%rbp), %r8
callq 0x5860
jmp 0x2a96
xorl %eax, %eax
movl %eax, %edx
leaq -0x288(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xd320
jmp 0x2aaf
movq -0xb0(%rbp), %rcx
leaq -0x2a0(%rbp), %rdi
leaq -0x270(%rbp), %rsi
leaq -0x288(%rbp), %rdx
callq 0x59e0
jmp 0x2ad2
leaq -0x288(%rbp), %rdi
callq 0x31e0
movq %rax, %rcx
leaq -0x2b8(%rbp), %rdi
leaq -0x270(%rbp), %rsi
leaq -0x2a0(%rbp), %rdx
callq 0x5c70
jmp 0x2afd
leaq -0x2f0(%rbp), %rdi
leaq -0x288(%rbp), %rsi
callq 0x3340
jmp 0x2b12
leaq -0x2d8(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
leaq -0x2b8(%rbp), %rdx
callq 0x3230
jmp 0x2b2e
leaq -0x2d8(%rbp), %rdi
callq 0x3200
movsd %xmm0, -0x450(%rbp)
jmp 0x2b44
leaq -0x2d8(%rbp), %rdi
callq 0x3430
leaq -0x2f0(%rbp), %rdi
callq 0x3430
movsd -0x450(%rbp), %xmm0
movsd %xmm0, -0x2c0(%rbp)
movq -0xb0(%rbp), %rcx
leaq -0x308(%rbp), %rdi
leaq -0x270(%rbp), %rsi
leaq -0x100(%rbp), %rdx
callq 0x6480
jmp 0x2b8f
leaq -0x100(%rbp), %rdi
callq 0xca80
movq %rdx, -0x460(%rbp)
movq %rax, -0x458(%rbp)
jmp 0x2bab
movq -0x460(%rbp), %rax
movq -0x458(%rbp), %rcx
movq %rcx, -0x340(%rbp)
movq %rax, -0x338(%rbp)
leaq -0x330(%rbp), %rdi
leaq -0x270(%rbp), %rsi
leaq -0x308(%rbp), %rdx
leaq -0x340(%rbp), %rcx
callq 0x67f0
jmp 0x2bea
leaq -0x398(%rbp), %rdi
leaq -0x330(%rbp), %rsi
callq 0x3490
jmp 0x2bff
leaq -0x370(%rbp), %rdi
leaq -0x100(%rbp), %rsi
leaq -0x398(%rbp), %rdx
callq 0xe7c0
jmp 0x2c1b
leaq -0x370(%rbp), %rdi
callq 0xd4d0
movq %rax, -0x468(%rbp)
jmp 0x2c30
movq -0x468(%rbp), %rdi
callq 0x3200
movsd %xmm0, -0x470(%rbp)
jmp 0x2c46
leaq -0x370(%rbp), %rdi
callq 0xca20
leaq -0x398(%rbp), %rdi
callq 0xca20
movsd -0x470(%rbp), %xmm0
movsd %xmm0, -0x348(%rbp)
movq 0x17333(%rip), %rdi     # 0x19fa8
leaq 0xf3da(%rip), %rsi      # 0x12056
callq 0x2200
movq %rax, -0x478(%rbp)
jmp 0x2c8a
movq -0x478(%rbp), %rdi
movsd -0x2c0(%rbp), %xmm0
callq 0x2390
movq %rax, -0x480(%rbp)
jmp 0x2ca7
movq -0x480(%rbp), %rdi
movq 0x172db(%rip), %rsi     # 0x19f90
callq 0x2230
jmp 0x2cbc
movq 0x172e5(%rip), %rdi     # 0x19fa8
leaq 0xf3b3(%rip), %rsi      # 0x1207d
callq 0x2200
movq %rax, -0x488(%rbp)
jmp 0x2cd8
movq -0x488(%rbp), %rdi
movsd -0x348(%rbp), %xmm0
callq 0x2390
movq %rax, -0x490(%rbp)
jmp 0x2cf5
movq -0x490(%rbp), %rdi
movq 0x1728d(%rip), %rsi     # 0x19f90
callq 0x2230
jmp 0x2d0a
movl $0x0, -0x4(%rbp)
leaq -0x330(%rbp), %rdi
callq 0xca20
leaq -0x308(%rbp), %rdi
callq 0x53e0
leaq -0x2b8(%rbp), %rdi
callq 0x3430
leaq -0x2a0(%rbp), %rdi
callq 0x4d10
leaq -0x288(%rbp), %rdi
callq 0x3430
leaq -0x270(%rbp), %rdi
callq 0x5990
leaq -0x1e0(%rbp), %rdi
callq 0x3430
leaq -0x1a0(%rbp), %rdi
callq 0x3430
leaq -0x160(%rbp), %rdi
callq 0x3430
leaq -0x120(%rbp), %rdi
callq 0x3430
leaq -0x100(%rbp), %rdi
callq 0xca20
jmp 0x301b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2dbd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x2130
leaq -0x71(%rbp), %rdi
callq 0x2250
jmp 0x3027
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2df1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x2130
leaq -0xa1(%rbp), %rdi
callq 0x2250
jmp 0x3027
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2e28
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x2130
leaq -0xd1(%rbp), %rdi
callq 0x2250
jmp 0x3027
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x300d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x141(%rbp), %rdi
callq 0x31c0
jmp 0x300d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x181(%rbp), %rdi
callq 0x31c0
jmp 0x3001
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x1c1(%rbp), %rdi
callq 0x31c0
jmp 0x2ff5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x201(%rbp), %rdi
callq 0x31c0
jmp 0x2fe9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2fdd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2fd1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2fc5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2fb9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2fad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2f39
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x2d8(%rbp), %rdi
callq 0x3430
leaq -0x2f0(%rbp), %rdi
callq 0x3430
jmp 0x2fad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2fa1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2f95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2f89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x370(%rbp), %rdi
callq 0xca20
leaq -0x398(%rbp), %rdi
callq 0xca20
leaq -0x330(%rbp), %rdi
callq 0xca20
leaq -0x308(%rbp), %rdi
callq 0x53e0
leaq -0x2b8(%rbp), %rdi
callq 0x3430
leaq -0x2a0(%rbp), %rdi
callq 0x4d10
leaq -0x288(%rbp), %rdi
callq 0x3430
leaq -0x270(%rbp), %rdi
callq 0x5990
leaq -0x1e0(%rbp), %rdi
callq 0x3430
leaq -0x1a0(%rbp), %rdi
callq 0x3430
leaq -0x160(%rbp), %rdi
callq 0x3430
leaq -0x120(%rbp), %rdi
callq 0x3430
leaq -0x100(%rbp), %rdi
callq 0xca20
jmp 0x3027
movl -0x4(%rbp), %eax
addq $0x490, %rsp            # imm = 0x490
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x2350