#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 input_event {
  unsigned int code;
  int value;
  int /*<<< orphan*/ type;
  int /*<<< orphan*/ time;
};
typedef int /*<<< orphan*/ event;

/* Variables and functions */
int /*<<< orphan*/ EV_KEY;
int /*<<< orphan*/ EV_SYN;
unsigned int SYN_REPORT;
int /*<<< orphan*/ gettimeofday(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
scalar_t__ write(int, struct input_event *, int);
void send_key_event(int fd, unsigned int keycode, int keyvalue) {
  struct input_event event;
  gettimeofday(&event.time, NULL);
  event.type = EV_KEY;
  event.code = keycode;
  event.value = keyvalue;
  if (write(fd, &event, sizeof(event)) < 0)
    printf("simulate key error\n");
  event.type = EV_SYN;
  event.code = SYN_REPORT;
  event.value = 0;
  write(fd, &event, sizeof(event));
  if (write(fd, &event, sizeof(event)) < 0)
    printf("simulate key error\n");
}


x86 assembly (clang, -O0):
<send_key_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
add    $0xc,%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1191 <send_key_event+0x61>
lea    0xe74(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11e8 <send_key_event+0xb8>
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_key_event>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,(%rsp)
mov    %r14d,0x4(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    1180 <send_key_event+0x50>
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
movl   $0x0,0x4(%rsp)
mov    %rsp,%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x10,%edx
call   1040 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x10,%edx
call   1040 <write@plt>
test   %rax,%rax
js     11d0 <send_key_event+0xa0>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe29(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
%struct.input_event = type { i32, i32, i32, i32 }
@EV_KEY = global i32 0
@.str = [20 x i8] c"simulate key error\0A\00"
@EV_SYN = global i32 0
@SYN_REPORT = global i32 0
define void @send_key_event(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.input_event
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 3
%9 = call i32 @gettimeofday(i32* %8, i32* null)
%10 = load i32, i32* @EV_KEY
%11 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* %5
%13 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* %6
%15 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* %4
%17 = call i64 @write(i32 %16, %struct.input_event* %7, i32 16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %3
%22 = load i32, i32* @EV_SYN
%23 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @SYN_REPORT
%25 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 0
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 1
store i32 0, i32* %26
%27 = load i32, i32* %4
%28 = call i64 @write(i32 %27, %struct.input_event* %7, i32 16)
%29 = load i32, i32* %4
%30 = call i64 @write(i32 %29, %struct.input_event* %7, i32 16)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %32, %21
ret void
}

LLVM IR (-O2):
%struct.input_event = type { i32, i32, i32, i32 }
@EV_KEY = global i32 0
@EV_SYN = global i32 0
@SYN_REPORT = global i32 0
@str.1 = [19 x i8] c"simulate key error\00"
define void @send_key_event(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.input_event
%5 = bitcast %struct.input_event* %4 to i8*
%6 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i64 0, i32 3
%7 = call i32 @gettimeofday(i32* %6, i32* null)
%8 = load i32, i32* @EV_KEY
%9 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i64 0, i32 2
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i64 0, i32 0
store i32 %1, i32* %10
%11 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i64 0, i32 1
store i32 %2, i32* %11
%12 = call i64 @write(i32 %0, %struct.input_event* %4, i32 16)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = load i32, i32* @EV_SYN
store i32 %17, i32* %9
%18 = load i32, i32* @SYN_REPORT
store i32 %18, i32* %10
store i32 0, i32* %11
%19 = call i64 @write(i32 %0, %struct.input_event* %4, i32 16)
%20 = call i64 @write(i32 %0, %struct.input_event* %4, i32 16)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
ret void
}