// File: /workspace/github_c_makefile_star2_O2/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 $0x168, %rsp            # imm = 0x168
movq %rcx, -0x88(%rbp)
movq %rsi, -0x80(%rbp)
cmpl $0x20, (%rdi)
jg 0x10856
movq %rdi, %r15
cmpl $0x20, 0x4(%rdi)
jg 0x10856
cmpl $0x21, 0x8(%r15)
jge 0x10856
movq %rdx, %rbx
movslq 0x28(%r15), %rax
movq %rsp, %rdx
imulq $0xc, %rax, %rcx
addq $0x1b, %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x68(%rbp)
movq %rdx, %rsp
xorl %r12d, %r12d
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, -0x70(%rbp)
leaq -0x30(%rbp), %r14
movl $0x0, -0x50(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x40(%rbp)
cmpq %r12, -0x70(%rbp)
je 0x1070d
movq 0x30(%r15), %rax
movq (%rax,%r12,8), %rdi
movq %rdi, -0x58(%rbp)
callq 0x58de
movl %eax, %r13d
movq 0x20(%r15), %rsi
movl 0x8(%r15), %edx
movq %r14, %rdi
movl %eax, %ecx
callq 0x10864
testb %al, %al
je 0x10580
movb -0x30(%rbp), %al
movl -0x2c(%rbp), %ecx
btsl %eax, %ecx
movl %ecx, -0x2c(%rbp)
jmp 0x106f5
movq 0x18(%r15), %rsi
movl 0x4(%r15), %edx
movq %r14, %rdi
movl %r13d, %ecx
callq 0x10864
testb %al, %al
je 0x105a8
movb -0x30(%rbp), %al
movl -0x50(%rbp), %ecx
btsl %eax, %ecx
movl %ecx, -0x50(%rbp)
jmp 0x106f1
movl $0x100, %edx            # imm = 0x100
leaq -0x190(%rbp), %r14
movq %r14, %rdi
movq -0x58(%rbp), %rsi
callq 0x31a0
movb $0x0, -0x91(%rbp)
movq %r14, %rdi
pushq $0x2d
popq %rsi
callq 0x3130
testq %rax, %rax
je 0x106dd
incq %rax
movq %rax, %rdi
pushq $0x2d
popq %r14
movl %r14d, %esi
callq 0x3130
testq %rax, %rax
je 0x1061f
movq %rax, %r13
movq %rax, -0x38(%rbp)
incq %r13
movq %r13, %rdi
movl %r14d, %esi
callq 0x3130
movq -0x38(%rbp), %rcx
movb $0x0, (%rcx)
testq %rax, %rax
je 0x10622
movb $0x0, (%rax)
incq %rax
movq %rax, -0x60(%rbp)
jmp 0x1062a
xorl %r13d, %r13d
movq $0x0, -0x60(%rbp)
movslq -0x40(%rbp), %rax
imulq $0xc, %rax, %r14
addq -0x68(%rbp), %r14
movq 0x10(%r15), %rax
movq %rax, -0x38(%rbp)
movl (%r15), %eax
movl %eax, -0x48(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x58de
movq %r14, %rdi
movq -0x38(%rbp), %rsi
movl -0x48(%rbp), %edx
movl %eax, %ecx
callq 0x10864
testb %al, %al
je 0x106dd
movq %r14, -0x38(%rbp)
addq $0x4, %r14
testq %r13, %r13
je 0x106a4
movq 0x18(%r15), %rax
movq %rax, -0x48(%rbp)
movl 0x4(%r15), %eax
movl %eax, -0x74(%rbp)
movq %r13, %rdi
callq 0x58de
movq %r14, %rdi
movq -0x48(%rbp), %rsi
movl -0x74(%rbp), %edx
movl %eax, %ecx
callq 0x10864
testb %al, %al
movq -0x38(%rbp), %r14
jne 0x106ac
jmp 0x106dd
orl $-0x1, (%r14)
movq -0x38(%rbp), %r14
addq $0x8, %r14
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x106fd
movq 0x20(%r15), %r13
movl 0x8(%r15), %eax
movl %eax, -0x38(%rbp)
callq 0x58de
movq %r14, %rdi
movq %r13, %rsi
movl -0x38(%rbp), %edx
movl %eax, %ecx
callq 0x10864
testb %al, %al
jne 0x10701
movq -0x88(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0xfee7
leaq -0x30(%rbp), %r14
incq %r12
jmp 0x1053b
orl $-0x1, (%r14)
movq -0x40(%rbp), %rax
incl %eax
movq %rax, -0x40(%rbp)
jmp 0x106f1
pushq $0x1
popq %r13
movq -0x40(%rbp), %rdx
testl %edx, %edx
jne 0x10733
movl 0x38(%r15), %eax
testl %eax, %eax
js 0x10731
movq -0x68(%rbp), %rcx
movl %eax, (%rcx)
orq $-0x1, 0x4(%rcx)
movl %r13d, %edx
jmp 0x10733
xorl %edx, %edx
movl 0x3c(%r15), %eax
movl 0x40(%r15), %ecx
movl %r13d, %esi
shll %cl, %esi
xorl %edi, %edi
testl %ecx, %ecx
cmovsl %edi, %esi
movl -0x2c(%rbp), %ecx
testl %ecx, %ecx
cmovnel %ecx, %esi
movl %esi, -0x38(%rbp)
movl %r13d, %esi
movl %eax, %ecx
shll %cl, %esi
testl %eax, %eax
cmovsl %edi, %esi
movl -0x50(%rbp), %eax
testl %eax, %eax
cmovnel %eax, %esi
movl %esi, -0x48(%rbp)
testl %edx, %edx
cmovlel %edi, %edx
movq %rdx, -0x40(%rbp)
cmpq %rdx, %rdi
je 0x10847
movq %rdi, -0x50(%rbp)
imulq $0xc, %rdi, %rax
movq -0x68(%rbp), %rdx
leaq (%rdx,%rax), %rcx
movq %rcx, -0x70(%rbp)
movl 0x4(%rdx,%rax), %ecx
movl %r13d, %edi
shll %cl, %edi
movl (%rdx,%rax), %r14d
testl %ecx, %ecx
cmovsl -0x48(%rbp), %edi
testl %edi, %edi
je 0x10837
movl %edi, -0x58(%rbp)
callq 0x5bff
movl %eax, %r12d
pushq $-0x2
popq %rax
movl %r12d, %ecx
roll %cl, %eax
movl %eax, -0x2c(%rbp)
movq -0x70(%rbp), %rax
movl 0x8(%rax), %ecx
shll %cl, %r13d
testl %ecx, %ecx
cmovsl -0x38(%rbp), %r13d
movq %r12, %r15
shlq $0x20, %r15
orq %r14, %r15
testl %r13d, %r13d
je 0x10828
movl %r13d, %edi
callq 0x5bff
btrl %eax, %r13d
movq 0x8(%rbx), %rcx
pushq $0x8
popq %rdx
subq $0x1, %rcx
jb 0x10815
movq (%rbx), %rsi
cmpl %r14d, -0x8(%rsi,%rdx)
jne 0x1080f
cmpl %r12d, -0x4(%rsi,%rdx)
jne 0x1080f
cmpl %eax, (%rsi,%rdx)
je 0x107db
addq $0xc, %rdx
jmp 0x107f3
movq %rbx, %rdi
movq -0x80(%rbp), %rsi
movq %r15, %rdx
movl %eax, %ecx
callq 0x1088d
jmp 0x107db
movl -0x58(%rbp), %edi
andl -0x2c(%rbp), %edi
pushq $0x1
popq %r13
jmp 0x107a2
movq -0x50(%rbp), %rdi
incq %rdi
movq -0x40(%rbp), %rdx
jmp 0x10772
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x495e(%rip), %rdi      # 0x151bb
xorl %eax, %eax
callq 0x56cd