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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int get_nth_format(int, int, int, struct v4l2_fmtdesc *);
int pixel_format_valid(int fd, enum v4l2_buf_type type, uint32_t pixel_format) {
  if (fd < 0) {
    errno = EINVAL;
    return -1;
  }
  struct v4l2_fmtdesc fmt = {0};
  for (int i = 0; - 1 != get_nth_format(fd, type, i, &fmt); i++) {
    if (fmt.pixelformat == pixel_format) {
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<pixel_format_valid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    115a <pixel_format_valid+0x3a>
mov    0x2e95(%rip),%rax
mov    (%rax),%ecx
mov    0x2e94(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <pixel_format_valid+0xa6>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    -0x28(%rbp),%rcx
call   1030 <get_nth_format@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     11bf <pixel_format_valid+0x9f>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11ac <pixel_format_valid+0x8c>
movl   $0x1,-0x4(%rbp)
jmp    11c6 <pixel_format_valid+0xa6>
jmp    11b1 <pixel_format_valid+0x91>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1171 <pixel_format_valid+0x51>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pixel_format_valid>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
js     1167 <pixel_format_valid+0x57>
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
xor    %ebp,%ebp
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r15d,%esi
mov    %ebp,%edx
mov    %r12,%rcx
call   1030 <get_nth_format@plt>
cmp    $0xffffffff,%eax
je     115d <pixel_format_valid+0x4d>
add    $0x1,%ebp
cmp    %r14,(%rsp)
jne    1140 <pixel_format_valid+0x30>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
setne  %cl
jmp    117e <pixel_format_valid+0x6e>
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    0x2e69(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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