#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 {
  int /*<<< orphan*/ member_0;
};
typedef enum v4l2_buf_type { ____Placeholder_v4l2_buf_type } v4l2_buf_type;

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ errno;
scalar_t__ get_nth_format(int, int, int, struct v4l2_fmtdesc *);
int count_formats(int fd, enum v4l2_buf_type type) {
  struct v4l2_fmtdesc f = {0};
  int fmt_cnt = 0;
  while (get_nth_format(fd, type, fmt_cnt, &f) >= 0) {
    fmt_cnt++;
  }
  if (errno != EINVAL) {
    return -1;
  }
  return fmt_cnt;
}


x86 assembly (clang, -O0):
<count_formats>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    -0x10(%rbp),%rcx
call   1030 <get_nth_format@plt>
cmp    $0x0,%rax
jl     116f <count_formats+0x4f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1145 <count_formats+0x25>
mov    0x2e6a(%rip),%rax
mov    (%rax),%rax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%rax
je     1195 <count_formats+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    119b <count_formats+0x7b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_formats>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebp
movl   $0x0,(%rsp)
mov    $0xffffffff,%ebx
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebx
mov    %ebp,%edi
mov    %r14d,%esi
mov    %ebx,%edx
mov    %r15,%rcx
call   1030 <get_nth_format@plt>
test   %rax,%rax
jns    1130 <count_formats+0x20>
mov    0x2e92(%rip),%rax
mov    (%rax),%rax
mov    0x2e80(%rip),%rcx
cmp    (%rcx),%rax
mov    $0xffffffff,%eax
cmove  %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.v4l2_fmtdesc = type { i32 }
@errno = global i64 0
@EINVAL = global i64 0
define i32 @count_formats(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.v4l2_fmtdesc
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = bitcast %struct.v4l2_fmtdesc* %6 to i8*
store i32 0, i32* %7
br label %9
9: ; preds = %15, %2
%10 = load i32, i32* %4
%11 = load i32, i32* %5
%12 = load i32, i32* %7
%13 = call i64 @get_nth_format(i32 %10, i32 %11, i32 %12, %struct.v4l2_fmtdesc* %6)
%14 = icmp sge i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i32, i32* %7
%17 = add nsw i32 %16, 1
store i32 %17, i32* %7
br label %9
18: ; preds = %9
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINVAL
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %3
br label %25
23: ; preds = %18
%24 = load i32, i32* %7
store i32 %24, i32* %3
br label %25
25: ; preds = %23, %22
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
%struct.v4l2_fmtdesc = type { i32 }
@errno = global i64 0
@EINVAL = global i64 0
define i32 @count_formats(i32 %0, i32 %1) {
%3 = alloca %struct.v4l2_fmtdesc
%4 = bitcast %struct.v4l2_fmtdesc* %3 to i8*
%5 = getelementptr inbounds %struct.v4l2_fmtdesc, %struct.v4l2_fmtdesc* %3, i64 0, i32 0
store i32 0, i32* %5
br label %6
6: ; preds = %6, %2
%7 = phi i32 [ 0, %2 ], [ %10, %6 ]
%8 = call i64 @get_nth_format(i32 %0, i32 %1, i32 %7, %struct.v4l2_fmtdesc* %3)
%9 = icmp sgt i64 %8, -1
%10 = add nuw nsw i32 %7, 1
br i1 %9, label %6, label %11
11: ; preds = %6
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINVAL
%14 = icmp eq i64 %12, %13
%15 = select i1 %14, i32 %7, i32 -1
ret i32 %15
}