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

/* Variables and functions */
void *EV_KEY;
void *EV_SYN;
void *SYN_REPORT;
int /*<<< orphan*/ gettimeofday(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct input_event *, int /*<<< orphan*/, int);
int /*<<< orphan*/ write(int, struct input_event *, int);
void simulate_key(int fd, int kval) {
  struct input_event event;
  gettimeofday(&event.time, 0);
  event.type = EV_KEY;
  event.value = 1;
  event.code = kval;
  write(fd, &event, sizeof(event));
  event.type = EV_SYN;
  event.value = 0;
  event.code = SYN_REPORT;
  write(fd, &event, sizeof(event));
  memset(&event, 0, sizeof(event));
  gettimeofday(&event.time, 0);
  event.type = EV_KEY;
  event.value = 0;
  event.code = kval;
  write(fd, &event, sizeof(event));
  event.type = EV_SYN;
  event.value = 0;
  event.code = SYN_REPORT;
  write(fd, &event, sizeof(event));
}


x86 assembly (clang, -O0):
<simulate_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
add    $0x10,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0x2e8c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   1030 <write@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   1030 <write@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rdi
add    $0x10,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   1030 <write@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   1030 <write@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simulate_key>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0x2e8a(%rip),%r12
mov    (%r12),%rax
mov    %rax,0x8(%rsp)
movl   $0x1,(%rsp)
mov    %r14d,0x4(%rsp)
mov    %rsp,%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x18,%edx
call   1030 <write@plt>
mov    0x2e6c(%rip),%r13
mov    0x0(%r13),%rax
mov    %rax,0x8(%rsp)
movl   $0x0,(%rsp)
mov    0x2e3d(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x18,%edx
call   1030 <write@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    (%r12),%rax
mov    %rax,0x8(%rsp)
movl   $0x0,(%rsp)
mov    %r14d,0x4(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x18,%edx
call   1030 <write@plt>
mov    0x0(%r13),%rax
mov    %rax,0x8(%rsp)
movl   $0x0,(%rsp)
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x18,%edx
call   1030 <write@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.input_event = type { i32, i32, i8*, i32 }
@EV_KEY = global i8* null
@EV_SYN = global i8* null
@SYN_REPORT = global i8* null
define void @simulate_key(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.input_event
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 3
%7 = call i32 @gettimeofday(i32* %6, i32 0)
%8 = load i8*, i8** @EV_KEY
%9 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 2
store i8* %8, i8** %9
%10 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 0
store i32 1, i32* %10
%11 = load i32, i32* %4
%12 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 1
store i32 %11, i32* %12
%13 = load i32, i32* %3
%14 = call i32 @write(i32 %13, %struct.input_event* %5, i32 24)
%15 = load i8*, i8** @EV_SYN
%16 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 2
store i8* %15, i8** %16
%17 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 0
store i32 0, i32* %17
%18 = load i8*, i8** @SYN_REPORT
%19 = ptrtoint i8* %18 to i32
%20 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* %3
%22 = call i32 @write(i32 %21, %struct.input_event* %5, i32 24)
%23 = call i32 @memset(%struct.input_event* %5, i32 0, i32 24)
%24 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 3
%25 = call i32 @gettimeofday(i32* %24, i32 0)
%26 = load i8*, i8** @EV_KEY
%27 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 2
store i8* %26, i8** %27
%28 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 0
store i32 0, i32* %28
%29 = load i32, i32* %4
%30 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* %3
%32 = call i32 @write(i32 %31, %struct.input_event* %5, i32 24)
%33 = load i8*, i8** @EV_SYN
%34 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 2
store i8* %33, i8** %34
%35 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 0
store i32 0, i32* %35
%36 = load i8*, i8** @SYN_REPORT
%37 = ptrtoint i8* %36 to i32
%38 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i32 0, i32 1
store i32 %37, i32* %38
%39 = load i32, i32* %3
%40 = call i32 @write(i32 %39, %struct.input_event* %5, i32 24)
ret void
}

LLVM IR (-O2):
%struct.input_event = type { i32, i32, i8*, i32 }
@EV_KEY = global i8* null
@EV_SYN = global i8* null
@SYN_REPORT = global i8* null
define void @simulate_key(i32 %0, i32 %1) {
%3 = alloca %struct.input_event
%4 = bitcast %struct.input_event* %3 to i8*
%5 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 3
%6 = call i32 @gettimeofday(i32* %5, i32 0)
%7 = load i8*, i8** @EV_KEY
%8 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 2
store i8* %7, i8** %8
%9 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 0
store i32 1, i32* %9
%10 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 1
store i32 %1, i32* %10
%11 = call i32 @write(i32 %0, %struct.input_event* %3, i32 24)
%12 = load i8*, i8** @EV_SYN
store i8* %12, i8** %8
store i32 0, i32* %9
%13 = load i8*, i8** @SYN_REPORT
%14 = ptrtoint i8* %13 to i64
%15 = trunc i64 %14 to i32
store i32 %15, i32* %10
%16 = call i32 @write(i32 %0, %struct.input_event* %3, i32 24)
%17 = call i32 @memset(%struct.input_event* %3, i32 0, i32 24)
%18 = call i32 @gettimeofday(i32* %5, i32 0)
%19 = load i8*, i8** @EV_KEY
store i8* %19, i8** %8
store i32 0, i32* %9
store i32 %1, i32* %10
%20 = call i32 @write(i32 %0, %struct.input_event* %3, i32 24)
%21 = load i8*, i8** @EV_SYN
store i8* %21, i8** %8
store i32 0, i32* %9
%22 = load i8*, i8** @SYN_REPORT
%23 = ptrtoint i8* %22 to i64
%24 = trunc i64 %23 to i32
store i32 %24, i32* %10
%25 = call i32 @write(i32 %0, %struct.input_event* %3, i32 24)
ret void
}