#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 hiddev_usage_ref {
  int report_type;
  int report_id;
  int field_index;
  int usage_index;
  int value;
};
struct hiddev_report_info {
  int report_type;
  int report_id;
  int field_index;
  int usage_index;
  int value;
};

/* Variables and functions */
int /*<<< orphan*/ HIDIOCSREPORT;
int /*<<< orphan*/ HIDIOCSUSAGE;
scalar_t__ control_hidden(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct hiddev_usage_ref *);
int /*<<< orphan*/ stderr;
bool set_control_value(int fd, int report_type, int report_id, int field_idx,
                       int usage_idx, int value) {
  if (control_hidden(report_id)) {
    fprintf(stderr, "Control 0x%x is hidden because it's probably broken.\n",
            report_id);
    return false;
  }
  struct hiddev_report_info rinfo = {
      .report_type = report_type,
      .report_id = report_id,
  };
  struct hiddev_usage_ref uref = {
      .report_type = report_type,
      .report_id = report_id,
      .field_index = field_idx,
      .usage_index = usage_idx,
      .value = value,
  };
  if (ioctl(fd, HIDIOCSUSAGE, &uref) < 0) {
    fprintf(stderr, "HIDIOCSUSAGE failed - unable to set control value.\n");
    return false;
  }
  if (ioctl(fd, HIDIOCSREPORT, &rinfo) < 0) {
    fprintf(stderr, "HIDIOCSREPORT failed - unable to set control value.\n");
    return false;
  }
  return true;
}


x86 assembly (clang, -O0):
<set_control_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    %r9d,-0x1c(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <control_hidden@plt>
cmp    $0x0,%rax
je     1184 <set_control_value+0x54>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
lea    0xe8f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124e <set_control_value+0x11e>
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1205 <set_control_value+0xd5>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124e <set_control_value+0x11e>
mov    -0x8(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1247 <set_control_value+0x117>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124e <set_control_value+0x11e>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_control_value>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9d,%r15d
mov    %r8d,%r12d
mov    %ecx,%r13d
mov    %edx,%ebp
mov    %esi,%ebx
mov    %edi,%r14d
mov    %edx,%edi
call   1040 <control_hidden@plt>
test   %rax,%rax
je     117a <set_control_value+0x4a>
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
lea    0xe96(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1207 <set_control_value+0xd7>
mov    %ebx,0x20(%rsp)
mov    %ebp,0x24(%rsp)
movq   $0x0,0x28(%rsp)
movl   $0x0,0x30(%rsp)
mov    %ebx,0x8(%rsp)
mov    %ebp,0xc(%rsp)
mov    %r13d,0x10(%rsp)
mov    %r12d,0x14(%rsp)
mov    %r15d,0x18(%rsp)
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     11ee <set_control_value+0xbe>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdx
mov    %r14d,%edi
call   1030 <ioctl@plt>
mov    $0x1,%ebx
test   %rax,%rax
jns    1207 <set_control_value+0xd7>
lea    0xe7e(%rip),%rsi
jmp    11f5 <set_control_value+0xc5>
lea    0xe41(%rip),%rsi
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hiddev_report_info = type { i32, i32, i32, i32, i32 }
%struct.hiddev_usage_ref = type { i32, i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [54 x i8] c"Control 0x%x is hidden because it's probably broken.\0A\00"
@HIDIOCSUSAGE = global i32 0
@.str.1 = [52 x i8] c"HIDIOCSUSAGE failed - unable to set control value.\0A\00"
@HIDIOCSREPORT = global i32 0
@.str.2 = [53 x i8] c"HIDIOCSREPORT failed - unable to set control value.\0A\00"
define i32 @set_control_value(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.hiddev_report_info
%15 = alloca %struct.hiddev_usage_ref
store i32 %0, i32* %8
store i32 %1, i32* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
%16 = load i32, i32* %10
%17 = call i64 @control_hidden(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %6
%20 = load i32, i32* @stderr
%21 = load i32, i32* %10
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 0, i32* %7
br label %58
23: ; preds = %6
%24 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %14, i32 0, i32 0
%25 = load i32, i32* %9
store i32 %25, i32* %24
%26 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %14, i32 0, i32 1
%27 = load i32, i32* %10
store i32 %27, i32* %26
%28 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %14, i32 0, i32 2
store i32 0, i32* %28
%29 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %14, i32 0, i32 3
store i32 0, i32* %29
%30 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %14, i32 0, i32 4
store i32 0, i32* %30
%31 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %15, i32 0, i32 0
%32 = load i32, i32* %9
store i32 %32, i32* %31
%33 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %15, i32 0, i32 1
%34 = load i32, i32* %10
store i32 %34, i32* %33
%35 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %15, i32 0, i32 2
%36 = load i32, i32* %11
store i32 %36, i32* %35
%37 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %15, i32 0, i32 3
%38 = load i32, i32* %12
store i32 %38, i32* %37
%39 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %15, i32 0, i32 4
%40 = load i32, i32* %13
store i32 %40, i32* %39
%41 = load i32, i32* %8
%42 = load i32, i32* @HIDIOCSUSAGE
%43 = call i64 @ioctl(i32 %41, i32 %42, %struct.hiddev_usage_ref* %15)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %23
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %7
br label %58
48: ; preds = %23
%49 = load i32, i32* %8
%50 = load i32, i32* @HIDIOCSREPORT
%51 = bitcast %struct.hiddev_report_info* %14 to %struct.hiddev_usage_ref*
%52 = call i64 @ioctl(i32 %49, i32 %50, %struct.hiddev_usage_ref* %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %7
br label %58
57: ; preds = %48
store i32 1, i32* %7
br label %58
58: ; preds = %57, %54, %45, %19
%59 = load i32, i32* %7
ret i32 %59
}

LLVM IR (-O2):
%struct.hiddev_report_info = type { i32, i32, i32, i32, i32 }
%struct.hiddev_usage_ref = type { i32, i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [54 x i8] c"Control 0x%x is hidden because it's probably broken.\0A\00"
@HIDIOCSUSAGE = global i32 0
@.str.1 = [52 x i8] c"HIDIOCSUSAGE failed - unable to set control value.\0A\00"
@HIDIOCSREPORT = global i32 0
@.str.2 = [53 x i8] c"HIDIOCSREPORT failed - unable to set control value.\0A\00"
define i32 @set_control_value(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca %struct.hiddev_report_info
%8 = alloca %struct.hiddev_usage_ref
%9 = tail call i64 @control_hidden(i32 %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %41
14: ; preds = %6
%15 = bitcast %struct.hiddev_report_info* %7 to i8*
%16 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %7, i64 0, i32 0
store i32 %1, i32* %16
%17 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %7, i64 0, i32 1
store i32 %2, i32* %17
%18 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %7, i64 0, i32 2
store i32 0, i32* %18
%19 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %7, i64 0, i32 3
store i32 0, i32* %19
%20 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %7, i64 0, i32 4
store i32 0, i32* %20
%21 = bitcast %struct.hiddev_usage_ref* %8 to i8*
%22 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %8, i64 0, i32 0
store i32 %1, i32* %22
%23 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %8, i64 0, i32 1
store i32 %2, i32* %23
%24 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %8, i64 0, i32 2
store i32 %3, i32* %24
%25 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %8, i64 0, i32 3
store i32 %4, i32* %25
%26 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %8, i64 0, i32 4
store i32 %5, i32* %26
%27 = load i32, i32* @HIDIOCSUSAGE
%28 = call i64 @ioctl(i32 %0, i32 %27, %struct.hiddev_usage_ref* %8)
%29 = icmp slt i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %14
%31 = load i32, i32* @HIDIOCSREPORT
%32 = bitcast %struct.hiddev_report_info* %7 to %struct.hiddev_usage_ref*
%33 = call i64 @ioctl(i32 %0, i32 %31, %struct.hiddev_usage_ref* %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %30, %14
%36 = phi i8* [ getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), %14 ], [ getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), %30 ]
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* %36)
br label %39
39: ; preds = %35, %30
%40 = phi i32 [ 1, %30 ], [ 0, %35 ]
br label %41
41: ; preds = %39, %11
%42 = phi i32 [ 0, %11 ], [ %40, %39 ]
ret i32 %42
}