// File: /workspace/github_cpp_makefile_star2_O3/jpbalarini[P]otsus/./framework/CInspector.cpp
// Function: CInspector::process_input(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CFramework*)
int CInspector::process_input(string input, CFramework *fw) {
  vector<string> tokens;

  int num_tokens = read_tokens(&tokens, input);
  if (num_tokens == 0) return INSPECTOR_OK; // just pressed <ENTER>

  string command = tokens[0];
  if (!check_command_arguments(command, num_tokens)) {
    return INSPECTOR_ERR_BAD_ARGUMENTS;
  }

  if (command == "l" || command == "list") {
    fw->print_matrix_list();
    return INSPECTOR_OK;
  }

  if (command == "u" || command == "use") {
    if (tokens.size() != 2) {
      return INSPECTOR_ERR_NO_MATRIX_ARGUMENT;
    }
    string name = tokens[1];

    MatrixInfo *matrix = fw->find_matrix_by_list_pos(atoi(name.c_str()));
    //MatrixInfo *matrix = fw->find_matrix(name);
    if (matrix == NULL) {
      return INSPECTOR_ERR_MATRIX_DOES_NOT_EXIST;
    }
    //
    G_using_matrix = matrix;
    describe_matrix(matrix);

    CInspector::prod_dims = new int[matrix->num_dims];
    acc_product_of_dims(matrix, CInspector::prod_dims);
  }

  if (command == "bt") {
    printf("Backtrace:\n");
    fw->print_backtrace(stdout);
    return INSPECTOR_OK;
  }


  if (command == "desc") {
    describe_matrix(G_using_matrix);
    return INSPECTOR_OK;
  }

  if (command == "g" || command == "get") {
    if (G_using_matrix == NULL) {
      return INSPECTOR_ERR_NO_MATRIX_SELECTED_YET;
    }

    // Check number of coordinates
    int num_coords = tokens.size() - 1;
    if (num_coords != G_using_matrix->num_dims) {
      return INSPECTOR_ERR_COORDINATES_ARGUMENT;
    }

    int coords[num_coords];
    for (int i = 0; i < num_coords; i++) {
      int coord = atoi(tokens[i+1].c_str());
      if (coord >= G_using_matrix->N[i]) {
        return INSPECTOR_ERR_OUT_OF_BOUNDS;
      }
      coords[i] = coord;
    }
    //
    int pos = get_pos_by_coordinates(coords, G_using_matrix);

    char coords_buffer[1024];
    list_of_ints(coords_buffer, ", ", coords, num_coords);


    printf("Matrix_%d[%s] == %f (pos %d).\n", G_using_matrix->id, coords_buffer, G_using_matrix->data[pos], pos);
    return INSPECTOR_OK;
  }

  if (command == "s" || command == "set") {
    if (G_using_matrix == NULL) {
      return INSPECTOR_ERR_NO_MATRIX_SELECTED_YET;
    }

    // Check number of coordinates
    int num_coords = tokens.size() - 2;
    if (num_coords != G_using_matrix->num_dims) {
      return INSPECTOR_ERR_COORDINATES_ARGUMENT;
    }

    int coords[num_coords];
    for (int i = 0; i < num_coords; i++) {
      int coord = atoi(tokens[i+1].c_str());
      if (coord >= G_using_matrix->N[i]) {
        return INSPECTOR_ERR_OUT_OF_BOUNDS;
      }
      coords[i] = coord;
    }
    //
    int pos = get_pos_by_coordinates(coords, G_using_matrix);
    float new_value = atof(tokens[num_coords+1].c_str());

    char coords_buffer[1024];
    list_of_ints(coords_buffer, ", ", coords, num_coords);

    printf("Before: M_%d[%s] == %f (pos %d).\n", G_using_matrix->id, coords_buffer, G_using_matrix->data[pos], pos);
    G_using_matrix->data[pos] = new_value;
    printf("After: M_%d[%s] == %f (pos %d).\n", G_using_matrix->id, coords_buffer, G_using_matrix->data[pos], pos);
    return INSPECTOR_OK;
  }

  if (command == "mem") {
    rusage usage;
    int status = getrusage(RUSAGE_SELF, &usage);
    if (status == 0) {
      printf("maximum resident set size: %lu\n", usage.ru_maxrss);
      printf("integral shared memory size: %lu\n", usage.ru_ixrss);
      printf("integral unshared data size: %lu\n", usage.ru_idrss);
      printf("integral unshared stack size: %lu\n", usage.ru_isrss);
      printf("page reclaims (soft page faults): %lu\n", usage.ru_minflt);
      printf("page faults (hard page faults): %lu\n", usage.ru_majflt);
      printf("swaps: %lu\n", usage.ru_nswap);
      printf("block input operations: %lu\n", usage.ru_inblock);
      printf("block output operations: %lu\n", usage.ru_oublock);
      printf("IPC messages sent: %lu\n", usage.ru_msgsnd);
      printf("IPC messages received: %lu\n", usage.ru_msgrcv);
      printf("signals received: %lu\n", usage.ru_nsignals);
      printf("voluntary context switches: %lu\n", usage.ru_nvcsw);
      printf("involuntary context switches: %lu\n", usage.ru_nivcsw);
    }
    return INSPECTOR_OK;
  }

  if (command == "save") {
    if (G_using_matrix == NULL)
      return INSPECTOR_ERR_NO_MATRIX_SELECTED_YET;
    if (tokens.size() != 2)
      return INSPECTOR_ERR_NO_FILENAME;
    //
    char *filename = (char*)tokens[1].c_str();
    int id = G_using_matrix->id;
    fw->save_matrix(G_using_matrix, filename);
    G_using_matrix = fw->find_matrix(id);
    return INSPECTOR_OK;
  }

  if (command == "load") {
    if (G_using_matrix == NULL)
      return INSPECTOR_ERR_NO_MATRIX_SELECTED_YET;
    if (tokens.size() != 2)
      return INSPECTOR_ERR_NO_FILENAME;
    //
    char *filename = (char*)tokens[1].c_str();
    int status = fw->load_matrix(G_using_matrix, filename);
    switch (status) {
      case  0: return INSPECTOR_OK; break;
      case -1: return INSPECTOR_ERR_CAN_NOT_OPEN_FILE; break;
      case -2: return INSPECTOR_ERR_INCOMPATIBLE_DIMENSIONS; break;
      case -3: return INSPECTOR_ERR_READ_ERROR; break;
    }
    return INSPECTOR_OK;
  }


  if (command == "h" || command == "help") {
    printf("Framework CLI inpector. Help not available yet, sorry.\n");
    return INSPECTOR_OK;
  }

  return INSPECTOR_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4a8, %rsp            # imm = 0x4A8
movq %rsi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
leaq -0x98(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%rdi), %rsi
movq 0x8(%rdi), %rdx
addq %rsi, %rdx
leaq -0xa8(%rbp), %rdi
callq 0x6e58
leaq -0x50(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x5c20
movl %eax, %r14d
movq -0xa8(%rbp), %rdi
cmpq %r15, %rdi
je 0x5f31
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x3260
testl %r14d, %r14d
je 0x61fb
movq -0x50(%rbp), %rax
leaq -0x60(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x70(%rbp), %rdi
callq 0x6e58
leaq -0xb8(%rbp), %r13
movq %r13, -0x10(%r13)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq %rsi, %rdx
leaq -0xc8(%rbp), %rdi
callq 0x6e58
leaq -0xc8(%rbp), %r12
movq %r12, %rdi
movl %r14d, %esi
callq 0x5e1e
movl %eax, %r15d
movq (%r12), %rdi
cmpq %r13, %rdi
je 0x5fa8
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x3260
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testb %r15b, %r15b
je 0x620b
leaq 0x26b0(%rip), %rsi      # 0x866e
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x6200
leaq 0x268f(%rip), %rsi      # 0x8665
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x6200
leaq 0x2682(%rip), %rsi      # 0x8670
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x6013
leaq 0x2668(%rip), %rsi      # 0x866a
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
jne 0x60ed
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rax, %rcx
movl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
cmpq $0x40, %rcx
jne 0x620b
leaq -0x4c0(%rbp), %r15
movq %r15, -0x10(%r15)
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
addq %rsi, %rdx
leaq -0x4d0(%rbp), %rdi
callq 0x6e58
movq -0x4d0(%rbp), %rdi
callq 0x3410
movq %rbx, %rdi
movl %eax, %esi
callq 0x420c
movq %rax, %r14
testq %rax, %rax
je 0x6372
movq %r14, 0x6267(%rip)      # 0xc2e0
movq %r14, %rdi
callq 0x5dc0
movslq 0xc(%r14), %rax
leaq (,%rax,4), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x3030
movq %rax, 0x6241(%rip)      # 0xc2e8
movl $0x1, (%rax)
cmpl $0x2, 0xc(%r14)
jl 0x60d2
movq 0x10(%r14), %rcx
movl (%rax), %edx
movl $0x1, %esi
imull (%rcx,%rsi,4), %edx
movl %edx, (%rax,%rsi,4)
incq %rsi
movslq 0xc(%r14), %rdi
cmpq %rdi, %rsi
jl 0x60bf
movq -0x4d0(%rbp), %rdi
cmpq %r15, %rdi
je 0x60ed
movq -0x4c0(%rbp), %rsi
incq %rsi
callq 0x3260
leaq 0x257e(%rip), %rsi      # 0x8672
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x623f
leaq 0x2569(%rip), %rsi      # 0x8675
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x6361
leaq 0x2556(%rip), %rsi      # 0x867a
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x625f
leaq 0x2540(%rip), %rsi      # 0x867c
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x625f
leaq 0x206c(%rip), %rsi      # 0x81c0
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x6398
leaq 0x2536(%rip), %rsi      # 0x86a2
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x6398
leaq 0x2565(%rip), %rsi      # 0x86e9
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x6522
leaq 0x26d9(%rip), %rsi      # 0x8875
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x6666
leaq 0x26c6(%rip), %rsi      # 0x887a
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x66b8
leaq 0x26b3(%rip), %rsi      # 0x887f
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
je 0x61ed
leaq 0x26a1(%rip), %rsi      # 0x8881
leaq -0x70(%rbp), %rdi
callq 0x3100
testl %eax, %eax
jne 0x6208
leaq 0x28d5(%rip), %rdi      # 0x8ac9
callq 0x3400
jmp 0x6208
xorl %r14d, %r14d
jmp 0x6224
movq %rbx, %rdi
callq 0x3ec0
xorl %r14d, %r14d
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x6224
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x3260
leaq -0x50(%rbp), %rdi
callq 0x6b4e
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x28ba(%rip), %rdi      # 0x8b00
callq 0x3400
movq 0x5d2e(%rip), %rax      # 0xbf80
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x440e
jmp 0x6208
movq 0x607a(%rip), %rax      # 0xc2e0
testq %rax, %rax
je 0x6707
movq -0x48(%rbp), %rcx
subq -0x50(%rbp), %rcx
shrq $0x5, %rcx
decl %ecx
movl $0xfffffffb, %r14d      # imm = 0xFFFFFFFB
cmpl 0xc(%rax), %ecx
jne 0x620b
movq %rsp, -0x80(%rbp)
movl %ecx, %r14d
movq %rsp, %r15
leaq 0xf(,%r14,4), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq 0x6035(%rip), %rax      # 0xc2e0
movq %rax, -0x30(%rbp)
movq %rcx, -0x78(%rbp)
testl %ecx, %ecx
jle 0x62ea
movq -0x50(%rbp), %r13
movq -0x30(%rbp), %rax
movq 0x10(%rax), %r12
shlq $0x2, %r14
xorl %ebx, %ebx
movq 0x20(%r13,%rbx,8), %rdi
callq 0x3410
cmpl (%r12,%rbx), %eax
jge 0x6513
movl %eax, (%r15,%rbx)
addq $0x4, %rbx
cmpq %rbx, %r14
jne 0x62c9
movq -0x30(%rbp), %rax
movslq 0xc(%rax), %rax
xorl %r14d, %r14d
movl $0x0, %r12d
testq %rax, %rax
jle 0x631f
xorl %ecx, %ecx
movq 0x5fdf(%rip), %rdx      # 0xc2e8
xorl %r12d, %r12d
movl (%r15,%rcx,4), %esi
imull (%rdx,%rcx,4), %esi
addl %esi, %r12d
incq %rcx
cmpq %rcx, %rax
jne 0x630c
leaq 0x235a(%rip), %rsi      # 0x8680
leaq -0x4d0(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rdx
movq -0x78(%rbp), %rcx
callq 0x5d42
movq 0x5f9d(%rip), %rax      # 0xc2e0
movl 0x8(%rax), %esi
movq 0x18(%rax), %rax
movslq %r12d, %rcx
cvtss2sd (%rax,%rcx,4), %xmm0
leaq 0x232a(%rip), %rdi      # 0x8683
movq %rbx, %rdx
jmp 0x650a
movq 0x5f78(%rip), %rdi      # 0xc2e0
callq 0x5dc0
jmp 0x6208
movq -0x4d0(%rbp), %rdi
cmpq %r15, %rdi
je 0x638d
movq -0x4c0(%rbp), %rsi
incq %rsi
callq 0x3260
movl $0xfffffffd, %r14d      # imm = 0xFFFFFFFD
jmp 0x620b
movq 0x5f41(%rip), %rax      # 0xc2e0
testq %rax, %rax
je 0x6707
movq -0x48(%rbp), %rdx
subq -0x50(%rbp), %rdx
movq %rdx, %rcx
shrq $0x5, %rcx
addl $-0x2, %ecx
movl $0xfffffffb, %r14d      # imm = 0xFFFFFFFB
cmpl 0xc(%rax), %ecx
jne 0x620b
movq %rdx, -0x88(%rbp)
movq %rsp, -0x80(%rbp)
movl %ecx, %r14d
movq %rsp, %r15
leaq 0xf(,%r14,4), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq 0x5eed(%rip), %rax      # 0xc2e0
movq %rax, -0x30(%rbp)
movq %rcx, -0x78(%rbp)
testl %ecx, %ecx
jle 0x6432
movq -0x50(%rbp), %r13
movq -0x30(%rbp), %rax
movq 0x10(%rax), %r12
shlq $0x2, %r14
xorl %ebx, %ebx
movq 0x20(%r13,%rbx,8), %rdi
callq 0x3410
cmpl (%r12,%rbx), %eax
jge 0x6513
movl %eax, (%r15,%rbx)
addq $0x4, %rbx
cmpq %rbx, %r14
jne 0x6411
movq -0x30(%rbp), %rax
movslq 0xc(%rax), %rax
xorl %r14d, %r14d
movl $0x0, %r12d
testq %rax, %rax
jle 0x6467
xorl %ecx, %ecx
movq 0x5e97(%rip), %rdx      # 0xc2e8
xorl %r12d, %r12d
movl (%r15,%rcx,4), %esi
imull (%rdx,%rcx,4), %esi
addl %esi, %r12d
incq %rcx
cmpq %rcx, %rax
jne 0x6454
movq -0x88(%rbp), %rcx
shlq $0x1b, %rcx
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
movq -0x50(%rbp), %rcx
sarq $0x1b, %rax
movq (%rcx,%rax), %rdi
callq 0x3330
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x30(%rbp)
leaq 0x21e0(%rip), %rsi      # 0x8680
leaq -0x4d0(%rbp), %r13
movq %r13, %rdi
movq %r15, %rdx
movq -0x78(%rbp), %rcx
callq 0x5d42
movq 0x5e23(%rip), %rax      # 0xc2e0
movl 0x8(%rax), %esi
movq 0x18(%rax), %rax
movslq %r12d, %rbx
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rbx,4), %xmm0
leaq 0x21d0(%rip), %rdi      # 0x86a6
movq %r13, %rdx
movl %ebx, %ecx
movb $0x1, %al
callq 0x3070
movq 0x5df7(%rip), %rax      # 0xc2e0
movq 0x18(%rax), %rcx
movss -0x30(%rbp), %xmm0
movss %xmm0, (%rcx,%rbx,4)
movl 0x8(%rax), %esi
cvtss2sd %xmm0, %xmm0
leaq 0x21c3(%rip), %rdi      # 0x86c8
movq %r13, %rdx
movl %ebx, %ecx
movb $0x1, %al
callq 0x3070
jmp 0x6519
movl $0xfffffffa, %r14d      # imm = 0xFFFFFFFA
movq -0x80(%rbp), %rsp
jmp 0x620b
xorl %r14d, %r14d
leaq -0x4d0(%rbp), %rsi
xorl %edi, %edi
callq 0x3180
testl %eax, %eax
jne 0x620b
movq -0x4b0(%rbp), %rsi
leaq 0x21a4(%rip), %rdi      # 0x86ed
xorl %eax, %eax
callq 0x3070
movq -0x4a8(%rbp), %rsi
leaq 0x21af(%rip), %rdi      # 0x870d
xorl %eax, %eax
callq 0x3070
movq -0x4a0(%rbp), %rsi
leaq 0x21bc(%rip), %rdi      # 0x872f
xorl %eax, %eax
callq 0x3070
movq -0x498(%rbp), %rsi
leaq 0x21c9(%rip), %rdi      # 0x8751
xorl %eax, %eax
callq 0x3070
movq -0x490(%rbp), %rsi
leaq 0x21d7(%rip), %rdi      # 0x8774
xorl %eax, %eax
callq 0x3070
movq -0x488(%rbp), %rsi
leaq 0x21e9(%rip), %rdi      # 0x879b
xorl %eax, %eax
callq 0x3070
movq -0x480(%rbp), %rsi
leaq 0x21f9(%rip), %rdi      # 0x87c0
xorl %eax, %eax
callq 0x3070
movq -0x478(%rbp), %rsi
leaq 0x21f0(%rip), %rdi      # 0x87cc
xorl %eax, %eax
callq 0x3070
movq -0x470(%rbp), %rsi
leaq 0x21f8(%rip), %rdi      # 0x87e9
xorl %eax, %eax
callq 0x3070
movq -0x468(%rbp), %rsi
leaq 0x2201(%rip), %rdi      # 0x8807
xorl %eax, %eax
callq 0x3070
movq -0x460(%rbp), %rsi
leaq 0x2204(%rip), %rdi      # 0x881f
xorl %eax, %eax
callq 0x3070
movq -0x458(%rbp), %rsi
leaq 0x220b(%rip), %rdi      # 0x883b
xorl %eax, %eax
callq 0x3070
movq -0x450(%rbp), %rsi
leaq 0x220f(%rip), %rdi      # 0x8854
xorl %eax, %eax
callq 0x3070
movq -0x448(%rbp), %rsi
leaq 0x21f8(%rip), %rdi      # 0x8852
xorl %eax, %eax
callq 0x3070
jmp 0x620b
movq 0x5c73(%rip), %rsi      # 0xc2e0
testq %rsi, %rsi
je 0x6707
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rax, %rcx
movl $0xfffffff9, %r14d      # imm = 0xFFFFFFF9
cmpq $0x40, %rcx
jne 0x620b
movq 0x20(%rax), %rdx
movl 0x8(%rsi), %r14d
movq %rbx, %rdi
callq 0x4248
movq %rbx, %rdi
movl %r14d, %esi
callq 0x4178
movq %rax, 0x5c2d(%rip)      # 0xc2e0
jmp 0x6208
movq 0x5c21(%rip), %rsi      # 0xc2e0
testq %rsi, %rsi
je 0x6707
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rax, %rcx
movl $0xfffffff9, %r14d      # imm = 0xFFFFFFF9
cmpq $0x40, %rcx
jne 0x620b
movq 0x20(%rax), %rdx
movq %rbx, %rdi
callq 0x4314
cmpl $-0x4, %eax
jbe 0x6208
cltq
leaq 0x2427(%rip), %rcx      # 0x8b24
movl 0xc(%rcx,%rax,4), %r14d
jmp 0x620b
movl $0xfffffffc, %r14d      # imm = 0xFFFFFFFC
jmp 0x620b
jmp 0x673a
jmp 0x673a
jmp 0x673a
movq %rax, %rbx
movq -0x4d0(%rbp), %rdi
cmpq %r15, %rdi
je 0x673d
movq -0x4c0(%rbp), %rsi
incq %rsi
callq 0x3260
jmp 0x673d
jmp 0x6770
movq %rax, %rbx
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x6773
movq -0x60(%rbp), %rsi
jmp 0x6766
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r15, %rdi
je 0x6773
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x3260
jmp 0x6773
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x6b4e
movq %rbx, %rdi
callq 0x34b0