// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %rdi
xorl %esi, %esi
callq 0x22c0
movq (%r14), %rdi
cmpl $-0x1, %eax
je 0x303c
movl $0x2, %esi
xorl %eax, %eax
callq 0x21a0
movl %eax, (%rbx)
cmpl $-0x1, %eax
jne 0x3083
movl $0x10, %edi
callq 0x2120
movq %rax, %rbx
leaq -0x58(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x303c(%rip), %rsi      # 0x6028
leaq 0x3042(%rip), %rdx      # 0x6035
leaq -0x68(%rbp), %rdi
callq 0x32e2
callq 0x2040
movl (%rax), %edi
callq 0x20a0
leaq -0x68(%rbp), %rdi
movq %rax, %rsi
callq 0x2350
leaq -0x38(%rbp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x317f
movups (%rcx), %xmm0
movups %xmm0, (%r13)
jmp 0x318a
movl $0xc2, %esi
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x21a0
movl %eax, (%rbx)
cmpl $-0x1, %eax
je 0x30e1
movl $0xfffff, %esi          # imm = 0xFFFFF
movl %eax, %edi
xorl %edx, %edx
callq 0x2340
cmpq $-0x1, %rax
je 0x314d
movl (%rbx), %edi
leaq 0x2fe7(%rip), %rsi      # 0x6060
movl $0x1, %edx
callq 0x20b0
movl (%rbx), %r8d
movl $0x100000, %esi         # imm = 0x100000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
xorl %r9d, %r9d
callq 0x2300
movq %rax, 0x8(%rbx)
cmpq $-0x1, %rax
je 0x30b8
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x2120
movq %rax, %rbx
callq 0x2040
movl (%rax), %edi
callq 0x20a0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x20d0
jmp 0x3169
movl $0x10, %edi
callq 0x2120
movq %rax, %rbx
leaq -0x58(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x2f07(%rip), %rsi      # 0x6004
leaq 0x2f14(%rip), %rdx      # 0x6018
leaq -0x68(%rbp), %rdi
callq 0x32e2
callq 0x2040
movl (%rax), %edi
callq 0x20a0
leaq -0x68(%rbp), %rdi
movq %rax, %rsi
callq 0x2350
leaq -0x38(%rbp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x31cb
movups (%rcx), %xmm0
movups %xmm0, (%r13)
jmp 0x31d6
movl $0x10, %edi
callq 0x2120
movq %rax, %rbx
leaq 0x2eb8(%rip), %rsi      # 0x6019
movq %rax, %rdi
callq 0x20d0
movq 0x5e80(%rip), %rsi      # 0x8ff0
movq 0x5e41(%rip), %rdx      # 0x8fb8
movq %rbx, %rdi
callq 0x22d0
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
movq 0x8(%rax), %rdx
leaq -0x48(%rbp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movb $0x1, %r12b
movq %rbx, %rdi
callq 0x22a0
xorl %r12d, %r12d
movq 0x5e36(%rip), %rsi      # 0x8ff0
movq 0x5df7(%rip), %rdx      # 0x8fb8
movq %rbx, %rdi
callq 0x22d0
jmp 0x3215
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
movq 0x8(%rax), %rdx
leaq -0x48(%rbp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movb $0x1, %r12b
movq %rbx, %rdi
callq 0x22a0
xorl %r12d, %r12d
movq 0x5dea(%rip), %rsi      # 0x8ff0
movq 0x5dab(%rip), %rdx      # 0x8fb8
movq %rbx, %rdi
callq 0x22d0
jmp 0x3219
jmp 0x3233
movq %rax, %r14
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0x3239
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2220
jmp 0x3239
movq %rax, %r14
movb $0x1, %r12b
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
je 0x325a
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2220
jmp 0x325a
jmp 0x3261
jmp 0x3254
movq %rax, %r14
movb $0x1, %r12b
testb %r12b, %r12b
jne 0x3264
jmp 0x326c
movq %rax, %r14
movq %rbx, %rdi
callq 0x2190
movq %r14, %rdi
callq 0x22f0