#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*/ HIDIOCGREPORT;
int /*<<< orphan*/ HIDIOCGUSAGE;
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 get_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,
  };
  if (ioctl(fd, HIDIOCGREPORT, &rinfo) < 0) {
    fprintf(stderr, "HIDIOCGREPORT failed - unable to get control value.\n");
    return false;
  }
  if (ioctl(fd, HIDIOCGUSAGE, &uref) < 0) {
    fprintf(stderr, "HIDIOCGUSAGE failed - unable to get control value.\n");
    return false;
  }
  *value = uref.value;
  return true;
}


x86 assembly (clang, -O0):
<get_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    %r9,-0x20(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <control_hidden@plt>
cmp    $0x0,%rax
je     1184 <get_control_value+0x54>
mov    0x2e73(%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    1258 <get_control_value+0x128>
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x44(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1206 <get_control_value+0xd6>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1258 <get_control_value+0x128>
mov    -0x8(%rbp),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1248 <get_control_value+0x118>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1258 <get_control_value+0x128>
mov    -0x40(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_control_value>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9,%r14
mov    %r8d,%r12d
mov    %ecx,%r13d
mov    %edx,%ebp
mov    %esi,%ebx
mov    %edi,%r15d
mov    %edx,%edi
call   1040 <control_hidden@plt>
test   %rax,%rax
je     117a <get_control_value+0x4a>
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
lea    0xe96(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    121c <get_control_value+0xec>
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)
movl   $0x0,0x18(%rsp)
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdx
mov    %r15d,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     11f1 <get_control_value+0xc1>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r15d,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     1203 <get_control_value+0xd3>
mov    0x18(%rsp),%eax
mov    %eax,(%r14)
mov    $0x1,%ebx
jmp    121c <get_control_value+0xec>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
jmp    1213 <get_control_value+0xe3>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
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"
@HIDIOCGREPORT = global i32 0
@.str.1 = [53 x i8] c"HIDIOCGREPORT failed - unable to get control value.\0A\00"
@HIDIOCGUSAGE = global i32 0
@.str.2 = [52 x i8] c"HIDIOCGUSAGE failed - unable to get control value.\0A\00"
define i32 @get_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 %60
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
store i32 0, i32* %39
%40 = load i32, i32* %8
%41 = load i32, i32* @HIDIOCGREPORT
%42 = bitcast %struct.hiddev_report_info* %14 to %struct.hiddev_usage_ref*
%43 = call i64 @ioctl(i32 %40, i32 %41, %struct.hiddev_usage_ref* %42)
%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 ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %7
br label %60
48: ; preds = %23
%49 = load i32, i32* %8
%50 = load i32, i32* @HIDIOCGUSAGE
%51 = call i64 @ioctl(i32 %49, i32 %50, %struct.hiddev_usage_ref* %15)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %48
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %7
br label %60
56: ; preds = %48
%57 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %15, i32 0, i32 4
%58 = load i32, i32* %57
%59 = load i32*, i32** %13
store i32 %58, i32* %59
store i32 1, i32* %7
br label %60
60: ; preds = %56, %53, %45, %19
%61 = load i32, i32* %7
ret i32 %61
}

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"
@HIDIOCGREPORT = global i32 0
@.str.1 = [53 x i8] c"HIDIOCGREPORT failed - unable to get control value.\0A\00"
@HIDIOCGUSAGE = global i32 0
@.str.2 = [52 x i8] c"HIDIOCGUSAGE failed - unable to get control value.\0A\00"
define i32 @get_control_value(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32* nocapture writeonly %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 %45
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 0, i32* %26
%27 = load i32, i32* @HIDIOCGREPORT
%28 = bitcast %struct.hiddev_report_info* %7 to %struct.hiddev_usage_ref*
%29 = call i64 @ioctl(i32 %0, i32 %27, %struct.hiddev_usage_ref* %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %14
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
br label %43
34: ; preds = %14
%35 = load i32, i32* @HIDIOCGUSAGE
%36 = call i64 @ioctl(i32 %0, i32 %35, %struct.hiddev_usage_ref* %8)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0))
br label %43
41: ; preds = %34
%42 = load i32, i32* %26
store i32 %42, i32* %5
br label %43
43: ; preds = %41, %38, %31
%44 = phi i32 [ 0, %31 ], [ 0, %38 ], [ 1, %41 ]
br label %45
45: ; preds = %43, %11
%46 = phi i32 [ 0, %11 ], [ %44, %43 ]
ret i32 %46
}