// File: /workspace/github_cpp_makefile_star2_O1/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 0x8219
movq %rsi, %rbx
movq %rdi, %r14
testq %rsi, %rsi
je 0x813c
xorl %eax, %eax
leaq 0x11024(%rip), %rcx     # 0x19140
xorl %r13d, %r13d
movzbl (%r14,%rax), %edx
movl (%rcx,%rdx,4), %edx
leaq (%rdx,%r13,2), %r13
incq %rax
cmpq %rax, %rbx
jne 0x811f
andl $0x3ff, %r13d           # imm = 0x3FF
jmp 0x813f
xorl %r13d, %r13d
leaq 0x1191a(%rip), %rax     # 0x19a60
movq (%rax,%r13,8), %r12
testq %r12, %r12
je 0x8177
cmpq %rbx, (%r12)
jne 0x8170
movq 0x8(%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2120
testl %eax, %eax
je 0x8207
movq 0x10(%r12), %r12
jmp 0x814a
leaq 0x1(%rbx), %rbp
movq 0x138e6(%rip), %rax     # 0x1ba68
subq 0x138d7(%rip), %rax     # 0x1ba60
cmpq %rax, %rbp
jb 0x81b3
leaq 0x1000(%rbx), %rdi
xorl %esi, %esi
callq 0x22dc
movq %rax, 0x138bd(%rip)     # 0x1ba60
addq %rbx, %rax
addq $0x1000, %rax           # imm = 0x1000
movq %rax, 0x138b5(%rip)     # 0x1ba68
movl $0x18, %edi
xorl %esi, %esi
callq 0x22dc
movq %rax, %r15
movq 0x13897(%rip), %r12     # 0x1ba60
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, 0x13870(%rip)     # 0x1ba60
leaq 0x11869(%rip), %rcx     # 0x19a60
movq (%rcx,%r13,8), %rax
movq %rax, 0x10(%r15)
movq %r15, (%rcx,%r13,8)
movq 0x8(%r15), %r15
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa016(%rip), %rdi      # 0x12236
leaq 0xa013(%rip), %rsi      # 0x1223a
leaq 0xa01b(%rip), %rcx      # 0x12249
movl $0x5d, %edx
callq 0x20b0