// File: /workspace/github_c_makefile_star2_O1/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, -0x80(%rbp)
movq %rdx, -0x68(%rbp)
movq %rsi, -0x78(%rbp)
cmpl $0x20, (%rdi)
jg 0x125f6
movq %rdi, %r15
cmpl $0x20, 0x4(%rdi)
jg 0x125f6
cmpl $0x21, 0x8(%r15)
jge 0x125f6
movslq 0x28(%r15), %r13
movq %rsp, %rcx
leaq (,%r13,2), %rax
addq %r13, %rax
leaq 0x1b(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
testq %r13, %r13
jle 0x12486
xorl %r12d, %r12d
movl $0x0, -0x2c(%rbp)
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %r13, -0x70(%rbp)
movq 0x30(%r15), %rax
movq (%rax,%r12,8), %rdi
movq %rdi, -0x60(%rbp)
callq 0x5df8
movslq 0x8(%r15), %rcx
testq %rcx, %rcx
setg %dl
jle 0x1221f
movq 0x20(%r15), %rdx
cmpl %eax, (%rdx)
jne 0x121f9
movb $0x1, %dl
movl $0x0, -0x40(%rbp)
jmp 0x1221f
leaq -0x1(%rcx), %rsi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x1221d
leaq 0x1(%rdi), %r8
cmpl %eax, 0x4(%rdx,%rdi,4)
movq %r8, %rdi
jne 0x121ff
cmpq %rcx, %r8
setb %dl
movl %r8d, -0x40(%rbp)
jmp 0x1221f
xorl %edx, %edx
testb %dl, %dl
je 0x1222f
movl -0x40(%rbp), %eax
btsl %eax, %r14d
jmp 0x12478
movslq 0x4(%r15), %rcx
testq %rcx, %rcx
setg %dl
jle 0x12274
movq 0x18(%r15), %rdx
cmpl %eax, (%rdx)
jne 0x1224e
movb $0x1, %dl
movl $0x0, -0x40(%rbp)
jmp 0x12274
leaq -0x1(%rcx), %rsi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x12272
leaq 0x1(%rdi), %r8
cmpl %eax, 0x4(%rdx,%rdi,4)
movq %r8, %rdi
jne 0x12254
cmpq %rcx, %r8
setb %dl
movl %r8d, -0x40(%rbp)
jmp 0x12274
xorl %edx, %edx
testb %dl, %dl
je 0x12283
movl -0x40(%rbp), %eax
btsl %eax, %ebx
jmp 0x12478
movl %r14d, -0x58(%rbp)
movl $0x100, %edx            # imm = 0x100
leaq -0x180(%rbp), %r14
movq %r14, %rdi
movq -0x60(%rbp), %rsi
callq 0x31a0
movb $0x0, -0x81(%rbp)
movq %r14, %rdi
movl $0x2d, %esi
callq 0x3130
movq %rax, %r14
testq %rax, %rax
je 0x122ce
leaq 0x1(%r14), %rdi
movl $0x2d, %esi
callq 0x3130
movq %rax, %r13
jmp 0x122d1
xorl %r13d, %r13d
testq %r13, %r13
movl %ebx, -0x30(%rbp)
je 0x122e9
leaq 0x1(%r13), %rdi
movl $0x2d, %esi
callq 0x3130
jmp 0x122eb
xorl %eax, %eax
testq %r14, %r14
je 0x12454
testq %r13, %r13
je 0x12312
movb $0x0, (%r13)
incq %r13
testq %rax, %rax
je 0x12315
movb $0x0, (%rax)
incq %rax
movq %rax, -0x48(%rbp)
jmp 0x1231d
xorl %r13d, %r13d
movq $0x0, -0x48(%rbp)
movslq -0x2c(%rbp), %rax
leaq (%rax,%rax,2), %rax
movq -0x50(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0x38(%rbp)
movq 0x10(%r15), %r14
movslq (%r15), %rbx
leaq -0x180(%rbp), %rdi
callq 0x5df8
testq %rbx, %rbx
setg %cl
movq -0x48(%rbp), %rdi
jle 0x12384
cmpl %eax, (%r14)
jne 0x1235b
movb $0x1, %cl
xorl %edx, %edx
jmp 0x1237e
leaq -0x1(%rbx), %rcx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x12454
leaq 0x1(%rsi), %rdx
cmpl %eax, 0x4(%r14,%rsi,4)
movq %rdx, %rsi
jne 0x12361
cmpq %rbx, %rdx
setb %cl
movq -0x38(%rbp), %rax
movl %edx, (%rax)
testb %cl, %cl
je 0x12454
testq %r13, %r13
je 0x123b8
movq 0x18(%r15), %r14
movslq 0x4(%r15), %rbx
movq %r13, %rdi
callq 0x5df8
movq -0x48(%rbp), %rdi
testq %rbx, %rbx
setg %cl
jle 0x123ef
cmpl %eax, (%r14)
jne 0x123c5
movb $0x1, %cl
xorl %edx, %edx
jmp 0x123e8
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
jmp 0x123f3
leaq -0x1(%rbx), %rcx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x12454
leaq 0x1(%rsi), %rdx
cmpl %eax, 0x4(%r14,%rsi,4)
movq %rdx, %rsi
jne 0x123cb
cmpq %rbx, %rdx
setb %cl
movq -0x38(%rbp), %rax
movl %edx, 0x4(%rax)
testb %cl, %cl
je 0x12454
testq %rdi, %rdi
je 0x12418
movq 0x20(%r15), %r14
movslq 0x8(%r15), %rbx
callq 0x5df8
testq %rbx, %rbx
setg %cl
jle 0x1244b
cmpl %eax, (%r14)
jne 0x12425
movb $0x1, %cl
xorl %edx, %edx
jmp 0x12444
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
jmp 0x1244f
leaq -0x1(%rbx), %rcx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x12454
leaq 0x1(%rsi), %rdx
cmpl %eax, 0x4(%r14,%rsi,4)
movq %rdx, %rsi
jne 0x1242b
cmpq %rbx, %rdx
setb %cl
movq -0x38(%rbp), %rax
movl %edx, 0x8(%rax)
testb %cl, %cl
je 0x12454
incl -0x2c(%rbp)
jmp 0x1246d
movl $0x1, %edx
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x12604
movq -0x60(%rbp), %rcx
movq %rcx, (%rax)
movl -0x30(%rbp), %ebx
movl -0x58(%rbp), %r14d
movq -0x70(%rbp), %r13
incq %r12
cmpq %r13, %r12
jne 0x121c9
jmp 0x12492
xorl %ebx, %ebx
xorl %r14d, %r14d
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %esi
testl %esi, %esi
jne 0x124b8
movl 0x38(%r15), %eax
testl %eax, %eax
js 0x124b6
movq -0x50(%rbp), %rcx
movl %eax, (%rcx)
movq $-0x1, 0x4(%rcx)
movl $0x1, %esi
jmp 0x124b8
xorl %esi, %esi
movl 0x3c(%r15), %eax
movl 0x40(%r15), %ecx
movl $0x1, %edi
shll %cl, %edi
xorl %edx, %edx
testl %ecx, %ecx
cmovsl %edx, %edi
movl $0x1, %r8d
testl %r14d, %r14d
cmovnel %r14d, %edi
movl %edi, -0x30(%rbp)
movl %eax, %ecx
shll %cl, %r8d
testl %eax, %eax
cmovsl %edx, %r8d
testl %ebx, %ebx
cmovnel %ebx, %r8d
movl %r8d, -0x48(%rbp)
testl %esi, %esi
jle 0x125e7
movl %esi, %eax
movq %rax, -0x38(%rbp)
xorl %eax, %eax
movq %rax, -0x58(%rbp)
leaq (%rax,%rax,2), %rax
movq -0x50(%rbp), %rcx
movl 0x4(%rcx,%rax,4), %ecx
movl $0x1, %edi
shll %cl, %edi
testl %ecx, %ecx
cmovsl -0x48(%rbp), %edi
testl %edi, %edi
je 0x125d6
movq -0x50(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0x40(%rbp)
movl (%rax), %r15d
movl %edi, -0x60(%rbp)
callq 0x6172
movl %eax, %r12d
movq -0x40(%rbp), %rax
movl 0x8(%rax), %ecx
movl $0x1, %r13d
shll %cl, %r13d
testl %ecx, %ecx
cmovsl -0x30(%rbp), %r13d
testl %r13d, %r13d
je 0x125c7
movq %r12, %rbx
shlq $0x20, %rbx
orq %r15, %rbx
movl %r13d, %edi
callq 0x6172
movl %eax, %r14d
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x125a5
movq -0x68(%rbp), %rcx
movq (%rcx), %rcx
addq $0x8, %rcx
cmpl %r15d, -0x8(%rcx)
jne 0x1259c
cmpl %r12d, -0x4(%rcx)
jne 0x1259c
cmpl %r14d, (%rcx)
je 0x125be
addq $0xc, %rcx
decq %rax
jne 0x1258b
movl $0x1, %edx
movq -0x68(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x1266b
movq %rbx, (%rax)
movl %r14d, 0x8(%rax)
btrl %r14d, %r13d
testl %r13d, %r13d
jne 0x12568
movl -0x60(%rbp), %edi
btrl %r12d, %edi
testl %edi, %edi
jne 0x12537
movq -0x58(%rbp), %rax
incq %rax
cmpq -0x38(%rbp), %rax
jne 0x12503
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4bc6(%rip), %rdi      # 0x171c3
xorl %eax, %eax
callq 0x5bdd