#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_queryctrl {
  unsigned int id;
  int maximum;
  int minimum;
  int value;
};
struct v4l2_control {
  unsigned int id;
  int maximum;
  int minimum;
  int value;
};

/* Variables and functions */
int /*<<< orphan*/ VIDIOC_QUERYCTRL;
int /*<<< orphan*/ VIDIOC_S_CTRL;
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct v4l2_queryctrl *);
int v4l2_ctrl_set(int fd, unsigned int id, int val) {
  struct v4l2_control ctrl;
  struct v4l2_queryctrl query;
  query.id = id;
  if (!ioctl(fd, VIDIOC_QUERYCTRL, &query)) {
    ctrl.id = id;
    if (val > query.maximum) {
      val = query.maximum;
    }
    if (val < query.minimum) {
      val = query.minimum;
    }
    ctrl.value = val;
    if (!ioctl(fd, VIDIOC_S_CTRL, &ctrl)) {
      return 0;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<v4l2_ctrl_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eaf(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%eax
jne    11a4 <v4l2_ctrl_set+0x94>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jle    115d <v4l2_ctrl_set+0x4d>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    116f <v4l2_ctrl_set+0x5f>
mov    -0x28(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%eax
jne    119f <v4l2_ctrl_set+0x8f>
movl   $0x0,-0x4(%rbp)
jmp    11ab <v4l2_ctrl_set+0x9b>
jmp    11a4 <v4l2_ctrl_set+0x94>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<v4l2_ctrl_set>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebp
mov    %esi,%ebx
mov    %edi,%r14d
mov    %esi,(%rsp)
mov    0x2eb7(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <ioctl@plt>
test   %eax,%eax
jne    116b <v4l2_ctrl_set+0x5b>
mov    0x4(%rsp),%eax
cmp    %ebp,%eax
cmovl  %eax,%ebp
mov    0x8(%rsp),%eax
cmp    %eax,%ebp
cmovl  %eax,%ebp
mov    %ebx,0x10(%rsp)
mov    %ebp,0x1c(%rsp)
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %r14d,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
je     1179 <v4l2_ctrl_set+0x69>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
jmp    1170 <v4l2_ctrl_set+0x60>

LLVM IR (-O0):
%struct.v4l2_control = type { i32, i32, i32, i32 }
%struct.v4l2_queryctrl = type { i32, i32, i32, i32 }
@VIDIOC_QUERYCTRL = global i32 0
@VIDIOC_S_CTRL = global i32 0
define i32 @v4l2_ctrl_set(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.v4l2_control
%9 = alloca %struct.v4l2_queryctrl
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %6
%11 = getelementptr inbounds %struct.v4l2_queryctrl, %struct.v4l2_queryctrl* %9, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* %5
%13 = load i32, i32* @VIDIOC_QUERYCTRL
%14 = call i32 @ioctl(i32 %12, i32 %13, %struct.v4l2_queryctrl* %9)
%15 = icmp ne i32 %14, 0
br i1 %15, label %44, label %16
16: ; preds = %3
%17 = load i32, i32* %6
%18 = getelementptr inbounds %struct.v4l2_control, %struct.v4l2_control* %8, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %7
%20 = getelementptr inbounds %struct.v4l2_queryctrl, %struct.v4l2_queryctrl* %9, i32 0, i32 1
%21 = load i32, i32* %20
%22 = icmp sgt i32 %19, %21
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = getelementptr inbounds %struct.v4l2_queryctrl, %struct.v4l2_queryctrl* %9, i32 0, i32 1
%25 = load i32, i32* %24
store i32 %25, i32* %7
br label %26
26: ; preds = %23, %16
%27 = load i32, i32* %7
%28 = getelementptr inbounds %struct.v4l2_queryctrl, %struct.v4l2_queryctrl* %9, i32 0, i32 2
%29 = load i32, i32* %28
%30 = icmp slt i32 %27, %29
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = getelementptr inbounds %struct.v4l2_queryctrl, %struct.v4l2_queryctrl* %9, i32 0, i32 2
%33 = load i32, i32* %32
store i32 %33, i32* %7
br label %34
34: ; preds = %31, %26
%35 = load i32, i32* %7
%36 = getelementptr inbounds %struct.v4l2_control, %struct.v4l2_control* %8, i32 0, i32 3
store i32 %35, i32* %36
%37 = load i32, i32* %5
%38 = load i32, i32* @VIDIOC_S_CTRL
%39 = bitcast %struct.v4l2_control* %8 to %struct.v4l2_queryctrl*
%40 = call i32 @ioctl(i32 %37, i32 %38, %struct.v4l2_queryctrl* %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %43, label %42
42: ; preds = %34
store i32 0, i32* %4
br label %45
43: ; preds = %34
br label %44
44: ; preds = %43, %3
store i32 -1, i32* %4
br label %45
45: ; preds = %44, %42
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.v4l2_control = type { i32, i32, i32, i32 }
%struct.v4l2_queryctrl = type { i32, i32, i32, i32 }
@VIDIOC_QUERYCTRL = global i32 0
@VIDIOC_S_CTRL = global i32 0
define i32 @v4l2_ctrl_set(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.v4l2_control
%5 = alloca %struct.v4l2_queryctrl
%6 = bitcast %struct.v4l2_control* %4 to i8*
%7 = bitcast %struct.v4l2_queryctrl* %5 to i8*
%8 = getelementptr inbounds %struct.v4l2_queryctrl, %struct.v4l2_queryctrl* %5, i64 0, i32 0
store i32 %1, i32* %8
%9 = load i32, i32* @VIDIOC_QUERYCTRL
%10 = call i32 @ioctl(i32 %0, i32 %9, %struct.v4l2_queryctrl* %5)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %27
12: ; preds = %3
%13 = getelementptr inbounds %struct.v4l2_control, %struct.v4l2_control* %4, i64 0, i32 0
store i32 %1, i32* %13
%14 = getelementptr inbounds %struct.v4l2_queryctrl, %struct.v4l2_queryctrl* %5, i64 0, i32 1
%15 = load i32, i32* %14
%16 = icmp slt i32 %15, %2
%17 = select i1 %16, i32 %15, i32 %2
%18 = getelementptr inbounds %struct.v4l2_queryctrl, %struct.v4l2_queryctrl* %5, i64 0, i32 2
%19 = load i32, i32* %18
%20 = icmp slt i32 %17, %19
%21 = select i1 %20, i32 %19, i32 %17
%22 = getelementptr inbounds %struct.v4l2_control, %struct.v4l2_control* %4, i64 0, i32 3
store i32 %21, i32* %22
%23 = load i32, i32* @VIDIOC_S_CTRL
%24 = bitcast %struct.v4l2_control* %4 to %struct.v4l2_queryctrl*
%25 = call i32 @ioctl(i32 %0, i32 %23, %struct.v4l2_queryctrl* %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %12, %3
br label %28
28: ; preds = %12, %27
%29 = phi i32 [ -1, %27 ], [ 0, %12 ]
ret i32 %29
}