// File: /workspace/github_c_makefile_star2_O0/deplinenoise[P]tundra/src/LuaInterpolate.cpp
// Function: t2::InterpolateVar(t2::StringBuffer&, char const*, unsigned long, t2::LuaEnvLookup&)
static bool InterpolateVar(StringBuffer& output, const char* str, size_t len, LuaEnvLookup& lookup)
{
  StringBuffer var_name(output.GetHeap());

  if (!DoInterpolate(var_name, str, len, lookup))
    return false;

  var_name.NullTerminate();

  enum
  {
    kMaxOptions = 10
  };

  size_t      data_size                = var_name.GetSize() - 1;
  char       *data                     = var_name.GetBuffer();
  size_t      option_count             = 0;
  char       *option_ptrs[kMaxOptions];

  if (char *options = strchr(data, ':'))
  {
    data_size = options - data;

    while (nullptr != (options = strchr(options, ':')))
    {
      if (options[-1] == '\\')
      {
        ++options;
        continue;
      }

      if (option_count == kMaxOptions)
        return false;

      *options = '\0';
      option_ptrs[option_count++] = options + 1;
      ++options;
    }

    for (size_t i = 0; i < option_count; ++i)
    {
      UnescapeOption(option_ptrs[i]);
    }
  }

  LuaEnvLookupScope scope(lookup, data, data_size);
  if (!scope.Valid())
    return false;

  const char *join_string = " ";
  size_t      first_index = 0;
  size_t      max_index   = scope.GetCount();

  for (size_t oi = 0; oi < option_count; ++oi)
  {
    const char* option = option_ptrs[oi];
    switch (option[0])
    {
      case 'j':
        join_string = &option[1];
        break;
      case '[':
        first_index = atoi(&option[1]) - 1;
        max_index = first_index + 1;
        if (first_index >= scope.GetCount())
          return false;
        break;
    }
  }

  const size_t  join_len    = strlen(join_string);

  for (size_t i = first_index; i < max_index; ++i)
  {
    size_t item_len;
    const char* item_text = scope.GetValue(i, &item_len);
    if (!item_text)
      return false;

    StringBuffer item(output.GetHeap());

    if (!DoInterpolate(item, item_text, item_len, lookup))
      return false;

    for (size_t oi = 0; oi < option_count; ++oi)
    {
      const char* option = option_ptrs[oi];
      switch (option[0])
      {
        case 'p': item.Prepend(&option[1], strlen(&option[1])); break;
        case 'a': item.Append(&option[1], strlen(&option[1])); break;
        case 'P': item.PrependUnless(&option[1], strlen(&option[1])); break;
        case 'A': item.AppendUnless(&option[1], strlen(&option[1])); break;
        case 'f': item.ToForwardSlashes(); break;
        case 'b': item.ToBackSlashes(); break;
        case 'n':
#if defined(TUNDRA_WIN32)
          item.ToBackSlashes();
#else
          item.ToForwardSlashes();
#endif
          break;
        case 'u': item.ToUppercase(); break;
        case 'l': item.ToLowercase(); break;
        case 'B': item.DropSuffix(); break;
        case 'F': item.GetFilename(); break;
        case 'D': item.GetFilenameDir(); break;
        case 'q': item.Quote(); break;
        case '#': item.EscapeForCmdlineDefine(); break;

        // Ignore these here
        case '[': case 'j':
          break;
        default:
          fprintf(stderr, "bad interpolation option: \"%s\"\n", option);
          return false;
      }
    }

    if (i > first_index)
      output.Add(join_string, join_len);

    output.Add(item.GetBuffer(), item.GetSize());
  }

  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8cc0
movq %rax, %rsi
leaq -0x90(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x8b60
movq -0x208(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x80a0
movb %al, -0x1f9(%rbp)
jmp 0x82da
movb -0x1f9(%rbp), %al
testb $0x1, %al
jne 0x8310
jmp 0x82e6
movb $0x0, -0x1(%rbp)
movl $0x1, -0xa0(%rbp)
jmp 0x8a75
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x8a8f
leaq -0x90(%rbp), %rdi
callq 0x8ce0
jmp 0x831e
leaq -0x90(%rbp), %rdi
callq 0x8be0
movq %rax, -0x210(%rbp)
jmp 0x8333
movq -0x210(%rbp), %rax
decq %rax
movq %rax, -0xa8(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x8ba0
movq %rax, -0x218(%rbp)
jmp 0x8359
movq -0x218(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq $0x0, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
movl $0x3a, %esi
callq 0x61f0
movq %rax, -0x118(%rbp)
cmpq $0x0, -0x118(%rbp)
je 0x84a9
movq -0x118(%rbp), %rax
movq -0xb0(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0x118(%rbp), %rdi
movl $0x3a, %esi
callq 0x61f0
movq %rax, %rcx
movq %rcx, -0x118(%rbp)
xorl %eax, %eax
cmpq %rcx, %rax
je 0x8460
movq -0x118(%rbp), %rax
movsbl -0x1(%rax), %eax
cmpl $0x5c, %eax
jne 0x83fa
movq -0x118(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x118(%rbp)
jmp 0x83b0
cmpq $0xa, -0xb8(%rbp)
jne 0x8417
movb $0x0, -0x1(%rbp)
movl $0x1, -0xa0(%rbp)
jmp 0x8a75
movq -0x118(%rbp), %rax
movb $0x0, (%rax)
movq -0x118(%rbp), %rcx
addq $0x1, %rcx
movq -0xb8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xb8(%rbp)
movq %rcx, -0x110(%rbp,%rax,8)
movq -0x118(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x118(%rbp)
jmp 0x83b0
movq $0x0, -0x120(%rbp)
movq -0x120(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jae 0x84a7
movq -0x120(%rbp), %rax
movq -0x110(%rbp,%rax,8), %rdi
callq 0x8ab0
jmp 0x8491
jmp 0x8493
movq -0x120(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x120(%rbp)
jmp 0x846b
jmp 0x84a9
movq -0x28(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq -0xa8(%rbp), %rcx
leaq -0x138(%rbp), %rdi
callq 0x8d10
jmp 0x84c9
leaq -0x138(%rbp), %rdi
callq 0x8f20
movb %al, -0x219(%rbp)
jmp 0x84dd
movb -0x219(%rbp), %al
testb $0x1, %al
jne 0x8513
jmp 0x84e9
movb $0x0, -0x1(%rbp)
movl $0x1, -0xa0(%rbp)
jmp 0x8a59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x8a67
leaq 0x44b32(%rip), %rax     # 0x4d04c
movq %rax, -0x140(%rbp)
movq $0x0, -0x148(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x8f40
movq %rax, -0x228(%rbp)
jmp 0x8541
movq -0x228(%rbp), %rax
movq %rax, -0x150(%rbp)
movq $0x0, -0x158(%rbp)
movq -0x158(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jae 0x864f
movq -0x158(%rbp), %rax
movq -0x110(%rbp,%rax,8), %rax
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0x22c(%rbp)
subl $0x5b, %eax
je 0x85c0
jmp 0x859b
movl -0x22c(%rbp), %eax
subl $0x6a, %eax
jne 0x8636
jmp 0x85ac
movq -0x160(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x140(%rbp)
jmp 0x8636
movq -0x160(%rbp), %rdi
incq %rdi
callq 0x6720
decl %eax
cltq
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rax
incq %rax
movq %rax, -0x150(%rbp)
movq -0x148(%rbp), %rax
movq %rax, -0x240(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x8f40
movq %rax, -0x238(%rbp)
jmp 0x860e
movq -0x240(%rbp), %rax
movq -0x238(%rbp), %rcx
cmpq %rcx, %rax
jb 0x8634
movb $0x0, -0x1(%rbp)
movl $0x1, -0xa0(%rbp)
jmp 0x8a59
jmp 0x8636
jmp 0x8638
movq -0x158(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x158(%rbp)
jmp 0x855a
movq -0x140(%rbp), %rdi
callq 0x6220
movq %rax, -0x168(%rbp)
movq -0x148(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rax
cmpq -0x150(%rbp), %rax
jae 0x8a4b
movq -0x170(%rbp), %rsi
leaq -0x138(%rbp), %rdi
leaq -0x178(%rbp), %rdx
callq 0x8f60
movq %rax, -0x248(%rbp)
jmp 0x86a7
movq -0x248(%rbp), %rax
movq %rax, -0x180(%rbp)
cmpq $0x0, -0x180(%rbp)
jne 0x86d2
movb $0x0, -0x1(%rbp)
movl $0x1, -0xa0(%rbp)
jmp 0x8a59
movq -0x10(%rbp), %rdi
callq 0x8cc0
movq %rax, -0x250(%rbp)
jmp 0x86e4
movq -0x250(%rbp), %rsi
leaq -0x1e8(%rbp), %rdi
callq 0x8b60
jmp 0x86f9
movq -0x180(%rbp), %rsi
movq -0x178(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq -0x1e8(%rbp), %rdi
callq 0x80a0
movb %al, -0x251(%rbp)
jmp 0x871f
movb -0x251(%rbp), %al
testb $0x1, %al
jne 0x8761
jmp 0x872b
movb $0x0, -0x1(%rbp)
movl $0x1, -0xa0(%rbp)
jmp 0x8a1a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x8c20
jmp 0x8a67
movq $0x0, -0x1f0(%rbp)
movq -0x1f0(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jae 0x8994
movq -0x1f0(%rbp), %rax
movq -0x110(%rbp,%rax,8), %rax
movq %rax, -0x1f8(%rbp)
movq -0x1f8(%rbp), %rax
movsbl (%rax), %eax
addl $-0x23, %eax
movl %eax, %ecx
movq %rcx, -0x260(%rbp)
subl $0x52, %eax
ja 0x8949
movq -0x260(%rbp), %rax
leaq 0x44bbd(%rip), %rcx     # 0x4d380
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x1f8(%rbp), %rdi
incq %rdi
movq %rdi, -0x268(%rbp)
callq 0x6220
movq -0x268(%rbp), %rsi
movq %rax, %rdx
leaq -0x1e8(%rbp), %rdi
callq 0x9040
jmp 0x87fa
jmp 0x897b
movq -0x1f8(%rbp), %rdi
incq %rdi
movq %rdi, -0x270(%rbp)
callq 0x6220
movq -0x270(%rbp), %rsi
movq %rax, %rdx
leaq -0x1e8(%rbp), %rdi
callq 0x90b0
jmp 0x882d
jmp 0x897b
movq -0x1f8(%rbp), %rdi
incq %rdi
movq %rdi, -0x278(%rbp)
callq 0x6220
movq -0x278(%rbp), %rsi
movq %rax, %rdx
leaq -0x1e8(%rbp), %rdi
callq 0x90e0
jmp 0x8860
jmp 0x897b
movq -0x1f8(%rbp), %rdi
incq %rdi
movq %rdi, -0x280(%rbp)
callq 0x6220
movq -0x280(%rbp), %rsi
movq %rax, %rdx
leaq -0x1e8(%rbp), %rdi
callq 0x9150
jmp 0x8893
jmp 0x897b
leaq -0x1e8(%rbp), %rdi
callq 0x91d0
jmp 0x88a6
jmp 0x897b
leaq -0x1e8(%rbp), %rdi
callq 0x9240
jmp 0x88b9
jmp 0x897b
leaq -0x1e8(%rbp), %rdi
callq 0x91d0
jmp 0x88cc
jmp 0x897b
leaq -0x1e8(%rbp), %rdi
callq 0x92b0
jmp 0x88df
jmp 0x897b
leaq -0x1e8(%rbp), %rdi
callq 0x9320
jmp 0x88f2
jmp 0x897b
leaq -0x1e8(%rbp), %rdi
callq 0x9390
jmp 0x8905
jmp 0x897b
leaq -0x1e8(%rbp), %rdi
callq 0x9410
jmp 0x8915
jmp 0x897b
leaq -0x1e8(%rbp), %rdi
callq 0x94d0
jmp 0x8925
jmp 0x897b
leaq -0x1e8(%rbp), %rdi
callq 0x9560
jmp 0x8935
jmp 0x897b
leaq -0x1e8(%rbp), %rdi
callq 0x9650
jmp 0x8945
jmp 0x897b
jmp 0x897b
movq 0x5a680(%rip), %rax     # 0x62fd0
movq (%rax), %rdi
movq -0x1f8(%rbp), %rdx
leaq 0x44bbd(%rip), %rsi     # 0x4d51e
movb $0x0, %al
callq 0x6630
movb $0x0, -0x1(%rbp)
movl $0x1, -0xa0(%rbp)
jmp 0x8a1a
jmp 0x897d
movq -0x1f0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1f0(%rbp)
jmp 0x876c
movq -0x170(%rbp), %rax
cmpq -0x148(%rbp), %rax
jbe 0x89bf
movq -0x10(%rbp), %rdi
movq -0x140(%rbp), %rsi
movq -0x168(%rbp), %rdx
callq 0x8c80
jmp 0x89bd
jmp 0x89bf
movq -0x10(%rbp), %rax
movq %rax, -0x290(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x8ba0
movq %rax, -0x288(%rbp)
jmp 0x89df
leaq -0x1e8(%rbp), %rdi
callq 0x8be0
movq %rax, -0x298(%rbp)
jmp 0x89f4
movq -0x298(%rbp), %rdx
movq -0x288(%rbp), %rsi
movq -0x290(%rbp), %rdi
callq 0x8c80
jmp 0x8a10
movl $0x0, -0xa0(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x8c20
movl -0xa0(%rbp), %eax
testl %eax, %eax
jne 0x8a59
jmp 0x8a32
jmp 0x8a34
movq -0x170(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x170(%rbp)
jmp 0x8670
movb $0x1, -0x1(%rbp)
movl $0x1, -0xa0(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x96a0
jmp 0x8a75
leaq -0x138(%rbp), %rdi
callq 0x96a0
jmp 0x8a8f
leaq -0x90(%rbp), %rdi
callq 0x8c20
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
leaq -0x90(%rbp), %rdi
callq 0x8c20
movq -0x98(%rbp), %rdi
callq 0x67e0
nopw (%rax,%rax)