// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movl $0x72, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x561a2
movl $0x1, -0x2c(%rbp)
movl -0x40(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movl $0x77, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x561c6
movl $0x1, -0x30(%rbp)
movl -0x40(%rbp), %eax
orl $0x12, %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movl $0x61, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x561f1
movl $0x1, -0x34(%rbp)
movl $0x1, -0x30(%rbp)
movl -0x40(%rbp), %eax
orl $0x6, %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movl $0x62, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x56215
movl $0x1, -0x38(%rbp)
movl -0x40(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movl $0x78, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x56230
movl $0x1, -0x3c(%rbp)
movq -0x18(%rbp), %rdi
movl $0x2b, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x56263
movl $0x1, -0x30(%rbp)
movl -0x40(%rbp), %eax
orl $0x3, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x56261
movl $0x1, -0x2c(%rbp)
jmp 0x56263
cmpl $0x0, -0x2c(%rbp)
je 0x562f5
xorl %eax, %eax
movl %eax, %edi
xorl %esi, %esi
callq 0x55ec0
movq %rax, -0x28(%rbp)
xorl %eax, %eax
cmpq -0x28(%rbp), %rax
jne 0x56291
movq $0x0, -0x8(%rbp)
jmp 0x5639a
movl -0x40(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0x562f3
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x562f1
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
addq $0x20, %rdx
movl -0x38(%rbp), %ecx
callq 0x563b0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
cmpl $0x0, -0x34(%rbp)
jne 0x562ef
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x4650
jmp 0x562f1
jmp 0x562f3
jmp 0x56342
cmpl $0x0, -0x30(%rbp)
je 0x5631e
xorl %eax, %eax
movl %eax, %edi
xorl %esi, %esi
callq 0x55ec0
movq %rax, -0x28(%rbp)
xorl %eax, %eax
cmpq -0x28(%rbp), %rax
jne 0x5631c
movq $0x0, -0x8(%rbp)
jmp 0x5639a
jmp 0x56340
movq 0x33cab(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x2736a(%rip), %rsi     # 0x7d699
movb $0x0, %al
callq 0x4830
movq $0x0, -0x8(%rbp)
jmp 0x5639a
jmp 0x56342
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl -0x40(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x1c(%rax)
cmpl $0x0, -0x3c(%rbp)
je 0x5636a
movq -0x28(%rbp), %rax
movl 0x1c(%rax), %ecx
orl $0x20, %ecx
movl %ecx, 0x1c(%rax)
cmpl $0x0, -0x34(%rbp)
je 0x56392
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x4650
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)