// File: /workspace/github_cpp_makefile_star2_O3/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 0x8111
movq %rsi, %rbx
movq %rdi, %r14
testq %rsi, %rsi
je 0x8034
xorl %eax, %eax
leaq 0x1212c(%rip), %rcx     # 0x1a140
xorl %r13d, %r13d
movzbl (%r14,%rax), %edx
movl (%rcx,%rdx,4), %edx
leaq (%rdx,%r13,2), %r13
incq %rax
cmpq %rax, %rbx
jne 0x8017
andl $0x3ff, %r13d           # imm = 0x3FF
jmp 0x8037
xorl %r13d, %r13d
leaq 0x12a22(%rip), %rax     # 0x1aa60
movq (%rax,%r13,8), %r12
testq %r12, %r12
je 0x806f
cmpq %rbx, (%r12)
jne 0x8068
movq 0x8(%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2120
testl %eax, %eax
je 0x80ff
movq 0x10(%r12), %r12
jmp 0x8042
leaq 0x1(%rbx), %rbp
movq 0x149ee(%rip), %rax     # 0x1ca68
subq 0x149df(%rip), %rax     # 0x1ca60
cmpq %rax, %rbp
jb 0x80ab
leaq 0x1000(%rbx), %rdi
xorl %esi, %esi
callq 0x22dc
movq %rax, 0x149c5(%rip)     # 0x1ca60
addq %rbx, %rax
addq $0x1000, %rax           # imm = 0x1000
movq %rax, 0x149bd(%rip)     # 0x1ca68
movl $0x18, %edi
xorl %esi, %esi
callq 0x22dc
movq %rax, %r15
movq 0x1499f(%rip), %r12     # 0x1ca60
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, 0x14978(%rip)     # 0x1ca60
leaq 0x12971(%rip), %rcx     # 0x1aa60
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 0xafe6(%rip), %rdi      # 0x130fe
leaq 0xafe3(%rip), %rsi      # 0x13102
leaq 0xafeb(%rip), %rcx      # 0x13111
movl $0x5d, %edx
callq 0x20b0