#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;
  scalar_t__ usage_index;
  scalar_t__ field_index;
  int /*<<< orphan*/ report_type;
};

/* Variables and functions */
int /*<<< orphan*/ HIDIOCGUCODE;
int /*<<< orphan*/ HID_REPORT_TYPE_OUTPUT;
int errno;
int ioctl(int, int /*<<< orphan*/, struct hiddev_usage_ref *);
int /*<<< orphan*/ printf(char *, char *);
char *strerror(int);
int get_usagecode(int fd) {
  struct hiddev_usage_ref uref;
  int rc, error;
  uref.report_type = HID_REPORT_TYPE_OUTPUT;
  uref.report_id = 0x0;
  uref.field_index = 0;
  uref.usage_index = 0;
  rc = ioctl(fd, HIDIOCGUCODE, &uref);
  if (rc < 0) {
    error = errno;
    printf("Error gettin usage code: %s\n", strerror(error));
    return rc;
  }
  return uref.usage_code;
}


x86 assembly (clang, -O0):
<get_usagecode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11ae <get_usagecode+0x7e>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   1050 <strerror@plt>
mov    %rax,%rsi
lea    0xe62(%rip),%rdi
call   1030 <printf@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b4 <get_usagecode+0x84>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_usagecode>:
push   %rbx
sub    $0x20,%rsp
mov    0x2eac(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movl   $0x0,(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1040 <ioctl@plt>
test   %eax,%eax
js     116c <get_usagecode+0x3c>
mov    0x4(%rsp),%ebx
jmp    118b <get_usagecode+0x5b>
mov    %eax,%ebx
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe7d(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.hiddev_usage_ref = type { i32, i32, i64, i64, i32 }
@HID_REPORT_TYPE_OUTPUT = global i32 0
@HIDIOCGUCODE = global i32 0
@errno = global i32 0
@.str = [29 x i8] c"Error gettin usage code: %s\0A\00"
define i32 @get_usagecode(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.hiddev_usage_ref
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* @HID_REPORT_TYPE_OUTPUT
%8 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %4, i32 0, i32 4
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %4, i32 0, i32 0
store i32 0, i32* %9
%10 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %4, i32 0, i32 3
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %4, i32 0, i32 2
store i64 0, i64* %11
%12 = load i32, i32* %3
%13 = load i32, i32* @HIDIOCGUCODE
%14 = call i32 @ioctl(i32 %12, i32 %13, %struct.hiddev_usage_ref* %4)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %1
%18 = load i32, i32* @errno
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* %5
store i32 %22, i32* %2
br label %26
23: ; preds = %1
%24 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %4, i32 0, i32 1
%25 = load i32, i32* %24
store i32 %25, i32* %2
br label %26
26: ; preds = %23, %17
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.hiddev_usage_ref = type { i32, i32, i64, i64, i32 }
@HID_REPORT_TYPE_OUTPUT = global i32 0
@HIDIOCGUCODE = global i32 0
@errno = global i32 0
@.str = [29 x i8] c"Error gettin usage code: %s\0A\00"
define i32 @get_usagecode(i32 %0) {
%2 = alloca %struct.hiddev_usage_ref
%3 = bitcast %struct.hiddev_usage_ref* %2 to i8*
%4 = load i32, i32* @HID_REPORT_TYPE_OUTPUT
%5 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %2, i64 0, i32 4
store i32 %4, i32* %5
%6 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %2, i64 0, i32 0
store i32 0, i32* %6
%7 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %2, i64 0, i32 2
%8 = bitcast i64* %7 to i8*
%9 = load i32, i32* @HIDIOCGUCODE
%10 = call i32 @ioctl(i32 %0, i32 %9, %struct.hiddev_usage_ref* %2)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %19
16: ; preds = %1
%17 = getelementptr inbounds %struct.hiddev_usage_ref, %struct.hiddev_usage_ref* %2, i64 0, i32 1
%18 = load i32, i32* %17
br label %19
19: ; preds = %16, %12
%20 = phi i32 [ %10, %12 ], [ %18, %16 ]
ret i32 %20
}