// File: Matan-Vinkler[P]Basic-File-System/blkdev.cpp
// Function: BlockDeviceSimulator::BlockDeviceSimulator(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
BlockDeviceSimulator::BlockDeviceSimulator(std::string fname) {

	// if file doesn't exist, create it
	if (access(fname.c_str(), F_OK) == -1) {
		fd = open(fname.c_str(), O_CREAT | O_RDWR | O_EXCL, 0664);
		if (fd == -1)
			throw std::runtime_error(
				std::string("open-create failed: ") + strerror(errno));

		if (lseek(fd, DEVICE_SIZE-1, SEEK_SET) == -1)
			throw std::runtime_error("Could not seek");

		::write(fd, "\0", 1);
	} else {
		fd = open(fname.c_str(), O_RDWR);
		if (fd == -1) {
			throw std::runtime_error(
				std::string("open failed: ") + strerror(errno));
		}
	}

	filemap = (unsigned char *)mmap(NULL, DEVICE_SIZE, PROT_READ | PROT_WRITE,
				        MAP_SHARED, fd, 0);
	if (filemap == (unsigned char *)-1)
		throw std::runtime_error(strerror(errno));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rsi, -0xc8(%rbp)
movq %rdi, %rax
movq -0xc8(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
movq %rax, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb8(%rbp)
callq 0x3110
movq %rax, %rdi
xorl %esi, %esi
callq 0x33f0
cmpl $-0x1, %eax
jne 0x40f5
movq -0xc0(%rbp), %rdi
callq 0x3110
movq %rax, %rdi
movl $0xc2, %esi
movl $0x1b4, %edx            # imm = 0x1B4
movb $0x0, %al
callq 0x3200
movl %eax, %ecx
movq -0xb8(%rbp), %rax
movl %ecx, (%rax)
cmpl $-0x1, (%rax)
jne 0x405f
movb $0x1, -0x65(%rbp)
movl $0x10, %edi
callq 0x3160
movq %rax, -0xd8(%rbp)
leaq -0x51(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x3440
movq -0xd0(%rbp), %rdx
leaq 0x505f(%rip), %rsi      # 0x9004
leaq -0x50(%rbp), %rdi
callq 0x4400
jmp 0x3fb0
callq 0x3040
movl (%rax), %edi
callq 0x30a0
movq %rax, %rdx
leaq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x43b0
jmp 0x3fce
movq -0xd8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x33d0
jmp 0x3fe0
movq -0xd8(%rbp), %rdi
movb $0x0, -0x65(%rbp)
movq 0x8ffe(%rip), %rsi      # 0xcff0
movq 0x8fbf(%rip), %rdx      # 0xcfb8
callq 0x3400
jmp 0x42f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x403d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x4034
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x34d8
leaq -0x50(%rbp), %rdi
callq 0x34d8
leaq -0x51(%rbp), %rdi
callq 0x32f0
testb $0x1, -0x65(%rbp)
jne 0x404e
jmp 0x405a
movq -0xd8(%rbp), %rdi
callq 0x31f0
jmp 0x42e7
movq -0xb8(%rbp), %rax
movl (%rax), %edi
movl $0xfffff, %esi          # imm = 0xFFFFF
xorl %edx, %edx
callq 0x3480
cmpq $-0x1, %rax
jne 0x40d6
movl $0x10, %edi
callq 0x3160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xe0(%rbp)
leaq 0x4f81(%rip), %rsi      # 0x9019
callq 0x30f0
jmp 0x409f
movq -0xe0(%rbp), %rdi
movq 0x8f43(%rip), %rsi      # 0xcff0
movq 0x8f04(%rip), %rdx      # 0xcfb8
callq 0x3400
movq -0xe0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x31f0
jmp 0x42e7
movq -0xb8(%rbp), %rax
movl (%rax), %edi
leaq 0x4f7a(%rip), %rsi      # 0x9060
movl $0x1, %edx
callq 0x30c0
jmp 0x4234
movq -0xc0(%rbp), %rdi
callq 0x3110
movq %rax, %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x3200
movl %eax, %ecx
movq -0xb8(%rbp), %rax
movl %ecx, (%rax)
cmpl $-0x1, (%rax)
jne 0x4232
movb $0x1, -0xaa(%rbp)
movl $0x10, %edi
callq 0x3160
movq %rax, -0xf0(%rbp)
leaq -0xa9(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x3440
movq -0xe8(%rbp), %rdx
leaq 0x4ecb(%rip), %rsi      # 0x9028
leaq -0xa8(%rbp), %rdi
callq 0x4400
jmp 0x416b
callq 0x3040
movl (%rax), %edi
callq 0x30a0
movq %rax, %rdx
leaq -0x88(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x43b0
jmp 0x418f
movq -0xf0(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x33d0
jmp 0x41a4
movq -0xf0(%rbp), %rdi
movb $0x0, -0xaa(%rbp)
movq 0x8e37(%rip), %rsi      # 0xcff0
movq 0x8df8(%rip), %rdx      # 0xcfb8
callq 0x3400
jmp 0x42f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x420a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x41fe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x34d8
leaq -0xa8(%rbp), %rdi
callq 0x34d8
leaq -0xa9(%rbp), %rdi
callq 0x32f0
testb $0x1, -0xaa(%rbp)
jne 0x4221
jmp 0x422d
movq -0xf0(%rbp), %rdi
callq 0x31f0
jmp 0x42e7
jmp 0x4234
movq -0xb8(%rbp), %rax
movl (%rax), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x100000, %esi         # imm = 0x100000
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x3450
movq %rax, %rcx
movq -0xb8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq $-0x1, %rcx
cmpq %rcx, 0x8(%rax)
jne 0x42de
movl $0x10, %edi
callq 0x3160
movq %rax, -0xf8(%rbp)
movq %rax, -0x100(%rbp)
callq 0x3040
movl (%rax), %edi
callq 0x30a0
movq -0xf8(%rbp), %rdi
movq %rax, %rsi
callq 0x30f0
jmp 0x42aa
movq -0x100(%rbp), %rdi
movq 0x8d38(%rip), %rsi      # 0xcff0
movq 0x8cf9(%rip), %rdx      # 0xcfb8
callq 0x3400
movq -0x100(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x31f0
jmp 0x42e7
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x3430