// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r15
movq %rsi, %rdi
movl $0x25, %esi
callq 0x4190
testq %rax, %rax
movq %r15, 0x20(%rsp)
je 0x57534
movq %rax, %r13
movl $0x1000, %ebp           # imm = 0x1000
movq %r13, %r14
subq %r12, %r14
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
subq %r14, %rbp
jbe 0x575ca
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x4250
addq %r14, %r15
movb 0x1(%r13), %al
cmpb $0x73, %al
jne 0x5744e
movq %rbx, %rdi
callq 0x41d0
subq %rax, %rbp
jbe 0x575ca
movq %rax, %r14
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4460
addq %r14, %r15
addq %r14, %rbx
addq $0x2, %r13
jmp 0x574e4
addb $-0x30, %al
cmpb $0x9, %al
ja 0x574c6
leaq 0x1(%r13), %r12
movq %r12, %rdi
leaq 0x18(%rsp), %rsi
movl $0xa, %edx
callq 0x46d0
movq %rbx, %rdi
movq %rax, %rbx
movq %rdi, 0x10(%rsp)
callq 0x41d0
movq %rax, %r14
cmpq %rax, %rbx
cmovbq %rbx, %r14
movq 0x18(%rsp), %rax
cmpb $0x73, (%rax)
jne 0x574e9
movq %rbp, %rbx
subq %r14, %rbx
jbe 0x574ef
movq %r15, %rdi
movq 0x10(%rsp), %r13
movq %r13, %rsi
movq %r14, %rdx
callq 0x4250
movb $0x0, (%r15,%r14)
addq %r14, %r15
addq %r14, %r13
movq 0x18(%rsp), %r12
incq %r12
movb $0x1, %al
movq %rbx, %rbp
movq %r13, %rbx
jmp 0x57511
cmpq $0x3, %rbp
jb 0x575ca
movb $0x25, (%r15)
movb 0x1(%r13), %al
addq $0x2, %r13
movb %al, 0x1(%r15)
addq $0x2, %r15
movq %r13, %r12
jmp 0x57519
cmpq $0x3, %rbp
jae 0x574f3
xorl %eax, %eax
jmp 0x5750c
movb $0x25, (%r15)
movb 0x1(%r13), %al
addq $0x2, %r13
movb %al, 0x1(%r15)
addq $0x2, %r15
movb $0x1, %al
movq %r13, %r12
movq 0x10(%rsp), %rbx
testb %al, %al
je 0x575ca
movq %r12, %rdi
movl $0x25, %esi
callq 0x4190
movq %rax, %r13
testq %rax, %rax
jne 0x573f0
jmp 0x57539
movl $0x1000, %ebp           # imm = 0x1000
movq %r12, %rdi
callq 0x41d0
subq %rax, %rbp
jbe 0x57588
movq %rax, %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x4460
addq %r15, %r13
movq %rbx, %rdi
callq 0x41d0
cmpb $0x0, (%rbx)
setne %dl
cmpq 0x20(%rsp), %r13
seta %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x57592
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
xorl %edx, %edx
cmpb $0x2f, -0x1(%r13)
setne %dl
jmp 0x5759c
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
jmp 0x575ca
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
xorl %edx, %edx
addq %rax, %rdx
cmpq %rbp, %rdx
jae 0x575ca
testb %cl, %cl
je 0x575b7
cmpb $0x2f, -0x1(%r13)
je 0x575b7
movb $0x2f, (%r13)
incq %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x4460
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq