#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 {
  int /*<<< orphan*/ name;
  TYPE_1__ id;
};

/* Variables and functions */
int /*<<< orphan*/ BUS_VIRTUAL;
int /*<<< orphan*/ EV_KEY;
int KEY_1;
int /*<<< orphan*/ KEY_LEFTSHIFT;
int KEY_SPACE;
int O_NONBLOCK;
int O_WRONLY;
int /*<<< orphan*/ UINPUT_MAX_NAME_SIZE;
int /*<<< orphan*/ UI_DEV_CREATE;
int /*<<< orphan*/ UI_DEV_DESTROY;
int /*<<< orphan*/ UI_SET_EVBIT;
int /*<<< orphan*/ UI_SET_KEYBIT;
int /*<<< orphan*/ check_posix(int, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ev_inject_keypresses(int, char *);
int ioctl(int, int /*<<< orphan*/, ...);
int open(char *, int);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ usleep(int);
int write(int, struct uinput_user_dev *, int);
int main(int argc, char *argv[]) {
  if (argc < 2)
    return -1;
  int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
  check_posix(fd, "failed to open uinput");
  check_posix(ioctl(fd, UI_SET_EVBIT, EV_KEY), "failed to set EV_KEY");
  check_posix(ioctl(fd, UI_SET_KEYBIT, KEY_LEFTSHIFT),
              "failed to set UI_SET_KEYBIT");
  for (int key = KEY_1; key <= KEY_SPACE; ++key) {
    check_posix(ioctl(fd, UI_SET_KEYBIT, key & 0xfff),
                "failed to set UI_SET_KEYBIT");
  }
  struct uinput_user_dev uidev = {.id.bustype = BUS_VIRTUAL,
                                  .id.vendor = 0x1,
                                  .id.product = 0x1,
                                  .id.version = 1};
  snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "Virtual Injector Keyboard");
  check_posix(write(fd, &uidev, sizeof(uidev)),
              "failed to write uinput_user_dev struct");
  check_posix(ioctl(fd, UI_DEV_CREATE), "failed to create uinput device");
  usleep(500000);
  ev_inject_keypresses(fd, argv[1]);
  ioctl(fd, UI_DEV_DESTROY);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ac <main+0x2c>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <main+0x1bb>
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    0x2e14(%rip),%rax
or     (%rax),%esi
lea    0xe3b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    0xe35(%rip),%rsi
call   1090 <check_posix@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1050 <ioctl@plt>
mov    %eax,%edi
lea    0xe21(%rip),%rsi
call   1090 <check_posix@plt>
mov    -0x14(%rbp),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2d77(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1050 <ioctl@plt>
mov    %eax,%edi
lea    0xe0c(%rip),%rsi
call   1090 <check_posix@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2d82(%rip),%rcx
cmp    (%rcx),%eax
jg     1286 <main+0x106>
mov    -0x14(%rbp),%edi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
and    $0xfff,%edx
mov    $0x0,%al
call   1050 <ioctl@plt>
mov    %eax,%edi
lea    0xdc4(%rip),%rsi
call   1090 <check_posix@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    123c <main+0xbc>
movl   $0x0,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
movl   $0x1,-0x28(%rbp)
movl   $0x1,-0x24(%rbp)
mov    0x2d2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%edi
mov    0x2cf8(%rip),%rax
mov    (%rax),%esi
lea    0xd92(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
mov    %eax,%edi
lea    0xd8d(%rip),%rsi
call   1090 <check_posix@plt>
mov    -0x14(%rbp),%edi
mov    0x2d09(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <ioctl@plt>
mov    %eax,%edi
lea    0xd93(%rip),%rsi
call   1090 <check_posix@plt>
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1070 <ev_inject_keypresses@plt>
mov    -0x14(%rbp),%edi
mov    0x2c7e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <ioctl@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0xffffffff,%r15d
cmp    $0x2,%edi
jl     1325 <main+0x1a5>
mov    %rsi,0x8(%rsp)
mov    0x2dd7(%rip),%rax
mov    0x2e20(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe55(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r13d
lea    0xe52(%rip),%rsi
mov    %eax,%edi
call   1090 <check_posix@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %r13d,%edi
xor    %eax,%eax
call   1050 <ioctl@plt>
lea    0xe3b(%rip),%rsi
mov    %eax,%edi
call   1090 <check_posix@plt>
mov    0x2d93(%rip),%r12
mov    (%r12),%esi
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
xor    %eax,%eax
call   1050 <ioctl@plt>
lea    0xe24(%rip),%rsi
mov    %eax,%edi
call   1090 <check_posix@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%r14d
mov    0x2d8d(%rip),%rbp
cmp    0x0(%rbp),%r14d
jg     127e <main+0xfe>
lea    0xdff(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    (%r12),%esi
mov    %r14d,%edx
and    $0xfff,%edx
mov    %r13d,%edi
xor    %eax,%eax
call   1050 <ioctl@plt>
mov    %eax,%edi
mov    %rbx,%rsi
call   1090 <check_posix@plt>
lea    0x1(%r14),%eax
cmp    0x0(%rbp),%r14d
mov    %eax,%r14d
jl     1250 <main+0xd0>
movaps 0xd7b(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
lea    0xdbc(%rip),%rdx
xor    %edi,%edi
call   1040 <snprintf@plt>
lea    0x10(%rsp),%rsi
mov    %r13d,%edi
mov    $0x14,%edx
call   1030 <write@plt>
lea    0xdb6(%rip),%rsi
mov    %eax,%edi
call   1090 <check_posix@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
xor    %eax,%eax
call   1050 <ioctl@plt>
lea    0xdbc(%rip),%rsi
mov    %eax,%edi
call   1090 <check_posix@plt>
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
mov    0x8(%rsp),%rax
mov    0x8(%rax),%rsi
mov    %r13d,%edi
call   1070 <ev_inject_keypresses@plt>
mov    0x2c97(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
xor    %eax,%eax
call   1050 <ioctl@plt>
mov    %r13d,%edi
call   1060 <close@plt>
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.uinput_user_dev = type { i32, %struct.TYPE_2__ }
%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 = [22 x i8] c"failed to open uinput\00"
@UI_SET_EVBIT = global i32 0
@EV_KEY = global i32 0
@.str.2 = [21 x i8] c"failed to set EV_KEY\00"
@UI_SET_KEYBIT = global i32 0
@KEY_LEFTSHIFT = global i32 0
@.str.3 = [28 x i8] c"failed to set UI_SET_KEYBIT\00"
@KEY_1 = global i32 0
@KEY_SPACE = global i32 0
@BUS_VIRTUAL = global i32 0
@UINPUT_MAX_NAME_SIZE = global i32 0
@.str.4 = [26 x i8] c"Virtual Injector Keyboard\00"
@.str.5 = [39 x i8] c"failed to write uinput_user_dev struct\00"
@UI_DEV_CREATE = global i32 0
@.str.6 = [31 x i8] c"failed to create uinput device\00"
@UI_DEV_DESTROY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.uinput_user_dev
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %74
12: ; preds = %2
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %13, %14
%16 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = call i32 @check_posix(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %6
%20 = load i32, i32* @UI_SET_EVBIT
%21 = load i32, i32* @EV_KEY
%22 = call i32 (i32, i32, ...) @ioctl(i32 %19, i32 %20, i32 %21)
%23 = call i32 @check_posix(i32 %22, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* %6
%25 = load i32, i32* @UI_SET_KEYBIT
%26 = load i32, i32* @KEY_LEFTSHIFT
%27 = call i32 (i32, i32, ...) @ioctl(i32 %24, i32 %25, i32 %26)
%28 = call i32 @check_posix(i32 %27, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32, i32* @KEY_1
store i32 %29, i32* %7
br label %30
30: ; preds = %41, %12
%31 = load i32, i32* %7
%32 = load i32, i32* @KEY_SPACE
%33 = icmp sle i32 %31, %32
br i1 %33, label %34, label %44
34: ; preds = %30
%35 = load i32, i32* %6
%36 = load i32, i32* @UI_SET_KEYBIT
%37 = load i32, i32* %7
%38 = and i32 %37, 4095
%39 = call i32 (i32, i32, ...) @ioctl(i32 %35, i32 %36, i32 %38)
%40 = call i32 @check_posix(i32 %39, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %34
%42 = load i32, i32* %7
%43 = add nsw i32 %42, 1
store i32 %43, i32* %7
br label %30
44: ; preds = %30
%45 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %8, i32 0, i32 0
store i32 0, i32* %45
%46 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %8, i32 0, i32 1
%47 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 0
store i32 1, i32* %47
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 1
store i32 1, i32* %48
%49 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 2
store i32 1, i32* %49
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 3
%51 = load i32, i32* @BUS_VIRTUAL
store i32 %51, i32* %50
%52 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %8, i32 0, i32 0
%53 = load i32, i32* %52
%54 = load i32, i32* @UINPUT_MAX_NAME_SIZE
%55 = call i32 @snprintf(i32 %53, i32 %54, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%56 = load i32, i32* %6
%57 = call i32 @write(i32 %56, %struct.uinput_user_dev* %8, i32 20)
%58 = call i32 @check_posix(i32 %57, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
%59 = load i32, i32* %6
%60 = load i32, i32* @UI_DEV_CREATE
%61 = call i32 (i32, i32, ...) @ioctl(i32 %59, i32 %60)
%62 = call i32 @check_posix(i32 %61, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%63 = call i32 @usleep(i32 500000)
%64 = load i32, i32* %6
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 1
%67 = load i8*, i8** %66
%68 = call i32 @ev_inject_keypresses(i32 %64, i8* %67)
%69 = load i32, i32* %6
%70 = load i32, i32* @UI_DEV_DESTROY
%71 = call i32 (i32, i32, ...) @ioctl(i32 %69, i32 %70)
%72 = load i32, i32* %6
%73 = call i32 @close(i32 %72)
br label %74
74: ; preds = %44, %11
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
%struct.uinput_user_dev = type { i32, %struct.TYPE_2__ }
%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 = [22 x i8] c"failed to open uinput\00"
@UI_SET_EVBIT = global i32 0
@EV_KEY = global i32 0
@.str.2 = [21 x i8] c"failed to set EV_KEY\00"
@UI_SET_KEYBIT = global i32 0
@KEY_LEFTSHIFT = global i32 0
@.str.3 = [28 x i8] c"failed to set UI_SET_KEYBIT\00"
@KEY_1 = global i32 0
@KEY_SPACE = global i32 0
@BUS_VIRTUAL = global i32 0
@UINPUT_MAX_NAME_SIZE = global i32 0
@.str.4 = [26 x i8] c"Virtual Injector Keyboard\00"
@.str.5 = [39 x i8] c"failed to write uinput_user_dev struct\00"
@UI_DEV_CREATE = global i32 0
@.str.6 = [31 x i8] c"failed to create uinput device\00"
@UI_DEV_DESTROY = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.uinput_user_dev
%4 = icmp slt i32 %0, 2
br i1 %4, label %50, label %5
5: ; preds = %2
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = tail call i32 @check_posix(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @UI_SET_EVBIT
%12 = load i32, i32* @EV_KEY
%13 = tail call i32 (i32, i32, ...) @ioctl(i32 %9, i32 %11, i32 %12)
%14 = tail call i32 @check_posix(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* @UI_SET_KEYBIT
%16 = load i32, i32* @KEY_LEFTSHIFT
%17 = tail call i32 (i32, i32, ...) @ioctl(i32 %9, i32 %15, i32 %16)
%18 = tail call i32 @check_posix(i32 %17, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%19 = load i32, i32* @KEY_1
%20 = load i32, i32* @KEY_SPACE
%21 = icmp sgt i32 %19, %20
br i1 %21, label %22, label %41
22: ; preds = %41, %5
%23 = bitcast %struct.uinput_user_dev* %3 to i8*
%24 = bitcast %struct.uinput_user_dev* %3 to <4 x i32>*
store <4 x i32> <i32 0, i32 1, i32 1, i32 1>, <4 x i32>* %24
%25 = getelementptr inbounds %struct.uinput_user_dev, %struct.uinput_user_dev* %3, i64 0, i32 1, i32 3
%26 = load i32, i32* @BUS_VIRTUAL
store i32 %26, i32* %25
%27 = load i32, i32* @UINPUT_MAX_NAME_SIZE
%28 = tail call i32 @snprintf(i32 0, i32 %27, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%29 = call i32 @write(i32 %9, %struct.uinput_user_dev* %3, i32 20)
%30 = tail call i32 @check_posix(i32 %29, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
%31 = load i32, i32* @UI_DEV_CREATE
%32 = tail call i32 (i32, i32, ...) @ioctl(i32 %9, i32 %31)
%33 = tail call i32 @check_posix(i32 %32, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%34 = tail call i32 @usleep(i32 500000)
%35 = getelementptr inbounds i8*, i8** %1, i64 1
%36 = load i8*, i8** %35
%37 = tail call i32 @ev_inject_keypresses(i32 %9, i8* %36)
%38 = load i32, i32* @UI_DEV_DESTROY
%39 = tail call i32 (i32, i32, ...) @ioctl(i32 %9, i32 %38)
%40 = tail call i32 @close(i32 %9)
br label %50
41: ; preds = %5, %41
%42 = phi i32 [ %47, %41 ], [ %19, %5 ]
%43 = load i32, i32* @UI_SET_KEYBIT
%44 = and i32 %42, 4095
%45 = tail call i32 (i32, i32, ...) @ioctl(i32 %9, i32 %43, i32 %44)
%46 = tail call i32 @check_posix(i32 %45, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%47 = add nsw i32 %42, 1
%48 = load i32, i32* @KEY_SPACE
%49 = icmp slt i32 %42, %48
br i1 %49, label %41, label %22
50: ; preds = %2, %22
%51 = phi i32 [ 0, %22 ], [ -1, %2 ]
ret i32 %51
}