// File: /workspace/github_cpp_makefile_star2_O2/baziotis[P]minijava-cpp/str_intern.cpp
// Function: str_intern_len(char const*, unsigned long)
const char *str_intern_len(const char *str, size_t len) {
    assert(str);
    size_t i, h;
    const char *end;
    InternedStr *p;

    h = 0;
    i = len;
    // Should point one-past last char after the loop.
    end = str;
    for (; i; --i) {
        h = (h<<1) + scatter[*(uint8_t*)end++];
    }
    constexpr size_t buckets_len = 1024;
    h &= buckets_len - 1;
    for (p = buckets[h]; p; p = p->link) {
        if (p->len == len && !memcmp(str, p->str, len)) {
            return p->str;
        }
    }
    // Pointer to the next free byte the current block.
    static char *next;
    // Pointer to one past the last free byte in the current chunk.
    static char *strlimit;
    // The `=` is because `strlimit` is _one-past_.
    if (len + 1 >= strlimit - next) {
        size_t n = len + 4 * 1024;
        next = (char*)allocate(n, MEM::STR_INTERN);
        strlimit = next + n;
    }
    // We use the current block (the one guarded by `next` and `strlimit`)
    // only to the string data per se. So, for the structs, we have to allocate
    // new memory
    p = (InternedStr*) allocate(sizeof(*p), MEM::STR_INTERN);
    // Point to allocated memory.
    p->str = next;
    // Copy
    memcpy(p->str, str, len);
    // Set `len`
    p->len = len;
    // Set NUL terminator.
    *(p->str + len) = 0;
    // Increment next;
    next += len+1;
    // Put in the start of the chain.
    p->link = buckets[h];
    buckets[h] = p;
    return p->str;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x731f
movq %rsi, %rbx
movq %rdi, %r14
xorl %ecx, %ecx
leaq 0x10f15(%rip), %rdx     # 0x18140
xorl %eax, %eax
cmpq %rcx, %rbx
je 0x7243
movzbl (%r14,%rcx), %esi
movl (%rdx,%rsi,4), %esi
leaq (%rsi,%rax,2), %rax
incq %rcx
jmp 0x722d
andl $0x3ff, %eax            # imm = 0x3FF
leaq 0x11811(%rip), %rcx     # 0x18a60
leaq (%rcx,%rax,8), %r13
movq %r13, %r12
movq (%r12), %r12
testq %r12, %r12
je 0x7286
cmpq %rbx, (%r12)
jne 0x7280
movq 0x8(%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2120
testl %eax, %eax
je 0x730d
addq $0x10, %r12
jmp 0x7256
leaq 0x1(%rbx), %rbp
movq 0x137d7(%rip), %rax     # 0x1aa68
subq 0x137c8(%rip), %rax     # 0x1aa60
cmpq %rax, %rbp
jb 0x72c2
leaq 0x1000(%rbx), %rdi
xorl %esi, %esi
callq 0x22dc
movq %rax, 0x137ae(%rip)     # 0x1aa60
addq %rbx, %rax
addq $0x1000, %rax           # imm = 0x1000
movq %rax, 0x137a6(%rip)     # 0x1aa68
pushq $0x18
popq %rdi
xorl %esi, %esi
callq 0x22dc
movq %rax, %r15
movq 0x1378a(%rip), %r12     # 0x1aa60
movq %r12, 0x8(%rax)
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x20e0
movq %rbx, (%r15)
movq 0x8(%r15), %rax
movb $0x0, (%rax,%rbx)
addq %r12, %rbp
movq %rbp, 0x13763(%rip)     # 0x1aa60
movq (%r13), %rax
movq %rax, 0x10(%r15)
movq %r15, (%r13)
movq 0x8(%r15), %r15
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8e68(%rip), %rdi      # 0x1018e
leaq 0x8e65(%rip), %rsi      # 0x10192
leaq 0x8e6d(%rip), %rcx      # 0x101a1
pushq $0x5d
popq %rdx
callq 0x20b0