// File: /workspace/github_cpp_makefile_star2_O3/karel-brinda[P]ococo/ext/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);
		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 {
        fprintf(stderr, "Must specify either r, w or a for mode\n");
        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, 0x10(%rsp)
movq %rsi, %r14
movq %rdi, 0x18(%rsp)
movq %rsi, %rdi
movl $0x72, %esi
callq 0x4200
xorl %r13d, %r13d
testq %rax, %rax
setne %r13b
movq %r14, %rdi
movl $0x77, %esi
callq 0x4200
leal 0x12(%r13), %r12d
movq %rax, 0x20(%rsp)
testq %rax, %rax
cmovel %r13d, %r12d
movq %r14, %rdi
movl $0x61, %esi
callq 0x4200
movq %rax, %rbx
movl %r12d, %r15d
orl $0x6, %r15d
testq %rax, %rax
setne 0xf(%rsp)
cmovel %r12d, %r15d
movq %r14, %rdi
movl $0x62, %esi
callq 0x4200
leal 0x8(%r15), %ebp
testq %rax, %rax
cmovel %r15d, %ebp
movq %r14, %rdi
movl $0x78, %esi
callq 0x4200
movq %rax, %r12
movq %r14, %rdi
movl $0x2b, %esi
callq 0x4200
movl %ebp, %r15d
orl $0x3, %r15d
testq %rax, %rax
cmovel %ebp, %r15d
setne %cl
andb 0xf(%rsp), %cl
orb %cl, %r13b
cmpb $0x1, %r13b
jne 0x3a486
movq %rbx, %r13
movl $0x38, %edi
callq 0x4700
testq %rax, %rax
je 0x3a524
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0xc(%rax)
movups %xmm0, (%rax)
movups %xmm0, 0x20(%rax)
movq $0x0, 0x30(%rax)
movl $0x3, 0x1c(%rax)
testb $0x10, %r15b
jne 0x3a4c6
movq %r14, %rdx
addq $0x20, %rdx
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x3a539
movq %rax, 0x8(%r14)
movq 0x20(%r14), %rax
movq %rax, 0x10(%r14)
testq %r13, %r13
je 0x3a4fd
movl %r15d, %ecx
orl $0x20, %ecx
testq %r12, %r12
movq %rbx, (%r14)
cmovel %r15d, %ecx
movl %ecx, 0x1c(%r14)
jmp 0x3a4e8
orq %rbx, %rax
orq 0x20(%rsp), %rax
je 0x3a51f
movq %rbx, %r13
movl $0x38, %edi
callq 0x4700
testq %rax, %rax
je 0x3a524
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0xc(%rax)
movups %xmm0, (%rax)
movups %xmm0, 0x20(%rax)
movq $0x0, 0x30(%rax)
movl $0x3, 0x1c(%rax)
movq 0x10(%rsp), %rbx
movl %r15d, %eax
orl $0x20, %eax
testq %r12, %r12
cmovel %r15d, %eax
movq %rbx, (%r14)
movl %eax, 0x1c(%r14)
testq %r13, %r13
je 0x3a527
movq 0x20(%r14), %rax
movq %rax, 0x30(%r14)
movq %rbx, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x45b0
jmp 0x3a527
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x45b0
movl %r15d, %eax
orl $0x20, %eax
testq %r12, %r12
movq %rbx, (%r14)
cmovel %r15d, %eax
movl %eax, 0x1c(%r14)
jmp 0x3a527
callq 0x5955
xorl %r14d, %r14d
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq