// File: /workspace/github_c_makefile_star2_O0/icebert[P]pblat/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 = fd->refUrl;
    SAM_hdr_type *ty;
    SAM_hdr_tag *tag;
    char path[PATH_MAX], path_tmp[PATH_MAX], cache[PATH_MAX];
    char *local_cache = fd->cacheDir;
    mFILE *mf;
    int local_path = 0;

    fd->md5Ref[0] = 0;
    if (fd->verbose)
	fprintf(stderr, "cram_populate_ref on fd %p, id %d\n", fd, id);

    if (!ref_path || *ref_path == '\0') {
	/*
	 * If we have no ref path, we use the EBI server.
	 * However to avoid spamming it we require a local ref cache too.
	 */
	ref_path = "http://www.ebi.ac.uk:80/ena/cram/md5/%s";
	if (!local_cache || *local_cache == '\0') {
	    const char *extra;
	    const char *base = get_cache_basedir(&extra);
	    snprintf(cache,PATH_MAX, "%s%s/hts-ref/%%2s/%%2s/%%s", base, extra);
	    local_cache = cache;
	    if (fd->verbose)
		fprintf(stderr, "Populating local cache: %s\n", local_cache);
	}
    }

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

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

    if (!(tag = sam_hdr_find_key(fd->header, ty, "M5", NULL)))
	goto no_M5;

    if (fd->verbose)
	fprintf(stderr, "Querying ref %s\n", tag->str+3);

    /* Use cache if available */
    if (local_cache && *local_cache) {
	expand_cache_path(path, local_cache, tag->str+3);
	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))) {
	strncpy(path, path2, PATH_MAX);
	free(path2);
	if (is_file(path)) // incase 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) && (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;

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


#ifdef UCSC_CRAM
    // don't download cram reference, just pass the file name upward
    sprintf(fd->md5Ref, "%s", tag->str+3);
    return -1;
#endif

    /* Otherwise search full REF_PATH; slower as loads entire file */
    if ((mf = open_path_mfile(tag->str+3, ref_path, NULL))) {
	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;
    } else {
	refs_t *refs;
	char *fn;

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

	fn = (strncmp(tag->str+3, "file:", 5) == 0)
	    ? tag->str+8
	    : 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;
    }

#ifdef UCSC_CRAM
    assert(!((local_cache && *local_cache))); 
#endif
    /* Populate the local disk cache if required */
    if (local_cache && *local_cache) {
	FILE *fp;
	int i;

	expand_cache_path(path, local_cache, tag->str+3);
	if (fd->verbose)
	    fprintf(stderr, "Path='%s'\n", path);
	mkdir_prefix(path, 01777);

	i = 0;
	do {
	    sprintf(path_tmp, "%s.tmp_%d", path, /*getpid(),*/ i);
	    i++;
	    fp = fopen(path_tmp, "wx");
	} while (fp == NULL && errno == EEXIST);
	if (!fp) {
	    perror(path_tmp);

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

	if (r->length != fwrite(r->seq, 1, r->length, fp)) {
	    perror(path);
	}
	if (-1 == paranoid_fclose(fp)) {
	    unlink(path_tmp);
	} else {
	    if (0 == chmod(path_tmp, 0444))
		rename(path_tmp, path);
	    else
		unlink(path_tmp);
	}
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3140, %rsp           # imm = 0x3140
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
addq $0x8cfc, %rax           # imm = 0x8CFC
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
addq $0x88fc, %rax           # imm = 0x88FC
movq %rax, -0x3048(%rbp)
movl $0x0, -0x3054(%rbp)
movq -0x10(%rbp), %rax
movb $0x0, 0x9cfc(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1fc(%rax)
je 0x82596
movq 0x82a52(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
movl -0x14(%rbp), %ecx
leaq 0x60bec(%rip), %rsi     # 0xe317b
movb $0x0, %al
callq 0x6a80
cmpq $0x0, -0x28(%rbp)
je 0x825ad
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x8264a
leaq 0x60bea(%rip), %rax     # 0xe319e
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x3048(%rbp)
je 0x825d1
movq -0x3048(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x82648
leaq -0x3060(%rbp), %rdi
callq 0x8c730
movq %rax, -0x3068(%rbp)
leaq -0x3040(%rbp), %rdi
movq -0x3068(%rbp), %rcx
movq -0x3060(%rbp), %r8
movl $0x1000, %esi           # imm = 0x1000
leaq 0x60bc1(%rip), %rdx     # 0xe31c6
movb $0x0, %al
callq 0x6c20
leaq -0x3040(%rbp), %rax
movq %rax, -0x3048(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1fc(%rax)
je 0x82646
movq 0x829a2(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
movq -0x3048(%rbp), %rdx
leaq 0x60ba2(%rip), %rsi     # 0xe31e1
movb $0x0, %al
callq 0x6a80
jmp 0x82648
jmp 0x8264a
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x82660
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x82c9a
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rcx
leaq 0x61da4(%rip), %rsi     # 0xe441a
leaq 0x6093d(%rip), %rdx     # 0xe2fba
callq 0x95220
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x82698
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x82c9a
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
leaq 0x60912(%rip), %rdx     # 0xe2fbd
xorl %eax, %eax
movl %eax, %ecx
callq 0x95990
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x826c3
jmp 0x828ec
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1fc(%rax)
je 0x826f4
movq 0x828f9(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdx
addq $0x3, %rdx
leaq 0x60b10(%rip), %rsi     # 0xe31fd
movb $0x0, %al
callq 0x6a80
cmpq $0x0, -0x3048(%rbp)
je 0x82736
movq -0x3048(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x82736
leaq -0x1040(%rbp), %rdi
movq -0x3048(%rbp), %rsi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdx
addq $0x3, %rdx
callq 0x813b0
movl $0x1, -0x3054(%rbp)
cmpl $0x0, -0x3054(%rbp)
je 0x8284c
leaq -0x1040(%rbp), %rdi
leaq -0x30f8(%rbp), %rsi
callq 0x6630
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x8284a
leaq -0x1040(%rbp), %rdi
leaq 0x5d626(%rip), %rsi     # 0xdfd96
callq 0x9afb0
movq %rax, -0x3100(%rbp)
cmpq $0x0, %rax
je 0x8284a
movq -0x30c8(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x24(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rdi
leaq -0x1040(%rbp), %rsi
callq 0x981b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x28(%rax)
je 0x8280c
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x28(%rax), %rdi
callq 0x9d380
cmpl $0x0, %eax
je 0x8280a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x82c9a
jmp 0x8280c
movq -0x3100(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rcx, 0x28(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rax
movl $0x1, 0x40(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x82c9a
jmp 0x8284c
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
addq $0x3, %rdi
movq -0x28(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x8f770
movq %rax, -0x3050(%rbp)
cmpq $0x0, %rax
je 0x828ea
movq -0x3050(%rbp), %rdi
leaq -0x3108(%rbp), %rsi
callq 0x8e6f0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x828a9
movq -0x20(%rbp), %rax
movq $0x0, 0x38(%rax)
jmp 0x828cb
movq -0x3050(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x3050(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x3108(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movl $0x1, 0x40(%rax)
jmp 0x82ac4
jmp 0x828ec
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
leaq 0x57857(%rip), %rdx     # 0xda156
xorl %eax, %eax
movl %eax, %ecx
callq 0x95990
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x8291e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x82c9a
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
addq $0x3, %rdi
leaq 0x608dd(%rip), %rsi     # 0xe320e
movl $0x5, %edx
callq 0x6320
cmpl $0x0, %eax
jne 0x82955
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x8, %rax
movq %rax, -0x3130(%rbp)
jmp 0x82968
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x3, %rax
movq %rax, -0x3130(%rbp)
movq -0x3130(%rbp), %rax
movq %rax, -0x3118(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x28(%rax)
je 0x829b7
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x28(%rax), %rdi
callq 0x9d380
cmpl $0x0, %eax
je 0x829a7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x82c9a
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq $0x0, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x3118(%rbp), %rsi
xorl %edx, %edx
callq 0x82e50
movq %rax, -0x3110(%rbp)
cmpq $0x0, %rax
jne 0x829e6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x82c9a
movq -0x10(%rbp), %rdi
callq 0x83600
movq -0x3110(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x28(%rax)
je 0x82a3f
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x28(%rax), %rdi
callq 0x9d380
cmpl $0x0, %eax
je 0x82a2f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x82c9a
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq $0x0, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x20(%rax)
jne 0x82a5a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x82c9a
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x80e30
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x82a86
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x82c9a
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x10(%rax)
je 0x82aac
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x82ab8
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x82c9a
movl $0x0, -0x4(%rbp)
jmp 0x82c9a
cmpq $0x0, -0x3048(%rbp)
je 0x82c93
movq -0x3048(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x82c93
leaq -0x1040(%rbp), %rdi
movq -0x3048(%rbp), %rsi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdx
addq $0x3, %rdx
callq 0x813b0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1fc(%rax)
je 0x82b30
movq 0x824b8(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
leaq -0x1040(%rbp), %rdx
leaq 0x606eb(%rip), %rsi     # 0xe3214
movb $0x0, %al
callq 0x6a80
leaq -0x1040(%rbp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
callq 0x81600
movl $0x0, -0x3124(%rbp)
leaq -0x2040(%rbp), %rdi
leaq -0x1040(%rbp), %rdx
movl -0x3124(%rbp), %ecx
leaq 0x606b9(%rip), %rsi     # 0xe321f
movb $0x0, %al
callq 0x6160
movl -0x3124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3124(%rbp)
leaq -0x2040(%rbp), %rdi
leaq 0x6069f(%rip), %rsi     # 0xe3229
callq 0x6860
movq %rax, -0x3120(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x3120(%rbp)
movb %al, -0x3131(%rbp)
jne 0x82bb9
callq 0x6060
cmpl $0x11, (%rax)
sete %al
movb %al, -0x3131(%rbp)
movb -0x3131(%rbp), %al
testb $0x1, %al
jne 0x82b4b
cmpq $0x0, -0x3120(%rbp)
jne 0x82be5
leaq -0x2040(%rbp), %rdi
callq 0x6590
movl $0x0, -0x4(%rbp)
jmp 0x82c9a
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x3140(%rbp)
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x3120(%rbp), %rcx
movl $0x1, %esi
callq 0x6e80
movq %rax, %rcx
movq -0x3140(%rbp), %rax
cmpq %rcx, %rax
je 0x82c30
leaq -0x1040(%rbp), %rdi
callq 0x6590
movq -0x3120(%rbp), %rdi
callq 0x80ba0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x82c55
leaq -0x2040(%rbp), %rdi
callq 0x6310
jmp 0x82c91
leaq -0x2040(%rbp), %rdi
movl $0x124, %esi            # imm = 0x124
callq 0x6640
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x82c83
leaq -0x2040(%rbp), %rdi
leaq -0x1040(%rbp), %rsi
callq 0x6420
jmp 0x82c8f
leaq -0x2040(%rbp), %rdi
callq 0x6310
jmp 0x82c91
jmp 0x82c93
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x3140, %rsp           # imm = 0x3140
popq %rbp
retq
nopw %cs:(%rax,%rax)