// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/cram/cram_io.c
// Function: expand_cache_path
static int expand_cache_path(char *path, char *dir, const char *fn) {
    char *cp, *start = path;
    size_t len;
    size_t sz = PATH_MAX;

    while ((cp = strchr(dir, '%'))) {
        if (cp-dir >= sz) return -1;
        strncpy(path, dir, cp-dir);
        path += cp-dir;
        sz -= cp-dir;

        if (*++cp == 's') {
            len = strlen(fn);
            if (len >= sz) return -1;
            strcpy(path, fn);
            path += len;
            sz -= len;
            fn += len;
            cp++;
        } else if (*cp >= '0' && *cp <= '9') {
            char *endp;
            long l;

            l = strtol(cp, &endp, 10);
            l = MIN(l, strlen(fn));
            if (*endp == 's') {
                if (l >= sz) return -1;
                strncpy(path, fn, l);
                path += l;
                fn += l;
                sz -= l;
                *path = 0;
                cp = endp+1;
            } else {
                if (sz < 3) return -1;
                *path++ = '%';
                *path++ = *cp++;
            }
        } else {
            if (sz < 3) return -1;
            *path++ = '%';
            *path++ = *cp++;
        }
        dir = cp;
    }

    len = strlen(dir);
    if (len >= sz) return -1;
    strcpy(path, dir);
    path += len;
    sz -= len;

    len = strlen(fn) + ((*fn && path > start && path[-1] != '/') ? 1 : 0);
    if (len >= sz) return -1;
    if (*fn && path > start && path[-1] != '/')
        *path++ = '/';
    strcpy(path, fn);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, (%rsp)
movq %rsi, %r14
movq %rdi, %r15
movl $0x1000, %ebx           # imm = 0x1000
pushq $0x25
popq %r12
movq %rdi, 0x10(%rsp)
movq %r14, %rdi
movl %r12d, %esi
callq 0x4190
testq %rax, %rax
je 0x46815
movq %rax, %rbp
movq %rax, %r13
subq %r14, %r13
subq %r13, %rbx
jbe 0x46873
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x4250
addq %r13, %r15
movb 0x1(%rbp), %al
cmpb $0x73, %al
je 0x46770
addb $-0x30, %al
cmpb $0x9, %al
jbe 0x467a9
cmpq $0x3, %rbx
jb 0x46873
movb $0x25, (%r15)
movq %rbp, %r14
addq $0x2, %r14
movb 0x1(%rbp), %al
movb %al, 0x1(%r15)
addq $0x2, %r15
jmp 0x4670a
movq (%rsp), %r13
movq %r13, %rdi
callq 0x41d0
subq %rax, %rbx
jbe 0x46873
movq %rax, %r14
movq %r15, %rdi
movq %r13, %rsi
callq 0x4460
addq %r14, %r15
addq %r14, %r13
movq %r13, (%rsp)
addq $0x2, %rbp
movq %rbp, %r14
jmp 0x4670a
leaq 0x1(%rbp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x46d0
movq %rax, %r13
movq (%rsp), %rdi
callq 0x41d0
movq %rax, %r14
cmpq %rax, %r13
cmovbq %r13, %r14
movq 0x8(%rsp), %rax
cmpb $0x73, (%rax)
jne 0x4674e
subq %r14, %rbx
jbe 0x46873
movq %r15, %rdi
movq (%rsp), %r13
movq %r13, %rsi
movq %r14, %rdx
callq 0x4250
movb $0x0, (%r15,%r14)
addq %r14, %r15
addq %r14, %r13
movq %r13, (%rsp)
movq 0x8(%rsp), %r14
incq %r14
jmp 0x4670a
movq %r14, %rdi
callq 0x41d0
subq %rax, %rbx
jbe 0x46873
movq %rax, %r12
movq %r15, %rdi
movq %r14, %rsi
callq 0x4460
addq %r15, %r12
movq (%rsp), %r14
movq %r14, %rdi
callq 0x41d0
cmpb $0x0, (%r14)
je 0x46885
cmpq 0x10(%rsp), %r12
jbe 0x46885
movb -0x1(%r12), %cl
xorl %edx, %edx
cmpb $0x2f, %cl
setne %dl
addq %rdx, %rax
cmpq %rbx, %rax
pushq $-0x1
popq %rax
jae 0x46876
cmpb $0x2f, %cl
je 0x4688d
movb $0x2f, (%r12)
incq %r12
jmp 0x4688d
pushq $-0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq %rbx, %rax
pushq $-0x1
popq %rax
jae 0x46876
movq %r12, %rdi
movq %r14, %rsi
callq 0x4460
xorl %eax, %eax
jmp 0x46876