#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uidev;
struct TYPE_2__ {
  int vendor;
  int product;
  int version;
  int /*<<< orphan*/ bustype;
};
struct uinput_user_dev {
  TYPE_1__ id;
  int /*<<< orphan*/ name;
};

/* Variables and functions */
int /*<<< orphan*/ BUS_USB;
int /*<<< orphan*/ EV_KEY;
int /*<<< orphan*/ EV_REL;
int O_NONBLOCK;
int O_WRONLY;
int /*<<< orphan*/ REL_X;
int /*<<< orphan*/ REL_Y;
int /*<<< orphan*/ UINPUT_MAX_NAME_SIZE;
int /*<<< orphan*/ UI_DEV_CREATE;
int /*<<< orphan*/ UI_SET_EVBIT;
int /*<<< orphan*/ UI_SET_KEYBIT;
int /*<<< orphan*/ UI_SET_RELBIT;
scalar_t__ ioctl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ memset(struct uinput_user_dev *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int /*<<< orphan*/, char *);
scalar_t__ write(int, struct uinput_user_dev *, int);
int setup_device(int key_max) {
  int uinput_fd;
  int i;
  struct uinput_user_dev uidev;
  uinput_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
  if (uinput_fd < 0) {
    uinput_fd = open("/dev/input/uinput", O_WRONLY | O_NONBLOCK);
    if (uinput_fd < 0)
      return -1;
  }
  if (ioctl(uinput_fd, UI_SET_EVBIT, EV_KEY) < 0)
    return -1;
  if (ioctl(uinput_fd, UI_SET_EVBIT, EV_REL) < 0)
    return -1;
  if (ioctl(uinput_fd, UI_SET_RELBIT, REL_X) < 0)
    return -1;
  if (ioctl(uinput_fd, UI_SET_RELBIT, REL_Y) < 0)
    return -1;
  for (i = 0; i < key_max; i++)
    if (ioctl(uinput_fd, UI_SET_KEYBIT, i) < 0)
      return -1;
  memset(&uidev, 0, sizeof(uidev));
  snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "Generic USB input mapper");
  uidev.id.bustype = BUS_USB;
  uidev.id.vendor = 0x1;
  uidev.id.product = 0x1;
  uidev.id.version = 1;
  if (write(uinput_fd, &uidev, sizeof(uidev)) < 0)
    return -1;
  if (ioctl(uinput_fd, UI_DEV_CREATE) < 0)
    return -1;
  return uinput_fd;
}


x86 assembly (clang, -O0):
<setup_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2e65(%rip),%rax
or     (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c2 <setup_device+0x72>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11bd <setup_device+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <setup_device+0x22b>
jmp    11c2 <setup_device+0x72>
mov    -0xc(%rbp),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    11f4 <setup_device+0xa4>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <setup_device+0x22b>
mov    -0xc(%rbp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1226 <setup_device+0xd6>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <setup_device+0x22b>
mov    -0xc(%rbp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1258 <setup_device+0x108>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <setup_device+0x22b>
mov    -0xc(%rbp),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    128a <setup_device+0x13a>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <setup_device+0x22b>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12dc <setup_device+0x18c>
mov    -0xc(%rbp),%edi
mov    0x2ce9(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    12c9 <setup_device+0x179>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <setup_device+0x22b>
jmp    12ce <setup_device+0x17e>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1291 <setup_device+0x141>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%edi
mov    0x2cb2(%rip),%rax
mov    (%rax),%esi
lea    0xd1f(%rip),%rdx
call   1040 <snprintf@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x28(%rbp)
movl   $0x1,-0x24(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    134c <setup_device+0x1fc>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <setup_device+0x22b>
mov    -0xc(%rbp),%edi
mov    0x2c9a(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1375 <setup_device+0x225>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <setup_device+0x22b>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_device>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r15d
mov    0x2e1a(%rip),%r14
mov    0x2e63(%rip),%rbp
mov    0x0(%rbp),%esi
or     (%r14),%esi
lea    0xe86(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11a1 <setup_device+0x51>
mov    0x0(%rbp),%esi
or     (%r14),%esi
lea    0xe7a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12e6 <setup_device+0x196>
mov    0x2e08(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     12ec <setup_device+0x19c>
mov    0x0(%rbp),%esi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
js     12ec <setup_device+0x19c>
mov    0x2dcf(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
js     12ec <setup_device+0x19c>
mov    0x0(%rbp),%esi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
js     12ec <setup_device+0x19c>
test   %r15d,%r15d
jle    1260 <setup_device+0x110>
mov    0x2d57(%rip),%r12
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%r12),%esi
mov    %ebx,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
js     12ec <setup_device+0x19c>
add    $0x1,%ebp
cmp    %ebp,%r15d
jne    1240 <setup_device+0xf0>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    0x18(%rsp),%edi
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
lea    0xd96(%rip),%rdx
call   1040 <snprintf@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
movabs $0x100000001,%rax
mov    %rax,0x8(%rsp)
movl   $0x1,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x14,%edx
call   1030 <write@plt>
test   %rax,%rax
js     12ec <setup_device+0x19c>
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
mov    $0xffffffff,%r14d
cmovns %ebx,%r14d
jmp    12ec <setup_device+0x19c>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.uinput_user_dev = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32, i32, i32, i32 }
@.str = [12 x i8] c"/dev/uinput\00"
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [18 x i8] c"/dev/input/uinput\00"
@UI_SET_EVBIT = global i32 0
@EV_KEY = global i32 0
@EV_REL = global i32 0
@UI_SET_RELBIT = global i32 0
@REL_X = global i32 0
@REL_Y = global i32 0
@UI_SET_KEYBIT = global i32 0
@UINPUT_MAX_NAME_SIZE = global i32 0
@.str.2 = [25 x i8] c"Generic USB input mapper\00"
@BUS_USB = global i32 0
@UI_DEV_CREATE = global i32 0
define i32 @setup_device(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.uinput_user_dev
store i32 %0, i32* %3
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %1
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_NONBLOCK
%16 = or i32 %14, %15
%17 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %13
store i32 -1, i32* %2
br label %93
21: ; preds = %13
br label %22
22: ; preds = %21, %1
%23 = load i32, i32* %4
%24 = load i32, i32* @UI_SET_EVBIT
%25 = load i32, i32* @EV_KEY
%26 = call i64 (i32, i32, ...) @ioctl(i32 %23, i32 %24, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
store i32 -1, i32* %2
br label %93
29: ; preds = %22
%30 = load i32, i32* %4
%31 = load i32, i32* @UI_SET_EVBIT
%32 = load i32, i32* @EV_REL
%33 = call i64 (i32, i32, ...) @ioctl(i32 %30, i32 %31, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29
store i32 -1, i32* %2
br label %93
36: ; preds = %29
%37 = load i32, i32* %4
%38 = load i32, i32* @UI_SET_RELBIT
%39 = load i32, i32* @REL_X
%40 = call i64 (i32, i32, ...) @ioctl(i32 %37, i32 %38, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 -1, i32* %2
br label %93
43: ; preds = %36
%44 = load i32, i32* %4
%45 = load i32, i32* @UI_SET_RELBIT
%46 = load i32, i32* @REL_Y
%47 = call i64 (i32, i32, ...) @ioctl(i32 %44, i32 %45, i32 %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %43
store i32 -1, i32* %2
br label %93
50: ; preds = %43
store i32 0, i32* %5
br label %51
51: ; preds = %63, %50
%52 = load i32, i32* %5
%53 = load i32, i32* %3
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %66
55: ; preds = %51
%56 = load i32, i32* %4
%57 = load i32, i32* @UI_SET_KEYBIT
%58 = load i32, i32* %5
%59 = call i64 (i32, i32, ...) @ioctl(i32 %56, i32 %57, i32 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55
store i32 -1, i32* %2
br label %93
62: ; preds = %55
br label %63
63: ; preds = %62
%64 = load i32, i32* %5
%65 = add nsw i32 %64, 1
store i32 %65, i32* %5
br label %51
66: ; preds = %51
%67 = call i32 @memset(%struct.uinput_user_dev* %6, i32 0, i32 20)
%68 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %6, i32 0, i32 1
%69 = load i32, i32* %68
%70 = load i32, i32* @UINPUT_MAX_NAME_SIZE
%71 = call i32 @snprintf(i32 %69, i32 %70, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%72 = load i32, i32* @BUS_USB
%73 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %6, i32 0, i32 0
%74 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %73, i32 0, i32 3
store i32 %72, i32* %74
%75 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %6, i32 0, i32 0
%76 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %75, i32 0, i32 0
store i32 1, i32* %76
%77 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %6, i32 0, i32 0
%78 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %77, i32 0, i32 1
store i32 1, i32* %78
%79 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %6, i32 0, i32 0
%80 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %79, i32 0, i32 2
store i32 1, i32* %80
%81 = load i32, i32* %4
%82 = call i64 @write(i32 %81, %struct.uinput_user_dev* %6, i32 20)
%83 = icmp slt i64 %82, 0
br i1 %83, label %84, label %85
84: ; preds = %66
store i32 -1, i32* %2
br label %93
85: ; preds = %66
%86 = load i32, i32* %4
%87 = load i32, i32* @UI_DEV_CREATE
%88 = call i64 (i32, i32, ...) @ioctl(i32 %86, i32 %87)
%89 = icmp slt i64 %88, 0
br i1 %89, label %90, label %91
90: ; preds = %85
store i32 -1, i32* %2
br label %93
91: ; preds = %85
%92 = load i32, i32* %4
store i32 %92, i32* %2
br label %93
93: ; preds = %91, %90, %84, %61, %49, %42, %35, %28, %20
%94 = load i32, i32* %2
ret i32 %94
}

LLVM IR (-O2):
%struct.uinput_user_dev = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32, i32, i32, i32 }
@.str = [12 x i8] c"/dev/uinput\00"
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [18 x i8] c"/dev/input/uinput\00"
@UI_SET_EVBIT = global i32 0
@EV_KEY = global i32 0
@EV_REL = global i32 0
@UI_SET_RELBIT = global i32 0
@REL_X = global i32 0
@REL_Y = global i32 0
@UI_SET_KEYBIT = global i32 0
@UINPUT_MAX_NAME_SIZE = global i32 0
@.str.2 = [25 x i8] c"Generic USB input mapper\00"
@BUS_USB = global i32 0
@UI_DEV_CREATE = global i32 0
define i32 @setup_device(i32 %0) {
%2 = alloca %struct.uinput_user_dev
%3 = bitcast %struct.uinput_user_dev* %2 to i8*
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_NONBLOCK
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %64, label %15
15: ; preds = %9, %1
%16 = phi i32 [ %13, %9 ], [ %7, %1 ]
%17 = load i32, i32* @UI_SET_EVBIT
%18 = load i32, i32* @EV_KEY
%19 = tail call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %17, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %64, label %21
21: ; preds = %15
%22 = load i32, i32* @UI_SET_EVBIT
%23 = load i32, i32* @EV_REL
%24 = tail call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %22, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %64, label %26
26: ; preds = %21
%27 = load i32, i32* @UI_SET_RELBIT
%28 = load i32, i32* @REL_X
%29 = tail call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %27, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %64, label %31
31: ; preds = %26
%32 = load i32, i32* @UI_SET_RELBIT
%33 = load i32, i32* @REL_Y
%34 = tail call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %32, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %64, label %36
36: ; preds = %31
%37 = icmp sgt i32 %0, 0
br i1 %37, label %40, label %46
38: ; preds = %40
%39 = icmp eq i32 %45, %0
br i1 %39, label %46, label %40
40: ; preds = %36, %38
%41 = phi i32 [ %45, %38 ], [ 0, %36 ]
%42 = load i32, i32* @UI_SET_KEYBIT
%43 = tail call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %42, i32 %41)
%44 = icmp slt i64 %43, 0
%45 = add nuw nsw i32 %41, 1
br i1 %44, label %64, label %38
46: ; preds = %38, %36
%47 = call i32 @memset(%struct.uinput_user_dev* %2, i32 0, i32 20)
%48 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %2, i64 0, i32 1
%49 = load i32, i32* %48
%50 = load i32, i32* @UINPUT_MAX_NAME_SIZE
%51 = call i32 @snprintf(i32 %49, i32 %50, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%52 = load i32, i32* @BUS_USB
%53 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %2, i64 0, i32 0, i32 3
store i32 %52, i32* %53
%54 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %2, i64 0, i32 0, i32 0
store i32 1, i32* %54
%55 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %2, i64 0, i32 0, i32 1
store i32 1, i32* %55
%56 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %2, i64 0, i32 0, i32 2
store i32 1, i32* %56
%57 = call i64 @write(i32 %16, %struct.uinput_user_dev* %2, i32 20)
%58 = icmp slt i64 %57, 0
br i1 %58, label %64, label %59
59: ; preds = %46
%60 = load i32, i32* @UI_DEV_CREATE
%61 = call i64 (i32, i32, ...) @ioctl(i32 %16, i32 %60)
%62 = icmp slt i64 %61, 0
%63 = select i1 %62, i32 -1, i32 %16
br label %64
64: ; preds = %40, %59, %46, %31, %26, %21, %15, %9
%65 = phi i32 [ -1, %9 ], [ -1, %15 ], [ -1, %21 ], [ -1, %26 ], [ -1, %31 ], [ -1, %46 ], [ %63, %59 ], [ -1, %40 ]
ret i32 %65
}