#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct v4l2_fmtdesc {
  char *description;
  int /*<<< orphan*/ index;
  scalar_t__ pixelformat;
  int /*<<< orphan*/ type;
};
typedef int /*<<< orphan*/ fmtdesc;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ V4L2_BUF_TYPE_VIDEO_CAPTURE;
int /*<<< orphan*/ VIDIOC_ENUM_FMT;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct v4l2_fmtdesc *);
int /*<<< orphan*/ memset(struct v4l2_fmtdesc *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ v4l2_close(int);
int v4l2_open(char *, int /*<<< orphan*/);
int main() {
  int fd = v4l2_open("/dev/video0", O_RDWR);
  if (fd != -1) {
    struct v4l2_fmtdesc fmtdesc;
    memset(&fmtdesc, 0, sizeof(fmtdesc));
    fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    while (ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) == 0) {
      printf("%d %s\n", (int)fmtdesc.pixelformat, fmtdesc.description);
      fmtdesc.index++;
    }
    v4l2_close(fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1070 <v4l2_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     11e8 <main+0x98>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jne    11e0 <main+0x90>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    -0x28(%rbp),%rdx
lea    0xe3f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    119d <main+0x4d>
mov    -0x8(%rbp),%edi
call   1060 <v4l2_close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1070 <v4l2_open@plt>
cmp    $0xffffffff,%eax
je     11f0 <main+0xa0>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e39(%rip),%r12
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    11e9 <main+0x99>
lea    0xe53(%rip),%r14
lea    0x8(%rsp),%r15
xchg   %ax,%ax
mov    0x18(%rsp),%esi
mov    0x8(%rsp),%rdx
mov    %r14,%rdi
call   1030 <printf@plt>
addl   $0x1,0x10(%rsp)
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
je     11c0 <main+0x70>
mov    %ebx,%edi
call   1060 <v4l2_close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.v4l2_fmtdesc = type { i8*, i32, i64, i32 }
@.str = [12 x i8] c"/dev/video0\00"
@O_RDWR = global i32 0
@V4L2_BUF_TYPE_VIDEO_CAPTURE = global i32 0
@VIDIOC_ENUM_FMT = global i32 0
@.str.1 = [7 x i8] c"%d %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.v4l2_fmtdesc
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = call i32 @v4l2_open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp ne i32 %6, -1
br i1 %7, label %8, label %30
8: ; preds = %0
%9 = call i32 @memset(%struct.v4l2_fmtdesc* %3, i32 0, i32 32)
%10 = load i32, i32* @V4L2_BUF_TYPE_VIDEO_CAPTURE
%11 = getelementptr inbounds %struct.v4l2_fmtdesc, %struct.v4l2_fmtdesc* %3, i32 0, i32 3
store i32 %10, i32* %11
br label %12
12: ; preds = %17, %8
%13 = load i32, i32* %2
%14 = load i32, i32* @VIDIOC_ENUM_FMT
%15 = call i64 @ioctl(i32 %13, i32 %14, %struct.v4l2_fmtdesc* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %12
%18 = getelementptr inbounds %struct.v4l2_fmtdesc, %struct.v4l2_fmtdesc* %3, i32 0, i32 2
%19 = load i64, i64* %18
%20 = trunc i64 %19 to i32
%21 = getelementptr inbounds %struct.v4l2_fmtdesc, %struct.v4l2_fmtdesc* %3, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %20, i8* %22)
%24 = getelementptr inbounds %struct.v4l2_fmtdesc, %struct.v4l2_fmtdesc* %3, i32 0, i32 1
%25 = load i32, i32* %24
%26 = add nsw i32 %25, 1
store i32 %26, i32* %24
br label %12
27: ; preds = %12
%28 = load i32, i32* %2
%29 = call i32 @v4l2_close(i32 %28)
br label %30
30: ; preds = %27, %0
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
%struct.v4l2_fmtdesc = type { i8*, i32, i64, i32 }
@.str = [12 x i8] c"/dev/video0\00"
@O_RDWR = global i32 0
@V4L2_BUF_TYPE_VIDEO_CAPTURE = global i32 0
@VIDIOC_ENUM_FMT = global i32 0
@.str.1 = [7 x i8] c"%d %s\0A\00"
define i32 @main() {
%1 = alloca %struct.v4l2_fmtdesc
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @v4l2_open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %29, label %5
5: ; preds = %0
%6 = bitcast %struct.v4l2_fmtdesc* %1 to i8*
%7 = call i32 @memset(%struct.v4l2_fmtdesc* %1, i32 0, i32 32)
%8 = load i32, i32* @V4L2_BUF_TYPE_VIDEO_CAPTURE
%9 = getelementptr inbounds %struct.v4l2_fmtdesc, %struct.v4l2_fmtdesc* %1, i64 0, i32 3
store i32 %8, i32* %9
%10 = load i32, i32* @VIDIOC_ENUM_FMT
%11 = call i64 @ioctl(i32 %3, i32 %10, %struct.v4l2_fmtdesc* %1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %5
%14 = getelementptr inbounds %struct.v4l2_fmtdesc, %struct.v4l2_fmtdesc* %1, i64 0, i32 2
%15 = getelementptr inbounds %struct.v4l2_fmtdesc, %struct.v4l2_fmtdesc* %1, i64 0, i32 0
%16 = getelementptr inbounds %struct.v4l2_fmtdesc, %struct.v4l2_fmtdesc* %1, i64 0, i32 1
br label %17
17: ; preds = %13, %17
%18 = load i64, i64* %14
%19 = trunc i64 %18 to i32
%20 = load i8*, i8** %15
%21 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %20)
%22 = load i32, i32* %16
%23 = add nsw i32 %22, 1
store i32 %23, i32* %16
%24 = load i32, i32* @VIDIOC_ENUM_FMT
%25 = call i64 @ioctl(i32 %3, i32 %24, %struct.v4l2_fmtdesc* %1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %17, label %27
27: ; preds = %17, %5
%28 = call i32 @v4l2_close(i32 %3)
br label %29
29: ; preds = %27, %0
ret i32 0
}