// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x148(%rsp)
movq %rsi, 0x140(%rsp)
cmpq $0x0, 0x148(%rsp)
jne 0x9d192
movl $0x0, 0x154(%rsp)
jmp 0x9d6b6
movq $0x0, 0x138(%rsp)
movq 0x148(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x130(%rsp)
movq $0x4, 0x128(%rsp)
movq $0x0, 0x120(%rsp)
movq 0x140(%rsp), %rdi
callq 0x9e360
movq %rax, 0x118(%rsp)
movq 0x120(%rsp), %rax
cmpq 0x118(%rsp), %rax
je 0x9d22f
movq 0x140(%rsp), %rdi
movq 0x120(%rsp), %rsi
callq 0x9e4a0
movq (%rax), %rax
addq 0x138(%rsp), %rax
movq %rax, 0x138(%rsp)
movq 0x120(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x120(%rsp)
jmp 0x9d1df
movq 0x130(%rsp), %rax
movq 0x138(%rsp), %rcx
imulq %rcx, %rax
movq %rax, 0x110(%rsp)
movq 0x140(%rsp), %rdi
callq 0x9e360
movq %rax, 0x68(%rsp)
movq 0x138(%rsp), %rax
movq 0x130(%rsp), %rcx
imulq %rcx, %rax
movq %rax, 0xf0(%rsp)
leaq 0xef(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0x9e4c0
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rcx
leaq 0xf8(%rsp), %rdi
leaq 0xf0(%rsp), %rdx
callq 0x9e4d0
jmp 0x9d2ac
leaq 0xef(%rsp), %rdi
callq 0x9e560
leaq 0xb8(%rsp), %rdi
callq 0x85c0
jmp 0x9d2c8
movq $0x0, 0xb0(%rsp)
movq 0x148(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, 0xa8(%rsp)
movq 0xb0(%rsp), %rax
cmpq 0xa8(%rsp), %rax
jae 0x9d5ef
leaq 0x15af0(%rip), %rsi     # 0xb2df5
leaq 0xb8(%rsp), %rdi
callq 0x85a0
jmp 0x9d314
movq $0x0, 0xa0(%rsp)
movq 0x148(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x98(%rsp)
movq 0xa0(%rsp), %rax
cmpq 0x98(%rsp), %rax
jae 0x9d48a
movq 0x148(%rsp), %rdi
addq $0x18, %rdi
movq 0xa0(%rsp), %rsi
callq 0x94620
movq %rax, 0x60(%rsp)
jmp 0x9d36a
movq 0x60(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x94640
movq %rax, 0x58(%rsp)
jmp 0x9d383
movq 0x58(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x8d190
movb %al, 0x57(%rsp)
jmp 0x9d39b
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0x9d3a8
jmp 0x9d46f
movq 0x130(%rsp), %rax
incq %rax
movq %rax, 0x130(%rsp)
movq 0x148(%rsp), %rdi
addq $0x18, %rdi
movq 0xa0(%rsp), %rsi
callq 0x94620
movq %rax, 0x48(%rsp)
jmp 0x9d3db
movq 0x48(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x94640
movq %rax, 0x40(%rsp)
jmp 0x9d3f4
movq 0x40(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x8050
jmp 0x9d408
jmp 0x9d46f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
leaq 0xef(%rsp), %rdi
callq 0x9e560
jmp 0x9d6c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
jmp 0x9d6a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x88f8
jmp 0x9d6a3
jmp 0x9d471
movq 0xa0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa0(%rsp)
jmp 0x9d334
movq 0x138(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x140(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x9e4a0
movq %rax, 0x38(%rsp)
jmp 0x9d4b3
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
movq %rax, 0x90(%rsp)
movq 0x140(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x9e4a0
movq %rax, 0x28(%rsp)
jmp 0x9d4e7
movq 0x28(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x8cff0
movq %rax, 0x20(%rsp)
jmp 0x9d500
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x128(%rsp), %rcx
movq 0xb0(%rsp), %rdx
incq %rdx
imulq %rdx, %rcx
addq %rcx, %rax
movq %rax, 0x138(%rsp)
movq 0x130(%rsp), %rax
movq 0x138(%rsp), %rcx
imulq %rcx, %rax
movq %rax, 0x88(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x960d0
movq %rax, 0x18(%rsp)
jmp 0x9d562
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x110(%rsp)
movq 0xb0(%rsp), %rax
movq 0x148(%rsp), %rcx
movq 0x38(%rcx), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x9d5d4
movq 0x138(%rsp), %rax
movq 0x130(%rsp), %rcx
imulq %rcx, %rax
movq %rax, 0x8(%rsp)
movq 0xb0(%rsp), %rsi
incq %rsi
leaq 0xf8(%rsp), %rdi
callq 0x9e2c0
movq %rax, 0x10(%rsp)
jmp 0x9d5c7
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
jmp 0x9d5d6
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x9d2e8
movq 0x148(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, 0x80(%rsp)
jbe 0x9d672
movq 0x80(%rsp), %rsi
decq %rsi
leaq 0xf8(%rsp), %rdi
callq 0x9e2c0
movq %rax, (%rsp)
jmp 0x9d62c
movq (%rsp), %rax
movq (%rax), %rax
cmpq 0x110(%rsp), %rax
jne 0x9d65a
movq 0x80(%rsp), %rax
subq $0x1, %rax
movl %eax, 0x154(%rsp)
movl $0x1, 0x7c(%rsp)
jmp 0x9d685
jmp 0x9d65c
movq 0x80(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x80(%rsp)
jmp 0x9d603
movl $0x0, 0x154(%rsp)
movl $0x1, 0x7c(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x88f8
jmp 0x9d694
leaq 0xf8(%rsp), %rdi
callq 0x9e2e0
jmp 0x9d6b6
jmp 0x9d6a5
leaq 0xf8(%rsp), %rdi
callq 0x9e2e0
jmp 0x9d6b4
jmp 0x9d6c5
movl 0x154(%rsp), %eax
addq $0x158, %rsp            # imm = 0x158
retq
movq 0xe0(%rsp), %rdi
callq 0x87d0
movq %rax, %rdi
callq 0x8cf60
nopw (%rax,%rax)