#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 dummy;
};
typedef enum v4l2_buf_type { ____Placeholder_v4l2_buf_type } v4l2_buf_type;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENOMEM;
int count_formats(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(struct v4l2_fmtdesc *);
int get_nth_format(int, int, int, struct v4l2_fmtdesc *);
struct v4l2_fmtdesc *malloc(int);
int enum_pixel_formats(int fd, enum v4l2_buf_type type,
                       struct v4l2_fmtdesc **format) {
  if (fd < 0 || format == NULL) {
    errno = EINVAL;
    return -1;
  }
  int fmt_cnt = count_formats(fd, type);
  if (fmt_cnt <= 0) {
    return fmt_cnt;
  }
  struct v4l2_fmtdesc *fmt = malloc(fmt_cnt * sizeof(struct v4l2_fmtdesc));
  if (fmt == NULL) {
    errno = ENOMEM;
    return -1;
  }
  for (int i = 0; i < fmt_cnt; i++) {
    if (get_nth_format(fd, type, i, &fmt[i]) == -1) {
      free(fmt);
      return -1;
    }
  }
  *format = fmt;
  return fmt_cnt;
}


x86 assembly (clang, -O0):
<enum_pixel_formats>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1167 <enum_pixel_formats+0x27>
cmpq   $0x0,-0x18(%rbp)
jne    1185 <enum_pixel_formats+0x45>
mov    0x2e62(%rip),%rax
mov    (%rax),%ecx
mov    0x2e61(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1256 <enum_pixel_formats+0x116>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <count_formats@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11a8 <enum_pixel_formats+0x68>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1256 <enum_pixel_formats+0x116>
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e4 <enum_pixel_formats+0xa4>
mov    0x2e13(%rip),%rax
mov    (%rax),%ecx
mov    0x2e02(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1256 <enum_pixel_formats+0x116>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1245 <enum_pixel_formats+0x105>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%rcx
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rcx
call   1040 <get_nth_format@plt>
cmp    $0xffffffff,%eax
jne    1232 <enum_pixel_formats+0xf2>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1256 <enum_pixel_formats+0x116>
jmp    1237 <enum_pixel_formats+0xf7>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11eb <enum_pixel_formats+0xab>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enum_pixel_formats>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
js     11ba <enum_pixel_formats+0x7a>
mov    %rdx,%r14
test   %rdx,%rdx
je     11ba <enum_pixel_formats+0x7a>
mov    %esi,%r15d
mov    %edi,%ebp
call   1050 <count_formats@plt>
mov    %eax,%r13d
test   %eax,%eax
jle    11d2 <enum_pixel_formats+0x92>
lea    0x0(,%r13,4),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11e4 <enum_pixel_formats+0xa4>
xor    %ebx,%ebx
mov    %rax,(%rsp)
mov    %rax,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %r15d,%esi
mov    %ebx,%edx
mov    %r12,%rcx
call   1040 <get_nth_format@plt>
cmp    $0xffffffff,%eax
je     11ed <enum_pixel_formats+0xad>
add    $0x1,%rbx
add    $0x4,%r12
cmp    %rbx,%r13
jne    1190 <enum_pixel_formats+0x50>
mov    (%rsp),%rax
mov    %rax,(%r14)
jmp    11d2 <enum_pixel_formats+0x92>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    0x2e0e(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2df5(%rip),%rax
jmp    11c1 <enum_pixel_formats+0x81>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    11cc <enum_pixel_formats+0x8c>

LLVM IR (-O0):
%struct.v4l2_fmtdesc = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @enum_pixel_formats(i32 %0, i32 %1, %struct.v4l2_fmtdesc** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.v4l2_fmtdesc**
%8 = alloca i32
%9 = alloca %struct.v4l2_fmtdesc*
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store %struct.v4l2_fmtdesc** %2, %struct.v4l2_fmtdesc*** %7
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load %struct.v4l2_fmtdesc**, %struct.v4l2_fmtdesc*** %7
%15 = icmp eq %struct.v4l2_fmtdesc** %14, null
br i1 %15, label %16, label %18
16: ; preds = %13, %3
%17 = load i32, i32* @EINVAL
store i32 %17, i32* @errno
store i32 -1, i32* %4
br label %62
18: ; preds = %13
%19 = load i32, i32* %5
%20 = load i32, i32* %6
%21 = call i32 @count_formats(i32 %19, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = load i32, i32* %8
store i32 %25, i32* %4
br label %62
26: ; preds = %18
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 4
%30 = trunc i64 %29 to i32
%31 = call %struct.v4l2_fmtdesc* @malloc(i32 %30)
store %struct.v4l2_fmtdesc* %31, %struct.v4l2_fmtdesc** %9
%32 = load %struct.v4l2_fmtdesc*, %struct.v4l2_fmtdesc** %9
%33 = icmp eq %struct.v4l2_fmtdesc* %32, null
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = load i32, i32* @ENOMEM
store i32 %35, i32* @errno
store i32 -1, i32* %4
br label %62
36: ; preds = %26
store i32 0, i32* %10
br label %37
37: ; preds = %55, %36
%38 = load i32, i32* %10
%39 = load i32, i32* %8
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %58
41: ; preds = %37
%42 = load i32, i32* %5
%43 = load i32, i32* %6
%44 = load i32, i32* %10
%45 = load %struct.v4l2_fmtdesc*, %struct.v4l2_fmtdesc** %9
%46 = load i32, i32* %10
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds %struct.v4l2_fmtdesc, %struct.v4l2_fmtdesc* %45, i64 %47
%49 = call i32 @get_nth_format(i32 %42, i32 %43, i32 %44, %struct.v4l2_fmtdesc* %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %41
%52 = load %struct.v4l2_fmtdesc*, %struct.v4l2_fmtdesc** %9
%53 = call i32 @free(%struct.v4l2_fmtdesc* %52)
store i32 -1, i32* %4
br label %62
54: ; preds = %41
br label %55
55: ; preds = %54
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
store i32 %57, i32* %10
br label %37
58: ; preds = %37
%59 = load %struct.v4l2_fmtdesc*, %struct.v4l2_fmtdesc** %9
%60 = load %struct.v4l2_fmtdesc**, %struct.v4l2_fmtdesc*** %7
store %struct.v4l2_fmtdesc* %59, %struct.v4l2_fmtdesc** %60
%61 = load i32, i32* %8
store i32 %61, i32* %4
br label %62
62: ; preds = %58, %51, %34, %24, %16
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
%struct.v4l2_fmtdesc = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @enum_pixel_formats(i32 %0, i32 %1, %struct.v4l2_fmtdesc** writeonly %2) {
%4 = icmp slt i32 %0, 0
%5 = icmp eq %struct.v4l2_fmtdesc** %2, null
%6 = or i1 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = load i32, i32* @EINVAL
store i32 %8, i32* @errno
br label %32
9: ; preds = %3
%10 = tail call i32 @count_formats(i32 %0, i32 %1)
%11 = icmp slt i32 %10, 1
br i1 %11, label %32, label %12
12: ; preds = %9
%13 = shl i32 %10, 2
%14 = tail call %struct.v4l2_fmtdesc* @malloc(i32 %13)
%15 = icmp eq %struct.v4l2_fmtdesc* %14, null
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = zext i32 %10 to i64
br label %22
18: ; preds = %12
%19 = load i32, i32* @ENOMEM
store i32 %19, i32* @errno
br label %32
20: ; preds = %22
%21 = icmp eq i64 %28, %17
br i1 %21, label %31, label %22
22: ; preds = %16, %20
%23 = phi i64 [ 0, %16 ], [ %28, %20 ]
%24 = getelementptr inbounds %struct.v4l2_fmtdesc, %struct.v4l2_fmtdesc* %14, i64 %23
%25 = trunc i64 %23 to i32
%26 = tail call i32 @get_nth_format(i32 %0, i32 %1, i32 %25, %struct.v4l2_fmtdesc* %24)
%27 = icmp eq i32 %26, -1
%28 = add nuw nsw i64 %23, 1
br i1 %27, label %29, label %20
29: ; preds = %22
%30 = tail call i32 @free(%struct.v4l2_fmtdesc* %14)
br label %32
31: ; preds = %20
store %struct.v4l2_fmtdesc* %14, %struct.v4l2_fmtdesc** %2
br label %32
32: ; preds = %29, %9, %31, %18, %7
%33 = phi i32 [ -1, %7 ], [ %10, %9 ], [ -1, %18 ], [ %10, %31 ], [ -1, %29 ]
ret i32 %33
}