// File: /workspace/github_c_makefile_star2_O1/intel[P]pf-bb-config/bb_acc_vfio.c
// Function: vfio_set_bus_master
static int vfio_set_bus_master(hw_device *hwd)
{
	int i;
	uint8_t config[VFIO_PCI_CONFIG_REGION_SIZE];

	for (i = 0; i < hwd->num_regions; i++) {
		struct vfio_region_info reg = {
				.argsz = sizeof(reg), .index = i
		};

		if (ioctl(hwd->vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg))
			continue;

		if (i == VFIO_PCI_CONFIG_REGION_INDEX) {
			uint16_t *vendor;
			uint16_t *cmd;

			if (vfio_read(hwd->vfio_dev_fd, config, sizeof(config), reg.offset))
				return -1;

			vendor = (uint16_t *)(config + PCI_VENDOR_ID);
			cmd = (uint16_t *)(config + PCI_COMMAND);

			if (*vendor == 0xffff) {
				LOG(ERR, "device in bad state");
				return -1;
			}

			*cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
			if (vfio_write(hwd->vfio_dev_fd, cmd,
					sizeof(*cmd), reg.offset + PCI_COMMAND))
				return -1;

			if (vfio_read(hwd->vfio_dev_fd, cmd,
					sizeof(*cmd), reg.offset + PCI_COMMAND))
				return -1;

			LOG(DEBUG, "vendor=%#x cmd=%#x device=%#x rev=%d",
					*vendor, *cmd,
					(uint32_t)*((uint16_t *)(config + PCI_DEVICE_ID)),
					(uint32_t)config[PCI_REVISION_ID]);

			break;
		}
	} /* loop on all regions */

	return 0;
}

// Assembly:
cmpl $0x0, 0xcc(%rdi)
jle 0x182f3
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdi, %rbx
leaq 0x34(%rsp), %r14
movq %rsp, %r15
leaq 0x30(%rsp), %r12
xorl %r13d, %r13d
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movaps %xmm0, 0x10(%rsp)
movl $0x20, (%rsp)
movl %r13d, 0x8(%rsp)
movl 0xc0(%rbx), %edi
movl $0x3b6c, %esi           # imm = 0x3B6C
movq %r15, %rdx
xorl %eax, %eax
callq 0xd190
movl $0x4, %ebp
testl %eax, %eax
jne 0x181f8
xorl %ebp, %ebp
cmpl $0x7, %r13d
jne 0x181f8
movl 0xc0(%rbx), %edi
movq 0x18(%rsp), %rcx
movl $0x100, %edx            # imm = 0x100
movq %r12, %rsi
callq 0x17d89
movl $0x1, %ebp
testl %eax, %eax
je 0x18218
xorl %eax, %eax
testl %ebp, %ebp
je 0x18207
cmpl $0x4, %ebp
jne 0x182f6
incl %r13d
cmpl 0xcc(%rbx), %r13d
jl 0x18199
jmp 0x18300
cmpw $-0x1, 0x30(%rsp)
je 0x182ca
orb $0x6, 0x34(%rsp)
movl 0xc0(%rbx), %edi
movq 0x18(%rsp), %rcx
addq $0x4, %rcx
movl $0x2, %edx
movq %r14, %rsi
callq 0x17dda
testl %eax, %eax
jne 0x181f8
movl 0xc0(%rbx), %edi
movq 0x18(%rsp), %rcx
addq $0x4, %rcx
movl $0x2, %edx
movq %r14, %rsi
callq 0x17d89
testl %eax, %eax
jne 0x181f8
callq 0x18f62
leaq 0x1bc5b(%rip), %rcx     # 0x33ed0
movq 0x18(%rcx), %r8
movzwl 0x30(%rsp), %ecx
movq %rcx, 0x28(%rsp)
movzwl 0x34(%rsp), %r11d
movzwl 0x32(%rsp), %ebp
movzbl 0x38(%rsp), %r10d
movl $0x3, %edi
xorl %esi, %esi
leaq 0x5cf5(%rip), %rdx      # 0x1df97
movq %rax, %rcx
leaq 0x5d1d(%rip), %r9       # 0x1dfc9
xorl %eax, %eax
pushq %r10
pushq %rbp
pushq %r11
pushq 0x40(%rsp)
callq 0x19ca8
addq $0x20, %rsp
movl $0x2, %ebp
jmp 0x181f8
callq 0x18f62
leaq 0x1bbfa(%rip), %rcx     # 0x33ed0
movq (%rcx), %r8
xorl %edi, %edi
xorl %esi, %esi
leaq 0x5c98(%rip), %rdx      # 0x1df7c
movq %rax, %rcx
xorl %eax, %eax
callq 0x19ca8
jmp 0x181f8
xorl %eax, %eax
retq
cmpl $0x2, %ebp
je 0x18300
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq