// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r13
movq %rsi, %rdi
callq 0x41d0
movq %rax, %rbx
movq %r13, %rdi
callq 0x41d0
movq %rax, %r12
leaq (%rbx,%rax), %rdi
addq $0x2, %rdi
callq 0x4680
testq %rax, %rax
je 0x5a21f
movq %rax, %r15
movq %r12, (%rsp)
cmpq $0x2, %rbx
jb 0x5a240
leaq 0x1(%r14), %r12
cmpb $0x2f, -0x1(%r14,%rbx)
jne 0x5a248
decq %rbx
cmpq $0x1, %rbx
ja 0x5a207
movl $0x1, %ebx
jmp 0x5a24c
leaq 0x445d8(%rip), %rsi     # 0x9e7fe
leaq 0x40458(%rip), %rdx     # 0x9a685
xorl %ebx, %ebx
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x5a3d4
movq %rbx, %r12
addq %r14, %r12
jmp 0x5a24c
leaq (%r14,%rbx), %r12
cmpb $0x2f, (%r13)
jne 0x5a273
movq (%rsp), %rdx
incq %rdx
movq %r15, %rdi
movq %r13, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x43c0
cmpq $0x1, %rbx
jne 0x5a27f
cmpb $0x2e, (%r14)
je 0x5a253
movq %r13, 0x8(%rsp)
movq %r14, %rdi
movl $0x25, %esi
callq 0x4190
testq %rax, %rax
movq %r15, 0x10(%rsp)
je 0x5a380
movq %rax, %rbx
movl %ebp, %eax
movq %rax, 0x20(%rsp)
movq %r15, %rbp
leaq 0x1(%rbx), %rdi
leaq 0x18(%rsp), %rsi
movl $0xa, %edx
callq 0x46d0
movq 0x18(%rsp), %r13
cmpb $0x73, (%r13)
jne 0x5a2e3
movq %rax, %r15
testq %rax, %rax
js 0x5a2e3
movq %rbx, %rax
notq %rax
addq %r13, %rax
cmpq 0x20(%rsp), %rax
jle 0x5a306
incq %r13
cmpq %r12, %r13
cmovaeq %r12, %r13
movq %r13, %r15
subq %r14, %r15
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x43c0
addq %rbp, %r15
jmp 0x5a35f
subq %r14, %rbx
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x43c0
addq %rbp, %rbx
movq (%rsp), %r13
cmpq %r15, %r13
movq %r15, %r14
cmovbq %r13, %r14
testq %r15, %r15
cmovleq %r13, %r14
movq %rbx, %rdi
movq 0x8(%rsp), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0x43c0
addq %r14, %rbx
addq %r14, %r15
movq %r15, 0x8(%rsp)
subq %r14, %r13
movq %r13, (%rsp)
movq 0x18(%rsp), %r13
incq %r13
movq %rbx, %r15
movq %r13, %rdi
movl $0x25, %esi
callq 0x4190
movq %rax, %rbx
movq %r13, %r14
movq %r15, %rbp
testq %rax, %rax
jne 0x5a2ac
jmp 0x5a383
movq %r14, %r13
cmpq %r12, %r13
jae 0x5a39c
subq %r13, %r12
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x43c0
addq %r12, %r15
movq 0x10(%rsp), %rbx
movq (%rsp), %r14
movq 0x8(%rsp), %rsi
cmpb $0x0, (%rsi)
je 0x5a3d0
cmpq %rbx, %r15
jbe 0x5a3c2
cmpb $0x2f, -0x1(%r15)
je 0x5a3c2
movb $0x2f, (%r15)
incq %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x43c0
addq %r14, %r15
movb $0x0, (%r15)
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq