// File: /workspace/github_c_makefile_star2_O3/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, %r14d
movq %rsi, %rbx
movq %rdi, %rbp
movq %rsi, %rdi
callq 0x41d0
movq %rax, %r15
movq %rbp, %rdi
callq 0x41d0
movq %rax, %r12
leaq (%r15,%rax), %rdi
addq $0x2, %rdi
callq 0x4680
movq %rax, %r13
testq %rax, %rax
je 0x577d8
movq %r12, 0x8(%rsp)
cmpq $0x2, %r15
jb 0x577f7
leaq 0x1(%rbx), %r12
cmpb $0x2f, -0x1(%rbx,%r15)
jne 0x577ff
decq %r15
cmpq $0x1, %r15
ja 0x577bf
cmpb $0x2f, (%rbp)
jne 0x5780f
jmp 0x57814
leaq 0x44217(%rip), %rsi     # 0x9b9f6
leaq 0x3feaf(%rip), %rdx     # 0x97695
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
jmp 0x57996
movq %r15, %r12
addq %rbx, %r12
jmp 0x57803
leaq (%rbx,%r15), %r12
cmpb $0x2f, (%rbp)
je 0x57814
cmpq $0x1, %r15
jne 0x57835
cmpb $0x2e, (%rbx)
jne 0x57835
movq 0x8(%rsp), %rdx
incq %rdx
movq %r13, %rdi
movq %rbp, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x43c0
movq %rbp, 0x10(%rsp)
movq %rbx, %rdi
movl $0x25, %esi
callq 0x4190
testq %rax, %rax
movq %r13, 0x18(%rsp)
je 0x5793e
movq %rax, %rbp
movl %r14d, %eax
movq %rax, 0x28(%rsp)
movq %r13, %r14
movq %r12, 0x20(%rsp)
leaq 0x1(%rbp), %rdi
leaq 0x30(%rsp), %rsi
movl $0xa, %edx
callq 0x46d0
movq 0x30(%rsp), %r13
cmpb $0x73, (%r13)
jne 0x5789f
movq %rax, %r15
testq %rax, %rax
js 0x5789f
movq %rbp, %rax
notq %rax
addq %r13, %rax
cmpq 0x28(%rsp), %rax
jle 0x578c2
incq %r13
cmpq %r12, %r13
cmovaeq %r12, %r13
movq %r13, %r15
subq %rbx, %r15
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x43c0
addq %r14, %r15
jmp 0x5791d
subq %rbx, %rbp
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x43c0
addq %r14, %rbp
movq 0x8(%rsp), %r12
cmpq %r15, %r12
movq %r15, %rbx
cmovbq %r12, %rbx
testq %r15, %r15
cmoveq %r12, %rbx
movq %rbp, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
movq %rbx, %rdx
callq 0x43c0
addq %rbx, %rbp
addq %rbx, %r14
movq %r14, 0x10(%rsp)
subq %rbx, %r12
movq %r12, 0x8(%rsp)
movq 0x20(%rsp), %r12
incq %r13
movq %rbp, %r15
movq %r13, %rdi
movl $0x25, %esi
callq 0x4190
movq %rax, %rbp
movq %r13, %rbx
movq %r15, %r14
testq %rax, %rax
jne 0x57868
jmp 0x57944
movq %r13, %r15
movq %rbx, %r13
cmpq %r12, %r13
jae 0x5795d
subq %r13, %r12
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x43c0
addq %r12, %r15
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rbx
cmpb $0x0, (%rsi)
je 0x57992
cmpq %r13, %r15
jbe 0x57984
cmpb $0x2f, -0x1(%r15)
je 0x57984
movb $0x2f, (%r15)
incq %r15
movq %r15, %rdi
movq %rbx, %rdx
callq 0x43c0
addq %rbx, %r15
movb $0x0, (%r15)
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq