// File: /workspace/github_c_makefile_star2_O3/deplinenoise[P]tundra/src/TargetSelect.cpp
// Function: t2::SelectTargets(t2::TargetSelectInput const&, t2::MemAllocHeap*, t2::Buffer<t2::TargetSpec>*, t2::Buffer<char const*>*)
void SelectTargets(const TargetSelectInput& input, MemAllocHeap* heap, Buffer<TargetSpec>* output, Buffer<const char*>* target_names)
{
  if (input.m_ConfigCount > 32 || input.m_VariantCount > 32 || input.m_SubVariantCount > 32)
    Croak("too many configs/variants/subvariants -- keep it below 32");

  int spec_count = 0;
  InputSpec* specs = (InputSpec*) alloca(sizeof(InputSpec) * (input.m_InputNameCount + 1));

  uint32_t subvariant_bits = 0;
  uint32_t variant_bits    = 0;

  for (int i = 0, count = input.m_InputNameCount; i < count; ++i)
  {
    const char* input_name = input.m_InputNames[i];
    const uint32_t input_hash = Djb2Hash(input_name);
    int item_index;

    if (FindHash(&item_index, input.m_SubVariantNameHashes, input.m_SubVariantCount, input_hash))
    {
      subvariant_bits |= 1 << item_index;
    }
    else if (FindHash(&item_index, input.m_VariantNameHashes, input.m_VariantCount, input_hash))
    {
      variant_bits |= 1 << item_index;
    }
    else if (DestructureInputSpec(input, &specs[spec_count], input_name))
    {
      spec_count++;
    }
    else
    {
      BufferAppendOne(target_names, heap, input_name);
    }
  }

  if (0 == spec_count && input.m_DefaultConfigIndex >= 0)
  {
    specs[0].m_ConfigIndex     = input.m_DefaultConfigIndex;
    specs[0].m_VariantIndex    = -1;
    specs[0].m_SubVariantIndex = -1;
    spec_count = 1;
  }

  if (0 == subvariant_bits && input.m_DefaultSubVariantIndex >= 0)
    subvariant_bits |= 1 << input.m_DefaultSubVariantIndex;

  if (0 == variant_bits && input.m_DefaultVariantIndex >= 0)
    variant_bits |= 1 << input.m_DefaultVariantIndex;

  for (int i = 0; i < spec_count; ++i)
  {
    const InputSpec& spec = specs[i];

    int config_index = spec.m_ConfigIndex;

    int vbits = spec.m_VariantIndex >= 0 ? 1 << spec.m_VariantIndex : variant_bits;

    while (vbits)
    {
      int variant_index = CountTrailingZeroes(vbits);
      vbits &= ~(1 << variant_index);

      int svbits = spec.m_SubVariantIndex >= 0 ? 1 << spec.m_SubVariantIndex : subvariant_bits;

      while (svbits)
      {
        int subvariant_index = CountTrailingZeroes(svbits);
        svbits &= ~(1 << subvariant_index);

        TargetSpec spec = { config_index, variant_index, subvariant_index };

        bool found = false;

        for (size_t o = 0, count = output->m_Size; o < count; ++o)
        {
          if (output->m_Storage[o] == spec)
          {
            found = true;
            break;
          }
        }

        if (!found)
        {
          BufferAppendOne(output, heap, spec);
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rcx, -0x58(%rbp)
movq %rdx, -0x68(%rbp)
movq %rsi, -0x70(%rbp)
cmpl $0x20, (%rdi)
jg 0x11eb9
movq %rdi, %r15
cmpl $0x20, 0x4(%rdi)
jg 0x11eb9
cmpl $0x21, 0x8(%r15)
jge 0x11eb9
movslq 0x28(%r15), %r12
movq %rsp, %rcx
leaq (%r12,%r12,2), %rax
leaq 0x1b(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
movl $0x0, %ebx
movl $0x0, %esi
testq %r12, %r12
jle 0x11d59
xorl %r14d, %r14d
movl $0x0, -0x34(%rbp)
xorl %ebx, %ebx
movl $0x0, -0x2c(%rbp)
movq %r12, -0x50(%rbp)
movq 0x30(%r15), %rax
movq (%rax,%r14,8), %r13
movq %r13, %rdi
callq 0x5c7c
movslq 0x8(%r15), %rcx
testq %rcx, %rcx
jle 0x11b42
movq 0x20(%r15), %rdx
xorl %esi, %esi
cmpl %eax, (%rdx)
jne 0x11b23
btsl %esi, %ebx
jmp 0x11d33
leaq -0x1(%rcx), %rdi
xorl %r8d, %r8d
cmpq %r8, %rdi
je 0x11b42
leaq 0x1(%r8), %rsi
cmpl %eax, 0x4(%rdx,%r8,4)
movq %rsi, %r8
jne 0x11b2a
cmpq %rcx, %rsi
jb 0x11b1b
movslq 0x4(%r15), %rcx
testq %rcx, %rcx
jle 0x11b82
movq 0x18(%r15), %rdx
xorl %esi, %esi
cmpl %eax, (%rdx)
jne 0x11b63
movl -0x2c(%rbp), %eax
btsl %esi, %eax
movl %eax, -0x2c(%rbp)
jmp 0x11d33
leaq -0x1(%rcx), %rdi
xorl %r8d, %r8d
cmpq %r8, %rdi
je 0x11b82
leaq 0x1(%r8), %rsi
cmpl %eax, 0x4(%rdx,%r8,4)
movq %rsi, %r8
jne 0x11b6a
cmpq %rcx, %rsi
jb 0x11b55
movl %ebx, -0x30(%rbp)
movl $0x100, %edx            # imm = 0x100
leaq -0x180(%rbp), %rbx
movq %rbx, %rdi
movq %r13, -0x60(%rbp)
movq %r13, %rsi
callq 0x31a0
movb $0x0, -0x81(%rbp)
movq %rbx, %rdi
movl $0x2d, %esi
callq 0x3130
testq %rax, %rax
je 0x11d13
incq %rax
movq %rax, %rdi
movl $0x2d, %esi
callq 0x3130
testq %rax, %rax
je 0x11bfc
movq %rax, %r13
movq %rax, %rbx
incq %r13
movq %r13, %rdi
movl $0x2d, %esi
callq 0x3130
movb $0x0, (%rbx)
testq %rax, %rax
je 0x11bff
movb $0x0, (%rax)
incq %rax
movq %rax, -0x40(%rbp)
jmp 0x11c07
xorl %r13d, %r13d
movq $0x0, -0x40(%rbp)
movq 0x10(%r15), %r12
movslq (%r15), %rbx
leaq -0x180(%rbp), %rdi
callq 0x5c7c
testq %rbx, %rbx
jle 0x11d13
movslq -0x34(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq -0x48(%rbp), %rdx
leaq (%rdx,%rcx,4), %r8
cmpl %eax, (%r12)
jne 0x11c9c
movl $0x0, (%r8)
testq %r13, %r13
je 0x11cbf
movq 0x18(%r15), %r12
movslq 0x4(%r15), %rax
movq %rax, -0x78(%rbp)
movq %r8, %rbx
movq %r13, %rdi
callq 0x5c7c
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
jle 0x11d13
movq %rbx, %r8
xorl %ecx, %ecx
cmpl %eax, (%r12)
je 0x11cc4
leaq -0x1(%rdi), %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x11d13
leaq 0x1(%rdx), %rsi
cmpl %eax, 0x4(%r12,%rdx,4)
movq %rsi, %rdx
jne 0x11c7a
movl %esi, 0x4(%r8)
cmpq %rdi, %rsi
jb 0x11cc8
jmp 0x11d13
leaq -0x1(%rbx), %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x11d13
leaq 0x1(%rdx), %rsi
cmpl %eax, 0x4(%r12,%rdx,4)
movq %rsi, %rdx
jne 0x11ca2
movl %esi, (%r8)
cmpq %rbx, %rsi
jb 0x11c40
jmp 0x11d13
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x4(%r8)
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x11d41
movq 0x20(%r15), %r12
movslq 0x8(%r15), %rbx
movq %r8, %r13
callq 0x5c7c
testq %rbx, %rbx
jle 0x11d13
movq %r13, %r8
xorl %ecx, %ecx
cmpl %eax, (%r12)
je 0x11d46
leaq -0x1(%rbx), %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x11d13
leaq 0x1(%rdx), %rsi
cmpl %eax, 0x4(%r12,%rdx,4)
movq %rsi, %rdx
jne 0x11cf7
movl %esi, 0x8(%r8)
cmpq %rbx, %rsi
jb 0x11d4a
movl $0x1, %edx
movq -0x58(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x11ec7
movq -0x60(%rbp), %rcx
movq %rcx, (%rax)
movl -0x30(%rbp), %ebx
movq -0x50(%rbp), %r12
incq %r14
cmpq %r12, %r14
jne 0x11af8
jmp 0x11d4f
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x8(%r8)
incl -0x34(%rbp)
jmp 0x11d2c
movl -0x34(%rbp), %edi
testl %edi, %edi
movl -0x2c(%rbp), %esi
jne 0x11d78
movl 0x38(%r15), %eax
testl %eax, %eax
js 0x11d76
movq -0x48(%rbp), %rcx
movl %eax, (%rcx)
movq $-0x1, 0x4(%rcx)
movl $0x1, %edi
jmp 0x11d78
xorl %edi, %edi
movl 0x3c(%r15), %eax
movl 0x40(%r15), %ecx
movl $0x1, %r8d
shll %cl, %r8d
xorl %edx, %edx
testl %ecx, %ecx
cmovsl %edx, %r8d
movl $0x1, %r9d
testl %ebx, %ebx
cmovnel %ebx, %r8d
movl %r8d, -0x2c(%rbp)
movl %eax, %ecx
shll %cl, %r9d
testl %eax, %eax
cmovsl %edx, %r9d
testl %esi, %esi
cmovnel %esi, %r9d
movl %r9d, -0x58(%rbp)
testl %edi, %edi
jle 0x11eaa
movl %edi, %eax
movq %rax, -0x40(%rbp)
xorl %eax, %eax
movq %rax, -0x50(%rbp)
leaq (%rax,%rax,2), %rax
movq -0x48(%rbp), %rcx
movl 0x4(%rcx,%rax,4), %ecx
movl $0x1, %edi
shll %cl, %edi
testl %ecx, %ecx
cmovsl -0x58(%rbp), %edi
testl %edi, %edi
je 0x11e99
movq -0x48(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0x60(%rbp)
movl (%rax), %r15d
movl %edi, -0x30(%rbp)
callq 0x5ff6
movl %eax, %r12d
movq -0x60(%rbp), %rax
movl 0x8(%rax), %ecx
movl $0x1, %r13d
shll %cl, %r13d
testl %ecx, %ecx
cmovsl -0x2c(%rbp), %r13d
testl %r13d, %r13d
je 0x11e8a
movq %r12, %rbx
shlq $0x20, %rbx
orq %r15, %rbx
movl %r13d, %edi
callq 0x5ff6
movl %eax, %r14d
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x11e68
movq -0x68(%rbp), %rcx
movq (%rcx), %rcx
addq $0x8, %rcx
cmpl %r15d, -0x8(%rcx)
jne 0x11e5f
cmpl %r12d, -0x4(%rcx)
jne 0x11e5f
cmpl %r14d, (%rcx)
je 0x11e81
addq $0xc, %rcx
decq %rax
jne 0x11e4e
movl $0x1, %edx
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x11f2e
movq %rbx, (%rax)
movl %r14d, 0x8(%rax)
btrl %r14d, %r13d
testl %r13d, %r13d
jne 0x11e2b
movl -0x30(%rbp), %edi
btrl %r12d, %edi
testl %edi, %edi
jne 0x11dfa
movq -0x50(%rbp), %rax
incq %rax
cmpq -0x40(%rbp), %rax
jne 0x11dc6
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5303(%rip), %rdi      # 0x171c3
xorl %eax, %eax
callq 0x5a61