// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/cram/mFILE.c
// Function: mfreopen
mFILE *mfreopen(const char *path, const char *mode_str, FILE *fp) {
    mFILE *mf;
    int r = 0, w = 0, a = 0, b = 0, x = 0, mode = 0;

    /* Parse mode:
     * r = read file contents (if truncated => don't read)
     * w = write on close
     * a = position at end of buffer
     * x = position at same location as the original fp, don't seek on flush
     * + = for update (read and write)
     * m = mmap (read only)
     */
    if (strchr(mode_str, 'r'))
        r = 1, mode |= MF_READ;
    if (strchr(mode_str, 'w'))
        w = 1, mode |= MF_WRITE | MF_TRUNC;
    if (strchr(mode_str, 'a'))
        w = a = 1, mode |= MF_WRITE | MF_APPEND;
    if (strchr(mode_str, 'b'))
        b = 1, mode |= MF_BINARY;
    if (strchr(mode_str, 'x'))
        x = 1;
    if (strchr(mode_str, '+')) {
        w = 1, mode |= MF_READ | MF_WRITE;
        if (a)
            r = 1;
    }
#ifdef HAVE_MMAP
    if (strchr(mode_str, 'm'))
        if (!w) mode |= MF_MMAP;
#endif

    if (r) {
        mf = mfcreate(NULL, 0);
        if (NULL == mf) return NULL;
        if (!(mode & MF_TRUNC)) {
#ifdef HAVE_MMAP
            if (mode & MF_MMAP) {
                if (mfmmap(mf, fp, path) == -1) {
                    mf->data = NULL;
                    mode &= ~MF_MMAP;
                }
            }
#endif
            if (!mf->data) {
                mf->data = mfload(fp, path, &mf->size, b);
                if (!mf->data) {
                    free(mf);
                    return NULL;
                }
                mf->alloced = mf->size;
                if (!a)
                    fseek(fp, 0, SEEK_SET);
            }
        }
    } else if (w) {
        /* Write - initialise the data structures */
        mf = mfcreate(NULL, 0);
        if (NULL == mf) return NULL;
    } else {
        hts_log_error("Must specify either r, w or a for mode");
        return NULL;
    }
    mf->fp = fp;
    mf->mode = mode;

    if (x) {
        mf->mode |= MF_MODEX;
    }

    if (a) {
        mf->flush_pos = mf->size;
        fseek(fp, 0, SEEK_END);
    }

    return mf;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, 0x8(%rsp)
movq %rsi, %rbp
movq %rdi, 0x10(%rsp)
pushq $0x72
popq %rsi
movq %rbp, %rdi
callq 0x4190
xorl %ebx, %ebx
testq %rax, %rax
setne %bl
pushq $0x77
popq %rsi
movq %rbp, %rdi
callq 0x4190
leal 0x12(%rbx), %r12d
movq %rax, 0x20(%rsp)
testq %rax, %rax
cmovel %ebx, %r12d
pushq $0x61
popq %rsi
movq %rbp, %rdi
callq 0x4190
movq %rax, %r15
movl %r12d, %r14d
orl $0x6, %r14d
testq %rax, %rax
setne %r13b
cmovel %r12d, %r14d
pushq $0x62
popq %rsi
movq %rbp, %rdi
callq 0x4190
leal 0x8(%r14), %r12d
testq %rax, %rax
cmovel %r14d, %r12d
pushq $0x78
popq %rsi
movq %rbp, %rdi
callq 0x4190
movq %rax, 0x18(%rsp)
pushq $0x2b
popq %rsi
movq %rbp, %rdi
callq 0x4190
movl %r12d, %r14d
orl $0x3, %r14d
testq %rax, %rax
cmovel %r12d, %r14d
setne %cl
andb %r13b, %cl
orb %cl, %bl
cmpb $0x1, %bl
jne 0x4807a
xorl %ebp, %ebp
xorl %edi, %edi
xorl %esi, %esi
callq 0x47e51
testq %rax, %rax
je 0x480ea
movq %rax, %r13
testb $0x10, %r14b
jne 0x48097
cmpq $0x0, 0x8(%r13)
jne 0x48097
movq %r13, %rdx
addq $0x20, %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x48108
movq %rax, 0x8(%r13)
testq %rax, %rax
je 0x480fc
movq 0x20(%r13), %rax
movq %rax, 0x10(%r13)
testq %r15, %r15
jne 0x48097
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4540
jmp 0x48097
orq %r15, %rax
orq 0x20(%rsp), %rax
je 0x480d0
xorl %ebp, %ebp
xorl %edi, %edi
xorl %esi, %esi
callq 0x47e51
movq %rax, %r13
testq %rax, %rax
je 0x480ea
movl %r14d, %eax
orl $0x20, %eax
cmpq $0x0, 0x18(%rsp)
cmovel %r14d, %eax
movq 0x8(%rsp), %rdi
movq %rdi, (%r13)
movl %eax, 0x1c(%r13)
testq %r15, %r15
je 0x480cb
movq 0x20(%r13), %rax
movq %rax, 0x30(%r13)
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x4540
movq %r13, %rbp
jmp 0x480ea
leaq 0x35658(%rip), %rsi     # 0x7d72f
leaq 0x3565a(%rip), %rdx     # 0x7d738
xorl %ebp, %ebp
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
movq %rbp, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0x45c0
xorl %ebp, %ebp
jmp 0x480ea