// File: /workspace/github_c_makefile_star2_O3/zuocheng-liu[P]ae-memcached/src/items.c
// Function: item_alloc
item *item_alloc(char *key, int flags, rel_time_t exptime, int nbytes) {
    int nsuffix, ntotal, len;
    item *it;
    unsigned int id;
    char suffix[40];

    ntotal = item_make_header(key, flags, nbytes, suffix, &nsuffix, &len);

    id = mem_cache_clsid(mem_cache, ntotal);
    if (id == 0)
        return 0;

    it = mem_cache_alloc(mem_cache, ntotal);
    if (it == 0) {
        int tries = 50;
        item *search;

        /* If requested to not push old items out of cache when memory runs out,
         * we're out of luck at this point...
         */

        if (!settings.evict_to_free) return 0;

        /*
         * try to get one off the right LRU
         * don't necessariuly unlink the tail because it may be locked: refcount>0
         * search up from tail an item with refcount==0 and unlink it; give up after 50
         * tries
         */

        if (id > LARGEST_ID) return 0;
        if (tails[id]==0) return 0;

        for (search = tails[id]; tries>0 && search; tries--, search=search->prev) {
            if (search->refcount==0) {
                item_unlink(search);
                break;
            }
        }
        it = mem_cache_alloc(mem_cache, ntotal);
        if (it==0) return 0;
    }

    assert(it->slabs_clsid == 0);

    it->slabs_clsid = id;

    assert(it != heads[it->slabs_clsid]);

    it->next = 0;
    it->prev = 0;
    it->h_next = 0;
    it->refcount = 0;
    it->it_flags = 0;
    it->nkey = len;
    it->nbytes = nbytes;
    strcpy(ITEM_key(it), key);
    it->exptime = exptime;
    memcpy(ITEM_suffix(it), suffix, nsuffix);
    it->nsuffix = nsuffix;
    return it;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %ecx, %ebp
movl %edx, %r12d
movq %rdi, %r14
leaq 0x10(%rsp), %rcx
leaq 0xc(%rsp), %r8
leaq 0x8(%rsp), %r9
movl %ebp, %edx
callq 0x4513
leaq 0x8d8a(%rip), %rbx      # 0xd338
movq (%rbx), %rdi
movslq %eax, %r13
movq %r13, %rsi
callq 0x4df4
movq %rax, %r15
testl %r15d, %r15d
je 0x46a8
movl %r12d, 0x4(%rsp)
movq (%rbx), %rdi
movq %r13, %rsi
callq 0x4d76
testq %rax, %rax
je 0x4644
movq %rax, %r12
movb %r15b, 0x28(%rax)
movw $0x0, 0x24(%rax)
movb $0x0, 0x27(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movl 0x8(%rsp), %eax
movzbl %al, %ebx
movb %bl, 0x29(%r12)
movl %ebp, 0x20(%r12)
leaq 0x30(%r12), %rdi
movq %r14, %rsi
callq 0x2090
movl 0x4(%rsp), %eax
movl %eax, 0x1c(%r12)
leaq (%r12,%rbx), %rdi
addq $0x30, %rdi
movslq 0xc(%rsp), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x2280
movb %bl, 0x26(%r12)
jmp 0x46ab
xorl %r12d, %r12d
cmpl $0xff, %r15d
ja 0x46ab
leaq 0x8c31(%rip), %rax      # 0xd288
cmpl $0x0, 0x24(%rax)
je 0x46ab
movzbl %r15b, %eax
leaq 0x94e8(%rip), %rcx      # 0xdb50
movq (%rcx,%rax,8), %rdi
testq %rdi, %rdi
je 0x46a8
movl $0x32, %eax
cmpw $0x0, 0x24(%rdi)
je 0x468f
cmpl $0x2, %eax
jb 0x4694
decl %eax
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
jne 0x4676
jmp 0x4694
callq 0x46bd
movq (%rbx), %rdi
movq %r13, %rsi
callq 0x4d76
testq %rax, %rax
jne 0x45dd
xorl %r12d, %r12d
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq