// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdi, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0xcc(%rcx), %eax
jge 0x1ee06
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0xd170
movl $0x20, -0x140(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x138(%rbp)
movq -0x18(%rbp), %rax
movl 0xc0(%rax), %edi
movl $0x3b6c, %esi           # imm = 0x3B6C
leaq -0x140(%rbp), %rdx
movb $0x0, %al
callq 0xd180
cmpl $0x0, %eax
je 0x1ec77
jmp 0x1edf8
cmpl $0x7, -0x1c(%rbp)
jne 0x1edf6
movq -0x18(%rbp), %rax
movl 0xc0(%rax), %edi
leaq -0x120(%rbp), %rsi
movq -0x128(%rbp), %rcx
movl $0x100, %edx            # imm = 0x100
callq 0x1e820
cmpl $0x0, %eax
je 0x1ecb4
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0x1ee0d
leaq -0x120(%rbp), %rax
movq %rax, -0x148(%rbp)
leaq -0x120(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x150(%rbp)
movq -0x148(%rbp), %rax
movzwl (%rax), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x1ed15
callq 0x20230
movq %rax, %rcx
leaq 0x1cbdc(%rip), %rax     # 0x3b8d0
movq (%rax), %r8
xorl %esi, %esi
leaq 0x727d(%rip), %rdx      # 0x25f7d
movl %esi, %edi
movb $0x0, %al
callq 0x216e0
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0x1ee0d
movq -0x150(%rbp), %rax
movzwl (%rax), %ecx
orl $0x6, %ecx
movw %cx, (%rax)
movq -0x18(%rbp), %rax
movl 0xc0(%rax), %edi
movq -0x150(%rbp), %rsi
movq -0x128(%rbp), %rcx
addq $0x4, %rcx
movl $0x2, %edx
callq 0x1e8a0
cmpl $0x0, %eax
je 0x1ed5c
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0x1ee0d
movq -0x18(%rbp), %rax
movl 0xc0(%rax), %edi
movq -0x150(%rbp), %rsi
movq -0x128(%rbp), %rcx
addq $0x4, %rcx
movl $0x2, %edx
callq 0x1e820
cmpl $0x0, %eax
je 0x1ed90
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0x1ee0d
callq 0x20230
movq %rax, %rcx
leaq 0x1cb31(%rip), %rax     # 0x3b8d0
movq 0x18(%rax), %r8
movq -0x148(%rbp), %rax
movzwl (%rax), %ebx
movq -0x150(%rbp), %rax
movzwl (%rax), %r11d
movzwl -0x11e(%rbp), %r10d
movzbl -0x118(%rbp), %eax
movl $0x3, %edi
xorl %esi, %esi
leaq 0x71c3(%rip), %rdx      # 0x25f98
leaq 0x71ee(%rip), %r9       # 0x25fca
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movb $0x0, %al
callq 0x216e0
jmp 0x1ee06
jmp 0x1edf8
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1ec17
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %rbp
retq
nopw (%rax,%rax)