#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_id;
  int usage_code;
  int usage_index;
  int num_fields;
  void *report_type;
  int /*<<< orphan*/ value;
  scalar_t__ field_index;
};
struct hiddev_report_info {
  int report_id;
  int usage_code;
  int usage_index;
  int num_fields;
  void *report_type;
  int /*<<< orphan*/ value;
  scalar_t__ field_index;
};

/* Variables and functions */
int /*<<< orphan*/ HIDIOCSREPORT;
int /*<<< orphan*/ HIDIOCSUSAGE;
void *HID_REPORT_TYPE_OUTPUT;
int errno;
int ioctl(int, int /*<<< orphan*/, struct hiddev_usage_ref *);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int hiddev_write(const unsigned char data[64], int fd, int usage_code) {
  int rc = 0, uindex, error;
  struct hiddev_usage_ref uref;
  struct hiddev_report_info rinfo;
  uref.report_id = *data++;
  uref.report_type = HID_REPORT_TYPE_OUTPUT;
  uref.field_index = 0;
  uref.usage_code = usage_code;
  for (uindex = 0; uindex < 63; uindex++) {
    uref.usage_index = uindex;
    uref.value = *data++;
    rc = ioctl(fd, HIDIOCSUSAGE, &uref);
    if (rc != 0)
      goto err;
  }
  rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
  rinfo.report_id = 0x0;
  rinfo.num_fields = 1;
  rc = ioctl(fd, HIDIOCSREPORT, &rinfo);
  if (rc != 0)
    goto err2;
  return 0;
err2:
  printf("HIDIOCSREPORT\n");
err:
  error = errno;
  printf("Error in IOCTL: %s\n", strerror(error));
  return rc;
}


x86 assembly (clang, -O0):
<hiddev_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movzbl (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x3f,-0x20(%rbp)
jge    11e3 <hiddev_write+0xb3>
mov    -0x20(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movzbl (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11d0 <hiddev_write+0xa0>
jmp    1240 <hiddev_write+0x110>
jmp    11d5 <hiddev_write+0xa5>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1184 <hiddev_write+0x54>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x68(%rbp)
movl   $0x0,-0x78(%rbp)
movl   $0x1,-0x6c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
lea    -0x78(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1226 <hiddev_write+0xf6>
jmp    1232 <hiddev_write+0x102>
movl   $0x0,-0x4(%rbp)
jmp    126b <hiddev_write+0x13b>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <strerror@plt>
mov    %rax,%rsi
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hiddev_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %esi,%r14d
mov    %rdi,%r13
movzbl (%rdi),%eax
mov    %eax,0x8(%rsp)
mov    0x2e86(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x18(%rsp)
movq   $0x0,0x28(%rsp)
mov    %edx,0xc(%rsp)
xor    %ebx,%ebx
mov    0x2e50(%rip),%r12
lea    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,0x10(%rsp)
movzbl 0x1(%r13,%rbx,1),%eax
mov    %eax,0x20(%rsp)
mov    (%r12),%esi
mov    %r14d,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
jne    1203 <hiddev_write+0xc3>
add    $0x1,%rbx
cmp    $0x3f,%ebx
jne    1190 <hiddev_write+0x50>
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x40(%rsp)
movl   $0x0,0x30(%rsp)
movl   $0x1,0x3c(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
lea    0x30(%rsp),%rdx
mov    %r14d,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
je     1235 <hiddev_write+0xf5>
mov    %eax,%ebp
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
jmp    1205 <hiddev_write+0xc5>
mov    %eax,%ebp
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xde6(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
jmp    1224 <hiddev_write+0xe4>

LLVM IR (-O0):
%struct.hiddev_usage_ref = type { i32, i32, i32, i32, i8*, i32, i64 }
%struct.hiddev_report_info = type { i32, i32, i32, i32, i8*, i32, i64 }
@HID_REPORT_TYPE_OUTPUT = global i8* null
@HIDIOCSUSAGE = global i32 0
@HIDIOCSREPORT = global i32 0
@.str = [15 x i8] c"HIDIOCSREPORT\0A\00"
@errno = global i32 0
@.str.1 = [20 x i8] c"Error in IOCTL: %s\0A\00"
define i32 @hiddev_write(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.hiddev_usage_ref
%12 = alloca %struct.hiddev_report_info
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %5
%15 = load i8, i8* %13
%16 = zext i8 %15 to i32
%17 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %11, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i8*, i8** @HID_REPORT_TYPE_OUTPUT
%19 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %11, i32 0, i32 4
store i8* %18, i8** %19
%20 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %11, i32 0, i32 6
store i64 0, i64* %20
%21 = load i32, i32* %7
%22 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %11, i32 0, i32 1
store i32 %21, i32* %22
store i32 0, i32* %9
br label %23
23: ; preds = %41, %3
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 63
br i1 %25, label %26, label %44
26: ; preds = %23
%27 = load i32, i32* %9
%28 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %11, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %5
%31 = load i8, i8* %29
%32 = zext i8 %31 to i32
%33 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %11, i32 0, i32 5
store i32 %32, i32* %33
%34 = load i32, i32* %6
%35 = load i32, i32* @HIDIOCSUSAGE
%36 = call i32 @ioctl(i32 %34, i32 %35, %struct.hiddev_usage_ref* %11)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %26
br label %59
40: ; preds = %26
br label %41
41: ; preds = %40
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %23
44: ; preds = %23
%45 = load i8*, i8** @HID_REPORT_TYPE_OUTPUT
%46 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %12, i32 0, i32 4
store i8* %45, i8** %46
%47 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %12, i32 0, i32 0
store i32 0, i32* %47
%48 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %12, i32 0, i32 3
store i32 1, i32* %48
%49 = load i32, i32* %6
%50 = load i32, i32* @HIDIOCSREPORT
%51 = bitcast %struct.hiddev_report_info* %12 to %struct.hiddev_usage_ref*
%52 = call i32 @ioctl(i32 %49, i32 %50, %struct.hiddev_usage_ref* %51)
store i32 %52, i32* %8
%53 = load i32, i32* %8
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %44
br label %57
56: ; preds = %44
store i32 0, i32* %4
br label %65
57: ; preds = %55
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %59
59: ; preds = %57, %39
%60 = load i32, i32* @errno
store i32 %60, i32* %10
%61 = load i32, i32* %10
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %62)
%64 = load i32, i32* %8
store i32 %64, i32* %4
br label %65
65: ; preds = %59, %56
%66 = load i32, i32* %4
ret i32 %66
}

LLVM IR (-O2):
%struct.hiddev_usage_ref = type { i32, i32, i32, i32, i8*, i32, i64 }
%struct.hiddev_report_info = type { i32, i32, i32, i32, i8*, i32, i64 }
@HID_REPORT_TYPE_OUTPUT = global i8* null
@HIDIOCSUSAGE = global i32 0
@HIDIOCSREPORT = global i32 0
@errno = global i32 0
@.str.1 = [20 x i8] c"Error in IOCTL: %s\0A\00"
@str = [14 x i8] c"HIDIOCSREPORT\00"
define i32 @hiddev_write(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.hiddev_usage_ref
%5 = alloca %struct.hiddev_report_info
%6 = bitcast %struct.hiddev_usage_ref* %4 to i8*
%7 = bitcast %struct.hiddev_report_info* %5 to i8*
%8 = load i8, i8* %0
%9 = zext i8 %8 to i32
%10 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %4, i64 0, i32 0
store i32 %9, i32* %10
%11 = load i8*, i8** @HID_REPORT_TYPE_OUTPUT
%12 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %4, i64 0, i32 4
store i8* %11, i8** %12
%13 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %4, i64 0, i32 6
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %4, i64 0, i32 1
store i32 %2, i32* %14
%15 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %4, i64 0, i32 2
%16 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %4, i64 0, i32 5
br label %20
17: ; preds = %20
%18 = add nuw nsw i32 %22, 1
%19 = icmp eq i32 %18, 63
br i1 %19, label %29, label %20
20: ; preds = %3, %17
%21 = phi i8* [ %0, %3 ], [ %23, %17 ]
%22 = phi i32 [ 0, %3 ], [ %18, %17 ]
%23 = getelementptr inbounds i8, i8* %21, i64 1
store i32 %22, i32* %15
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
store i32 %25, i32* %16
%26 = load i32, i32* @HIDIOCSUSAGE
%27 = call i32 @ioctl(i32 %1, i32 %26, %struct.hiddev_usage_ref* %4)
%28 = icmp eq i32 %27, 0
br i1 %28, label %17, label %40
29: ; preds = %17
%30 = load i8*, i8** @HID_REPORT_TYPE_OUTPUT
%31 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %5, i64 0, i32 4
store i8* %30, i8** %31
%32 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %5, i64 0, i32 0
store i32 0, i32* %32
%33 = getelementptr inbounds %struct.hiddev_report_info, %struct.hiddev_report_info* %5, i64 0, i32 3
store i32 1, i32* %33
%34 = load i32, i32* @HIDIOCSREPORT
%35 = bitcast %struct.hiddev_report_info* %5 to %struct.hiddev_usage_ref*
%36 = call i32 @ioctl(i32 %1, i32 %34, %struct.hiddev_usage_ref* %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %45, label %38
38: ; preds = %29
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %40
40: ; preds = %20, %38
%41 = phi i32 [ %36, %38 ], [ %27, %20 ]
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %29, %40
%46 = phi i32 [ %41, %40 ], [ 0, %29 ]
ret i32 %46
}