// File: /workspace/github_c_makefile_star2_O1/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 0x4(%rsp), %r8
leaq 0xc(%rsp), %r9
movl %ebp, %edx
callq 0x44fb
leaq 0x8da2(%rip), %rbx      # 0xd338
movq (%rbx), %rdi
movslq %eax, %r13
movq %r13, %rsi
callq 0x4e1b
movq %rax, %r15
testl %r15d, %r15d
je 0x46a9
movl %r12d, 0x8(%rsp)
movq (%rbx), %rdi
movq %r13, %rsi
callq 0x4d95
movq %rax, %r12
testq %rax, %rax
jne 0x463c
xorl %r12d, %r12d
movl $0x0, %eax
cmpl $0xff, %r15d
ja 0x4638
leaq 0x8ca8(%rip), %rcx      # 0xd288
cmpl $0x0, 0x24(%rcx)
je 0x4638
movzbl %r15b, %eax
leaq 0x955f(%rip), %rcx      # 0xdb50
movq (%rcx,%rax,8), %rdi
testq %rdi, %rdi
je 0x4618
movl $0x32, %eax
cmpw $0x0, 0x24(%rdi)
je 0x461f
cmpl $0x2, %eax
jb 0x4624
decl %eax
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
jne 0x45ff
jmp 0x4624
xorl %r12d, %r12d
xorl %eax, %eax
jmp 0x4638
callq 0x46be
movq (%rbx), %rdi
movq %r13, %rsi
callq 0x4d95
movq %rax, %r12
testq %rax, %rax
setne %al
testb %al, %al
je 0x46a9
movb %r15b, 0x28(%r12)
movw $0x0, 0x24(%r12)
movb $0x0, 0x27(%r12)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq $0x0, 0x10(%r12)
movl 0xc(%rsp), %eax
movzbl %al, %ebx
movb %bl, 0x29(%r12)
movl %ebp, 0x20(%r12)
leaq 0x30(%r12), %rdi
movq %r14, %rsi
callq 0x2090
movl 0x8(%rsp), %eax
movl %eax, 0x1c(%r12)
leaq (%r12,%rbx), %rdi
addq $0x30, %rdi
movslq 0x4(%rsp), %rdx
leaq 0x10(%rsp), %rsi
callq 0x2280
movb 0x4(%rsp), %al
movb %al, 0x26(%r12)
jmp 0x46ac
xorl %r12d, %r12d
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq