#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_input {
  int index;
  scalar_t__ name;
};

/* Variables and functions */
int /*<<< orphan*/ VIDIOC_ENUMINPUT;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct v4l2_input *);
scalar_t__ strcasecmp(char *, char const *const);
int v4l2_set_input_by_index(int, int);
int __attribute__((unused))
v4l2_set_input_by_name(int videofd, const char *const name) {
  struct v4l2_input input;
  int input_idx;
  input.index = 0;
  input_idx = -1;
  while (ioctl(videofd, VIDIOC_ENUMINPUT, &input) == 0) {
    if (name && strcasecmp((char *)input.name, name) == 0) {
      input_idx = input.index;
      break;
    }
    ++input.index;
  }
  return v4l2_set_input_by_index(videofd, input_idx);
}


x86 assembly (clang, -O0):
<v4l2_set_input_by_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jne    11a7 <v4l2_set_input_by_name+0x77>
cmpq   $0x0,-0x10(%rbp)
je     1199 <v4l2_set_input_by_name+0x69>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1199 <v4l2_set_input_by_name+0x69>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11a7 <v4l2_set_input_by_name+0x77>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    114d <v4l2_set_input_by_name+0x1d>
mov    -0x4(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1050 <v4l2_set_input_by_index@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<v4l2_set_input_by_name>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x0,(%rsp)
mov    0x2e99(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%rdx
call   1040 <ioctl@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jne    11a2 <v4l2_set_input_by_name+0x72>
mov    %rsp,%r15
jmp    1187 <v4l2_set_input_by_name+0x57>
nopl   0x0(%rax,%rax,1)
addl   $0x1,(%rsp)
mov    (%r12),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
jne    11a2 <v4l2_set_input_by_name+0x72>
test   %rbx,%rbx
je     1170 <v4l2_set_input_by_name+0x40>
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
call   1030 <strcasecmp@plt>
test   %rax,%rax
jne    1170 <v4l2_set_input_by_name+0x40>
mov    (%rsp),%r14d
mov    %ebp,%edi
mov    %r14d,%esi
call   1050 <v4l2_set_input_by_index@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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