// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/cram/open_trace_file.c
// Function: expand_path
static char *expand_path(const char *file, const char *dirname,
                         int max_s_digits) {
    size_t len = strlen(dirname);
    size_t lenf = strlen(file);
    const char *end_dirname = dirname + len, *cp;
    char *path;

    path = malloc(len+lenf+2); // worst expansion DIR/FILE
    if (!path) {
        hts_log_error("Out of memory");
        return NULL;
    }

    // Remove trailing '/'s, unless the path matches "/"
    while (len > 1 && dirname[len-1] == '/') {
        len--;
        end_dirname--;
    }

    /* Special case for "./" or absolute filenames */
    if (*file == '/' || (len==1 && *dirname == '.')) {
        memcpy(path, file, lenf + 1);
    } else {
        /* Handle %[0-9]*s expansions, if required */
        char *path_end = path;

        while ((cp = strchr(dirname, '%'))) {
            char *endp;
            // Get optional length
            long l = strtol(cp+1, &endp, 10);
            if (*endp != 's' || l < 0 || endp - cp - 1 > max_s_digits) {
                // Not %[0-9]s. Copy over directly, taking care of edge cases
                // like the string ending with '%' or '%[0-9]*'.
                const char *e = MIN(endp+1, end_dirname);
                memcpy(path_end, dirname, e - dirname);
                path_end += e - dirname;
                dirname = e;
                continue;
            }

            // Copy part up to '%'
            memcpy(path_end, dirname, cp-dirname);
            path_end += cp-dirname;

            // Insert segment from file
            size_t to_copy = l > 0 ? MIN(lenf, l) : lenf;
            memcpy(path_end, file, to_copy);
            path_end += to_copy;
            file     += to_copy;
            lenf     -= to_copy;

            // Skip to part of dirname after the 's'
            dirname = endp+1;
        }

        // Add anything left in dirname
        if (dirname < end_dirname) {
            memcpy(path_end, dirname, end_dirname - dirname);
            path_end += end_dirname - dirname;
        }

        if (*file) {
            // Add remainder of file
            if (path_end > path && *(path_end - 1) != '/')
                *path_end++ = '/';
            memcpy(path_end, file, lenf);
            path_end += lenf;
        }
        // Terminate string
        *path_end = '\0';
    }

    // fprintf(stderr, "*PATH=\"%s\"\n", path);
    return path;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %ebx
movq %rsi, %r12
movq %rdi, %r14
movq %rsi, %rdi
callq 0x41d0
movq %rax, %rbp
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x41d0
movq %rax, %r14
leaq (%rax,%rbp), %rdi
addq $0x2, %rdi
callq 0x4680
testq %rax, %rax
je 0x48bb3
movq %r14, 0x10(%rsp)
leaq (%r12,%rbp), %rdx
movq %rbp, %r15
cmpq $0x2, %r15
jb 0x48bd6
cmpb $0x2f, -0x1(%r12,%r15)
jne 0x48cdb
decq %r15
decq %rdx
jmp 0x48b99
leaq 0x34c37(%rip), %rsi     # 0x7d7f1
leaq 0x30a24(%rip), %rdx     # 0x795e5
pushq $0x1
popq %rdi
movq %rax, %rbx
xorl %eax, %eax
callq 0x110c6
movq %rbx, %rax
jmp 0x48d6a
movq 0x8(%rsp), %rcx
cmpb $0x2f, (%rcx)
je 0x48ce9
testq %rbp, %rbp
je 0x48bf4
cmpb $0x2e, (%r12)
je 0x48ce9
movq %rdx, 0x20(%rsp)
movl %ebx, %ecx
movq %rcx, 0x28(%rsp)
addq %r12, %r15
movq %rax, 0x18(%rsp)
movq %rax, %rbx
movq %r12, %rdi
pushq $0x25
popq %rsi
callq 0x4190
testq %rax, %rax
je 0x48d0c
movq %rax, %rbp
leaq 0x1(%rax), %rdi
leaq 0x30(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x46d0
movq 0x30(%rsp), %r13
cmpb $0x73, (%r13)
jne 0x48c57
movq %rax, %r14
testq %rax, %rax
js 0x48c57
movq %rbp, %rax
notq %rax
addq %r13, %rax
cmpq 0x28(%rsp), %rax
jle 0x48c7d
incq %r13
cmpq %r15, %r13
cmovaeq %r15, %r13
movq %r13, %r14
subq %r12, %r14
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x43c0
addq %r14, %rbx
movq %r13, %r12
jmp 0x48c0b
subq %r12, %rbp
movq %rbx, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x43c0
addq %rbx, %rbp
movq 0x10(%rsp), %r12
cmpq %r14, %r12
movq %r14, %rbx
cmovbq %r12, %rbx
testq %r14, %r14
cmoveq %r12, %rbx
movq %rbp, %rdi
movq 0x8(%rsp), %r14
movq %r14, %rsi
movq %rbx, %rdx
callq 0x43c0
addq %rbx, %rbp
addq %rbx, %r14
movq %r14, 0x8(%rsp)
subq %rbx, %r12
movq %r12, 0x10(%rsp)
incq %r13
movq %rbp, %rbx
movq %r13, %r12
jmp 0x48c0b
movq 0x8(%rsp), %rcx
cmpb $0x2f, (%rcx)
jne 0x48bf4
movq 0x10(%rsp), %rdx
incq %rdx
movq %rax, %rdi
movq 0x8(%rsp), %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x43c0
cmpq %r15, %r12
jae 0x48d2a
movq 0x20(%rsp), %r14
subq %r12, %r14
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x43c0
addq %r14, %rbx
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rcx
cmpb $0x0, (%rcx)
je 0x48d67
cmpq %rax, %rbx
jbe 0x48d4f
cmpb $0x2f, -0x1(%rbx)
je 0x48d4f
movb $0x2f, (%rbx)
incq %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x43c0
addq %r14, %rbx
movq 0x18(%rsp), %rax
movb $0x0, (%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq