// File: /workspace/github_c_makefile_star2_O2/cktan[P]tomlc99/toml.c
// Function: norm_basic_str
static char *norm_basic_str(const char *src, int srclen, int multiline,
                            char *errbuf, int errbufsz) {
  char *dst = 0; /* will write to dst[] and return it */
  int max = 0;   /* max size of dst[] */
  int off = 0;   /* cur offset in dst[] */
  const char *sp = src;
  const char *sq = src + srclen;
  int ch;

  /* scan forward on src */
  for (;;) {
    if (off >= max - 10) { /* have some slack for misc stuff */
      int newmax = max + 50;
      char *x = expand(dst, max, newmax);
      if (!x) {
        xfree(dst);
        snprintf(errbuf, errbufsz, "out of memory");
        return 0;
      }
      dst = x;
      max = newmax;
    }

    /* finished? */
    if (sp >= sq)
      break;

    ch = *sp++;
    if (ch != '\\') {
      /* these chars must be escaped: U+0000 to U+0008, U+000A to U+001F, U+007F
       */
      if ((0 <= ch && ch <= 0x08) || (0x0a <= ch && ch <= 0x1f) ||
          (ch == 0x7f)) {
        if (!(multiline && (ch == '\r' || ch == '\n'))) {
          xfree(dst);
          snprintf(errbuf, errbufsz, "invalid char U+%04x", ch);
          return 0;
        }
      }

      // a plain copy suffice
      dst[off++] = ch;
      continue;
    }

    /* ch was backslash. we expect the escape char. */
    if (sp >= sq) {
      snprintf(errbuf, errbufsz, "last backslash is invalid");
      xfree(dst);
      return 0;
    }

    /* for multi-line, we want to kill line-ending-backslash ... */
    if (multiline) {

      // if there is only whitespace after the backslash ...
      if (sp[strspn(sp, " \t\r")] == '\n') {
        /* skip all the following whitespaces */
        sp += strspn(sp, " \t\r\n");
        continue;
      }
    }

    /* get the escaped char */
    ch = *sp++;
    switch (ch) {
    case 'u':
    case 'U': {
      int64_t ucs = 0;
      int nhex = (ch == 'u' ? 4 : 8);
      for (int i = 0; i < nhex; i++) {
        if (sp >= sq) {
          snprintf(errbuf, errbufsz, "\\%c expects %d hex chars", ch, nhex);
          xfree(dst);
          return 0;
        }
        ch = *sp++;
        int v = ('0' <= ch && ch <= '9')
                    ? ch - '0'
                    : (('A' <= ch && ch <= 'F') ? ch - 'A' + 10 : -1);
        if (-1 == v) {
          snprintf(errbuf, errbufsz, "invalid hex chars for \\u or \\U");
          xfree(dst);
          return 0;
        }
        ucs = ucs * 16 + v;
      }
      int n = toml_ucs_to_utf8(ucs, &dst[off]);
      if (-1 == n) {
        snprintf(errbuf, errbufsz, "illegal ucs code in \\u or \\U");
        xfree(dst);
        return 0;
      }
      off += n;
    }
      continue;

    case 'b':
      ch = '\b';
      break;
    case 't':
      ch = '\t';
      break;
    case 'n':
      ch = '\n';
      break;
    case 'f':
      ch = '\f';
      break;
    case 'r':
      ch = '\r';
      break;
    case '"':
      ch = '"';
      break;
    case '\\':
      ch = '\\';
      break;
    default:
      snprintf(errbuf, errbufsz, "illegal escape char \\%c", ch);
      xfree(dst);
      return 0;
    }

    dst[off++] = ch;
  }

  // Cap with NUL and return it.
  dst[off++] = 0;
  return dst;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, 0x4(%rsp)
movq %rcx, 0x8(%rsp)
movl %edx, 0x14(%rsp)
movq %rdi, %rbp
movslq %esi, %rax
addq %rdi, %rax
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %r9d, %r9d
movq %rbp, 0x20(%rsp)
movq %r9, %r15
leal -0xa(%r14), %eax
cmpl %eax, %r13d
jge 0x3b62
movq %r15, %r9
jmp 0x3b83
leal 0x32(%r14), %r12d
movq %r15, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x2fae
movq %rax, %r9
movl %r12d, %r14d
testq %rax, %rax
je 0x3d82
movq %r13, %rbx
movslq %r13d, %r13
cmpq 0x18(%rsp), %rbp
jae 0x3d51
leaq 0x1(%rbp), %r12
movzbl (%rbp), %r15d
cmpl $0x5c, %r15d
jne 0x3beb
cmpq 0x18(%rsp), %r12
jae 0x3d58
cmpl $0x0, 0x14(%rsp)
je 0x3c31
movq %r9, %r15
movq %r12, %rdi
leaq 0x19e8(%rip), %rsi      # 0x55aa
callq 0x1120
cmpb $0xa, (%r12,%rax)
jne 0x3c38
movq %r12, %rdi
leaq 0x1ace(%rip), %rsi      # 0x56a6
callq 0x1120
movq %rax, %rbp
addq %r12, %rbp
movq %rbx, %r13
jmp 0x3b54
cmpb $0x9, %r15b
jb 0x3c05
leal -0xa(%r15), %eax
cmpb $0x16, %al
setae %al
cmpb $0x7f, %r15b
setne %cl
testb %al, %cl
jne 0x3c20
cmpl $0x0, 0x14(%rsp)
je 0x3de5
cmpl $0xa, %r15d
je 0x3c20
cmpl $0xd, %r15d
jne 0x3de5
incl %ebx
movb %r15b, (%r9,%r13)
movq %rbx, %r13
movq %r12, %rbp
jmp 0x3b4c
movq 0x20(%rsp), %r10
jmp 0x3c3e
movq %rbp, %r10
movq %r15, %r9
addq $0x2, %r10
movb 0x1(%rbp), %al
movsbl %al, %ecx
movzbl %cl, %esi
leal -0x6e(%rsi), %edx
cmpl $0x7, %edx
jbe 0x3c86
cmpl $0x22, %esi
je 0x3d0e
cmpl $0x66, %esi
movq %r10, %rbp
je 0x3d19
cmpl $0x5c, %esi
je 0x3d15
movb $0x8, %dl
cmpl $0x62, %esi
je 0x3d1b
cmpl $0x55, %esi
je 0x3c9d
jmp 0x3e0f
leaq 0x14c3(%rip), %rsi      # 0x5150
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
movq %r10, %rbp
jmpq *%rdx
movb $0xa, %dl
jmp 0x3d1b
xorl %edx, %edx
cmpb $0x75, %al
setne %dl
leaq 0x4(,%rdx,4), %r8
leaq 0x4(,%rdx,4), %r15
addq %rbp, %r15
xorl %eax, %eax
xorl %edi, %edi
cmpl %eax, %r8d
je 0x3d29
leaq (%rax,%rbp), %rdx
cmpq 0x18(%rsp), %rdx
jae 0x3dc9
movsbl (%rdx), %ecx
leal -0x30(%rcx), %edx
cmpb $0x9, %dl
ja 0x3cdf
leal -0x30(%rcx), %edx
jmp 0x3cee
leal -0x41(%rcx), %edx
cmpb $0x5, %dl
ja 0x3dbc
leal -0x37(%rcx), %edx
cmpl $-0x1, %edx
je 0x3dbc
shlq $0x4, %rdi
movslq %edx, %rdx
addq %rdx, %rdi
incq %rax
jmp 0x3cbb
movb $0x9, %dl
jmp 0x3d1b
movb $0xd, %dl
jmp 0x3d1b
movl %eax, %edx
movq %r10, %rbp
jmp 0x3d1b
movl %eax, %edx
jmp 0x3d1b
movb $0xc, %dl
incl %ebx
movb %dl, (%r9,%r13)
movq %rbx, %r13
jmp 0x3b4c
movq %r9, %r12
addq %r9, %r13
movq %r13, %rsi
callq 0x1c63
cmpl $-0x1, %eax
je 0x3e2e
movq %rbx, %r13
addl %eax, %r13d
movq %r15, %rbp
movq %r12, %r9
jmp 0x3b4c
movb $0x0, (%r9,%r13)
jmp 0x3daa
movl 0x4(%rsp), %esi
leaq 0x1929(%rip), %rdx      # 0x568c
movq 0x8(%rsp), %rdi
xorl %eax, %eax
movq %r9, %rbx
callq 0x10e0
testq %rbx, %rbx
je 0x3da7
movq %rbx, %rdi
callq *0x4398(%rip)           # 0x8118
jmp 0x3da7
testq %r15, %r15
je 0x3d90
movq %r15, %rdi
callq *0x4388(%rip)           # 0x8118
movl 0x4(%rsp), %esi
leaq 0x142a(%rip), %rdx      # 0x51c5
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0x10e0
xorl %r9d, %r9d
movq %r9, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rsp), %esi
leaq 0x18fd(%rip), %rdx      # 0x56c4
jmp 0x3d63
movl 0x4(%rsp), %esi
leaq 0x18d7(%rip), %rdx      # 0x56ab
movq 0x8(%rsp), %rdi
xorl %eax, %eax
movq %r9, %rbx
callq 0x10e0
jmp 0x3d72
testq %r9, %r9
je 0x3df3
movq %r9, %rdi
callq *0x4325(%rip)           # 0x8118
movl 0x4(%rsp), %esi
leaq 0x187a(%rip), %rdx      # 0x5678
movq 0x8(%rsp), %rdi
movl %r15d, %ecx
xorl %eax, %eax
callq 0x10e0
jmp 0x3da7
movl 0x4(%rsp), %esi
leaq 0x18e6(%rip), %rdx      # 0x5700
movq 0x8(%rsp), %rdi
xorl %eax, %eax
movq %r9, %rbx
callq 0x10e0
jmp 0x3d72
movl 0x4(%rsp), %esi
leaq 0x18aa(%rip), %rdx      # 0x56e3
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0x10e0
testq %r12, %r12
je 0x3da7
movq %r12, %rdi
jmp 0x3d7a