// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/formatter.cpp
// Function: get_grouped_level_count(ash::ResultSet const*, std::vector<unsigned long, std::allocator<unsigned long>> const&)
int get_grouped_level_count(const ResultSet * rs, const vector<size_t> & widths)
{
  if (!rs) return 0;  // Sanity check.

  size_t width = 0, length = rs -> rows, XX = 4;
  for (size_t i = 0, e = widths.size(); i != e; ++i) width += widths[i];
  size_t min_area = length * width;
  
  // examine the columns from left to right simulating how much screen space
  // would be saved by grouping that column.  If there is a net reduction in
  // screen 'area', then the column will be grouped.  Otherwise it will not.

  // store area of output after simulating grouping at each level successively.
  // the rightmost minimum area will be chosen.
  //
  // For example, consider the following areas after simulating grouping:
  //   areas = [100, 90, 92, 90, 140, 281]
  //
  // With 1 level of grouping and with 3 levels of grouping we get the same
  // screen area, however the rightmost value is chosen, so the return value
  // will be 3.
  vector<size_t> areas(widths.size(), width * length);
  
  string prev;
  for (size_t c = 0, cols = rs -> columns; c < cols; ++c) {
    // test each row in the column to see if it is a duplicate of the previous
    // row.  If so, it will be de-duped in the output.  If not, it means an
    // extra row will be added, so we adjust the new_len variable accordingly.
    prev = "";
    for (size_t r = 0, rows = rs -> rows; r < rows; ++r) {
      if (prev != rs -> data[r][c]) {
        ++length;
        prev = rs -> data[r][c];
      }
    }
    // to calculate the new width, we need to consider both the width of the 
    // grouped column and the width of the remaining columns.  we also need to
    // consider the width of the indent.
    width = max(width - widths[c], widths[c]) + XX * (c + 1);
    min_area = min(length * width, min_area);
    if (c < rs -> columns - 1) areas[c + 1] = width * length;
  }
  // Find the rightmost minimum area from all simulated areas.
  for (size_t c = rs -> columns; c > 0; --c) {
    if (areas[c - 1] == min_area) return c - 1;
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
testq %rdi, %rdi
je 0x6eda3
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x3, %rsi
movq 0x30(%rdi), %rbp
xorl %r15d, %r15d
cmpq %rax, %rcx
je 0x6ec18
xorl %ecx, %ecx
addq (%rax,%rcx,8), %r15
incq %rcx
cmpq %rcx, %rsi
jne 0x6ec0c
movq %r15, %r12
imulq %rbp, %r12
leaq 0x10(%rsp), %rdx
movq %r12, (%rdx)
leaq 0x50(%rsp), %rdi
leaq 0x7(%rsp), %rcx
callq 0x6f346
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x38(%rbx), %rax
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0x6ed50
movq $0x0, 0x8(%rsp)
movq %r14, 0x30(%rsp)
movq %r12, 0x48(%rsp)
movq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
leaq 0xf6de(%rip), %rcx      # 0x7e360
xorl %r8d, %r8d
callq 0x85f0
movq %r15, 0x40(%rsp)
movq 0x30(%rbx), %r12
testq %r12, %r12
je 0x6ecec
movq 0x8(%rsp), %r14
shlq $0x5, %r14
xorl %r15d, %r15d
movq 0x18(%rbx), %rax
movq (%rax,%r15), %rax
leaq (%rax,%r14), %r13
movq 0x18(%rsp), %rdx
cmpq 0x8(%rax,%r14), %rdx
jne 0x6ecd3
testq %rdx, %rdx
je 0x6ece3
movq (%r13), %rsi
movq 0x10(%rsp), %rdi
callq 0x8410
testl %eax, %eax
je 0x6ece3
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x81f0
incq %rbp
addq $0x18, %r15
decq %r12
jne 0x6eca4
movq 0x30(%rsp), %r14
movq (%r14), %rax
movq 0x8(%rsp), %rsi
movq (%rax,%rsi,8), %rax
movq 0x40(%rsp), %r15
subq %rax, %r15
cmpq %rax, %r15
cmovbeq %rax, %r15
leaq (%r15,%rsi,4), %r15
addq $0x4, %r15
movq %r15, %rcx
imulq %rbp, %rcx
movq 0x48(%rsp), %r12
cmpq %rcx, %r12
cmovaeq %rcx, %r12
leaq 0x1(%rsi), %rax
movq 0x38(%rbx), %rdx
decq %rdx
cmpq %rdx, %rsi
jae 0x6ed40
movq 0x50(%rsp), %rdx
movq %rcx, (%rdx,%rax,8)
movq %rax, 0x8(%rsp)
cmpq 0x38(%rsp), %rax
jne 0x6ec6a
movq 0x38(%rbx), %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,8), %rcx
addq $-0x8, %rcx
xorl %ebx, %ebx
xorl %edx, %edx
movq %rax, %rsi
addq %rdx, %rsi
je 0x6ed7e
leaq -0x1(%rdx), %rsi
cmpq %r12, (%rcx,%rdx,8)
movq %rsi, %rdx
jne 0x6ed65
addl %esi, %eax
movl %eax, %ebx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x6ed92
callq 0x8390
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x6eda5
callq 0x8390
jmp 0x6eda5
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6edb8
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x6edcf
callq 0x8390
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x6edde
callq 0x8390
movq %rbx, %rdi
callq 0x8720