// File: /workspace/github_c_makefile_star2_O3/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 0x6eace
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 %r12d, %r12d
cmpq %rax, %rcx
je 0x6e990
xorl %ecx, %ecx
addq (%rax,%rcx,8), %r12
incq %rcx
cmpq %rcx, %rsi
jne 0x6e984
movq %r12, %r15
imulq %rbp, %r15
leaq 0x10(%rsp), %rdx
movq %r15, (%rdx)
leaq 0x50(%rsp), %rdi
leaq 0x7(%rsp), %rcx
callq 0x6f0b0
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 0x6ead2
movq $0x0, 0x8(%rsp)
movq %r14, 0x30(%rsp)
movq %r12, 0x48(%rsp)
movq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
leaq 0xf9b6(%rip), %rcx      # 0x7e3b0
xorl %r8d, %r8d
callq 0x85f0
movq %r15, 0x40(%rsp)
movq 0x30(%rbx), %r12
testq %r12, %r12
je 0x6ea64
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 0x6ea4b
testq %rdx, %rdx
je 0x6ea5b
movq (%r13), %rsi
movq 0x10(%rsp), %rdi
callq 0x8410
testl %eax, %eax
je 0x6ea5b
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x81f0
incq %rbp
addq $0x18, %r15
decq %r12
jne 0x6ea1c
movq 0x30(%rsp), %r14
movq (%r14), %rax
movq 0x8(%rsp), %rsi
movq (%rax,%rsi,8), %rax
movq 0x48(%rsp), %r12
subq %rax, %r12
cmpq %rax, %r12
cmovbeq %rax, %r12
leaq (%r12,%rsi,4), %r12
addq $0x4, %r12
movq %r12, %rcx
imulq %rbp, %rcx
movq 0x40(%rsp), %r15
cmpq %rcx, %r15
cmovaeq %rcx, %r15
leaq 0x1(%rsi), %rax
movq 0x38(%rbx), %rdx
decq %rdx
cmpq %rdx, %rsi
jae 0x6eab8
movq 0x50(%rsp), %rdx
movq %rcx, (%rdx,%rax,8)
movq %rax, 0x8(%rsp)
cmpq 0x38(%rsp), %rax
jne 0x6e9e2
movq 0x38(%rbx), %rbx
jmp 0x6ead4
xorl %ebx, %ebx
jmp 0x6eb0f
xorl %ebx, %ebx
movq 0x50(%rsp), %rdi
subq $0x1, %rbx
jb 0x6eae7
cmpq %r15, (%rdi,%rbx,8)
jne 0x6ead9
jmp 0x6eae9
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
leaq 0x20(%rsp), %rcx
cmpq %rcx, %rax
je 0x6eb05
movq %rax, %rdi
callq 0x8390
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x6eb0f
callq 0x8390
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6eb22
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x6eb39
callq 0x8390
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x6eb48
callq 0x8390
movq %rbx, %rdi
callq 0x8720