// File: MITRECND[P]snugglefish/src/file.cpp
// Function: snugglefish::file::open(char)
bool file::open(char readwrite){
    switch (readwrite){
        case 'r':
        {    
            this->fd = ::open(this->filename, O_RDONLY);
            this->size = this->get_size();
            this->readonly = true;
            break;
        }
        case 'w':
        {
            if (this->bufferparam > 0){
                this->buffersize = this->bufferparam;
                this->buffer = (char*) malloc(buffersize);
            }
            this->fd = ::open(this->filename, O_RDWR);
            this->readonly = false;
            break;
        }
        default:
            throw runtime_error("Unrecognized read/write mode");
            break;
    }

    if (this->fd <= 0){
        cerr << "Error Opening File: " << this->filename << " -- Error: " << strerror(errno) << endl;
        throw runtime_error("Opening File");
    }

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movb %sil, %al
movq %rdi, -0x8(%rbp)
movb %al, -0x9(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x28(%rbp)
movsbl -0x9(%rbp), %eax
movl %eax, -0x20(%rbp)
subl $0x72, %eax
je 0x17eb2
jmp 0x17ea8
movl -0x20(%rbp), %eax
subl $0x77, %eax
je 0x17ee2
jmp 0x17f2f
movq -0x28(%rbp), %rax
movq (%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0xe240
movq -0x28(%rbp), %rdi
movl %eax, 0xc(%rdi)
callq 0x18050
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movb $0x1, 0x8(%rax)
jmp 0x17f82
movq -0x28(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jbe 0x17f0d
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rcx
movq %rcx, 0x28(%rax)
movq 0x28(%rax), %rdi
callq 0xe440
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0xe240
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xc(%rax)
movb $0x0, 0x8(%rax)
jmp 0x17f82
movl $0x10, %edi
callq 0xe170
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x30(%rbp)
leaq 0x283c(%rip), %rsi      # 0x1a786
callq 0xe100
jmp 0x17f51
movq -0x30(%rbp), %rdi
movq 0x9094(%rip), %rsi      # 0x20ff0
movq 0x9045(%rip), %rdx      # 0x20fa8
callq 0xe5b0
movq -0x30(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0xe230
jmp 0x18046
movq -0x28(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jg 0x1803c
movq 0x9051(%rip), %rdi      # 0x20fe8
leaq 0x2805(%rip), %rsi      # 0x1a7a3
callq 0xe2f0
movq %rax, %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rsi
callq 0xe2f0
movq %rax, %rdi
leaq 0x20ec(%rip), %rsi      # 0x1a0a8
callq 0xe2f0
movq %rax, -0x40(%rbp)
callq 0xe050
movl (%rax), %edi
callq 0xe0d0
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0xe2f0
movq %rax, %rdi
movq 0x8fa9(%rip), %rsi      # 0x20f90
callq 0xe340
movl $0x10, %edi
callq 0xe170
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x38(%rbp)
leaq 0x27b1(%rip), %rsi      # 0x1a7b8
callq 0xe100
jmp 0x1800e
movq -0x38(%rbp), %rdi
movq 0x8fd7(%rip), %rsi      # 0x20ff0
movq 0x8f88(%rip), %rdx      # 0x20fa8
callq 0xe5b0
movq -0x38(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0xe230
jmp 0x18046
movb $0x1, %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq
movq -0x18(%rbp), %rdi
callq 0xe5d0
nop