// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0xb8f9
jmp 0xb918
leaq 0xfc05(%rip), %rdi      # 0x1b505
leaq 0xfc02(%rip), %rsi      # 0x1b509
movl $0x5d, %edx
leaq 0xfc05(%rip), %rcx      # 0x1b518
callq 0x20d0
movq $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xb972
movq -0x28(%rbp), %rax
shlq %rax
movq -0x30(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movzbl (%rcx), %ecx
movl %ecx, %edx
leaq 0x187f7(%rip), %rcx     # 0x24150
movslq (%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0xb930
movq $0x400, -0x40(%rbp)     # imm = 0x400
movq -0x28(%rbp), %rax
andq $0x3ff, %rax            # imm = 0x3FF
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rcx
leaq 0x190dd(%rip), %rax     # 0x24a70
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0xb9ea
movq -0x38(%rbp), %rax
movq (%rax), %rax
cmpq -0x18(%rbp), %rax
jne 0xb9da
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rdx
callq 0x20a0
cmpl $0x0, %eax
jne 0xb9da
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0xbad5
jmp 0xb9dc
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0xb99b
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq 0x1b07f(%rip), %rcx     # 0x26a78
movq 0x1b070(%rip), %rdx     # 0x26a70
subq %rdx, %rcx
cmpq %rcx, %rax
jb 0xba3a
movq -0x18(%rbp), %rax
addq $0x1000, %rax           # imm = 0x1000
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x2340
movq %rax, 0x1b048(%rip)     # 0x26a70
movq 0x1b041(%rip), %rax     # 0x26a70
addq -0x48(%rbp), %rax
movq %rax, 0x1b03e(%rip)     # 0x26a78
movl $0x18, %edi
xorl %esi, %esi
callq 0x2340
movq %rax, -0x38(%rbp)
movq 0x1b01f(%rip), %rcx     # 0x26a70
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x2100
movq -0x18(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x18(%rbp), %rax
addq $0x1, %rax
addq 0x1afd8(%rip), %rax     # 0x26a70
movq %rax, 0x1afd1(%rip)     # 0x26a70
movq -0x28(%rbp), %rcx
leaq 0x18fc6(%rip), %rax     # 0x24a70
movq (%rax,%rcx,8), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq 0x18fab(%rip), %rax     # 0x24a70
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nop