// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq %rdi, %r15
movq %rsi, %rdi
movl $0x25, %esi
callq 0x4190
testq %rax, %rax
movq %r15, 0x10(%rsp)
je 0x54ba5
movq %rax, %r14
movl $0x1000, %ebx           # imm = 0x1000
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %r14, %r13
subq %r12, %r13
subq %r13, %rbx
jbe 0x54c1c
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x4250
addq %r13, %r15
movb 0x1(%r14), %al
cmpb $0x73, %al
jne 0x54afc
movq (%rsp), %rdi
callq 0x41d0
subq %rax, %rbx
jbe 0x54c1c
movq %rax, %r12
movq %r15, %rdi
movq (%rsp), %r13
movq %r13, %rsi
callq 0x4460
addq %r12, %r15
addq %r12, %r13
movq %r13, (%rsp)
addq $0x2, %r14
jmp 0x54b87
addb $-0x30, %al
cmpb $0x9, %al
ja 0x54b69
leaq 0x1(%r14), %rdi
leaq 0x8(%rsp), %rsi
movl $0xa, %edx
callq 0x46d0
movq %rax, %r13
movq (%rsp), %rdi
callq 0x41d0
movq %rax, %r12
cmpq %rax, %r13
cmovbq %r13, %r12
movq 0x8(%rsp), %rax
cmpb $0x73, (%rax)
jne 0x54b69
subq %r12, %rbx
jbe 0x54c1c
movq %r15, %rdi
movq (%rsp), %r14
movq %r14, %rsi
movq %r12, %rdx
callq 0x4250
movb $0x0, (%r15,%r12)
addq %r12, %r15
addq %r12, %r14
movq %r14, (%rsp)
movq 0x8(%rsp), %r12
incq %r12
jmp 0x54b8a
cmpq $0x3, %rbx
jb 0x54c1c
movb $0x25, (%r15)
movb 0x1(%r14), %al
addq $0x2, %r14
movb %al, 0x1(%r15)
addq $0x2, %r15
movq %r14, %r12
movq %r12, %rdi
movl $0x25, %esi
callq 0x4190
movq %rax, %r14
testq %rax, %rax
jne 0x54a9d
jmp 0x54baa
movl $0x1000, %ebx           # imm = 0x1000
movq %r12, %rdi
callq 0x41d0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
subq %rax, %rbx
jbe 0x54c1c
movq %rax, %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x4460
addq %r15, %r13
movq (%rsp), %r14
movq %r14, %rdi
callq 0x41d0
cmpb $0x0, (%r14)
je 0x54c09
cmpq 0x10(%rsp), %r13
jbe 0x54c09
movb -0x1(%r13), %cl
xorl %edx, %edx
cmpb $0x2f, %cl
setne %dl
addq %rdx, %rax
cmpq %rbx, %rax
jae 0x54c1c
cmpb $0x2f, %cl
je 0x54c0e
movb $0x2f, (%r13)
incq %r13
jmp 0x54c0e
cmpq %rbx, %rax
jae 0x54c1c
movq %r13, %rdi
movq (%rsp), %rsi
callq 0x4460
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq