// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/cram/cram_io.c
// Function: cram_populate_ref
static int cram_populate_ref(cram_fd *fd, int id, ref_entry *r) {
    char *ref_path = getenv("REF_PATH");
    sam_hrec_type_t *ty;
    sam_hrec_tag_t *tag;
    char path[PATH_MAX];
    kstring_t path_tmp = KS_INITIALIZE;
    char *local_cache = getenv("REF_CACHE");
    mFILE *mf;
    int local_path = 0;

    hts_log_info("Running cram_populate_ref on fd %p, id %d", (void *)fd, id);

    if (!r->name)
        return -1;

    if (!(ty = sam_hrecs_find_type_id(fd->header->hrecs, "SQ", "SN", r->name)))
        return -1;

    if (!(tag = sam_hrecs_find_key(ty, "M5", NULL)))
        goto no_M5;

    hts_log_info("Querying ref %s", tag->str+3);

    /* Use cache if available */
    if (local_cache && *local_cache) {
        struct stat sb;
        if (expand_cache_path(path, local_cache, tag->str+3) == 0 &&
            stat(path, &sb) == 0)
            // Found it in the local cache
            local_path = 1;
    }

#ifndef HAVE_MMAP
    char *path2;
    /* Search local files in REF_PATH; we can open them and return as above */
    if (!local_path && (path2 = find_path(tag->str+3, ref_path))) {
        int len = snprintf(path, PATH_MAX, "%s", path2);
        free(path2);
        if (len > 0 && len < PATH_MAX) // in case it's too long
            local_path = 1;
    }
#endif

    /* Found via REF_CACHE or local REF_PATH file */
    if (local_path) {
        struct stat sb;
        BGZF *fp;

        if (0 == stat(path, &sb)
            && S_ISREG(sb.st_mode)
            && (fp = bgzf_open(path, "r"))) {
            r->length = sb.st_size;
            r->offset = r->line_length = r->bases_per_line = 0;

            r->fn = string_dup(fd->refs->pool, path);

            if (fd->refs->fp)
                if (bgzf_close(fd->refs->fp) != 0)
                    return -1;
            fd->refs->fp = fp;
            fd->refs->fn = r->fn;
            r->is_md5 = 1;
            r->validated_md5 = 1;

            // Fall back to cram_get_ref() where it'll do the actual
            // reading of the file.
            return 0;
        }
    }


    /* Otherwise search full REF_PATH; slower as loads entire file */
    int is_local = 0;
    if ((mf = open_path_mfile(tag->str+3, ref_path, NULL, &is_local))) {
        size_t sz;
        r->seq = mfsteal(mf, &sz);
        if (r->seq) {
            r->mf = NULL;
        } else {
            // keep mf around as we couldn't detach
            r->seq = mf->data;
            r->mf = mf;
        }
        r->length = sz;
        r->is_md5 = 1;
        r->validated_md5 = 1;
    } else {
        refs_t *refs;
        const char *fn;
        sam_hrec_tag_t *UR_tag;

    no_M5:
        /* Failed to find in search path or M5 cache, see if @SQ UR: tag? */
        if (!(UR_tag = sam_hrecs_find_key(ty, "UR", NULL)))
            return -1;

        if (strstr(UR_tag->str+3, "://") &&
            strncmp(UR_tag->str+3, "file:", 5) != 0) {
            // Documented as omitted, but accidentally supported until now
            hts_log_error("UR tags pointing to remote files are not supported");
            return -1;
        }

        fn = (strncmp(UR_tag->str+3, "file:", 5) == 0)
            ? UR_tag->str+8
            : UR_tag->str+3;

        if (fd->refs->fp) {
            if (bgzf_close(fd->refs->fp) != 0)
                return -1;
            fd->refs->fp = NULL;
        }
        if (!(refs = refs_load_fai(fd->refs, fn, 0)))
            return -1;
        sanitise_SQ_lines(fd);

        fd->refs = refs;
        if (fd->refs->fp) {
            if (bgzf_close(fd->refs->fp) != 0)
                return -1;
            fd->refs->fp = NULL;
        }

        if (!fd->refs->fn)
            return -1;

        if (-1 == refs2id(fd->refs, fd->header))
            return -1;
        if (!fd->refs->ref_id || !fd->refs->ref_id[id])
            return -1;

        // Local copy already, so fall back to cram_get_ref().
        return 0;
    }

    /* Populate the local disk cache if required */
    if (!is_local && local_cache && *local_cache) {
        hFILE *fp;

        if (expand_cache_path(path, local_cache, tag->str+3) < 0) {
            return 0; // Not fatal - we have the data already so keep going.
        }
        hts_log_info("Writing cache file '%s'", path);
        mkdir_prefix(path, 01777);

        fp = hts_open_tmpfile(path, "wx", &path_tmp);
        if (!fp) {
            perror(path_tmp.s);
            free(path_tmp.s);

            // Not fatal - we have the data already so keep going.
            return 0;
        }

        // Check md5sum
        hts_md5_context *md5;
        char unsigned md5_buf1[16];
        char md5_buf2[33];

        if (!(md5 = hts_md5_init())) {
            hclose_abruptly(fp);
            unlink(path_tmp.s);
            free(path_tmp.s);
            return -1;
        }
        hts_md5_update(md5, r->seq, r->length);
        hts_md5_final(md5_buf1, md5);
        hts_md5_destroy(md5);
        hts_md5_hex(md5_buf2, md5_buf1);

        if (strncmp(tag->str+3, md5_buf2, 32) != 0) {
            hts_log_error("Mismatching md5sum for downloaded reference");
            hclose_abruptly(fp);
            unlink(path_tmp.s);
            free(path_tmp.s);
            return -1;
        }

        ssize_t length_written = hwrite(fp, r->seq, r->length);
        if (hclose(fp) < 0 || length_written != r->length ||
            chmod(path_tmp.s, 0444) < 0 ||
            rename(path_tmp.s, path) < 0) {
            hts_log_error("Creating reference at %s failed: %s",
                          path, strerror(errno));
            unlink(path_tmp.s);
        }
    }

    free(path_tmp.s);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1240, %rsp           # imm = 0x1240
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
leaq 0x82c80(%rip), %rdi     # 0xf5e2d
callq 0x45e0
movq %rax, -0x28(%rbp)
leaq -0x1058(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4250
leaq 0x82c66(%rip), %rdi     # 0xf5e36
callq 0x45e0
movq %rax, -0x1060(%rbp)
movl $0x0, -0x106c(%rbp)
movq -0x10(%rbp), %rcx
movl -0x14(%rbp), %r8d
movl $0x4, %edi
leaq 0x82c46(%rip), %rsi     # 0xf5e40
leaq 0x82c51(%rip), %rdx     # 0xf5e52
movb $0x0, %al
callq 0x1b030
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x7321e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x38(%rax), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rcx
leaq 0x7f4b4(%rip), %rsi     # 0xf26ec
leaq 0x8071e(%rip), %rdx     # 0xf395d
callq 0xd9960
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x7325a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
movq -0x30(%rbp), %rdi
leaq 0x806fb(%rip), %rsi     # 0xf3960
xorl %eax, %eax
movl %eax, %edx
callq 0xdaf50
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x7327d
jmp 0x7357a
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x3, %rcx
movl $0x4, %edi
leaq 0x82bab(%rip), %rsi     # 0xf5e40
leaq 0x82be0(%rip), %rdx     # 0xf5e7c
movb $0x0, %al
callq 0x1b030
cmpq $0x0, -0x1060(%rbp)
je 0x73304
movq -0x1060(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x73304
leaq -0x1040(%rbp), %rdi
movq -0x1060(%rbp), %rsi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdx
addq $0x3, %rdx
callq 0x7c630
cmpl $0x0, %eax
jne 0x73302
leaq -0x1040(%rbp), %rdi
leaq -0x1100(%rbp), %rsi
callq 0x4410
cmpl $0x0, %eax
jne 0x73302
movl $0x1, -0x106c(%rbp)
jmp 0x73304
cmpl $0x0, -0x106c(%rbp)
jne 0x73383
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
addq $0x3, %rdi
movq -0x28(%rbp), %rsi
callq 0x81d80
movq %rax, -0x1108(%rbp)
cmpq $0x0, %rax
je 0x73383
leaq -0x1040(%rbp), %rdi
movq -0x1108(%rbp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x7f6ab(%rip), %rdx     # 0xf29f4
movb $0x0, %al
callq 0x47e0
movl %eax, -0x110c(%rbp)
movq -0x1108(%rbp), %rdi
callq 0x45a0
cmpl $0x0, -0x110c(%rbp)
jle 0x73381
cmpl $0x1000, -0x110c(%rbp)  # imm = 0x1000
jge 0x73381
movl $0x1, -0x106c(%rbp)
jmp 0x73383
cmpl $0x0, -0x106c(%rbp)
je 0x734ba
leaq -0x1040(%rbp), %rdi
leaq -0x11a0(%rbp), %rsi
callq 0x4410
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x734b8
movl -0x1188(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x734b8
leaq -0x1040(%rbp), %rdi
leaq 0x7f71e(%rip), %rsi     # 0xf2af1
callq 0xc090
movq %rax, -0x11a8(%rbp)
cmpq $0x0, %rax
je 0x734b8
movq -0x1170(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x2c(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq (%rax), %rdi
leaq -0x1040(%rbp), %rsi
callq 0x82610
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, 0x28(%rax)
je 0x7346f
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x28(%rax), %rdi
callq 0x103d0
cmpl $0x0, %eax
je 0x7346d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
jmp 0x7346f
movq -0x11a8(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x28(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rax
movl $0x1, 0x48(%rax)
movq -0x20(%rbp), %rax
movl $0x1, 0x4c(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x73a6f
jmp 0x734ba
movl $0x0, -0x11ac(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
addq $0x3, %rdi
movq -0x28(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
leaq -0x11ac(%rbp), %rcx
callq 0x81ac0
movq %rax, -0x1068(%rbp)
cmpq $0x0, %rax
je 0x73578
movq -0x1068(%rbp), %rdi
leaq -0x11b8(%rbp), %rsi
callq 0x808c0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x7352c
movq -0x20(%rbp), %rax
movq $0x0, 0x40(%rax)
jmp 0x7354e
movq -0x1068(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x1068(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x11b8(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movl $0x1, 0x48(%rax)
movq -0x20(%rbp), %rax
movl $0x1, 0x4c(%rax)
jmp 0x737c2
jmp 0x7357a
movq -0x30(%rbp), %rdi
leaq 0x8251f(%rip), %rsi     # 0xf5aa4
xorl %eax, %eax
movl %eax, %edx
callq 0xdaf50
movq %rax, -0x11d0(%rbp)
cmpq $0x0, %rax
jne 0x735a7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
movq -0x11d0(%rbp), %rax
movq 0x8(%rax), %rdi
addq $0x3, %rdi
leaq 0x7c149(%rip), %rsi     # 0xef706
callq 0x40c0
cmpq $0x0, %rax
je 0x73613
movq -0x11d0(%rbp), %rax
movq 0x8(%rax), %rdi
addq $0x3, %rdi
leaq 0x828ae(%rip), %rsi     # 0xf5e8c
movl $0x5, %edx
callq 0x4200
cmpl $0x0, %eax
je 0x73613
movl $0x1, %edi
leaq 0x82847(%rip), %rsi     # 0xf5e40
leaq 0x82892(%rip), %rdx     # 0xf5e92
movb $0x0, %al
callq 0x1b030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
movq -0x11d0(%rbp), %rax
movq 0x8(%rax), %rdi
addq $0x3, %rdi
leaq 0x82863(%rip), %rsi     # 0xf5e8c
movl $0x5, %edx
callq 0x4200
cmpl $0x0, %eax
jne 0x73650
movq -0x11d0(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x8, %rax
movq %rax, -0x1230(%rbp)
jmp 0x73666
movq -0x11d0(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x3, %rax
movq %rax, -0x1230(%rbp)
movq -0x1230(%rbp), %rax
movq %rax, -0x11c8(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, 0x28(%rax)
je 0x736b5
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x28(%rax), %rdi
callq 0x103d0
cmpl $0x0, %eax
je 0x736a5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq $0x0, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x11c8(%rbp), %rsi
xorl %edx, %edx
callq 0x73c10
movq %rax, -0x11c0(%rbp)
cmpq $0x0, %rax
jne 0x736e4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
movq -0x10(%rbp), %rdi
callq 0x74460
movq -0x11c0(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, 0x28(%rax)
je 0x7373d
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x28(%rax), %rdi
callq 0x103d0
cmpl $0x0, %eax
je 0x7372d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq $0x0, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, 0x20(%rax)
jne 0x73758
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x719c0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x73784
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, 0x10(%rax)
je 0x737aa
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x737b6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
movl $0x0, -0x4(%rbp)
jmp 0x73a6f
cmpl $0x0, -0x11ac(%rbp)
jne 0x73a5c
cmpq $0x0, -0x1060(%rbp)
je 0x73a5c
movq -0x1060(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x73a5c
leaq -0x1040(%rbp), %rdi
movq -0x1060(%rbp), %rsi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdx
addq $0x3, %rdx
callq 0x7c630
cmpl $0x0, %eax
jge 0x73820
movl $0x0, -0x4(%rbp)
jmp 0x73a6f
leaq -0x1040(%rbp), %rcx
movl $0x4, %edi
leaq 0x8260d(%rip), %rsi     # 0xf5e40
leaq 0x8268b(%rip), %rdx     # 0xf5ec5
movb $0x0, %al
callq 0x1b030
leaq -0x1040(%rbp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
callq 0x7c9e0
leaq -0x1040(%rbp), %rdi
leaq 0x7d577(%rip), %rsi     # 0xf0dd7
leaq -0x1058(%rbp), %rdx
callq 0x1dc90
movq %rax, -0x11d8(%rbp)
cmpq $0x0, -0x11d8(%rbp)
jne 0x738a1
movq -0x1048(%rbp), %rdi
callq 0x43c0
movq -0x1048(%rbp), %rdi
callq 0x45a0
movl $0x0, -0x4(%rbp)
jmp 0x73a6f
callq 0xe18c0
movq %rax, -0x11e0(%rbp)
cmpq $0x0, %rax
jne 0x738e3
movq -0x11d8(%rbp), %rdi
callq 0x149d0
movq -0x1048(%rbp), %rdi
callq 0x41f0
movq -0x1048(%rbp), %rdi
callq 0x45a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
movq -0x11e0(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0xe04d0
leaq -0x11f0(%rbp), %rdi
movq -0x11e0(%rbp), %rsi
callq 0xe1630
movq -0x11e0(%rbp), %rdi
callq 0xe1910
leaq -0x1220(%rbp), %rdi
leaq -0x11f0(%rbp), %rsi
callq 0xe1940
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
addq $0x3, %rdi
leaq -0x1220(%rbp), %rsi
movl $0x20, %edx
callq 0x4200
cmpl $0x0, %eax
je 0x7399d
movl $0x1, %edi
leaq 0x824e1(%rip), %rsi     # 0xf5e40
leaq 0x82577(%rip), %rdx     # 0xf5edd
movb $0x0, %al
callq 0x1b030
movq -0x11d8(%rbp), %rdi
callq 0x149d0
movq -0x1048(%rbp), %rdi
callq 0x41f0
movq -0x1048(%rbp), %rdi
callq 0x45a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73a6f
movq -0x11d8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x6d1e0
movq %rax, -0x1228(%rbp)
movq -0x11d8(%rbp), %rdi
callq 0x14920
cmpl $0x0, %eax
jl 0x73a10
movq -0x1228(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x73a10
movq -0x1048(%rbp), %rdi
movl $0x124, %esi            # imm = 0x124
callq 0x4420
cmpl $0x0, %eax
jl 0x73a10
movq -0x1048(%rbp), %rdi
leaq -0x1040(%rbp), %rsi
callq 0x42b0
cmpl $0x0, %eax
jge 0x73a5a
leaq -0x1040(%rbp), %rax
movq %rax, -0x1238(%rbp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq -0x1238(%rbp), %rcx
movq %rax, %r8
movl $0x1, %edi
leaq 0x82400(%rip), %rsi     # 0xf5e40
leaq 0x824c2(%rip), %rdx     # 0xf5f09
movb $0x0, %al
callq 0x1b030
movq -0x1048(%rbp), %rdi
callq 0x41f0
jmp 0x73a5c
movq -0x1048(%rbp), %rdi
callq 0x45a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1240, %rsp           # imm = 0x1240
popq %rbp
retq
nopl (%rax,%rax)