// File: /workspace/github_cpp_makefile_star2_O3/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 0x303f
movl $0x2, %esi
xorl %eax, %eax
callq 0x21a0
movl %eax, %r8d
movl %eax, (%rbx)
cmpl $-0x1, %eax
jne 0x3089
movl $0x10, %edi
callq 0x2120
movq %rax, %r14
leaq -0x38(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x3039(%rip), %rsi      # 0x6028
leaq 0x303f(%rip), %rdx      # 0x6035
leaq -0x48(%rbp), %rdi
callq 0x32f2
callq 0x2040
movl (%rax), %edi
callq 0x20a0
leaq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x2350
leaq -0x58(%rbp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x3182
movups (%rcx), %xmm0
movups %xmm0, (%r13)
jmp 0x318d
movl $0xc2, %esi
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x21a0
movl %eax, (%rbx)
cmpl $-0x1, %eax
je 0x30e4
movl $0xfffff, %esi          # imm = 0xFFFFF
movl %eax, %edi
xorl %edx, %edx
callq 0x2340
cmpq $-0x1, %rax
je 0x3150
movl (%rbx), %edi
leaq 0x2fe4(%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 0x30bb
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x2120
movq %rax, %r14
callq 0x2040
movl (%rax), %edi
callq 0x20a0
movq %r14, %rdi
movq %rax, %rsi
callq 0x20d0
jmp 0x316c
movl $0x10, %edi
callq 0x2120
movq %rax, %r14
leaq -0x38(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x2f04(%rip), %rsi      # 0x6004
leaq 0x2f11(%rip), %rdx      # 0x6018
leaq -0x48(%rbp), %rdi
callq 0x32f2
callq 0x2040
movl (%rax), %edi
callq 0x20a0
leaq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x2350
leaq -0x58(%rbp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x31ce
movups (%rcx), %xmm0
movups %xmm0, (%r13)
jmp 0x31d9
movl $0x10, %edi
callq 0x2120
movq %rax, %r14
leaq 0x2eb5(%rip), %rsi      # 0x6019
movq %rax, %rdi
callq 0x20d0
movq 0x5e7d(%rip), %rsi      # 0x8ff0
movq 0x5e3e(%rip), %rdx      # 0x8fb8
movq %r14, %rdi
callq 0x22d0
movq %rdx, -0x68(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x58(%rbp)
movq 0x8(%rax), %rdx
leaq -0x68(%rbp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movb $0x1, %r12b
movq %r14, %rdi
callq 0x22a0
xorl %r12d, %r12d
movq 0x5e33(%rip), %rsi      # 0x8ff0
movq 0x5df4(%rip), %rdx      # 0x8fb8
movq %r14, %rdi
callq 0x22d0
jmp 0x3218
movq %rdx, -0x68(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x58(%rbp)
movq 0x8(%rax), %rdx
leaq -0x68(%rbp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movb $0x1, %r12b
movq %r14, %rdi
callq 0x22a0
xorl %r12d, %r12d
movq 0x5de7(%rip), %rsi      # 0x8ff0
movq 0x5da8(%rip), %rdx      # 0x8fb8
movq %r14, %rdi
callq 0x22d0
jmp 0x321c
jmp 0x3250
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
je 0x3234
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2220
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
je 0x3249
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2220
testb %r12b, %r12b
jne 0x3273
jmp 0x327b
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
je 0x3273
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2220
jmp 0x3273
jmp 0x3270
jmp 0x3270
jmp 0x3270
movq %rax, %rbx
movq %r14, %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x22f0
nop