#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 {
  unsigned int report_type;
  int report_id;
  int num_fields;
  int maxusage;
  int usage_index;
  unsigned char value;
  scalar_t__ field_index;
};
struct hiddev_report_info {
  unsigned int report_type;
  int report_id;
  int num_fields;
  int maxusage;
  int usage_index;
  unsigned char value;
  scalar_t__ field_index;
};
struct hiddev_field_info {
  unsigned int report_type;
  int report_id;
  int num_fields;
  int maxusage;
  int usage_index;
  unsigned char value;
  scalar_t__ field_index;
};

/* Variables and functions */
int /*<<< orphan*/ HIDIOCGFIELDINFO;
int /*<<< orphan*/ HIDIOCGREPORT;
int /*<<< orphan*/ HIDIOCGREPORTINFO;
int /*<<< orphan*/ HIDIOCGUCODE;
int /*<<< orphan*/ HIDIOCGUSAGE;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct hiddev_usage_ref *);
int /*<<< orphan*/ stderr;
int aq5_get_report(int fd, int report_id, unsigned report_type,
                   unsigned char *report_data) {
  struct hiddev_report_info rinfo;
  struct hiddev_field_info finfo;
  struct hiddev_usage_ref uref;
  int j;
  rinfo.report_type = report_type;
  rinfo.report_id = report_id;
  rinfo.num_fields = 1;
  if (ioctl(fd, HIDIOCGREPORTINFO, &rinfo) != 0) {
    fprintf(stderr, "failed to fetch input report id %d\n", report_id);
    return -1;
  }
  finfo.report_type = rinfo.report_type;
  finfo.report_id = rinfo.report_id;
  finfo.field_index = 0;
  if (ioctl(fd, HIDIOCGFIELDINFO, &finfo) != 0) {
    fprintf(stderr, "failed to fetch field info, report %d\n", report_id);
    return -1;
  }
  report_data[0] = report_id;
  if (ioctl(fd, HIDIOCGREPORT, &rinfo) != 0) {
    fprintf(stderr, "Failed to HIDIOCGREPORT for report %d\n", finfo.report_id);
    return -1;
  }
  for (j = 0; j < finfo.maxusage; j++) {
    uref.report_type = finfo.report_type;
    uref.report_id = finfo.report_id;
    uref.field_index = 0;
    uref.usage_index = j;
    ioctl(fd, HIDIOCGUCODE, &uref);
    ioctl(fd, HIDIOCGUSAGE, &uref);
    report_data[j + 1] = uref.value;
  }
  return 0;
}


x86 assembly (clang, -O0):
<aq5_get_report>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     118e <aq5_get_report+0x6e>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe83(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <aq5_get_report+0x192>
mov    -0x38(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x54(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    -0x58(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     11e5 <aq5_get_report+0xc5>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe50(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <aq5_get_report+0x192>
mov    -0xc(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1233 <aq5_get_report+0x113>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
mov    -0x54(%rbp),%edx
lea    0xe29(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <aq5_get_report+0x192>
movl   $0x0,-0x7c(%rbp)
mov    -0x7c(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jge    12ab <aq5_get_report+0x18b>
mov    -0x58(%rbp),%eax
mov    %eax,-0x78(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,-0x74(%rbp)
movq   $0x0,-0x60(%rbp)
mov    -0x7c(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
lea    -0x78(%rbp),%rdx
call   1030 <ioctl@plt>
mov    -0x8(%rbp),%edi
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
lea    -0x78(%rbp),%rdx
call   1030 <ioctl@plt>
mov    -0x64(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x7c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x7c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7c(%rbp)
jmp    123a <aq5_get_report+0x11a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aq5_get_report>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rcx,%r14
mov    %esi,%ebx
mov    %edi,%ebp
mov    %edx,0x28(%rsp)
mov    %esi,0x2c(%rsp)
movl   $0x1,0x30(%rsp)
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
lea    0x28(%rsp),%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
je     116f <aq5_get_report+0x4f>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
lea    0xe93(%rip),%rsi
jmp    11ac <aq5_get_report+0x8c>
mov    0x28(%rsp),%rax
mov    %rax,0x8(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
je     11c9 <aq5_get_report+0xa9>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %bl,(%r14)
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    0x28(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
je     11fc <aq5_get_report+0xdc>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    0xc(%rsp),%edx
lea    0xe51(%rip),%rsi
jmp    11ae <aq5_get_report+0x8e>
cmpl   $0x0,0x14(%rsp)
jle    1276 <aq5_get_report+0x156>
xor    %ebx,%ebx
lea    0x48(%rsp),%r15
mov    0x2daf(%rip),%r13
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x8(%rsp),%rax
mov    %rax,0x48(%rsp)
movq   $0x0,0x60(%rsp)
mov    %r12d,0x58(%rsp)
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1030 <ioctl@plt>
mov    0x0(%r13),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1030 <ioctl@plt>
movzbl 0x5c(%rsp),%eax
mov    %al,0x1(%r14,%r12,1)
add    $0x1,%r12
movslq 0x14(%rsp),%rax
cmp    %rax,%r12
jl     1220 <aq5_get_report+0x100>
jmp    11b8 <aq5_get_report+0x98>
xor    %ebx,%ebx
jmp    11b8 <aq5_get_report+0x98>

LLVM IR (-O0):
%struct.hiddev_report_info = type { i32, i32, i32, i32, i32, i8, i64 }
%struct.hiddev_field_info = type { i32, i32, i32, i32, i32, i8, i64 }
%struct.hiddev_usage_ref = type { i32, i32, i32, i32, i32, i8, i64 }
@HIDIOCGREPORTINFO = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"failed to fetch input report id %d\0A\00"
@HIDIOCGFIELDINFO = global i32 0
@.str.1 = [39 x i8] c"failed to fetch field info, report %d\0A\00"
@HIDIOCGREPORT = global i32 0
@.str.2 = [39 x i8] c"Failed to HIDIOCGREPORT for report %d\0A\00"
@HIDIOCGUCODE = global i32 0
@HIDIOCGUSAGE = global i32 0
define i32 @aq5_get_report(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca %struct.hiddev_report_info
%11 = alloca %struct.hiddev_field_info
%12 = alloca %struct.hiddev_usage_ref
%13 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%14 = load i32, i32* %8
%15 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %10, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %7
%17 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %10, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %10, i32 0, i32 2
store i32 1, i32* %18
%19 = load i32, i32* %6
%20 = load i32, i32* @HIDIOCGREPORTINFO
%21 = bitcast %struct.hiddev_report_info* %10 to %struct.hiddev_usage_ref*
%22 = call i64 @ioctl(i32 %19, i32 %20, %struct.hiddev_usage_ref* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %4
%25 = load i32, i32* @stderr
%26 = load i32, i32* %7
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 -1, i32* %5
br label %93
28: ; preds = %4
%29 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %10, i32 0, i32 0
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.hiddev_field_info, %struct.hiddev_field_info* %11, i32 0, i32 0
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %10, i32 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.hiddev_field_info, %struct.hiddev_field_info* %11, i32 0, i32 1
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.hiddev_field_info, %struct.hiddev_field_info* %11, i32 0, i32 6
store i64 0, i64* %35
%36 = load i32, i32* %6
%37 = load i32, i32* @HIDIOCGFIELDINFO
%38 = bitcast %struct.hiddev_field_info* %11 to %struct.hiddev_usage_ref*
%39 = call i64 @ioctl(i32 %36, i32 %37, %struct.hiddev_usage_ref* %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %28
%42 = load i32, i32* @stderr
%43 = load i32, i32* %7
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %43)
store i32 -1, i32* %5
br label %93
45: ; preds = %28
%46 = load i32, i32* %7
%47 = trunc i32 %46 to i8
%48 = load i8*, i8** %9
%49 = getelementptr inbounds i8, i8* %48, i64 0
store i8 %47, i8* %49
%50 = load i32, i32* %6
%51 = load i32, i32* @HIDIOCGREPORT
%52 = bitcast %struct.hiddev_report_info* %10 to %struct.hiddev_usage_ref*
%53 = call i64 @ioctl(i32 %50, i32 %51, %struct.hiddev_usage_ref* %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %45
%56 = load i32, i32* @stderr
%57 = getelementptr inbounds %struct.hiddev_field_info, %struct.hiddev_field_info* %11, i32 0, i32 1
%58 = load i32, i32* %57
%59 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %58)
store i32 -1, i32* %5
br label %93
60: ; preds = %45
store i32 0, i32* %13
br label %61
61: ; preds = %89, %60
%62 = load i32, i32* %13
%63 = getelementptr inbounds %struct.hiddev_field_info, %struct.hiddev_field_info* %11, i32 0, i32 3
%64 = load i32, i32* %63
%65 = icmp slt i32 %62, %64
br i1 %65, label %66, label %92
66: ; preds = %61
%67 = getelementptr inbounds %struct.hiddev_field_info, %struct.hiddev_field_info* %11, i32 0, i32 0
%68 = load i32, i32* %67
%69 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %12, i32 0, i32 0
store i32 %68, i32* %69
%70 = getelementptr inbounds %struct.hiddev_field_info, %struct.hiddev_field_info* %11, i32 0, i32 1
%71 = load i32, i32* %70
%72 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %12, i32 0, i32 1
store i32 %71, i32* %72
%73 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %12, i32 0, i32 6
store i64 0, i64* %73
%74 = load i32, i32* %13
%75 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %12, i32 0, i32 4
store i32 %74, i32* %75
%76 = load i32, i32* %6
%77 = load i32, i32* @HIDIOCGUCODE
%78 = call i64 @ioctl(i32 %76, i32 %77, %struct.hiddev_usage_ref* %12)
%79 = load i32, i32* %6
%80 = load i32, i32* @HIDIOCGUSAGE
%81 = call i64 @ioctl(i32 %79, i32 %80, %struct.hiddev_usage_ref* %12)
%82 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %12, i32 0, i32 5
%83 = load i8, i8* %82
%84 = load i8*, i8** %9
%85 = load i32, i32* %13
%86 = add nsw i32 %85, 1
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %84, i64 %87
store i8 %83, i8* %88
br label %89
89: ; preds = %66
%90 = load i32, i32* %13
%91 = add nsw i32 %90, 1
store i32 %91, i32* %13
br label %61
92: ; preds = %61
store i32 0, i32* %5
br label %93
93: ; preds = %92, %55, %41, %24
%94 = load i32, i32* %5
ret i32 %94
}

LLVM IR (-O2):
%struct.hiddev_report_info = type { i32, i32, i32, i32, i32, i8, i64 }
%struct.hiddev_field_info = type { i32, i32, i32, i32, i32, i8, i64 }
%struct.hiddev_usage_ref = type { i32, i32, i32, i32, i32, i8, i64 }
@HIDIOCGREPORTINFO = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"failed to fetch input report id %d\0A\00"
@HIDIOCGFIELDINFO = global i32 0
@.str.1 = [39 x i8] c"failed to fetch field info, report %d\0A\00"
@HIDIOCGREPORT = global i32 0
@.str.2 = [39 x i8] c"Failed to HIDIOCGREPORT for report %d\0A\00"
@HIDIOCGUCODE = global i32 0
@HIDIOCGUSAGE = global i32 0
define i32 @aq5_get_report(i32 %0, i32 %1, i32 %2, i8* nocapture writeonly %3) {
%5 = alloca %struct.hiddev_report_info
%6 = alloca %struct.hiddev_field_info
%7 = alloca %struct.hiddev_usage_ref
%8 = bitcast %struct.hiddev_report_info* %5 to i8*
%9 = bitcast %struct.hiddev_field_info* %6 to i8*
%10 = bitcast %struct.hiddev_usage_ref* %7 to i8*
%11 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %5, i64 0, i32 0
store i32 %2, i32* %11
%12 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %5, i64 0, i32 1
store i32 %1, i32* %12
%13 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %5, i64 0, i32 2
store i32 1, i32* %13
%14 = load i32, i32* @HIDIOCGREPORTINFO
%15 = bitcast %struct.hiddev_report_info* %5 to %struct.hiddev_usage_ref*
%16 = call i64 @ioctl(i32 %0, i32 %14, %struct.hiddev_usage_ref* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %68
21: ; preds = %4
%22 = load i32, i32* %11
%23 = getelementptr inbounds %struct.hiddev_field_info, %struct.hiddev_field_info* %6, i64 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %12
%25 = getelementptr inbounds %struct.hiddev_field_info, %struct.hiddev_field_info* %6, i64 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.hiddev_field_info, %struct.hiddev_field_info* %6, i64 0, i32 6
store i64 0, i64* %26
%27 = load i32, i32* @HIDIOCGFIELDINFO
%28 = bitcast %struct.hiddev_field_info* %6 to %struct.hiddev_usage_ref*
%29 = call i64 @ioctl(i32 %0, i32 %27, %struct.hiddev_usage_ref* %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %21
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %68
34: ; preds = %21
%35 = trunc i32 %1 to i8
store i8 %35, i8* %3
%36 = load i32, i32* @HIDIOCGREPORT
%37 = call i64 @ioctl(i32 %0, i32 %36, %struct.hiddev_usage_ref* %15)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %49
39: ; preds = %34
%40 = getelementptr inbounds %struct.hiddev_field_info, %struct.hiddev_field_info* %6, i64 0, i32 3
%41 = load i32, i32* %40
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %68
43: ; preds = %39
%44 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %7, i64 0, i32 0
%45 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %7, i64 0, i32 1
%46 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %7, i64 0, i32 6
%47 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %7, i64 0, i32 4
%48 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %7, i64 0, i32 5
br label %53
49: ; preds = %34
%50 = load i32, i32* @stderr
%51 = load i32, i32* %25
%52 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %51)
br label %68
53: ; preds = %43, %53
%54 = phi i64 [ 0, %43 ], [ %63, %53 ]
%55 = load i32, i32* %23
store i32 %55, i32* %44
%56 = load i32, i32* %25
store i32 %56, i32* %45
store i64 0, i64* %46
%57 = trunc i64 %54 to i32
store i32 %57, i32* %47
%58 = load i32, i32* @HIDIOCGUCODE
%59 = call i64 @ioctl(i32 %0, i32 %58, %struct.hiddev_usage_ref* %7)
%60 = load i32, i32* @HIDIOCGUSAGE
%61 = call i64 @ioctl(i32 %0, i32 %60, %struct.hiddev_usage_ref* %7)
%62 = load i8, i8* %48
%63 = add nuw nsw i64 %54, 1
%64 = getelementptr inbounds i8, i8* %3, i64 %63
store i8 %62, i8* %64
%65 = load i32, i32* %40
%66 = sext i32 %65 to i64
%67 = icmp slt i64 %63, %66
br i1 %67, label %53, label %68
68: ; preds = %53, %39, %49, %31, %18
%69 = phi i32 [ -1, %18 ], [ -1, %31 ], [ -1, %49 ], [ 0, %39 ], [ 0, %53 ]
ret i32 %69
}