#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_frmsizeenum {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(struct v4l2_frmsizeenum *);
int get_framesize_count(int, int);
scalar_t__ get_nth_frame_size(int, int, int, struct v4l2_frmsizeenum *);
struct v4l2_frmsizeenum *malloc(int);
int enum_frame_size(int fd, int pixel_format,
                    struct v4l2_frmsizeenum **frm_sz_enum) {
  if (fd < 0 || frm_sz_enum == NULL) {
    errno = EINVAL;
    return -1;
  }
  int frm_sz_cnt = get_framesize_count(fd, pixel_format);
  if (frm_sz_cnt < 0) {
    return frm_sz_cnt;
  }
  struct v4l2_frmsizeenum *fsze =
      malloc(frm_sz_cnt * sizeof(struct v4l2_frmsizeenum));
  if (fsze == NULL) {
    errno = ENOMEM;
    return -1;
  }
  for (int i = 0; i < frm_sz_cnt; i++) {
    if (get_nth_frame_size(fd, pixel_format, i, &fsze[i]) < 0) {
      free(fsze);
      fsze = NULL;
      return -1;
    }
  }
  *frm_sz_enum = fsze;
  return frm_sz_cnt;
}


x86 assembly (clang, -O0):
<enum_frame_size>:
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_frame_size+0x27>
cmpq   $0x0,-0x18(%rbp)
jne    1185 <enum_frame_size+0x45>
mov    0x2e62(%rip),%rax
mov    (%rax),%ecx
mov    0x2e61(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <enum_frame_size+0x11f>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1060 <get_framesize_count@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a8 <enum_frame_size+0x68>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125f <enum_frame_size+0x11f>
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e4 <enum_frame_size+0xa4>
mov    0x2e13(%rip),%rax
mov    (%rax),%ecx
mov    0x2e02(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <enum_frame_size+0x11f>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    124e <enum_frame_size+0x10e>
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_frame_size@plt>
cmp    $0x0,%rax
jge    123b <enum_frame_size+0xfb>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <enum_frame_size+0x11f>
jmp    1240 <enum_frame_size+0x100>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11eb <enum_frame_size+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_frame_size>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edi,%edi
js     11d0 <enum_frame_size+0x90>
mov    %rdx,%rbp
test   %rdx,%rdx
je     11d0 <enum_frame_size+0x90>
mov    %esi,%r13d
mov    %edi,%ebx
call   1060 <get_framesize_count@plt>
mov    %eax,%r15d
test   %eax,%eax
js     11e8 <enum_frame_size+0xa8>
lea    0x0(,%r15,4),%edi
call   1050 <malloc@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     11fa <enum_frame_size+0xba>
mov    %rbp,0x10(%rsp)
test   %r15d,%r15d
je     11c1 <enum_frame_size+0x81>
mov    %r15d,%r14d
xor    %ebp,%ebp
mov    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r13d,%esi
mov    %ebp,%edx
mov    %r12,%rcx
call   1040 <get_nth_frame_size@plt>
test   %rax,%rax
js     1203 <enum_frame_size+0xc3>
add    $0x1,%rbp
add    $0x4,%r12
cmp    %rbp,%r14
jne    11a0 <enum_frame_size+0x60>
mov    0x10(%rsp),%rax
mov    0x8(%rsp),%rcx
mov    %rcx,(%rax)
jmp    11e8 <enum_frame_size+0xa8>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    0x2df8(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ddf(%rip),%rax
jmp    11d7 <enum_frame_size+0x97>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    11e2 <enum_frame_size+0xa2>

LLVM IR (-O0):
%struct.v4l2_frmsizeenum = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @enum_frame_size(i32 %0, i32 %1, %struct.v4l2_frmsizeenum** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.v4l2_frmsizeenum**
%8 = alloca i32
%9 = alloca %struct.v4l2_frmsizeenum*
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store %struct.v4l2_frmsizeenum** %2, %struct.v4l2_frmsizeenum*** %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_frmsizeenum**, %struct.v4l2_frmsizeenum*** %7
%15 = icmp eq %struct.v4l2_frmsizeenum** %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 @get_framesize_count(i32 %19, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt 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_frmsizeenum* @malloc(i32 %30)
store %struct.v4l2_frmsizeenum* %31, %struct.v4l2_frmsizeenum** %9
%32 = load %struct.v4l2_frmsizeenum*, %struct.v4l2_frmsizeenum** %9
%33 = icmp eq %struct.v4l2_frmsizeenum* %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_frmsizeenum*, %struct.v4l2_frmsizeenum** %9
%46 = load i32, i32* %10
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds %struct.v4l2_frmsizeenum, %struct.v4l2_frmsizeenum* %45, i64 %47
%49 = call i64 @get_nth_frame_size(i32 %42, i32 %43, i32 %44, %struct.v4l2_frmsizeenum* %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %41
%52 = load %struct.v4l2_frmsizeenum*, %struct.v4l2_frmsizeenum** %9
%53 = call i32 @free(%struct.v4l2_frmsizeenum* %52)
store %struct.v4l2_frmsizeenum* null, %struct.v4l2_frmsizeenum** %9
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_frmsizeenum*, %struct.v4l2_frmsizeenum** %9
%60 = load %struct.v4l2_frmsizeenum**, %struct.v4l2_frmsizeenum*** %7
store %struct.v4l2_frmsizeenum* %59, %struct.v4l2_frmsizeenum** %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_frmsizeenum = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @enum_frame_size(i32 %0, i32 %1, %struct.v4l2_frmsizeenum** writeonly %2) {
%4 = icmp slt i32 %0, 0
%5 = icmp eq %struct.v4l2_frmsizeenum** %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 %34
9: ; preds = %3
%10 = tail call i32 @get_framesize_count(i32 %0, i32 %1)
%11 = icmp slt i32 %10, 0
br i1 %11, label %34, label %12
12: ; preds = %9
%13 = shl i32 %10, 2
%14 = tail call %struct.v4l2_frmsizeenum* @malloc(i32 %13)
%15 = icmp eq %struct.v4l2_frmsizeenum* %14, null
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = icmp eq i32 %10, 0
br i1 %17, label %33, label %18
18: ; preds = %16
%19 = zext i32 %10 to i64
br label %24
20: ; preds = %12
%21 = load i32, i32* @ENOMEM
store i32 %21, i32* @errno
br label %34
22: ; preds = %24
%23 = icmp eq i64 %30, %19
br i1 %23, label %33, label %24
24: ; preds = %18, %22
%25 = phi i64 [ 0, %18 ], [ %30, %22 ]
%26 = getelementptr inbounds %struct.v4l2_frmsizeenum, %struct.v4l2_frmsizeenum* %14, i64 %25
%27 = trunc i64 %25 to i32
%28 = tail call i64 @get_nth_frame_size(i32 %0, i32 %1, i32 %27, %struct.v4l2_frmsizeenum* %26)
%29 = icmp slt i64 %28, 0
%30 = add nuw nsw i64 %25, 1
br i1 %29, label %31, label %22
31: ; preds = %24
%32 = tail call i32 @free(%struct.v4l2_frmsizeenum* %14)
br label %34
33: ; preds = %22, %16
store %struct.v4l2_frmsizeenum* %14, %struct.v4l2_frmsizeenum** %2
br label %34
34: ; preds = %31, %9, %33, %20, %7
%35 = phi i32 [ -1, %7 ], [ %10, %9 ], [ -1, %20 ], [ %10, %33 ], [ -1, %31 ]
ret i32 %35
}