// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq 0x64af(%rip), %rax      # 0x262bc
movq %rax, -0x4c(%rbp)
movq 0x64ac(%rip), %rax      # 0x262c4
movq %rax, -0x44(%rbp)
movl 0x64aa(%rip), %eax      # 0x262cc
movl %eax, -0x3c(%rbp)
movq 0x64a4(%rip), %rax      # 0x262d0
movq %rax, -0x70(%rbp)
movq 0x64a1(%rip), %rax      # 0x262d8
movq %rax, -0x68(%rbp)
movq 0x649e(%rip), %rax      # 0x262e0
movq %rax, -0x60(%rbp)
movq 0x649b(%rip), %rax      # 0x262e8
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rax
movl 0xc0(%rax), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %edi
movl $0x3b6b, %esi           # imm = 0x3B6B
leaq -0x4c(%rbp), %rdx
movb $0x0, %al
callq 0xd180
cmpl $0x0, %eax
je 0x1feab
callq 0x20230
movq %rax, %rcx
leaq 0x1ba4b(%rip), %rax     # 0x3b8d0
movq (%rax), %r8
movl -0x24(%rbp), %r9d
xorl %esi, %esi
leaq 0x5ff6(%rip), %rdx      # 0x25e8b
movl %esi, %edi
movb $0x0, %al
callq 0x216e0
movq $0x0, -0x8(%rbp)
jmp 0x20005
callq 0x20230
movq %rax, %rcx
leaq 0x1ba16(%rip), %rax     # 0x3b8d0
movq 0x18(%rax), %r8
movl -0x44(%rbp), %r10d
movl -0x40(%rbp), %eax
movl $0x3, %edi
xorl %esi, %esi
leaq 0x5fe5(%rip), %rdx      # 0x25eb8
leaq 0x600f(%rip), %r9       # 0x25ee9
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x216e0
movl -0x44(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0xcc(%rax)
movq -0x38(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x20020
cmpl $0x0, %eax
jge 0x1ff38
callq 0x20230
movq %rax, %rcx
leaq 0x1b9ba(%rip), %rax     # 0x3b8d0
movq (%rax), %r8
xorl %esi, %esi
leaq 0x5fdd(%rip), %rdx      # 0x25eff
movl %esi, %edi
movb $0x0, %al
callq 0x216e0
movq $0x0, -0x8(%rbp)
jmp 0x20005
movl $0x0, -0x68(%rbp)
movl -0x24(%rbp), %edi
movl $0x3b6c, %esi           # imm = 0x3B6C
leaq -0x70(%rbp), %rdx
movb $0x0, %al
callq 0xd180
cmpl $0x0, %eax
je 0x1ff89
callq 0x20230
movq %rax, %rcx
leaq 0x1b96a(%rip), %rax     # 0x3b8d0
movq (%rax), %r8
movl -0x68(%rbp), %r9d
xorl %esi, %esi
leaq 0x5fc0(%rip), %rdx      # 0x25f36
movl %esi, %edi
movb $0x0, %al
callq 0x216e0
movq $0x0, -0x8(%rbp)
jmp 0x20005
movl -0x6c(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x1fff4
movq -0x60(%rbp), %rsi
movl -0x24(%rbp), %r8d
movq -0x58(%rbp), %r9
xorl %eax, %eax
movl %eax, %edi
movl $0x3, %edx
movl $0x1, %ecx
callq 0xd100
movq %rax, -0x30(%rbp)
movq $-0x1, %rax
cmpq %rax, -0x30(%rbp)
jne 0x1fff2
callq 0x20230
movq %rax, %rcx
leaq 0x1b8fd(%rip), %rax     # 0x3b8d0
movq (%rax), %r8
xorl %esi, %esi
leaq 0x5f7c(%rip), %rdx      # 0x25f5b
movl %esi, %edi
movb $0x0, %al
callq 0x216e0
movq $0x0, -0x8(%rbp)
jmp 0x20005
jmp 0x1fff4
movq -0x38(%rbp), %rdi
callq 0x1ec00
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)