#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 KEY_MAX;
int O_NONBLOCK;
int O_WRONLY;
int /*<<< orphan*/ UINPUT_MAX_NAME_SIZE;
int /*<<< orphan*/ UI_DEV_CREATE;
int /*<<< orphan*/ UI_SET_EVBIT;
int /*<<< orphan*/ UI_SET_KEYBIT;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ioctl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ memset(struct uinput_user_dev *, int /*<<< orphan*/, int);
int open(char const *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                            char const *);
int /*<<< orphan*/ stderr;
int write(int, struct uinput_user_dev *, int);
int create_output(const char *dev_uinput_path, const char *dev_name) {
  int fd = open(dev_uinput_path, O_WRONLY | O_NONBLOCK);
  if (fd == -1) {
    fprintf(stderr, "error: cannot open uinput device(%s)\n", dev_uinput_path);
    return fd;
  }
  int ok = ioctl(fd, UI_SET_EVBIT, EV_KEY) != -1;
  for (int code = 0; code < KEY_MAX; code++) {
    ok = ok && ioctl(fd, UI_SET_KEYBIT, code) != -1;
  }
  if (ok) {
    struct uinput_user_dev uidev;
    memset(&uidev, 0, sizeof(uidev));
    snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "%s", dev_name);
    uidev.id.bustype = BUS_USB;
    uidev.id.vendor = 1;
    uidev.id.product = 1;
    uidev.id.version = 1;
    ok = ok && write(fd, &uidev, sizeof(uidev)) != -1;
  }
  ok = ok && ioctl(fd, UI_DEV_CREATE) != -1;
  if (ok) {
    printf("uinput device created\n");
  } else {
    fprintf(stderr, "error: failed to initialize uinput device\n");
  }
  return ok ? fd : -1;
}


x86 assembly (clang, -O0):
<create_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    0x2e44(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ce <create_output+0x5e>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1373 <create_output+0x203>
mov    -0x1c(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%eax
jge    1258 <create_output+0xe8>
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x39(%rbp)
je     123f <create_output+0xcf>
mov    -0x1c(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
mov    $0x0,%al
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ff <create_output+0x8f>
cmpl   $0x0,-0x20(%rbp)
je     12e3 <create_output+0x173>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    -0x28(%rbp),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rcx
lea    0xd9d(%rip),%rdx
call   1050 <snprintf@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x1,-0x38(%rbp)
movl   $0x1,-0x34(%rbp)
movl   $0x1,-0x30(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x3a(%rbp)
je     12d8 <create_output+0x168>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
setne  %al
mov    %al,-0x3a(%rbp)
mov    -0x3a(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x3b(%rbp)
je     130e <create_output+0x19e>
mov    -0x1c(%rbp),%edi
mov    0x2cfc(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
setne  %al
mov    %al,-0x3b(%rbp)
mov    -0x3b(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1334 <create_output+0x1c4>
lea    0xcff(%rip),%rdi
call   1040 <printf@plt>
jmp    134b <create_output+0x1db>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
lea    0xcfc(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
cmpl   $0x0,-0x20(%rbp)
je     1360 <create_output+0x1f0>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
jmp    136d <create_output+0x1fd>
mov    $0xffffffff,%eax
mov    %eax,-0x40(%rbp)
jmp    136d <create_output+0x1fd>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_output>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
mov    0x2e07(%rip),%rax
mov    0x2e48(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12b5 <create_output+0x145>
mov    %eax,%ebx
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
setne  %al
mov    0x2dd4(%rip),%r15
mov    (%r15),%ecx
test   %ecx,%ecx
jle    1207 <create_output+0x97>
mov    0x2dd6(%rip),%r12
xor    %ebp,%ebp
jmp    11ff <create_output+0x8f>
xchg   %ax,%ax
mov    (%r12),%esi
mov    %ebx,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
setne  %al
mov    (%r15),%ecx
add    $0x1,%ebp
cmp    %ecx,%ebp
jge    1207 <create_output+0x97>
test   $0x1,%al
jne    11e0 <create_output+0x70>
xor    %eax,%eax
jmp    11f8 <create_output+0x88>
test   %al,%al
je     129c <create_output+0x12c>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    0x18(%rsp),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
lea    0xdef(%rip),%rdx
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    0x2da2(%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   1040 <write@plt>
cmp    $0xffffffff,%eax
je     129c <create_output+0x12c>
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     129c <create_output+0x12c>
lea    0xdbf(%rip),%rdi
call   1030 <puts@plt>
jmp    12d4 <create_output+0x164>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xd7d(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12cf <create_output+0x15f>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
lea    0xd3b(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%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 }
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"error: cannot open uinput device(%s)\0A\00"
@UI_SET_EVBIT = global i32 0
@EV_KEY = global i32 0
@KEY_MAX = global i32 0
@UI_SET_KEYBIT = global i32 0
@UINPUT_MAX_NAME_SIZE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@BUS_USB = global i32 0
@UI_DEV_CREATE = global i32 0
@.str.2 = [23 x i8] c"uinput device created\0A\00"
@.str.3 = [43 x i8] c"error: failed to initialize uinput device\0A\00"
define i32 @create_output(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.uinput_user_dev
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %4
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* %6
store i32 %21, i32* %3
br label %102
22: ; preds = %2
%23 = load i32, i32* %6
%24 = load i32, i32* @UI_SET_EVBIT
%25 = load i32, i32* @EV_KEY
%26 = call i32 (i32, i32, ...) @ioctl(i32 %23, i32 %24, i32 %25)
%27 = icmp ne i32 %26, -1
%28 = zext i1 %27 to i32
store i32 %28, i32* %7
store i32 0, i32* %8
br label %29
29: ; preds = %45, %22
%30 = load i32, i32* %8
%31 = load i32, i32* @KEY_MAX
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %48
33: ; preds = %29
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %33
%37 = load i32, i32* %6
%38 = load i32, i32* @UI_SET_KEYBIT
%39 = load i32, i32* %8
%40 = call i32 (i32, i32, ...) @ioctl(i32 %37, i32 %38, i32 %39)
%41 = icmp ne i32 %40, -1
br label %42
42: ; preds = %36, %33
%43 = phi i1 [ false, %33 ], [ %41, %36 ]
%44 = zext i1 %43 to i32
store i32 %44, i32* %7
br label %45
45: ; preds = %42
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
store i32 %47, i32* %8
br label %29
48: ; preds = %29
%49 = load i32, i32* %7
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %76
51: ; preds = %48
%52 = call i32 @memset(%struct.uinput_user_dev* %9, i32 0, i32 20)
%53 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %9, i32 0, i32 1
%54 = load i32, i32* %53
%55 = load i32, i32* @UINPUT_MAX_NAME_SIZE
%56 = load i8*, i8** %5
%57 = call i32 @snprintf(i32 %54, i32 %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %56)
%58 = load i32, i32* @BUS_USB
%59 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %9, i32 0, i32 0
%60 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %59, i32 0, i32 3
store i32 %58, i32* %60
%61 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %9, i32 0, i32 0
%62 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %61, i32 0, i32 0
store i32 1, i32* %62
%63 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %9, i32 0, i32 0
%64 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %63, i32 0, i32 1
store i32 1, i32* %64
%65 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %9, i32 0, i32 0
%66 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %65, i32 0, i32 2
store i32 1, i32* %66
%67 = load i32, i32* %7
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %51
%70 = load i32, i32* %6
%71 = call i32 @write(i32 %70, %struct.uinput_user_dev* %9, i32 20)
%72 = icmp ne i32 %71, -1
br label %73
73: ; preds = %69, %51
%74 = phi i1 [ false, %51 ], [ %72, %69 ]
%75 = zext i1 %74 to i32
store i32 %75, i32* %7
br label %76
76: ; preds = %73, %48
%77 = load i32, i32* %7
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %84
79: ; preds = %76
%80 = load i32, i32* %6
%81 = load i32, i32* @UI_DEV_CREATE
%82 = call i32 (i32, i32, ...) @ioctl(i32 %80, i32 %81)
%83 = icmp ne i32 %82, -1
br label %84
84: ; preds = %79, %76
%85 = phi i1 [ false, %76 ], [ %83, %79 ]
%86 = zext i1 %85 to i32
store i32 %86, i32* %7
%87 = load i32, i32* %7
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %84
%90 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %94
91: ; preds = %84
%92 = load i32, i32* @stderr
%93 = call i32 (i32, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
br label %94
94: ; preds = %91, %89
%95 = load i32, i32* %7
%96 = icmp ne i32 %95, 0
br i1 %96, label %97, label %99
97: ; preds = %94
%98 = load i32, i32* %6
br label %100
99: ; preds = %94
br label %100
100: ; preds = %99, %97
%101 = phi i32 [ %98, %97 ], [ -1, %99 ]
store i32 %101, i32* %3
br label %102
102: ; preds = %100, %17
%103 = load i32, i32* %3
ret i32 %103
}

LLVM IR (-O2):
%struct.uinput_user_dev = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32, i32, i32, i32 }
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"error: cannot open uinput device(%s)\0A\00"
@UI_SET_EVBIT = global i32 0
@EV_KEY = global i32 0
@KEY_MAX = global i32 0
@UI_SET_KEYBIT = global i32 0
@UINPUT_MAX_NAME_SIZE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@BUS_USB = global i32 0
@UI_DEV_CREATE = global i32 0
@.str.3 = [43 x i8] c"error: failed to initialize uinput device\0A\00"
@str = [22 x i8] c"uinput device created\00"
define i32 @create_output(i8* %0, i8* %1) {
%3 = alloca %struct.uinput_user_dev
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_NONBLOCK
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %58
12: ; preds = %2
%13 = load i32, i32* @UI_SET_EVBIT
%14 = load i32, i32* @EV_KEY
%15 = tail call i32 (i32, i32, ...) @ioctl(i32 %7, i32 %13, i32 %14)
%16 = icmp ne i32 %15, -1
%17 = load i32, i32* @KEY_MAX
%18 = icmp sgt i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %30, %12
%20 = phi i1 [ %16, %12 ], [ %32, %30 ]
br i1 %20, label %35, label %55
21: ; preds = %12, %30
%22 = phi i32 [ %31, %30 ], [ %17, %12 ]
%23 = phi i32 [ %33, %30 ], [ 0, %12 ]
%24 = phi i1 [ %32, %30 ], [ %16, %12 ]
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i32, i32* @UI_SET_KEYBIT
%27 = tail call i32 (i32, i32, ...) @ioctl(i32 %7, i32 %26, i32 %23)
%28 = icmp ne i32 %27, -1
%29 = load i32, i32* @KEY_MAX
br label %30
30: ; preds = %25, %21
%31 = phi i32 [ %22, %21 ], [ %29, %25 ]
%32 = phi i1 [ false, %21 ], [ %28, %25 ]
%33 = add nuw nsw i32 %23, 1
%34 = icmp slt i32 %33, %31
br i1 %34, label %21, label %19
35: ; preds = %19
%36 = bitcast %struct.uinput_user_dev* %3 to i8*
%37 = call i32 @memset(%struct.uinput_user_dev* %3, i32 0, i32 20)
%38 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %3, i64 0, i32 1
%39 = load i32, i32* %38
%40 = load i32, i32* @UINPUT_MAX_NAME_SIZE
%41 = call i32 @snprintf(i32 %39, i32 %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%42 = load i32, i32* @BUS_USB
%43 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %3, i64 0, i32 0, i32 3
store i32 %42, i32* %43
%44 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %3, i64 0, i32 0, i32 0
store i32 1, i32* %44
%45 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %3, i64 0, i32 0, i32 1
store i32 1, i32* %45
%46 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %3, i64 0, i32 0, i32 2
store i32 1, i32* %46
%47 = call i32 @write(i32 %7, %struct.uinput_user_dev* %3, i32 20)
%48 = icmp eq i32 %47, -1
br i1 %48, label %55, label %49
49: ; preds = %35
%50 = load i32, i32* @UI_DEV_CREATE
%51 = call i32 (i32, i32, ...) @ioctl(i32 %7, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %55, label %53
53: ; preds = %49
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %58
55: ; preds = %19, %35, %49
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %53, %55, %9
%59 = phi i32 [ -1, %9 ], [ -1, %55 ], [ %7, %53 ]
ret i32 %59
}