// File: /workspace/github_cpp_makefile_star2_O1/p-ranav[P]video_device_discovery/src/video_device_info.cpp
// Function: find_video_devices(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&)
std::vector<VideoDeviceInfo>
find_video_devices(const std::vector<std::string>& filter_pixel_formats) {
  std::vector<VideoDeviceInfo> video_devices;
  
  for (auto& device: get_video_device_files()) {

    VideoDeviceInfo new_device;
    new_device.file = device;

    // Save sensor_id
    std::string split_on = "/dev/video";
    std::stringstream os(device.string().substr(split_on.length()));
    os >> new_device.sensor_id;

    int fd;
    fd = open(device.string().c_str(), O_RDWR);
    if (fd == -1) {
      // couldn't find capture device
      perror("Failed to open video device");
      return {};
    }

    struct v4l2_capability capability;
    if(ioctl(fd, VIDIOC_QUERYCAP, &capability) < 0){
      perror("Failed to get device capabilities, VIDIOC_QUERYCAP");
      return {};
    }

    new_device.driver = std::string{reinterpret_cast<const char*>(capability.driver)};
    new_device.bus_info = std::string{reinterpret_cast<const char*>(capability.bus_info)};
    new_device.card = std::string{reinterpret_cast<const char*>(capability.card)};
    
    // Get supported resolutions
    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    struct v4l2_fmtdesc fmt;
    struct v4l2_frmsizeenum frmsize;

    fmt.index = 0;
    fmt.type = type;
    while (ioctl(fd, VIDIOC_ENUM_FMT, &fmt) >= 0) {
      frmsize.pixel_format = fmt.pixelformat;
      new_device.pixel_formats.push_back(pixfmt_to_string(fmt.pixelformat));
      frmsize.index = 0;
      while (ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frmsize) >= 0) {
	if (frmsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
	  new_device.resolutions.push_back(std::make_pair(frmsize.discrete.width, frmsize.discrete.height));
	} else if (frmsize.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
	  new_device.resolutions.push_back(std::make_pair(frmsize.stepwise.max_width, frmsize.stepwise.max_height));
	}
	frmsize.index++;
      }
      fmt.index++;
    }
    
    if (filter_pixel_formats.empty()) {
      // no filter
      video_devices.push_back(new_device);
    } else {
      // filter by pixel format
      for (auto& pxlfmt: filter_pixel_formats) {
	for (auto& supported_formats: new_device.pixel_formats) {
	  if (supported_formats.find(pxlfmt) != std::string::npos) {
	    video_devices.push_back(new_device);
	    break;
	  }
	}
      }
    }
  }

  // sort by file, e.g., /dev/video0, /dev/video1, /dev/video2, ...
  std::sort(video_devices.begin(), video_devices.end(), video_device_info_less_than());

  return video_devices;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3d8, %rsp            # imm = 0x3D8
movq %rsi, -0x1b8(%rbp)
movq %rdi, -0x30(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0xc859
movq -0x1e0(%rbp), %r12
movq -0x1d8(%rbp), %rax
movq %rax, -0x1c0(%rbp)
cmpq %rax, %r12
je 0x70e6
leaq -0x188(%rbp), %r13
leaq -0x400(%rbp), %rbx
leaq -0x20c(%rbp), %r14
leaq -0x198(%rbp), %rax
movq %rax, -0x1a8(%rbp)
xorl %ecx, %ecx
movq %rcx, -0x1a0(%rbp)
movb %cl, -0x198(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x9(%r13)
movups %xmm0, (%r13)
leaq -0x168(%rbp), %rax
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0x128(%rbp), %rax
movq %rax, -0x138(%rbp)
movq %rcx, -0x130(%rbp)
movb %cl, -0x128(%rbp)
leaq -0x108(%rbp), %rax
movq %rax, -0x118(%rbp)
movq %rcx, -0x110(%rbp)
movb %cl, -0x108(%rbp)
leaq -0xe8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq %rcx, -0xf0(%rbp)
movb %cl, -0xe8(%rbp)
leaq -0x1a8(%rbp), %rdi
movq %r12, %rsi
callq 0x41e0
leaq 0x20(%r12), %rsi
movq %r13, %rdi
callq 0x7570
movq %rbx, %r15
movb 0x38(%r12), %al
movb %al, -0x170(%rbp)
leaq -0xc8(%rbp), %rax
movq %rax, -0xd8(%rbp)
leaq -0xd8(%rbp), %rdi
leaq 0x184a8(%rip), %rsi     # 0x1f060
leaq 0x184ab(%rip), %rdx     # 0x1f06a
callq 0x7ada
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq -0xa8(%rbp), %rax
movq %rax, -0xb8(%rbp)
leaq -0xb8(%rbp), %rbx
movq %rbx, %rdi
callq 0x68ca
leaq -0x278(%rbp), %r13
movq -0xd0(%rbp), %rdx
movq %r13, %rdi
movq %rbx, %rsi
movq $-0x1, %rcx
callq 0x4370
movq %r15, %rdi
movq %r13, %rsi
movl $0x18, %edx
callq 0x4540
movq -0x278(%rbp), %rdi
leaq -0x268(%rbp), %rax
cmpq %rax, %rdi
je 0x6c3f
movq -0x268(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x6c61
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x4360
movq %r15, %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x41f0
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq -0x268(%rbp), %rax
movq %rax, -0x278(%rbp)
movq %r13, %rdi
callq 0x68ca
movq -0x278(%rbp), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x4290
movl %eax, %r13d
movq -0x278(%rbp), %rdi
leaq -0x268(%rbp), %rax
cmpq %rax, %rdi
je 0x6cca
movq -0x268(%rbp), %rsi
incq %rsi
callq 0x4360
cmpl $-0x1, %r13d
je 0x7169
movl $0xc02c564a, %eax       # imm = 0xC02C564A
leaq -0x3fc4004a(%rax), %rsi
movl %r13d, %edi
leaq -0x278(%rbp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4460
movl %eax, %r15d
testl %eax, %eax
js 0x70c3
movq %rbx, %rdi
movl %r15d, %ebx
movq %r12, -0x1c8(%rbp)
leaq -0xa8(%rbp), %r12
movq %r12, -0xb8(%rbp)
callq 0x4150
leaq (%rax,%rbp), %rdx
addq $-0x278, %rdx           # imm = 0xFD88
leaq -0xb8(%rbp), %r15
movq %r15, %rdi
leaq -0x278(%rbp), %rsi
callq 0x7ada
movl %ebx, -0x54(%rbp)
leaq -0x138(%rbp), %rdi
movq %r15, %rsi
callq 0x43e0
movq -0xb8(%rbp), %rdi
cmpq %r12, %rdi
je 0x6d6d
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x4360
movq %r12, -0xb8(%rbp)
leaq -0x248(%rbp), %rbx
movq %rbx, %rdi
callq 0x4150
leaq (%rax,%rbx), %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x7ada
leaq -0xf8(%rbp), %rdi
movq %r15, %rsi
callq 0x43e0
movq -0xb8(%rbp), %rdi
cmpq %r12, %rdi
je 0x6dbc
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x4360
movq %r12, -0xb8(%rbp)
leaq -0x268(%rbp), %rbx
movq %rbx, %rdi
callq 0x4150
leaq (%rax,%rbx), %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x7ada
leaq -0x118(%rbp), %rdi
movq %r15, %rsi
callq 0x43e0
movq -0xb8(%rbp), %rdi
cmpq %r12, %rdi
leaq -0x68(%rbp), %rbx
je 0x6e0f
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x4360
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, -0xb8(%rbp)
movl $0xc02c564a, %eax       # imm = 0xC02C564A
leaq 0x13ffb8(%rax), %r12
movl %r13d, %edi
movq %r12, %rsi
leaq -0xb8(%rbp), %rdx
xorl %eax, %eax
callq 0x4460
testl %eax, %eax
js 0x6f49
movl -0x8c(%rbp), %esi
movl %esi, -0x208(%rbp)
leaq -0x78(%rbp), %r15
movq %r15, %rdi
callq 0xaac8
leaq -0x168(%rbp), %rdi
movq %r15, %rsi
callq 0x7b74
movq -0x78(%rbp), %rdi
cmpq %rbx, %rdi
je 0x6e84
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x4360
movl $0x0, -0x20c(%rbp)
movl $0xc02c564a, %esi       # imm = 0xC02C564A
movl %r13d, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x4460
testl %eax, %eax
js 0x6f3e
movl -0x204(%rbp), %eax
cmpl $0x3, %eax
je 0x6eee
cmpl $0x1, %eax
jne 0x6f33
movl -0x200(%rbp), %eax
movl -0x1fc(%rbp), %ecx
movq %rax, -0x78(%rbp)
movq %rcx, -0x70(%rbp)
movq -0x148(%rbp), %rsi
cmpq -0x140(%rbp), %rsi
jne 0x6f12
leaq -0x150(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x7df2
jmp 0x6f33
movl -0x1fc(%rbp), %eax
movl -0x1f0(%rbp), %ecx
movq %rax, -0x78(%rbp)
movq %rcx, -0x70(%rbp)
movq -0x148(%rbp), %rsi
cmpq -0x140(%rbp), %rsi
je 0x6f23
movups -0x78(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x148(%rbp)
jmp 0x6f33
leaq -0x150(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x7df2
incl -0x20c(%rbp)
jmp 0x6e8e
incl -0xb8(%rbp)
jmp 0x6e2c
movq -0x1b8(%rbp), %rax
movq (%rax), %r13
movq 0x8(%rax), %r15
cmpq %r15, %r13
je 0x6fa8
movq -0x168(%rbp), %r12
movq -0x160(%rbp), %rbx
cmpq %rbx, %r12
je 0x6f9d
movq (%r13), %rsi
movq 0x8(%r13), %rcx
movq %r12, %rdi
xorl %edx, %edx
callq 0x45a0
cmpq $-0x1, %rax
jne 0x6f8d
addq $0x20, %r12
jmp 0x6f6a
leaq -0x50(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x7452
addq $0x20, %r13
cmpq %r15, %r13
jne 0x6f5c
jmp 0x6fb8
leaq -0x50(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x7452
movq -0x1c8(%rbp), %r12
movl -0x54(%rbp), %r15d
leaq -0x400(%rbp), %rbx
movq %rbx, %rdi
movq 0x1ffa4(%rip), %rsi     # 0x26f78
callq 0x42a0
leaq -0x380(%rbp), %rdi
callq 0x4110
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
leaq -0x188(%rbp), %r13
je 0x700e
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0xf8(%rbp), %rdi
leaq -0xe8(%rbp), %rax
cmpq %rax, %rdi
je 0x7030
movq -0xe8(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x118(%rbp), %rdi
leaq -0x108(%rbp), %rax
cmpq %rax, %rdi
je 0x7052
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x138(%rbp), %rdi
leaq -0x128(%rbp), %rax
cmpq %rax, %rdi
je 0x7074
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x150(%rbp), %rdi
testq %rdi, %rdi
je 0x708f
movq -0x140(%rbp), %rsi
subq %rdi, %rsi
callq 0x4360
leaq -0x168(%rbp), %rdi
callq 0x6814
leaq -0x1a8(%rbp), %rdi
callq 0x6998
testl %r15d, %r15d
js 0x7261
addq $0x40, %r12
cmpq -0x1c0(%rbp), %r12
jne 0x6ae5
jmp 0x70e6
leaq 0x17fbd(%rip), %rdi     # 0x1f087
callq 0x42e0
movq -0x30(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
jmp 0x6fc3
leaq -0x1e0(%rbp), %rdi
callq 0x7520
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
cmpq %r14, %rbx
je 0x713a
movq %r14, %rax
subq %rbx, %rax
sarq $0x3, %rax
movabsq $-0x7b425ed097b425ed, %rcx # imm = 0x84BDA12F684BDA13
imulq %rax, %rcx
bsrq %rcx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8746
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8837
movq -0x50(%rbp), %rax
movq -0x30(%rbp), %rbx
movq %rax, (%rbx)
movq -0x48(%rbp), %rax
movq %rax, 0x8(%rbx)
movq -0x40(%rbp), %rax
movq %rax, 0x10(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
jmp 0x7271
leaq 0x17efb(%rip), %rdi     # 0x1f06b
callq 0x42e0
xorps %xmm0, %xmm0
movq -0x30(%rbp), %rax
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x1fdea(%rip), %rsi     # 0x26f78
leaq -0x400(%rbp), %rdi
callq 0x42a0
leaq -0x380(%rbp), %rdi
callq 0x4110
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x71c8
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0xf8(%rbp), %rdi
leaq -0xe8(%rbp), %rax
cmpq %rax, %rdi
je 0x71ea
movq -0xe8(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x118(%rbp), %rdi
leaq -0x108(%rbp), %rax
cmpq %rax, %rdi
je 0x720c
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x138(%rbp), %rdi
leaq -0x128(%rbp), %rax
cmpq %rax, %rdi
je 0x722e
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x150(%rbp), %rdi
testq %rdi, %rdi
je 0x7249
movq -0x140(%rbp), %rsi
subq %rdi, %rsi
callq 0x4360
leaq -0x168(%rbp), %rdi
callq 0x6814
leaq -0x1a8(%rbp), %rdi
callq 0x6998
leaq -0x1e0(%rbp), %rdi
callq 0x7520
movq -0x30(%rbp), %rbx
leaq -0x50(%rbp), %rdi
callq 0x687e
movq %rbx, %rax
addq $0x3d8, %rsp            # imm = 0x3D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7291
movq %rax, %rbx
jmp 0x7440
jmp 0x7357
jmp 0x7357
jmp 0x7357
jmp 0x7357
movq %rax, %rbx
jmp 0x7379
leaq -0xa8(%rbp), %r14
movq %rax, %rbx
movq -0x278(%rbp), %rdi
leaq -0x268(%rbp), %rax
cmpq %rax, %rdi
je 0x72f1
movq -0x268(%rbp), %rsi
incq %rsi
callq 0x4360
jmp 0x72f1
jmp 0x7357
jmp 0x732f
leaq -0xa8(%rbp), %r14
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
cmpq %r14, %rdi
je 0x7379
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x4360
jmp 0x7379
jmp 0x7357
movq %rax, %rbx
movq -0x278(%rbp), %rdi
leaq -0x268(%rbp), %rax
cmpq %rax, %rdi
je 0x735a
movq -0x268(%rbp), %rsi
jmp 0x7349
movq %rax, %rbx
jmp 0x739b
jmp 0x7357
movq %rbx, %rcx
movq %rax, %rbx
movq -0x78(%rbp), %rdi
cmpq %rcx, %rdi
je 0x735a
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x4360
jmp 0x735a
jmp 0x7357
jmp 0x7357
movq %rax, %rbx
movq 0x1fc17(%rip), %rsi     # 0x26f78
leaq -0x400(%rbp), %rdi
callq 0x42a0
leaq -0x380(%rbp), %rdi
callq 0x4110
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x739b
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0xf8(%rbp), %rdi
leaq -0xe8(%rbp), %rax
cmpq %rax, %rdi
je 0x73bd
movq -0xe8(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x118(%rbp), %rdi
leaq -0x108(%rbp), %rax
cmpq %rax, %rdi
je 0x73df
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x138(%rbp), %rdi
leaq -0x128(%rbp), %rax
cmpq %rax, %rdi
je 0x7401
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x150(%rbp), %rdi
testq %rdi, %rdi
je 0x741c
movq -0x140(%rbp), %rsi
subq %rdi, %rsi
callq 0x4360
leaq -0x168(%rbp), %rdi
callq 0x6814
leaq -0x1a8(%rbp), %rdi
callq 0x6998
leaq -0x1e0(%rbp), %rdi
callq 0x7520
leaq -0x50(%rbp), %rdi
callq 0x687e
movq %rbx, %rdi
callq 0x45b0
nop