// File: /workspace/github_cpp_makefile_star2_O2/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 $0x3e8, %rsp            # imm = 0x3E8
movq %rsi, -0x68(%rbp)
movq %rdi, -0x30(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
andq $0x0, -0x40(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x9ae5
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq -0x178(%rbp), %r14
leaq -0x218(%rbp), %r13
leaq -0xbc(%rbp), %r12
leaq -0xe0(%rbp), %rbx
cmpq -0x70(%rbp), %rsi
je 0x6eaf
leaq -0x1c0(%rbp), %rax
movq %rax, -0x1d0(%rbp)
andq $0x0, -0x1c8(%rbp)
xorl %ecx, %ecx
movb %cl, -0x1c0(%rbp)
xorps %xmm0, %xmm0
leaq -0x1d0(%rbp), %rdi
movups %xmm0, 0x29(%rdi)
movups %xmm0, 0x20(%rdi)
leaq -0x190(%rbp), %rax
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0x150(%rbp), %rax
movq %rax, -0x160(%rbp)
andq $0x0, -0x158(%rbp)
movb %cl, -0x150(%rbp)
leaq -0x130(%rbp), %rax
movq %rax, -0x140(%rbp)
andq $0x0, -0x138(%rbp)
movb %cl, -0x130(%rbp)
leaq -0x110(%rbp), %rax
movq %rax, -0x120(%rbp)
andq $0x0, -0x118(%rbp)
movb %cl, -0x110(%rbp)
movq %rsi, -0x58(%rbp)
callq 0x700a
leaq -0x100(%rbp), %rdi
leaq 0x164b7(%rip), %rsi     # 0x1d060
leaq -0x408(%rbp), %rdx
callq 0x7038
movq %r13, %rdi
movq -0x58(%rbp), %rsi
callq 0x6774
movq %r13, %rsi
movq -0xf8(%rbp), %rdx
leaq -0x280(%rbp), %r15
movq %r15, %rdi
pushq $-0x1
popq %rcx
callq 0x43a0
leaq -0x408(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
pushq $0x18
popq %rdx
callq 0x4570
movq %r15, %rdi
callq 0x4170
leaq -0x218(%rbp), %rdi
callq 0x4170
movq %r13, %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x4200
movq %r15, %rdi
movq -0x58(%rbp), %rsi
callq 0x6774
movq -0x280(%rbp), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x42a0
movl %eax, %r13d
movq %r15, %rdi
callq 0x4170
cmpl $-0x1, %r13d
je 0x6ef1
movl $0x80685600, %esi       # imm = 0x80685600
movl %r13d, %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x4490
movl %eax, -0x5c(%rbp)
testl %eax, %eax
js 0x6e92
movq %r15, %rsi
leaq -0x218(%rbp), %r15
movq %r15, %rdi
movq %r12, %rdx
callq 0x7038
leaq -0x160(%rbp), %rdi
movq %r15, %rsi
callq 0x43f0
movq %r15, %rdi
callq 0x4170
movq %r15, %rdi
leaq -0x250(%rbp), %rsi
movq %r12, %rdx
callq 0x7038
leaq -0x120(%rbp), %rdi
movq %r15, %rsi
callq 0x43f0
movq %r15, %rdi
callq 0x4170
movq %r15, %rdi
leaq -0x270(%rbp), %rsi
movq %r12, %rdx
callq 0x7038
leaq -0x140(%rbp), %rdi
movq %r15, %rsi
callq 0x43f0
movq %r15, %rdi
callq 0x4170
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, -0x218(%rbp)
movl $0x80685600, %eax       # imm = 0x80685600
leaq 0x3fd80002(%rax), %rsi
movl %r13d, %edi
leaq -0x218(%rbp), %rdx
xorl %eax, %eax
callq 0x4490
testl %eax, %eax
js 0x6ddf
movl -0x1ec(%rbp), %esi
movl %esi, -0xb8(%rbp)
movq %rbx, %r15
movq %rbx, %rdi
callq 0x8a32
leaq -0x190(%rbp), %rdi
movq %r15, %rsi
callq 0x75f4
movq %r15, %rdi
callq 0x4170
xorl %eax, %eax
movl %eax, -0xbc(%rbp)
movl $0x80685600, %eax       # imm = 0x80685600
leaq 0x3fc4004a(%rax), %rsi
movl %r13d, %edi
movq %r12, %rdx
xorl %eax, %eax
callq 0x4490
testl %eax, %eax
js 0x6dd4
movl -0xb4(%rbp), %eax
cmpl $0x3, %eax
je 0x6da2
cmpl $0x1, %eax
jne 0x6dc7
movl -0xb0(%rbp), %eax
movl -0xac(%rbp), %ecx
movq %rax, -0xe0(%rbp)
movq %rcx, -0xd8(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x77e4
jmp 0x6dc7
movl -0xac(%rbp), %eax
movl -0xa0(%rbp), %ecx
movq %rax, -0xe0(%rbp)
movq %rcx, -0xd8(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x77e4
movl -0xbc(%rbp), %eax
incl %eax
jmp 0x6d48
incl -0x218(%rbp)
jmp 0x6cf3
movq -0x68(%rbp), %rax
movq (%rax), %r13
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
cmpq %rax, %r13
je 0x6e39
cmpq -0x78(%rbp), %r13
je 0x6e49
movq -0x190(%rbp), %r14
movq -0x188(%rbp), %r15
cmpq %r15, %r14
je 0x6e33
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x46a0
addq $0x20, %r14
cmpq $-0x1, %rax
je 0x6e07
leaq -0x50(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x7074
addq $0x20, %r13
jmp 0x6df3
leaq -0x50(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x7074
leaq -0x178(%rbp), %r14
leaq -0x408(%rbp), %rdi
callq 0x42b0
leaq -0x100(%rbp), %rdi
callq 0x4170
leaq -0x1d8(%rbp), %rdi
callq 0x69e4
movq -0x58(%rbp), %rsi
addq $0x40, %rsi
cmpl $0x0, -0x5c(%rbp)
leaq -0x218(%rbp), %r13
jns 0x6af2
jmp 0x6f30
leaq 0x161ee(%rip), %rdi     # 0x1d087
callq 0x42f0
movq -0x30(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
jmp 0x6e50
leaq -0x90(%rbp), %rdi
callq 0x70b2
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x7f38
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)
andq $0x0, -0x40(%rbp)
jmp 0x6f40
leaq 0x16173(%rip), %rdi     # 0x1d06b
callq 0x42f0
xorps %xmm0, %xmm0
movq -0x30(%rbp), %rax
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq -0x408(%rbp), %rdi
callq 0x42b0
leaq -0x100(%rbp), %rdi
callq 0x4170
leaq -0x1d8(%rbp), %rdi
callq 0x69e4
leaq -0x90(%rbp), %rdi
callq 0x70b2
movq -0x30(%rbp), %rbx
leaq -0x50(%rbp), %rdi
callq 0x6790
movq %rbx, %rax
addq $0x3e8, %rsp            # imm = 0x3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6f60
movq %rax, %rbx
jmp 0x6ff8
jmp 0x6fc5
jmp 0x6fc5
jmp 0x6fc5
jmp 0x6fc5
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
jmp 0x6fba
jmp 0x6f96
movq %rax, %rbx
jmp 0x6fd4
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
callq 0x4170
jmp 0x6f9e
jmp 0x6fc5
movq %rax, %rbx
jmp 0x6fe0
movq %rax, %rbx
leaq -0x218(%rbp), %rdi
callq 0x4170
jmp 0x6fd4
jmp 0x6fc5
jmp 0x6fc5
movq %rax, %rbx
leaq -0xe0(%rbp), %rdi
callq 0x4170
jmp 0x6fc8
jmp 0x6fc5
jmp 0x6fc5
movq %rax, %rbx
leaq -0x408(%rbp), %rdi
callq 0x42b0
leaq -0x100(%rbp), %rdi
callq 0x4170
leaq -0x1d8(%rbp), %rdi
callq 0x69e4
leaq -0x90(%rbp), %rdi
callq 0x70b2
leaq -0x50(%rbp), %rdi
callq 0x6790
movq %rbx, %rdi
callq 0x45e0
nop