#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 /*<<< orphan*/ time;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ gettimeofday(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ is_ev_syn(struct input_event *);
int /*<<< orphan*/ stderr;
int write(int, struct input_event *, int);
int forward_events(int uinput, struct input_event *ev) {
  int i;
  ssize_t ret;
  for (i = 0; i < 16; i++) {
    ;
    gettimeofday(&ev[i].time, NULL);
    ret = write(uinput, ev + i, sizeof(struct input_event));
    if (ret != sizeof(struct input_event)) {
      fprintf(stderr, "Unable to forward event %u to uinput (ret = %lu)\n", i,
              ret);
    }
    if (is_ev_syn(ev + i))
      return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<forward_events>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x10,-0x14(%rbp)
jge    11fd <forward_events+0xbd>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
cmp    $0x4,%rax
je     11c0 <forward_events+0x80>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe45(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1040 <is_ev_syn@plt>
cmp    $0x0,%rax
je     11ea <forward_events+0xaa>
movl   $0x0,-0x4(%rbp)
jmp    1204 <forward_events+0xc4>
jmp    11ef <forward_events+0xaf>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1156 <forward_events+0x16>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forward_events>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%r14d
xor    %ebp,%ebp
mov    0x2e89(%rip),%r12
lea    0xea2(%rip),%r15
xchg   %ax,%ax
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     118f <forward_events+0x4f>
mov    (%r12),%edi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %eax,%ecx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <is_ev_syn@plt>
test   %rax,%rax
jne    11ad <forward_events+0x6d>
lea    0x1(%rbp),%rax
add    $0x4,%rbx
cmp    $0xf,%rbp
mov    %rax,%rbp
jne    1160 <forward_events+0x20>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.input_event = type { i32 }
@stderr = global i32 0
@.str = [50 x i8] c"Unable to forward event %u to uinput (ret = %lu)\0A\00"
define i32 @forward_events(i32 %0, %struct.input_event* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.input_event*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store %struct.input_event* %1, %struct.input_event** %5
store i32 0, i32* %6
br label %8
8: ; preds = %41, %2
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 16
br i1 %10, label %11, label %44
11: ; preds = %8
%12 = load %struct.input_event*, %struct.input_event** %5
%13 = load i32, i32* %6
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds %struct.input_event, %struct.input_event* %12, i64 %14
%16 = getelementptr inbounds %struct.input_event, %struct.input_event* %15, i32 0, i32 0
%17 = call i32 @gettimeofday(i32* %16, i32* null)
%18 = load i32, i32* %4
%19 = load %struct.input_event*, %struct.input_event** %5
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds %struct.input_event, %struct.input_event* %19, i64 %21
%23 = call i32 @write(i32 %18, %struct.input_event* %22, i32 4)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = icmp ne i64 %25, 4
br i1 %26, label %27, label %32
27: ; preds = %11
%28 = load i32, i32* @stderr
%29 = load i32, i32* %6
%30 = load i32, i32* %7
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %29, i32 %30)
br label %32
32: ; preds = %27, %11
%33 = load %struct.input_event*, %struct.input_event** %5
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds %struct.input_event, %struct.input_event* %33, i64 %35
%37 = call i64 @is_ev_syn(%struct.input_event* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %32
store i32 0, i32* %3
br label %45
40: ; preds = %32
br label %41
41: ; preds = %40
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 1
store i32 %43, i32* %6
br label %8
44: ; preds = %8
store i32 0, i32* %3
br label %45
45: ; preds = %44, %39
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.input_event = type { i32 }
@stderr = global i32 0
@.str = [50 x i8] c"Unable to forward event %u to uinput (ret = %lu)\0A\00"
define i32 @forward_events(i32 %0, %struct.input_event* %1) {
br label %3
3: ; preds = %14, %2
%4 = phi i64 [ 0, %2 ], [ %17, %14 ]
%5 = getelementptr inbounds %struct.input_event, %struct.input_event* %1, i64 %4
%6 = getelementptr inbounds %struct.input_event, %struct.input_event* %5, i64 0, i32 0
%7 = tail call i32 @gettimeofday(i32* %6, i32* null)
%8 = tail call i32 @write(i32 %0, %struct.input_event* %5, i32 4)
%9 = icmp eq i32 %8, 4
br i1 %9, label %14, label %10
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = trunc i64 %4 to i32
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %8)
br label %14
14: ; preds = %10, %3
%15 = tail call i64 @is_ev_syn(%struct.input_event* %5)
%16 = icmp ne i64 %15, 0
%17 = add nuw nsw i64 %4, 1
%18 = icmp eq i64 %17, 16
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %20, label %3
20: ; preds = %14
ret i32 0
}