// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %rdi
xorl %esi, %esi
callq 0x22b0
movq (%r14), %rdi
cmpl $-0x1, %eax
je 0x29d5
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x2180
movl %eax, %r8d
movl %eax, (%rbx)
cmpl $-0x1, %eax
jne 0x2a19
pushq $0x10
popq %rdi
callq 0x2110
movq %rax, %rbx
leaq 0x26a9(%rip), %rsi      # 0x5028
leaq -0x40(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x2bc6
callq 0x2030
movl (%rax), %edi
callq 0x2080
leaq -0x60(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movq %rax, %rdx
callq 0x2b9c
movb $0x1, %r15b
leaq -0x60(%rbp), %rsi
movq %rbx, %rdi
callq 0x2290
xorl %r15d, %r15d
movq 0x562f(%rip), %rsi      # 0x7ff0
movq 0x55f0(%rip), %rdx      # 0x7fb8
movq %rbx, %rdi
callq 0x22c0
jmp 0x2aca
movl $0xc2, %esi
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x2180
movl %eax, (%rbx)
cmpl $-0x1, %eax
je 0x2a67
movl $0xfffff, %esi          # imm = 0xFFFFF
movl %eax, %edi
xorl %edx, %edx
callq 0x2320
cmpq $-0x1, %rax
je 0x2aca
movl (%rbx), %edi
leaq 0x2652(%rip), %rsi      # 0x5060
pushq $0x1
popq %rdx
callq 0x2090
movl (%rbx), %r8d
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x100000, %esi         # imm = 0x100000
xorl %edi, %edi
xorl %r9d, %r9d
callq 0x22f0
movq %rax, 0x8(%rbx)
cmpq $-0x1, %rax
je 0x2a43
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x2110
movq %rax, %rbx
callq 0x2030
movl (%rax), %edi
callq 0x2080
movq %rbx, %rdi
movq %rax, %rsi
callq 0x20b0
jmp 0x2ae4
pushq $0x10
popq %rdi
callq 0x2110
movq %rax, %rbx
leaq 0x258b(%rip), %rsi      # 0x5004
leaq -0x40(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x2bc6
callq 0x2030
movl (%rax), %edi
callq 0x2080
leaq -0x60(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movq %rax, %rdx
callq 0x2b9c
movb $0x1, %r15b
leaq -0x60(%rbp), %rsi
movq %rbx, %rdi
callq 0x2290
xorl %r15d, %r15d
movq 0x5535(%rip), %rsi      # 0x7ff0
movq 0x54f6(%rip), %rdx      # 0x7fb8
movq %rbx, %rdi
callq 0x22c0
pushq $0x10
popq %rdi
callq 0x2110
movq %rax, %rbx
leaq 0x253d(%rip), %rsi      # 0x5019
movq %rax, %rdi
callq 0x20b0
movq 0x5505(%rip), %rsi      # 0x7ff0
movq 0x54c6(%rip), %rdx      # 0x7fb8
movq %rbx, %rdi
callq 0x22c0
jmp 0x2afe
jmp 0x2b0c
movq %rax, %r14
leaq -0x60(%rbp), %rdi
callq 0x2368
jmp 0x2b12
movq %rax, %r14
movb $0x1, %r15b
leaq -0x40(%rbp), %rdi
callq 0x2368
testb %r15b, %r15b
jne 0x2b2b
jmp 0x2b33
jmp 0x2b28
jmp 0x2b28
jmp 0x2b28
movq %rax, %r14
movq %rbx, %rdi
callq 0x2170
movq %r14, %rdi
callq 0x22e0
nop