// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rsi, %rbx
cmpl $0x2, %edi
jg 0x268f
movq 0xb9c0(%rip), %rdi      # 0xdfb0
leaq 0x6a49(%rip), %rsi      # 0x9040
movl $0x7, %edx
callq 0x21e0
movq (%rbx), %rsi
leaq 0x30(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
callq 0x2f00
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0xb989(%rip), %rdi      # 0xdfb0
callq 0x21e0
movq %rax, %rbx
leaq 0x6a12(%rip), %rsi      # 0x9048
movl $0x1d, %edx
movq %rax, %rdi
callq 0x21e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2685
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21d0
movl $0x1, %eax
jmp 0x2c83
movq 0x8(%rbx), %rsi
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x2f00
movq 0x30(%rsp), %r12
callq 0x2040
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
leaq 0xc0(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
movl $0xa, %edx
callq 0x2200
cmpq %r12, (%r13)
je 0x2c95
movq %rax, %r15
movslq %r15d, %rax
cmpq %r15, %rax
jne 0x2ca1
movl (%r14), %eax
cmpl $0x22, %eax
je 0x2ca1
testl %eax, %eax
jne 0x26fb
movl %ebp, (%r14)
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2716
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x10(%rbx), %rsi
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x2f00
movq 0x30(%rsp), %r13
movl (%r14), %eax
movl %eax, (%rsp)
movl $0x0, (%r14)
leaq 0xc0(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
movl $0xa, %edx
callq 0x2200
cmpq %r13, (%rbp)
je 0x2cad
movq %rax, %r12
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq $-0x80000000, %rax      # imm = 0x80000000
cmpq %rcx, %rax
jb 0x2cb9
movl (%r14), %eax
cmpl $0x22, %eax
je 0x2cb9
testl %eax, %eax
jne 0x278f
movl (%rsp), %eax
movl %eax, (%r14)
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x27ab
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x18(%rbx), %rsi
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x2f00
movq 0x30(%rsp), %r13
movl (%r14), %ebx
movl $0x0, (%r14)
leaq 0xc0(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
movl $0xa, %edx
callq 0x2200
cmpq %r13, (%rbp)
je 0x2cc5
leaq -0x80000000(%rax), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
cmpq %rdx, %rcx
jb 0x2cd1
movq %rax, %r13
movl (%r14), %eax
cmpl $0x22, %eax
je 0x2cd1
testl %eax, %eax
jne 0x281f
movl %ebx, (%r14)
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x283b
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21d0
leaq 0xc0(%rsp), %r14
xorpd %xmm0, %xmm0
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x6218
movq %r14, %rdi
xorl %esi, %esi
callq 0x6c60
movapd 0x67a9(%rip), %xmm0     # 0x9010
leaq 0x30(%rsp), %rsi
movapd %xmm0, (%rsi)
leaq 0x160(%rsp), %rdi
leaq 0x8(%rsp), %rcx
movl $0x2, %edx
callq 0x2f3e
movapd 0x6791(%rip), %xmm0     # 0x9020
leaq 0x30(%rsp), %rsi
movapd %xmm0, (%rsi)
leaq 0x148(%rsp), %rdi
leaq 0x8(%rsp), %rcx
movl $0x2, %edx
callq 0x2f3e
leaq 0x30(%rsp), %rsi
movapd 0x6754(%rip), %xmm0     # 0x9010
movapd %xmm0, (%rsi)
leaq 0x130(%rsp), %rdi
leaq 0x8(%rsp), %rcx
movl $0x2, %edx
callq 0x2f3e
movapd 0x6751(%rip), %xmm0     # 0x9030
leaq 0x30(%rsp), %rsi
movapd %xmm0, (%rsi)
leaq 0x118(%rsp), %rdi
leaq 0x8(%rsp), %rcx
movl $0x2, %edx
callq 0x2f3e
leaq 0x30(%rsp), %rdi
leaq 0x160(%rsp), %rsi
leaq 0x148(%rsp), %rdx
leaq 0x130(%rsp), %rcx
leaq 0x118(%rsp), %r8
callq 0x395a
leaq 0xa8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
xorl %edx, %edx
callq 0x6912
leaq 0x190(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
movq %r13, %rcx
callq 0x3a6c
movq 0xb0(%rsp), %rcx
subq 0xa8(%rsp), %rcx
sarq $0x3, %rcx
leaq 0x100(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x190(%rsp), %rdx
callq 0x3d24
leaq 0xe8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x3034
leaq 0x8(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
leaq 0x100(%rsp), %rdx
callq 0x2f83
leaq 0x8(%rsp), %rdi
movq %rdi, %rsi
callq 0x31e3
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x29d7
sqrtsd %xmm0, %xmm0
jmp 0x29dc
callq 0x2170
movsd %xmm0, (%rsp)
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x29f8
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x2a15
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0x178(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
movq %r13, %rcx
callq 0x42d2
leaq 0xc0(%rsp), %rdi
callq 0x639a
leaq 0x1a8(%rsp), %rcx
movq %rax, (%rcx)
movq %rdx, 0x8(%rcx)
leaq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x178(%rsp), %rdx
callq 0x4578
leaq 0x18(%rsp), %rsi
movupd -0x10(%rsi), %xmm0
leaq 0x1e0(%rsp), %rdi
movapd %xmm0, -0x10(%rdi)
callq 0x3034
leaq 0x1a8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0x1d0(%rsp), %rdx
callq 0x749c
leaq 0x1a8(%rsp), %rdi
callq 0x6994
movq %rax, %rdi
movq %rax, %rsi
callq 0x31e3
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2ac6
sqrtsd %xmm0, %xmm0
jmp 0x2acb
callq 0x2170
movsd %xmm0, 0xa0(%rsp)
leaq 0x1a8(%rsp), %rdi
callq 0x6376
leaq 0x1d0(%rsp), %rdi
callq 0x6376
movq 0xb4bb(%rip), %rdi      # 0xdfb0
leaq 0x656a(%rip), %rsi      # 0x9066
movl $0x26, %edx
callq 0x21e0
movq 0xb4a3(%rip), %rdi      # 0xdfb0
movsd (%rsp), %xmm0
callq 0x2260
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
movq 0xb468(%rip), %rdi      # 0xdfb0
leaq 0x653e(%rip), %rsi      # 0x908d
movl $0x26, %edx
callq 0x21e0
movq 0xb450(%rip), %rdi      # 0xdfb0
movsd 0xa0(%rsp), %xmm0
callq 0x2260
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
leaq 0x8(%rsp), %rdi
callq 0x6376
leaq 0x178(%rsp), %rdi
callq 0x3740
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x2bcc
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0x190(%rsp), %rdi
callq 0x340c
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x2bf6
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0x30(%rsp), %rdi
callq 0x3a12
movq 0x118(%rsp), %rdi
testq %rdi, %rdi
je 0x2c1d
movq 0x128(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0x130(%rsp), %rdi
testq %rdi, %rdi
je 0x2c3a
movq 0x140(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0x148(%rsp), %rdi
testq %rdi, %rdi
je 0x2c57
movq 0x158(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x2c74
movq 0x170(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0xc0(%rsp), %rdi
callq 0x6376
xorl %eax, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6418(%rip), %rdi      # 0x90b4
callq 0x2100
leaq 0x640c(%rip), %rdi      # 0x90b4
callq 0x2220
leaq 0x6400(%rip), %rdi      # 0x90b4
callq 0x2100
leaq 0x63f4(%rip), %rdi      # 0x90b4
callq 0x2220
leaq 0x63e8(%rip), %rdi      # 0x90b4
callq 0x2100
leaq 0x63dc(%rip), %rdi      # 0x90b4
callq 0x2220
movq %rax, %r15
jmp 0x2d99
jmp 0x2e0b
jmp 0x2d31
movq %rax, %r15
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x2d0b
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x2d0b
movq %rax, %r15
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x2e25
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x2e25
movq %rax, %r15
jmp 0x2e25
movq %rax, %r15
jmp 0x2e42
movq %rax, %r15
jmp 0x2e4f
movq %rax, %r15
jmp 0x2e6c
movq %rax, %r15
jmp 0x2e76
movq %rax, %r15
jmp 0x2e93
movq %rax, %r15
jmp 0x2eb0
movq %rax, %r15
jmp 0x2ecd
jmp 0x2d73
movq %rax, %r15
jmp 0x2eea
jmp 0x2d81
jmp 0x2d81
jmp 0x2d81
movq %rax, %r15
jmp 0x2ef7
movq %rax, %r15
leaq 0x1a8(%rsp), %rdi
callq 0x6376
leaq 0x1d0(%rsp), %rdi
callq 0x6376
jmp 0x2e0e
movq %rax, %r15
jmp 0x2e18
movq %rax, %r15
cmpl $0x0, (%r14)
jne 0x2dca
movl %ebx, (%r14)
jmp 0x2dca
movq %rax, %r15
cmpl $0x0, (%r14)
jne 0x2dca
movl (%rsp), %eax
movl %eax, (%r14)
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
jmp 0x2df0
movq %rax, %r15
cmpl $0x0, (%r14)
jne 0x2de7
movl %ebp, (%r14)
jmp 0x2de7
movq %rax, %r15
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2ef7
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21d0
jmp 0x2ef7
movq %rax, %r15
leaq 0x8(%rsp), %rdi
callq 0x6376
leaq 0x178(%rsp), %rdi
callq 0x3740
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x2e42
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0x190(%rsp), %rdi
callq 0x340c
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x2e6c
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0x30(%rsp), %rdi
callq 0x3a12
movq 0x118(%rsp), %rdi
testq %rdi, %rdi
je 0x2e93
movq 0x128(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0x130(%rsp), %rdi
testq %rdi, %rdi
je 0x2eb0
movq 0x140(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0x148(%rsp), %rdi
testq %rdi, %rdi
je 0x2ecd
movq 0x158(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x2eea
movq 0x170(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0xc0(%rsp), %rdi
callq 0x6376
movq %r15, %rdi
callq 0x22d0
nop