// File: /workspace/github_cpp_makefile_star2_O3/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, -0x1c0(%rbp)
movq %rdi, -0x30(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0xca92
movq -0x1e0(%rbp), %rbx
movq -0x1d8(%rbp), %rax
movq %rax, -0x1c8(%rbp)
cmpq %rax, %rbx
je 0x70fd
leaq -0x190(%rbp), %r15
leaq -0xa8(%rbp), %r12
leaq -0xb8(%rbp), %r13
leaq -0x20c(%rbp), %r14
leaq -0x1a0(%rbp), %rax
movq %rax, -0x1b0(%rbp)
xorl %ecx, %ecx
movq %rcx, -0x1a8(%rbp)
movb %cl, -0x1a0(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x9(%r15)
movups %xmm0, (%r15)
leaq -0x170(%rbp), %rax
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0x130(%rbp), %rax
movq %rax, -0x140(%rbp)
movq %rcx, -0x138(%rbp)
movb %cl, -0x130(%rbp)
leaq -0x110(%rbp), %rax
movq %rax, -0x120(%rbp)
movq %rcx, -0x118(%rbp)
movb %cl, -0x110(%rbp)
leaq -0xf0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq %rcx, -0xf8(%rbp)
movb %cl, -0xf0(%rbp)
leaq -0x1b0(%rbp), %rdi
movq %rbx, %rsi
callq 0x41e0
leaq 0x20(%rbx), %rsi
movq %r15, %rdi
callq 0x7588
movb 0x38(%rbx), %al
movb %al, -0x178(%rbp)
leaq -0xc8(%rbp), %rax
movq %rax, -0xd8(%rbp)
leaq -0xd8(%rbp), %rdi
leaq 0x19498(%rip), %rsi     # 0x20060
leaq 0x1949b(%rip), %rdx     # 0x2006a
callq 0x7b08
movq (%rbx), %rsi
movq %rbx, -0xe0(%rbp)
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
movq %r12, -0xb8(%rbp)
movq %r13, %rbx
movq %r13, %rdi
callq 0x68ce
leaq -0x278(%rbp), %r15
movq -0xd0(%rbp), %rdx
movq %r15, %rdi
movq %rbx, %rsi
movq $-0x1, %rcx
callq 0x4370
leaq -0x400(%rbp), %rdi
movq %r15, %rsi
movl $0x18, %edx
callq 0x4540
movq -0xe0(%rbp), %rbx
movq -0x278(%rbp), %rdi
leaq -0x268(%rbp), %rax
cmpq %rax, %rdi
je 0x6c54
movq -0x268(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0xb8(%rbp), %rdi
cmpq %r12, %rdi
je 0x6c6f
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x4360
leaq -0x400(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0x41f0
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x268(%rbp), %rax
movq %rax, -0x278(%rbp)
movq %r15, %rdi
callq 0x68ce
movq -0x278(%rbp), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x4290
movl %eax, %r15d
movq -0x278(%rbp), %rdi
leaq -0x268(%rbp), %rax
cmpq %rax, %rdi
je 0x6cda
movq -0x268(%rbp), %rsi
incq %rsi
callq 0x4360
cmpl $-0x1, %r15d
je 0x7183
movl $0xc02c564a, %eax       # imm = 0xC02C564A
leaq -0x3fc4004a(%rax), %rsi
movl %r15d, %edi
leaq -0x278(%rbp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4460
movl %eax, %r12d
testl %eax, %eax
js 0x70da
movq %rbx, %rdi
movl %r12d, %ebx
leaq -0xa8(%rbp), %r12
movq %r12, -0xb8(%rbp)
callq 0x4150
leaq (%rax,%rbp), %rdx
addq $-0x278, %rdx           # imm = 0xFD88
movq %r13, %rdi
leaq -0x278(%rbp), %rsi
callq 0x7b08
movl %ebx, -0x54(%rbp)
leaq -0x140(%rbp), %rdi
movq %r13, %rsi
callq 0x43e0
movq -0xb8(%rbp), %rdi
cmpq %r12, %rdi
movq %r12, %r13
je 0x6d72
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x4360
movq %r13, -0xb8(%rbp)
leaq -0x248(%rbp), %rbx
movq %rbx, %rdi
callq 0x4150
leaq (%rax,%rbx), %rdx
leaq -0xb8(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7b08
leaq -0x100(%rbp), %rdi
movq %r12, %rsi
callq 0x43e0
movq -0xb8(%rbp), %rdi
cmpq %r13, %rdi
je 0x6dc8
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x4360
movq %r13, -0xb8(%rbp)
leaq -0x268(%rbp), %rbx
movq %rbx, %rdi
callq 0x4150
leaq (%rax,%rbx), %rdx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7b08
leaq -0x120(%rbp), %rdi
movq %r12, %rsi
callq 0x43e0
movq -0xb8(%rbp), %rdi
cmpq %r13, %rdi
leaq -0x68(%rbp), %rbx
je 0x6e1b
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), %r13
movl %r15d, %edi
movq %r13, %rsi
leaq -0xb8(%rbp), %rdx
xorl %eax, %eax
callq 0x4460
testl %eax, %eax
js 0x6f55
movl -0x8c(%rbp), %esi
movl %esi, -0x208(%rbp)
leaq -0x78(%rbp), %r12
movq %r12, %rdi
callq 0xad04
leaq -0x170(%rbp), %rdi
movq %r12, %rsi
callq 0x7ba6
movq -0x78(%rbp), %rdi
cmpq %rbx, %rdi
je 0x6e90
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x4360
movl $0x0, -0x20c(%rbp)
movl $0xc02c564a, %esi       # imm = 0xC02C564A
movl %r15d, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x4460
testl %eax, %eax
js 0x6f4a
movl -0x204(%rbp), %eax
cmpl $0x3, %eax
je 0x6efa
cmpl $0x1, %eax
jne 0x6f3f
movl -0x200(%rbp), %eax
movl -0x1fc(%rbp), %ecx
movq %rax, -0x78(%rbp)
movq %rcx, -0x70(%rbp)
movq -0x150(%rbp), %rsi
cmpq -0x148(%rbp), %rsi
jne 0x6f1e
leaq -0x158(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x7e2c
jmp 0x6f3f
movl -0x1fc(%rbp), %eax
movl -0x1f0(%rbp), %ecx
movq %rax, -0x78(%rbp)
movq %rcx, -0x70(%rbp)
movq -0x150(%rbp), %rsi
cmpq -0x148(%rbp), %rsi
je 0x6f2f
movups -0x78(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x150(%rbp)
jmp 0x6f3f
leaq -0x158(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x7e2c
incl -0x20c(%rbp)
jmp 0x6e9a
incl -0xb8(%rbp)
jmp 0x6e38
movq -0x1c0(%rbp), %rax
movq (%rax), %r13
movq 0x8(%rax), %r12
cmpq %r12, %r13
je 0x6fb4
movq -0x170(%rbp), %r15
movq -0x168(%rbp), %rbx
cmpq %rbx, %r15
je 0x6fa9
movq (%r13), %rsi
movq 0x8(%r13), %rcx
movq %r15, %rdi
xorl %edx, %edx
callq 0x45a0
cmpq $-0x1, %rax
jne 0x6f99
addq $0x20, %r15
jmp 0x6f76
leaq -0x50(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0x7464
addq $0x20, %r13
cmpq %r12, %r13
jne 0x6f68
jmp 0x6fc4
leaq -0x50(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0x7464
leaq -0xb8(%rbp), %r13
movl -0x54(%rbp), %r12d
leaq -0x400(%rbp), %rdi
movq 0x20f9b(%rip), %rsi     # 0x27f78
callq 0x42a0
leaq -0x380(%rbp), %rdi
callq 0x4110
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
leaq -0x190(%rbp), %r15
je 0x7017
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x100(%rbp), %rdi
leaq -0xf0(%rbp), %rax
cmpq %rax, %rdi
movq -0xe0(%rbp), %rbx
je 0x7040
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
cmpq %rax, %rdi
je 0x7062
movq -0x110(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
cmpq %rax, %rdi
je 0x7084
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x158(%rbp), %rdi
testq %rdi, %rdi
je 0x709f
movq -0x148(%rbp), %rsi
subq %rdi, %rsi
callq 0x4360
leaq -0x170(%rbp), %rdi
callq 0x6814
leaq -0x1b0(%rbp), %rdi
callq 0x69a6
testl %r12d, %r12d
js 0x727b
addq $0x40, %rbx
cmpq -0x1c8(%rbp), %rbx
leaq -0xa8(%rbp), %r12
jne 0x6afc
jmp 0x70fd
leaq 0x18fa6(%rip), %rdi     # 0x20087
callq 0x42e0
movq -0x30(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
jmp 0x6fcf
leaq -0x1e0(%rbp), %rdi
callq 0x7532
movq -0x50(%rbp), %r14
movq -0x48(%rbp), %rbx
cmpq %rbx, %r14
je 0x7159
movq %rbx, %rax
subq %r14, %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 %r14, %rdi
movq %rbx, %rsi
callq 0x8780
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8871
movq -0x50(%rbp), %r14
movq -0x48(%rbp), %rbx
movq -0x30(%rbp), %rax
movq %r14, (%rax)
movq %rax, %r14
movq %rbx, 0x8(%rax)
movq -0x40(%rbp), %rax
movq %rax, 0x10(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
jmp 0x728b
leaq 0x18ee1(%rip), %rdi     # 0x2006b
callq 0x42e0
xorps %xmm0, %xmm0
movq -0x30(%rbp), %rax
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x20dd0(%rip), %rsi     # 0x27f78
leaq -0x400(%rbp), %rdi
callq 0x42a0
leaq -0x380(%rbp), %rdi
callq 0x4110
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x71e2
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x100(%rbp), %rdi
leaq -0xf0(%rbp), %rax
cmpq %rax, %rdi
je 0x7204
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
cmpq %rax, %rdi
je 0x7226
movq -0x110(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
cmpq %rax, %rdi
je 0x7248
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x158(%rbp), %rdi
testq %rdi, %rdi
je 0x7263
movq -0x148(%rbp), %rsi
subq %rdi, %rsi
callq 0x4360
leaq -0x170(%rbp), %rdi
callq 0x6814
leaq -0x1b0(%rbp), %rdi
callq 0x69a6
leaq -0x1e0(%rbp), %rdi
callq 0x7532
movq -0x30(%rbp), %r14
leaq -0x50(%rbp), %rdi
callq 0x6882
movq %r14, %rax
addq $0x3d8, %rsp            # imm = 0x3D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x72ab
movq %rax, %rbx
jmp 0x7452
jmp 0x7369
jmp 0x7369
jmp 0x7369
jmp 0x7369
movq %rax, %rbx
jmp 0x738b
movq %r12, %r14
movq %rax, %rbx
movq -0x278(%rbp), %rdi
leaq -0x268(%rbp), %rax
cmpq %rax, %rdi
je 0x7303
movq -0x268(%rbp), %rsi
incq %rsi
callq 0x4360
jmp 0x7303
jmp 0x7369
jmp 0x7341
movq %r12, %r14
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
cmpq %r14, %rdi
je 0x738b
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x4360
jmp 0x738b
jmp 0x7369
movq %rax, %rbx
movq -0x278(%rbp), %rdi
leaq -0x268(%rbp), %rax
cmpq %rax, %rdi
je 0x736c
movq -0x268(%rbp), %rsi
jmp 0x735b
movq %rax, %rbx
jmp 0x73ad
jmp 0x7369
movq %rbx, %rcx
movq %rax, %rbx
movq -0x78(%rbp), %rdi
cmpq %rcx, %rdi
je 0x736c
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x4360
jmp 0x736c
jmp 0x7369
jmp 0x7369
movq %rax, %rbx
movq 0x20c05(%rip), %rsi     # 0x27f78
leaq -0x400(%rbp), %rdi
callq 0x42a0
leaq -0x380(%rbp), %rdi
callq 0x4110
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x73ad
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x100(%rbp), %rdi
leaq -0xf0(%rbp), %rax
cmpq %rax, %rdi
je 0x73cf
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
cmpq %rax, %rdi
je 0x73f1
movq -0x110(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
cmpq %rax, %rdi
je 0x7413
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x4360
movq -0x158(%rbp), %rdi
testq %rdi, %rdi
je 0x742e
movq -0x148(%rbp), %rsi
subq %rdi, %rsi
callq 0x4360
leaq -0x170(%rbp), %rdi
callq 0x6814
leaq -0x1b0(%rbp), %rdi
callq 0x69a6
leaq -0x1e0(%rbp), %rdi
callq 0x7532
leaq -0x50(%rbp), %rdi
callq 0x6882
movq %rbx, %rdi
callq 0x45b0
nop