// File: /workspace/github_c_makefile_star2_O1/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 0x5662(%rip), %xmm0     # 0x1e2c0
leaq 0x20(%rsp), %rdx
movaps %xmm0, (%rdx)
movl $0x0, 0x10(%rdx)
movups 0x563c(%rip), %xmm0     # 0x1e2b0
movaps %xmm0, 0x10(%rsp)
movups 0x5620(%rip), %xmm0     # 0x1e2a0
movaps %xmm0, (%rsp)
movl 0xc0(%rbx), %ebp
movl $0x3b6b, %esi           # imm = 0x3B6B
movl %ebp, %edi
xorl %eax, %eax
callq 0xd190
movl %eax, %r14d
callq 0x18f62
testl %r14d, %r14d
je 0x18ccf
leaq 0x1b224(%rip), %rcx     # 0x33ed0
movq (%rcx), %r8
leaq 0x51d4(%rip), %rdx      # 0x1de8a
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
movq %rax, %rcx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x19ca8
jmp 0x18e7e
leaq 0x1b1fa(%rip), %r12     # 0x33ed0
movq 0x18(%r12), %r8
movl 0x28(%rsp), %r10d
movl 0x2c(%rsp), %r11d
leaq 0x51cb(%rip), %rdx      # 0x1deb7
leaq 0x51f5(%rip), %r9       # 0x1dee8
movl $0x3, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x19ca8
addq $0x10, %rsp
movl 0x28(%rsp), %eax
movl %eax, 0xcc(%rbx)
movl $0x18, %edi
callq 0xd280
movq %rax, %r14
callq 0x18f62
testq %r14, %r14
je 0x18e44
movq 0x18(%r12), %r8
leaq 0x5506(%rip), %rdx      # 0x1e243
leaq 0x5538(%rip), %r9       # 0x1e27c
xorl %r15d, %r15d
movl $0x3, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x19ca8
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 0x18d86
cmpl $0x4, %ecx
jne 0x18d8c
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 0x18d76
movb $0x0, (%rax)
callq 0x18f62
movq 0x18(%r12), %r8
subq $0x8, %rsp
leaq 0x48(%rsp), %r10
leaq 0x5498(%rip), %rdx      # 0x1e28b
leaq 0x5482(%rip), %r9       # 0x1e27c
movl $0x3, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
pushq %r10
callq 0x19ca8
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 0x18f62
testl %r15d, %r15d
je 0x18e8e
movq (%r12), %r8
leaq 0x50bc(%rip), %rdx      # 0x1defe
jmp 0x18e4f
movq (%r12), %r8
leaq 0x53d5(%rip), %rdx      # 0x1e224
xorl %edi, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x19ca8
callq 0x18f62
movq (%r12), %r8
leaq 0x5091(%rip), %rdx      # 0x1defe
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x19ca8
movq %r14, %rax
addq $0x70, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%r12), %r8
leaq 0x268a(%rip), %rdx      # 0x1b524
leaq 0x53db(%rip), %r9       # 0x1e27c
movl $0x3, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x19ca8
movq %rsp, %rdx
movl $0x0, 0x8(%rdx)
movl $0x3b6c, %esi           # imm = 0x3B6C
movl %ebp, %edi
xorl %eax, %eax
callq 0xd190
testl %eax, %eax
je 0x18ef2
callq 0x18f62
movq (%r12), %r8
movl 0x8(%rsp), %r9d
leaq 0x5052(%rip), %rdx      # 0x1df35
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
movq %rax, %rcx
jmp 0x18cc3
testb $0x4, 0x4(%rsp)
jne 0x18efe
xorl %r14d, %r14d
jmp 0x18f25
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 0x18f32
movq %rbx, %rdi
callq 0x18168
jmp 0x18e7e
callq 0x18f62
movq (%r12), %r8
leaq 0x5018(%rip), %rdx      # 0x1df5a
jmp 0x18e6d