// File: /workspace/github_cpp_makefile_star2_O1/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 0x26af
movq 0xb9a0(%rip), %rdi      # 0xdfb0
leaq 0x69ed(%rip), %rsi      # 0x9004
movl $0x7, %edx
callq 0x21e0
movq (%rbx), %rsi
leaq 0x38(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
callq 0x2f2e
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0xb969(%rip), %rdi      # 0xdfb0
callq 0x21e0
movq %rax, %rbx
leaq 0x69b6(%rip), %rsi      # 0x900c
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 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x26a5
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21d0
movl $0x1, %eax
jmp 0x2cb0
movq 0x8(%rbx), %rsi
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x2f2e
movq 0x38(%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 0x2cc2
movq %rax, %r15
movslq %r15d, %rax
cmpq %r15, %rax
jne 0x2cce
movl (%r14), %eax
cmpl $0x22, %eax
je 0x2cce
testl %eax, %eax
jne 0x271b
movl %ebp, (%r14)
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2736
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x10(%rbx), %rsi
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x2f2e
movq 0x38(%rsp), %r13
movl (%r14), %eax
movl %eax, 0x8(%rsp)
movl $0x0, (%r14)
leaq 0xc0(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
movl $0xa, %edx
callq 0x2200
cmpq %r13, (%rbp)
je 0x2cda
movq %rax, %r12
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq $-0x80000000, %rax      # imm = 0x80000000
cmpq %rcx, %rax
jb 0x2ce6
movl (%r14), %eax
cmpl $0x22, %eax
je 0x2ce6
testl %eax, %eax
jne 0x27b1
movl 0x8(%rsp), %eax
movl %eax, (%r14)
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x27cd
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21d0
movq 0x18(%rbx), %rsi
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x2f2e
movq 0x38(%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 0x2cf2
leaq -0x80000000(%rax), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
cmpq %rdx, %rcx
jb 0x2cfe
movq %rax, %r13
movl (%r14), %eax
cmpl $0x22, %eax
je 0x2cfe
testl %eax, %eax
jne 0x2841
movl %ebx, (%r14)
movslq %r15d, %r14
movslq %r12d, %r15
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x2863
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21d0
leaq 0xc0(%rsp), %r12
xorpd %xmm0, %xmm0
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6134
movq %r12, %rdi
xorl %esi, %esi
callq 0x6ad6
movabsq $0x3fe6a09e667f3bcc, %r14 # imm = 0x3FE6A09E667F3BCC
leaq 0x38(%rsp), %rsi
movq %r14, (%rsi)
movq %r14, 0x8(%rsi)
leaq 0x160(%rsp), %rdi
leaq 0x10(%rsp), %rcx
movl $0x2, %edx
callq 0x2f6c
movabsq $-0x40195f619980c434, %rbx # imm = 0xBFE6A09E667F3BCC
leaq 0x38(%rsp), %rsi
movq %rbx, (%rsi)
movq %r14, 0x8(%rsi)
leaq 0x148(%rsp), %rdi
leaq 0x10(%rsp), %rcx
movl $0x2, %edx
callq 0x2f6c
leaq 0x38(%rsp), %rsi
movq %r14, (%rsi)
movq %r14, 0x8(%rsi)
leaq 0x130(%rsp), %rdi
leaq 0x10(%rsp), %rcx
movl $0x2, %edx
callq 0x2f6c
leaq 0x38(%rsp), %rsi
movq %r14, (%rsi)
movq %rbx, 0x8(%rsi)
leaq 0x118(%rsp), %rdi
leaq 0x10(%rsp), %rcx
movl $0x2, %edx
callq 0x2f6c
leaq 0x38(%rsp), %rdi
leaq 0x160(%rsp), %rsi
leaq 0x148(%rsp), %rdx
leaq 0x130(%rsp), %rcx
leaq 0x118(%rsp), %r8
callq 0x3974
leaq 0xa8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
xorl %edx, %edx
callq 0x682c
movslq %r13d, %rbx
leaq 0x190(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
movq %rbx, %rcx
callq 0x3a86
movq 0xb0(%rsp), %rcx
subq 0xa8(%rsp), %rcx
sarq $0x3, %rcx
leaq 0x100(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x190(%rsp), %rdx
callq 0x3ce0
leaq 0xe8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x3062
leaq 0x10(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
leaq 0x100(%rsp), %rdx
callq 0x2fb1
leaq 0x10(%rsp), %rdi
movq %rdi, %rsi
callq 0x3217
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2a02
sqrtsd %xmm0, %xmm0
jmp 0x2a07
callq 0x2170
movsd %xmm0, 0x8(%rsp)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x2a24
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x2a41
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0x178(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
movq %rbx, %rcx
callq 0x422e
leaq 0xc0(%rsp), %rdi
callq 0x62b6
leaq 0x1a8(%rsp), %rcx
movq %rax, (%rcx)
movq %rdx, 0x8(%rcx)
leaq 0x10(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x178(%rsp), %rdx
callq 0x44d4
leaq 0x20(%rsp), %rsi
movupd -0x10(%rsi), %xmm0
leaq 0x1e0(%rsp), %rdi
movapd %xmm0, -0x10(%rdi)
callq 0x3062
leaq 0x1a8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0x1d0(%rsp), %rdx
callq 0x7300
leaq 0x1a8(%rsp), %rdi
callq 0x68ae
movq %rax, %rdi
movq %rax, %rsi
callq 0x3217
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2af2
sqrtsd %xmm0, %xmm0
jmp 0x2af7
callq 0x2170
movsd %xmm0, 0xa0(%rsp)
leaq 0x1a8(%rsp), %rdi
callq 0x6292
leaq 0x1d0(%rsp), %rdi
callq 0x6292
movq 0xb48f(%rip), %rdi      # 0xdfb0
leaq 0x6502(%rip), %rsi      # 0x902a
movl $0x26, %edx
callq 0x21e0
movq 0xb477(%rip), %rdi      # 0xdfb0
movsd 0x8(%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 0xb43b(%rip), %rdi      # 0xdfb0
leaq 0x64d5(%rip), %rsi      # 0x9051
movl $0x26, %edx
callq 0x21e0
movq 0xb423(%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 0x10(%rsp), %rdi
callq 0x6292
leaq 0x178(%rsp), %rdi
callq 0x375a
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x2bf9
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0x190(%rsp), %rdi
callq 0x343a
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x2c23
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0x38(%rsp), %rdi
callq 0x3a2c
movq 0x118(%rsp), %rdi
testq %rdi, %rdi
je 0x2c4a
movq 0x128(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0x130(%rsp), %rdi
testq %rdi, %rdi
je 0x2c67
movq 0x140(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0x148(%rsp), %rdi
testq %rdi, %rdi
je 0x2c84
movq 0x158(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x2ca1
movq 0x170(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0xc0(%rsp), %rdi
callq 0x6292
xorl %eax, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x63af(%rip), %rdi      # 0x9078
callq 0x2100
leaq 0x63a3(%rip), %rdi      # 0x9078
callq 0x2220
leaq 0x6397(%rip), %rdi      # 0x9078
callq 0x2100
leaq 0x638b(%rip), %rdi      # 0x9078
callq 0x2220
leaq 0x637f(%rip), %rdi      # 0x9078
callq 0x2100
leaq 0x6373(%rip), %rdi      # 0x9078
callq 0x2220
movq %rax, %r15
jmp 0x2dc6
jmp 0x2e39
jmp 0x2d5e
movq %rax, %r15
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x2d38
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x2d38
movq %rax, %r15
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x2e53
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x2e53
movq %rax, %r15
jmp 0x2e53
movq %rax, %r15
jmp 0x2e70
movq %rax, %r15
jmp 0x2e7d
movq %rax, %r15
jmp 0x2e9a
movq %rax, %r15
jmp 0x2ea4
movq %rax, %r15
jmp 0x2ec1
movq %rax, %r15
jmp 0x2ede
movq %rax, %r15
jmp 0x2efb
jmp 0x2da0
movq %rax, %r15
jmp 0x2f18
jmp 0x2dae
jmp 0x2dae
jmp 0x2dae
movq %rax, %r15
jmp 0x2f25
movq %rax, %r15
leaq 0x1a8(%rsp), %rdi
callq 0x6292
leaq 0x1d0(%rsp), %rdi
callq 0x6292
jmp 0x2e3c
movq %rax, %r15
jmp 0x2e46
movq %rax, %r15
cmpl $0x0, (%r14)
jne 0x2df8
movl %ebx, (%r14)
jmp 0x2df8
movq %rax, %r15
cmpl $0x0, (%r14)
jne 0x2df8
movl 0x8(%rsp), %eax
movl %eax, (%r14)
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
jmp 0x2e1e
movq %rax, %r15
cmpl $0x0, (%r14)
jne 0x2e15
movl %ebp, (%r14)
jmp 0x2e15
movq %rax, %r15
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2f25
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21d0
jmp 0x2f25
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x6292
leaq 0x178(%rsp), %rdi
callq 0x375a
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x2e70
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0x190(%rsp), %rdi
callq 0x343a
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x2e9a
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0x38(%rsp), %rdi
callq 0x3a2c
movq 0x118(%rsp), %rdi
testq %rdi, %rdi
je 0x2ec1
movq 0x128(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0x130(%rsp), %rdi
testq %rdi, %rdi
je 0x2ede
movq 0x140(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0x148(%rsp), %rdi
testq %rdi, %rdi
je 0x2efb
movq 0x158(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x2f18
movq 0x170(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
leaq 0xc0(%rsp), %rdi
callq 0x6292
movq %r15, %rdi
callq 0x22d0
nop