#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 uinput_user_dev {
  scalar_t__ *name;
};
typedef int /*<<< orphan*/ dev;

/* Variables and functions */
int /*<<< orphan*/ EV_KEY;
int /*<<< orphan*/ EV_REP;
int KEY_RESERVED;
int KEY_UNKNOWN;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ UI_DEV_CREATE;
int /*<<< orphan*/ UI_SET_EVBIT;
int /*<<< orphan*/ UI_SET_KEYBIT;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ memset(struct uinput_user_dev *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(scalar_t__ *, char const *, int);
int write(int, struct uinput_user_dev *, int);
int setup_uinputfd(const char *name) {
  int fd;
  int key;
  struct uinput_user_dev dev;
  fd = open("/dev/input/uinput", O_RDWR);
  if (fd == -1) {
    fd = open("/dev/uinput", O_RDWR);
    if (fd == -1) {
      fd = open("/dev/misc/uinput", O_RDWR);
      if (fd == -1) {
        fprintf(stderr, "could not open %s\n", "uinput");
        perror(NULL);
        return -1;
      }
    }
  }
  memset(&dev, 0, sizeof(dev));
  strncpy(dev.name, name, sizeof(dev.name));
  dev.name[sizeof(dev.name) - 1] = 0;
  if (write(fd, &dev, sizeof(dev)) != sizeof(dev) ||
      ioctl(fd, UI_SET_EVBIT, EV_KEY) != 0 ||
      ioctl(fd, UI_SET_EVBIT, EV_REP) != 0) {
    goto setup_error;
  }
  for (key = KEY_RESERVED; key <= KEY_UNKNOWN; key++) {
    if (ioctl(fd, UI_SET_KEYBIT, key) != 0) {
      goto setup_error;
    }
  }
  if (ioctl(fd, UI_DEV_CREATE) != 0) {
    goto setup_error;
  }
  return fd;
setup_error:
  fprintf(stderr, "could not setup %s\n", "uinput");
  perror(NULL);
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<setup_uinputfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    0xe64(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    122d <setup_uinputfd+0xad>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe54(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1228 <setup_uinputfd+0xa8>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0xe3e(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1223 <setup_uinputfd+0xa3>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
lea    0xe1f(%rip),%rdx
call   1080 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <setup_uinputfd+0x200>
jmp    1228 <setup_uinputfd+0xa8>
jmp    122d <setup_uinputfd+0xad>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,0x38(%rax)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1040 <write@plt>
cltq
cmp    $0x8,%rax
jne    12c4 <setup_uinputfd+0x144>
mov    -0x14(%rbp),%edi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
mov    0x2d4d(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jne    12c4 <setup_uinputfd+0x144>
mov    -0x14(%rbp),%edi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
je     12c9 <setup_uinputfd+0x149>
jmp    134c <setup_uinputfd+0x1cc>
mov    0x2cd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2cf1(%rip),%rcx
cmp    (%rcx),%eax
jg     131f <setup_uinputfd+0x19f>
mov    -0x14(%rbp),%edi
mov    0x2cbf(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
je     130c <setup_uinputfd+0x18c>
jmp    134c <setup_uinputfd+0x1cc>
jmp    1311 <setup_uinputfd+0x191>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12d5 <setup_uinputfd+0x155>
mov    -0x14(%rbp),%edi
mov    0x2ccf(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
je     1341 <setup_uinputfd+0x1c1>
jmp    134c <setup_uinputfd+0x1cc>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1380 <setup_uinputfd+0x200>
mov    0x2c55(%rip),%rax
mov    (%rax),%edi
lea    0xce6(%rip),%rsi
lea    0xcc5(%rip),%rdx
call   1080 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_uinputfd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e4f(%rip),%rbx
mov    (%rbx),%esi
lea    0xe66(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11d7 <setup_uinputfd+0x57>
mov    (%rbx),%esi
lea    0xe62(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11d7 <setup_uinputfd+0x57>
mov    (%rbx),%esi
lea    0xe58(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     12f5 <setup_uinputfd+0x175>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    (%rsp),%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    (%rsp),%rax
movq   $0x0,0x38(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
cmp    $0x8,%eax
jne    12b6 <setup_uinputfd+0x136>
mov    0x2d9a(%rip),%rbx
mov    (%rbx),%esi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
jne    12b6 <setup_uinputfd+0x136>
mov    (%rbx),%esi
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
jne    12b6 <setup_uinputfd+0x136>
mov    0x2d3f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d66(%rip),%rbp
cmp    0x0(%rbp),%ebx
jg     129e <setup_uinputfd+0x11e>
mov    0x2d3a(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%esi
mov    %r15d,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
jne    12b6 <setup_uinputfd+0x136>
lea    0x1(%rbx),%eax
cmp    0x0(%rbp),%ebx
mov    %eax,%ebx
jl     1280 <setup_uinputfd+0x100>
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
je     12e7 <setup_uinputfd+0x167>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
lea    0xd5b(%rip),%rdx
call   1080 <fprintf@plt>
xor    %edi,%edi
call   10a0 <perror@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
lea    0xd2a(%rip),%rsi
lea    0xd1c(%rip),%rdx
call   1080 <fprintf@plt>
xor    %edi,%edi
call   10a0 <perror@plt>
jmp    12e1 <setup_uinputfd+0x161>

LLVM IR (-O0):
%struct.uinput_user_dev = type { i64* }
@.str = [18 x i8] c"/dev/input/uinput\00"
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"/dev/uinput\00"
@.str.2 = [17 x i8] c"/dev/misc/uinput\00"
@stderr = global i32 0
@.str.3 = [19 x i8] c"could not open %s\0A\00"
@.str.4 = [7 x i8] c"uinput\00"
@UI_SET_EVBIT = global i32 0
@EV_KEY = global i32 0
@EV_REP = global i32 0
@KEY_RESERVED = global i32 0
@KEY_UNKNOWN = global i32 0
@UI_SET_KEYBIT = global i32 0
@UI_DEV_CREATE = global i32 0
@.str.5 = [20 x i8] c"could not setup %s\0A\00"
define i32 @setup_uinputfd(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.uinput_user_dev
store i8* %0, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %27
11: ; preds = %1
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %26
16: ; preds = %11
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%24 = call i32 @perror(i32* null)
store i32 -1, i32* %2
br label %84
25: ; preds = %16
br label %26
26: ; preds = %25, %11
br label %27
27: ; preds = %26, %1
%28 = call i32 @memset(%struct.uinput_user_dev* %6, i32 0, i32 8)
%29 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %6, i32 0, i32 0
%30 = load i64*, i64** %29
%31 = load i8*, i8** %3
%32 = call i32 @strncpy(i64* %30, i8* %31, i32 8)
%33 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %6, i32 0, i32 0
%34 = load i64*, i64** %33
%35 = getelementptr inbounds i64, i64* %34, i64 7
store i64 0, i64* %35
%36 = load i32, i32* %4
%37 = call i32 @write(i32 %36, %struct.uinput_user_dev* %6, i32 8)
%38 = sext i32 %37 to i64
%39 = icmp ne i64 %38, 8
br i1 %39, label %52, label %40
40: ; preds = %27
%41 = load i32, i32* %4
%42 = load i32, i32* @UI_SET_EVBIT
%43 = load i32, i32* @EV_KEY
%44 = call i64 (i32, i32, ...) @ioctl(i32 %41, i32 %42, i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %52, label %46
46: ; preds = %40
%47 = load i32, i32* %4
%48 = load i32, i32* @UI_SET_EVBIT
%49 = load i32, i32* @EV_REP
%50 = call i64 (i32, i32, ...) @ioctl(i32 %47, i32 %48, i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %46, %40, %27
br label %78
53: ; preds = %46
%54 = load i32, i32* @KEY_RESERVED
store i32 %54, i32* %5
br label %55
55: ; preds = %67, %53
%56 = load i32, i32* %5
%57 = load i32, i32* @KEY_UNKNOWN
%58 = icmp sle i32 %56, %57
br i1 %58, label %59, label %70
59: ; preds = %55
%60 = load i32, i32* %4
%61 = load i32, i32* @UI_SET_KEYBIT
%62 = load i32, i32* %5
%63 = call i64 (i32, i32, ...) @ioctl(i32 %60, i32 %61, i32 %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %59
br label %78
66: ; preds = %59
br label %67
67: ; preds = %66
%68 = load i32, i32* %5
%69 = add nsw i32 %68, 1
store i32 %69, i32* %5
br label %55
70: ; preds = %55
%71 = load i32, i32* %4
%72 = load i32, i32* @UI_DEV_CREATE
%73 = call i64 (i32, i32, ...) @ioctl(i32 %71, i32 %72)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %70
br label %78
76: ; preds = %70
%77 = load i32, i32* %4
store i32 %77, i32* %2
br label %84
78: ; preds = %75, %65, %52
%79 = load i32, i32* @stderr
%80 = call i32 @fprintf(i32 %79, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%81 = call i32 @perror(i32* null)
%82 = load i32, i32* %4
%83 = call i32 @close(i32 %82)
store i32 -1, i32* %2
br label %84
84: ; preds = %78, %76, %21
%85 = load i32, i32* %2
ret i32 %85
}

LLVM IR (-O2):
%struct.uinput_user_dev = type { i64* }
@.str = [18 x i8] c"/dev/input/uinput\00"
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"/dev/uinput\00"
@.str.2 = [17 x i8] c"/dev/misc/uinput\00"
@stderr = global i32 0
@.str.3 = [19 x i8] c"could not open %s\0A\00"
@.str.4 = [7 x i8] c"uinput\00"
@UI_SET_EVBIT = global i32 0
@EV_KEY = global i32 0
@EV_REP = global i32 0
@KEY_RESERVED = global i32 0
@KEY_UNKNOWN = global i32 0
@UI_SET_KEYBIT = global i32 0
@UI_DEV_CREATE = global i32 0
@.str.5 = [20 x i8] c"could not setup %s\0A\00"
define i32 @setup_uinputfd(i8* %0) {
%2 = alloca %struct.uinput_user_dev
%3 = bitcast %struct.uinput_user_dev* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %19
7: ; preds = %1
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %19
11: ; preds = %7
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%18 = tail call i32 @perror(i32* null)
br label %61
19: ; preds = %7, %11, %1
%20 = phi i32 [ %13, %11 ], [ %9, %7 ], [ %5, %1 ]
%21 = call i32 @memset(%struct.uinput_user_dev* %2, i32 0, i32 8)
%22 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %2, i64 0, i32 0
%23 = load i64*, i64** %22
%24 = call i32 @strncpy(i64* %23, i8* %0, i32 8)
%25 = load i64*, i64** %22
%26 = getelementptr inbounds i64, i64* %25, i64 7
store i64 0, i64* %26
%27 = call i32 @write(i32 %20, %struct.uinput_user_dev* %2, i32 8)
%28 = icmp eq i32 %27, 8
br i1 %28, label %29, label %56
29: ; preds = %19
%30 = load i32, i32* @UI_SET_EVBIT
%31 = load i32, i32* @EV_KEY
%32 = call i64 (i32, i32, ...) @ioctl(i32 %20, i32 %30, i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %56
34: ; preds = %29
%35 = load i32, i32* @UI_SET_EVBIT
%36 = load i32, i32* @EV_REP
%37 = call i64 (i32, i32, ...) @ioctl(i32 %20, i32 %35, i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %56
39: ; preds = %34
%40 = load i32, i32* @KEY_RESERVED
%41 = load i32, i32* @KEY_UNKNOWN
%42 = icmp sgt i32 %40, %41
br i1 %42, label %52, label %47
43: ; preds = %47
%44 = add nsw i32 %48, 1
%45 = load i32, i32* @KEY_UNKNOWN
%46 = icmp slt i32 %48, %45
br i1 %46, label %47, label %52
47: ; preds = %39, %43
%48 = phi i32 [ %44, %43 ], [ %40, %39 ]
%49 = load i32, i32* @UI_SET_KEYBIT
%50 = call i64 (i32, i32, ...) @ioctl(i32 %20, i32 %49, i32 %48)
%51 = icmp eq i64 %50, 0
br i1 %51, label %43, label %56
52: ; preds = %43, %39
%53 = load i32, i32* @UI_DEV_CREATE
%54 = call i64 (i32, i32, ...) @ioctl(i32 %20, i32 %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %61, label %56
56: ; preds = %47, %52, %19, %29, %34
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%59 = call i32 @perror(i32* null)
%60 = call i32 @close(i32 %20)
br label %61
61: ; preds = %52, %56, %15
%62 = phi i32 [ -1, %15 ], [ -1, %56 ], [ %20, %52 ]
ret i32 %62
}