// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/htscodecs/htscodecs/utils.c
// Function: htscodecs_tls_alloc
void *htscodecs_tls_alloc(size_t size) {
    int i;

    int err = pthread_once(&rans_once, htscodecs_tls_init);
    if (err != 0) {
        fprintf(stderr, "Initialising TLS data failed: pthread_once: %s\n",
                strerror(err));
        return NULL;
    }

    // Initialise tls_pool on first usage
    tls_pool *tls = pthread_getspecific(rans_key);
    if (!tls) {
        if (!(tls = calloc(1, sizeof(*tls))))
            return NULL;
        pthread_setspecific(rans_key, tls);
    }

    // Query pool for size
    int avail = -1;
    for (i = 0; i < MAX_TLS_BUFS; i++) {
        if (!tls->used[i]) {
            if (size <= tls->sizes[i]) {
                tls->used[i] = 1;
#ifdef TLS_DEBUG
                fprintf(stderr, "Reuse %d: %ld/%ld = %p\n",
                        i, size, tls->sizes[i], tls->bufs[i]);
#endif
                return tls->bufs[i];
            } else if (avail == -1) {
                avail = i;
            }
        }
    }

    if (i == MAX_TLS_BUFS && avail == -1) {
        // Shouldn't happen given our very limited use of this function
        fprintf(stderr, "Error: out of rans_tls_alloc slots\n");
        return NULL;
    }

    if (tls->bufs[avail])
        free(tls->bufs[avail]);
    if (!(tls->bufs[avail] = calloc(1, size)))
        return NULL;
#ifdef TLS_DEBUG
    fprintf(stderr, "Alloc %d: %ld = %p\n", avail, size, tls->bufs[avail]);
#endif
    tls->sizes[avail] = size;
    tls->used[avail] = 1;

    return tls->bufs[avail];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
leaq 0x441f5(%rip), %rdi     # 0x1128a8
leaq 0x1c6(%rip), %rsi       # 0xce880
callq 0x4460
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0xce700
movq 0x3e911(%rip), %rax     # 0x10cfe0
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movl -0x18(%rbp), %edi
callq 0x4130
movq -0x30(%rbp), %rdi
movq %rax, %rdx
leaq 0x27e2a(%rip), %rsi     # 0xf6516
movb $0x0, %al
callq 0x46d0
movq $0x0, -0x8(%rbp)
jmp 0xce875
movl 0x441a6(%rip), %edi     # 0x1128ac
callq 0x4790
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xce74b
movl $0x1, %edi
movl $0xc8, %esi
callq 0x42f0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0xce73c
movq $0x0, -0x8(%rbp)
jmp 0xce875
movl 0x4416a(%rip), %edi     # 0x1128ac
movq -0x20(%rbp), %rsi
callq 0x40b0
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x14(%rbp)
cmpl $0xa, -0x14(%rbp)
jge 0xce7c9
movq -0x20(%rbp), %rax
movslq -0x14(%rbp), %rcx
cmpl $0x0, 0xa0(%rax,%rcx,4)
jne 0xce7bc
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq -0x14(%rbp), %rdx
cmpq 0x50(%rcx,%rdx,8), %rax
ja 0xce7ac
movq -0x20(%rbp), %rax
movslq -0x14(%rbp), %rcx
movl $0x1, 0xa0(%rax,%rcx,4)
movq -0x20(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x8(%rbp)
jmp 0xce875
cmpl $-0x1, -0x24(%rbp)
jne 0xce7b8
movl -0x14(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0xce7ba
jmp 0xce7bc
jmp 0xce7be
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xce759
cmpl $0xa, -0x14(%rbp)
jne 0xce7f7
cmpl $-0x1, -0x24(%rbp)
jne 0xce7f7
movq 0x3e804(%rip), %rax     # 0x10cfe0
movq (%rax), %rdi
leaq 0x28b39(%rip), %rsi     # 0xf731f
movb $0x0, %al
callq 0x46d0
movq $0x0, -0x8(%rbp)
jmp 0xce875
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0xce817
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x45a0
movq -0x10(%rbp), %rsi
movl $0x1, %edi
callq 0x42f0
movq -0x20(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movq %rax, (%rcx,%rdx,8)
cmpq $0x0, %rax
jne 0xce841
movq $0x0, -0x8(%rbp)
jmp 0xce875
movq -0x10(%rbp), %rdx
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq %rdx, 0x50(%rax,%rcx,8)
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl $0x1, 0xa0(%rax,%rcx,4)
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nop