// File: Ciaxur[P]yagb-emu/include/imgui/imgui_tables.cpp
// Function: ImGui::TableSortSpecsSanitize(ImGuiTable*)
void ImGui::TableSortSpecsSanitize(ImGuiTable* table)
{
    IM_ASSERT(table->Flags & ImGuiTableFlags_Sortable);

    // Clear SortOrder from hidden column and verify that there's no gap or duplicate.
    int sort_order_count = 0;
    ImU64 sort_order_mask = 0x00;
    for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
    {
        ImGuiTableColumn* column = &table->Columns[column_n];
        if (column->SortOrder != -1 && !column->IsEnabled)
            column->SortOrder = -1;
        if (column->SortOrder == -1)
            continue;
        sort_order_count++;
        sort_order_mask |= ((ImU64)1 << column->SortOrder);
        IM_ASSERT(sort_order_count < (int)sizeof(sort_order_mask) * 8);
    }

    const bool need_fix_linearize = ((ImU64)1 << sort_order_count) != (sort_order_mask + 1);
    const bool need_fix_single_sort_order = (sort_order_count > 1) && !(table->Flags & ImGuiTableFlags_SortMulti);
    if (need_fix_linearize || need_fix_single_sort_order)
    {
        ImU64 fixed_mask = 0x00;
        for (int sort_n = 0; sort_n < sort_order_count; sort_n++)
        {
            // Fix: Rewrite sort order fields if needed so they have no gap or duplicate.
            // (e.g. SortOrder 0 disappeared, SortOrder 1..2 exists --> rewrite then as SortOrder 0..1)
            int column_with_smallest_sort_order = -1;
            for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
                if ((fixed_mask & ((ImU64)1 << (ImU64)column_n)) == 0 && table->Columns[column_n].SortOrder != -1)
                    if (column_with_smallest_sort_order == -1 || table->Columns[column_n].SortOrder < table->Columns[column_with_smallest_sort_order].SortOrder)
                        column_with_smallest_sort_order = column_n;
            IM_ASSERT(column_with_smallest_sort_order != -1);
            fixed_mask |= ((ImU64)1 << column_with_smallest_sort_order);
            table->Columns[column_with_smallest_sort_order].SortOrder = (ImGuiTableColumnIdx)sort_n;

            // Fix: Make sure only one column has a SortOrder if ImGuiTableFlags_MultiSortable is not set.
            if (need_fix_single_sort_order)
            {
                sort_order_count = 1;
                for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
                    if (column_n != column_with_smallest_sort_order)
                        table->Columns[column_n].SortOrder = -1;
                break;
            }
        }
    }

    // Fallback default sort order (if no column had the ImGuiTableColumnFlags_DefaultSort flag)
    if (sort_order_count == 0 && !(table->Flags & ImGuiTableFlags_SortTristate))
        for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
        {
            ImGuiTableColumn* column = &table->Columns[column_n];
            if (column->IsEnabled && !(column->Flags & ImGuiTableColumnFlags_NoSort))
            {
                sort_order_count = 1;
                column->SortOrder = 0;
                column->SortDirection = (ImU8)TableGetColumnAvailSortDirection(column, 0);
                break;
            }
        }

    table->SortSpecsCount = (ImGuiTableColumnIdx)sort_order_count;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x13268d
jmp 0x1326ac
leaq 0x5aa0f(%rip), %rdi     # 0x18d0a3
leaq 0x59907(%rip), %rsi     # 0x18bfa2
movl $0xa13, %edx            # imm = 0xA13
leaq 0x5aa24(%rip), %rcx     # 0x18d0cb
callq 0xd180
movl $0x0, -0xc(%rbp)
movq $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x6c(%rcx), %eax
jge 0x132770
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movl -0x1c(%rbp), %esi
callq 0xeea20
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movsbl 0x56(%rax), %eax
cmpl $-0x1, %eax
je 0x132705
movq -0x28(%rbp), %rax
testb $0x1, 0x5a(%rax)
jne 0x132705
movq -0x28(%rbp), %rax
movb $-0x1, 0x56(%rax)
movq -0x28(%rbp), %rax
movsbl 0x56(%rax), %eax
cmpl $-0x1, %eax
jne 0x132714
jmp 0x132762
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
movq -0x28(%rbp), %rax
movsbl 0x56(%rax), %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
orq -0x18(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpl $0x40, -0xc(%rbp)
jge 0x132741
jmp 0x132760
leaq 0x5a9b4(%rip), %rdi     # 0x18d0fc
leaq 0x59853(%rip), %rsi     # 0x18bfa2
movl $0xa21, %edx            # imm = 0xA21
leaq 0x5a970(%rip), %rcx     # 0x18d0cb
callq 0xd180
jmp 0x132762
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1326c2
movl -0xc(%rbp), %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq -0x18(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
setne %al
andb $0x1, %al
movb %al, -0x29(%rbp)
xorl %eax, %eax
cmpl $0x1, -0xc(%rbp)
movb %al, -0x59(%rbp)
jle 0x1327b2
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x4000000, %eax        # imm = 0x4000000
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
andb $0x1, %al
movb %al, -0x2a(%rbp)
testb $0x1, -0x29(%rbp)
jne 0x1327ca
testb $0x1, -0x2a(%rbp)
je 0x132940
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x13293e
movl $0xffffffff, -0x40(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x6c(%rcx), %eax
jge 0x13288b
movq -0x38(%rbp), %rax
movslq -0x44(%rbp), %rcx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
jne 0x13287b
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movl -0x44(%rbp), %esi
callq 0xeea20
movsbl 0x56(%rax), %eax
cmpl $-0x1, %eax
je 0x13287b
cmpl $-0x1, -0x40(%rbp)
je 0x132873
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movl -0x44(%rbp), %esi
callq 0xeea20
movsbl 0x56(%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movl -0x40(%rbp), %esi
callq 0xeea20
movq %rax, %rcx
movl -0x60(%rbp), %eax
movsbl 0x56(%rcx), %ecx
cmpl %ecx, %eax
jge 0x132879
movl -0x44(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x13287b
jmp 0x13287d
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1327f3
cmpl $-0x1, -0x40(%rbp)
je 0x132893
jmp 0x1328b2
leaq 0x5a896(%rip), %rdi     # 0x18d130
leaq 0x59701(%rip), %rsi     # 0x18bfa2
movl $0xa32, %edx            # imm = 0xA32
leaq 0x5a81e(%rip), %rcx     # 0x18d0cb
callq 0xd180
movl -0x40(%rbp), %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
orq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
movb %al, -0x61(%rbp)
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movl -0x40(%rbp), %esi
callq 0xeea20
movb -0x61(%rbp), %cl
movb %cl, 0x56(%rax)
testb $0x1, -0x2a(%rbp)
je 0x13292e
movl $0x1, -0xc(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x6c(%rcx), %eax
jge 0x13292c
movl -0x48(%rbp), %eax
cmpl -0x40(%rbp), %eax
je 0x13291f
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movl -0x48(%rbp), %esi
callq 0xeea20
movb $-0x1, 0x56(%rax)
jmp 0x132921
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1328f7
jmp 0x13293e
jmp 0x132930
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1327d9
jmp 0x132940
cmpl $0x0, -0xc(%rbp)
jne 0x1329df
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x8000000, %eax        # imm = 0x8000000
cmpl $0x0, %eax
jne 0x1329df
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x6c(%rcx), %eax
jge 0x1329dd
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movl -0x4c(%rbp), %esi
callq 0xeea20
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
testb $0x1, 0x5a(%rax)
je 0x1329d0
movq -0x58(%rbp), %rax
movl (%rax), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
jne 0x1329d0
movl $0x1, -0xc(%rbp)
movq -0x58(%rbp), %rax
movb $0x0, 0x56(%rax)
movq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x132320
movb %al, %dl
movq -0x58(%rbp), %rax
movb 0x64(%rax), %cl
andb $0x3, %dl
andb $-0x4, %cl
orb %dl, %cl
movb %cl, 0x64(%rax)
jmp 0x1329dd
jmp 0x1329d2
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x132966
jmp 0x1329df
movl -0xc(%rbp), %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x230(%rax)
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)