// File: /workspace/github_c_makefile_star2_O3/intel[P]pf-bb-config/bb_acc_vfio.c
// Function: vfio_get_bar0_mapping
void *vfio_get_bar0_mapping(const char *pci_addr,
			unsigned int bar_size, void *dev)
{
	int vfio_dev_fd;
	void *map = NULL;
	hw_device *hwd = (hw_device *)dev;

	struct vfio_device_info device_info = {
		.argsz = sizeof(device_info)
	};

	struct vfio_region_info region_info = {
		.argsz = sizeof(region_info)
	};

	vfio_dev_fd = hwd->vfio_dev_fd;

	if (ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, &device_info)) {
		LOG(ERR, "Failed to get device info, dev fd: %d",
				vfio_dev_fd);
		return NULL;
	}
	LOG(DEBUG, "Device supports %d regions, %d irqs",
			device_info.num_regions, device_info.num_irqs);
	hwd->num_regions = device_info.num_regions;

	/* Configure VFIO token */
	if (vfio_set_token(hwd, vfio_dev_fd) < 0) {
		LOG(ERR, "Fail to set VFIO_DEVICE_FEATURE with UUID token");
		return NULL;
	}

	/* Map BAR0 region */
	region_info.index = 0;
	if (ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO,
			&region_info)) {
		LOG(ERR, "Failed to get region(%d) info",
				region_info.index);
		return NULL;
	}

	if (region_info.flags & VFIO_REGION_INFO_FLAG_MMAP) {
		map = mmap(NULL, (size_t)region_info.size,
				PROT_READ | PROT_WRITE, MAP_SHARED, vfio_dev_fd,
				(off_t)region_info.offset);
		if (map == MAP_FAILED) {
			LOG(ERR, "mmap failed for BAR0 space");
			return NULL;
		}
	}

	/* Enable BME */
	vfio_set_bus_master(hwd);

	return map;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x70, %rsp
movq %rdx, %rbx
movups 0x59e4(%rip), %xmm0     # 0x1e320
leaq 0x20(%rsp), %rdx
movaps %xmm0, (%rdx)
movl $0x0, 0x10(%rdx)
movups 0x59be(%rip), %xmm0     # 0x1e310
movaps %xmm0, 0x10(%rsp)
movups 0x59a2(%rip), %xmm0     # 0x1e300
movaps %xmm0, (%rsp)
movl 0xc0(%rbx), %ebp
movl $0x3b6b, %esi           # imm = 0x3B6B
movl %ebp, %edi
xorl %eax, %eax
callq 0xd190
movl %eax, %r14d
callq 0x18c40
testl %r14d, %r14d
je 0x189ad
leaq 0x1b546(%rip), %rcx     # 0x33ed0
movq (%rcx), %r8
leaq 0x5555(%rip), %rdx      # 0x1dee9
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
movq %rax, %rcx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x199b4
jmp 0x18b5c
leaq 0x1b51c(%rip), %r12     # 0x33ed0
movq 0x18(%r12), %r8
movl 0x28(%rsp), %r10d
movl 0x2c(%rsp), %r11d
leaq 0x554c(%rip), %rdx      # 0x1df16
leaq 0x5576(%rip), %r9       # 0x1df47
movl $0x3, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x199b4
addq $0x10, %rsp
movl 0x28(%rsp), %eax
movl %eax, 0xcc(%rbx)
movl $0x18, %edi
callq 0xd270
movq %rax, %r14
callq 0x18c40
testq %r14, %r14
je 0x18b22
movq 0x18(%r12), %r8
leaq 0x5887(%rip), %rdx      # 0x1e2a2
leaq 0x58b9(%rip), %r9       # 0x1e2db
xorl %r15d, %r15d
movl $0x3, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x199b4
movabsq $0x2000000000018, %rax  # imm = 0x2000000000018
movq %rax, (%r14)
movups 0xdc(%rbx), %xmm0
movups %xmm0, 0x8(%r14)
leaq 0x40(%rsp), %rax
movl %r15d, %ecx
andl $0xd, %ecx
cmpl $0x8, %ecx
je 0x18a64
cmpl $0x4, %ecx
jne 0x18a6a
movb $0x2d, (%rax)
incq %rax
movb 0x8(%r14,%r15), %cl
movl %ecx, %edx
shrb $0x4, %dl
leal 0x30(%rdx), %esi
addb $0x57, %dl
cmpb $-0x60, %cl
movzbl %sil, %ecx
movzbl %dl, %edx
cmovbl %ecx, %edx
movb %dl, (%rax)
movb 0x8(%r14,%r15), %cl
andb $0xf, %cl
leal 0x30(%rcx), %edx
leal 0x57(%rcx), %esi
cmpb $0xa, %cl
movzbl %dl, %ecx
movzbl %sil, %edx
cmovbl %ecx, %edx
movb %dl, 0x1(%rax)
addq $0x2, %rax
incq %r15
cmpq $0x10, %r15
jne 0x18a54
movb $0x0, (%rax)
callq 0x18c40
movq 0x18(%r12), %r8
subq $0x8, %rsp
leaq 0x48(%rsp), %r10
leaq 0x5819(%rip), %rdx      # 0x1e2ea
leaq 0x5803(%rip), %r9       # 0x1e2db
movl $0x3, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
pushq %r10
callq 0x199b4
addq $0x10, %rsp
movl $0x3b75, %esi           # imm = 0x3B75
movl %ebp, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0xd190
movl %eax, %r15d
movq %r14, %rdi
callq 0xd030
callq 0x18c40
testl %r15d, %r15d
je 0x18b6c
movq (%r12), %r8
leaq 0x543d(%rip), %rdx      # 0x1df5d
jmp 0x18b2d
movq (%r12), %r8
leaq 0x5756(%rip), %rdx      # 0x1e283
xorl %edi, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x199b4
callq 0x18c40
movq (%r12), %r8
leaq 0x5412(%rip), %rdx      # 0x1df5d
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x199b4
movq %r14, %rax
addq $0x70, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%r12), %r8
leaq 0x29ac(%rip), %rdx      # 0x1b524
leaq 0x575c(%rip), %r9       # 0x1e2db
movl $0x3, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x199b4
movq %rsp, %rdx
movl $0x0, 0x8(%rdx)
movl $0x3b6c, %esi           # imm = 0x3B6C
movl %ebp, %edi
xorl %eax, %eax
callq 0xd190
testl %eax, %eax
je 0x18bd0
callq 0x18c40
movq (%r12), %r8
movl 0x8(%rsp), %r9d
leaq 0x53d3(%rip), %rdx      # 0x1df94
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
movq %rax, %rcx
jmp 0x189a1
testb $0x4, 0x4(%rsp)
jne 0x18bdc
xorl %r14d, %r14d
jmp 0x18c03
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %r9
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebp, %r8d
callq 0xd120
movq %rax, %r14
cmpq $-0x1, %rax
je 0x18c10
movq %rbx, %rdi
callq 0x17e8b
jmp 0x18b5c
callq 0x18c40
movq (%r12), %r8
leaq 0x5399(%rip), %rdx      # 0x1dfb9
jmp 0x18b4b