#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 timezone {
  int dummy;
};
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, struct timezone *);
char **twiddler_get_table(unsigned long);
int /*<<< orphan*/ twiddler_use_item(char *);
int twiddler_key(unsigned long message) {
  char **table = twiddler_get_table(message);
  char *val;
  static int last_message;
  static int marked;
  static struct timeval tv1, tv2;
  static int nclick, last_pressed;
  if (!table)
    return 0;
  message &= 0xff;
  val = table[message];
  if ((message < last_message) && !marked) {
    marked++;
    twiddler_use_item(table[last_message]);
    if (last_pressed != last_message) {
      nclick = 1;
      (gettimeofday(&tv1, (struct timezone *)NULL));
    }
    last_pressed = last_message;
  }
  if (message < last_message) {
    last_message = message;
    return 0;
  }
  if (message != last_pressed) {
    marked = 0;
    last_message = message;
    return 0;
  }
  if (message > last_message) {
    marked = 0;
    (gettimeofday(&tv2, (struct timezone *)NULL));
    nclick = 1 + (((tv2.tv_sec - tv1.tv_sec) * 1000 +
                   (tv2.tv_usec - tv1.tv_usec) / 1000) < 300);
    last_message = message;
    if (nclick == 1)
      (gettimeofday(&tv1, (struct timezone *)NULL));
    return 1;
  }
  if (nclick == 2) {
    (gettimeofday(&tv1, (struct timezone *)NULL));
    if (((tv1.tv_sec - tv2.tv_sec) * 1000 +
         (tv1.tv_usec - tv2.tv_usec) / 1000) > 500)
      twiddler_use_item(table[message]);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<twiddler_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <twiddler_get_table@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1160 <twiddler_key+0x30>
movl   $0x0,-0x4(%rbp)
jmp    137b <twiddler_key+0x24b>
mov    -0x10(%rbp),%rax
and    $0xff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq 0x2eb3(%rip),%rcx
cmp    %rcx,%rax
jae    11fa <twiddler_key+0xca>
cmpl   $0x0,0x2ea7(%rip)
jne    11fa <twiddler_key+0xca>
mov    0x2e9b(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e92(%rip)
mov    -0x18(%rbp),%rax
movslq 0x2e83(%rip),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <twiddler_use_item@plt>
mov    0x2e90(%rip),%eax
cmp    0x2e6e(%rip),%eax
je     11ee <twiddler_key+0xbe>
movl   $0x1,0x2e76(%rip)
lea    0x2e5f(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    0x2e48(%rip),%eax
mov    %eax,0x2e5e(%rip)
mov    -0x10(%rbp),%rax
movslq 0x2e37(%rip),%rcx
cmp    %rcx,%rax
jae    1224 <twiddler_key+0xf4>
mov    -0x10(%rbp),%rax
mov    %eax,0x2e24(%rip)
movl   $0x0,-0x4(%rbp)
jmp    137b <twiddler_key+0x24b>
mov    -0x10(%rbp),%rax
movslq 0x2e29(%rip),%rcx
cmp    %rcx,%rax
je     1258 <twiddler_key+0x128>
movl   $0x0,0x2dfe(%rip)
mov    -0x10(%rbp),%rax
mov    %eax,0x2df0(%rip)
movl   $0x0,-0x4(%rbp)
jmp    137b <twiddler_key+0x24b>
mov    -0x10(%rbp),%rax
movslq 0x2dd9(%rip),%rcx
cmp    %rcx,%rax
jbe    12ff <twiddler_key+0x1cf>
movl   $0x0,0x2dca(%rip)
lea    0x2dcf(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    0x2dc0(%rip),%eax
sub    0x2db2(%rip),%eax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x24(%rbp)
mov    0x2daf(%rip),%eax
sub    0x2da1(%rip),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
cmp    $0x12c,%eax
setl   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x1,%eax
mov    %eax,0x2d88(%rip)
mov    -0x10(%rbp),%rax
mov    %eax,0x2d66(%rip)
cmpl   $0x1,0x2d77(%rip)
jne    12f3 <twiddler_key+0x1c3>
lea    0x2d5a(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
movl   $0x1,-0x4(%rbp)
jmp    137b <twiddler_key+0x24b>
cmpl   $0x2,0x2d4e(%rip)
jne    1374 <twiddler_key+0x244>
lea    0x2d31(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    0x2d22(%rip),%eax
sub    0x2d24(%rip),%eax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x28(%rbp)
mov    0x2d11(%rip),%eax
sub    0x2d13(%rip),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
cmp    $0x1f4,%eax
jle    1368 <twiddler_key+0x238>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <twiddler_use_item@plt>
movl   $0x1,-0x4(%rbp)
jmp    137b <twiddler_key+0x24b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<twiddler_key>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbp
call   1040 <twiddler_get_table@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     1259 <twiddler_key+0x129>
mov    %rax,%rbx
movzbl %bpl,%ebp
mov    0x2ee7(%rip),%eax
cmp    %rax,%rbp
jae    11a7 <twiddler_key+0x77>
testb  $0x1,0x2edf(%rip)
jne    11a7 <twiddler_key+0x77>
movb   $0x1,0x2ed6(%rip)
mov    (%rbx,%rax,8),%rdi
call   1030 <twiddler_use_item@plt>
mov    0x2edf(%rip),%eax
cmp    0x2ebd(%rip),%eax
je     119f <twiddler_key+0x6f>
movl   $0x1,0x2ec9(%rip)
lea    0x2eb2(%rip),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    0x2e9d(%rip),%eax
mov    %eax,0x2eb3(%rip)
mov    %eax,%eax
cmp    %rax,%rbp
jb     1253 <twiddler_key+0x123>
mov    0x2ea2(%rip),%ecx
cmp    %rcx,%rbp
jne    124c <twiddler_key+0x11c>
cmp    %rax,%rbp
jbe    1261 <twiddler_key+0x131>
movb   $0x0,0x2e71(%rip)
lea    0x2e76(%rip),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    0x2e69(%rip),%eax
sub    0x2e5b(%rip),%eax
imul   $0x3e8,%eax,%eax
mov    0x2e5b(%rip),%ecx
sub    0x2e4d(%rip),%ecx
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
add    %eax,%ecx
xor    %eax,%eax
cmp    $0x12c,%ecx
setl   %al
add    $0x1,%eax
mov    %eax,0x2e2c(%rip)
mov    %ebp,0x2e0e(%rip)
mov    $0x1,%r14d
cmp    $0x12c,%ecx
jl     1259 <twiddler_key+0x129>
lea    0x2e01(%rip),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
jmp    1259 <twiddler_key+0x129>
movb   $0x0,0x2ded(%rip)
mov    %ebp,0x2de3(%rip)
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
cmpl   $0x2,0x2dec(%rip)
jne    1259 <twiddler_key+0x129>
lea    0x2dd3(%rip),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    0x2dc6(%rip),%eax
sub    0x2dc8(%rip),%eax
imul   $0x3e8,%eax,%eax
mov    0x2db8(%rip),%ecx
sub    0x2dba(%rip),%ecx
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
add    %eax,%ecx
mov    $0x1,%r14d
cmp    $0x1f5,%ecx
jl     1259 <twiddler_key+0x129>
mov    (%rbx,%rbp,8),%rdi
call   1030 <twiddler_use_item@plt>
jmp    1259 <twiddler_key+0x129>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.timezone = type { i32 }
@twiddler_key.last_message = internal global i32 0
@twiddler_key.marked = internal global i32 0
@twiddler_key.tv1 = internal global %struct.timeval zeroinitializer
@twiddler_key.tv2 = internal global %struct.timeval zeroinitializer
@twiddler_key.nclick = internal global i32 0
@twiddler_key.last_pressed = internal global i32 0
define i32 @twiddler_key(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8**
%5 = alloca i8*
store i64 %0, i64* %3
%6 = load i64, i64* %3
%7 = call i8** @twiddler_get_table(i64 %6)
store i8** %7, i8*** %4
%8 = load i8**, i8*** %4
%9 = icmp ne i8** %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i32 0, i32* %2
br label %106
11: ; preds = %1
%12 = load i64, i64* %3
%13 = and i64 %12, 255
store i64 %13, i64* %3
%14 = load i8**, i8*** %4
%15 = load i64, i64* %3
%16 = getelementptr inbounds i8*, i8** %14, i64 %15
%17 = load i8*, i8** %16
store i8* %17, i8** %5
%18 = load i64, i64* %3
%19 = load i32, i32* @twiddler_key.last_message
%20 = sext i32 %19 to i64
%21 = icmp ult i64 %18, %20
br i1 %21, label %22, label %41
22: ; preds = %11
%23 = load i32, i32* @twiddler_key.marked
%24 = icmp ne i32 %23, 0
br i1 %24, label %41, label %25
25: ; preds = %22
%26 = load i32, i32* @twiddler_key.marked
%27 = add nsw i32 %26, 1
store i32 %27, i32* @twiddler_key.marked
%28 = load i8**, i8*** %4
%29 = load i32, i32* @twiddler_key.last_message
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
%32 = load i8*, i8** %31
%33 = call i32 @twiddler_use_item(i8* %32)
%34 = load i32, i32* @twiddler_key.last_pressed
%35 = load i32, i32* @twiddler_key.last_message
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %25
store i32 1, i32* @twiddler_key.nclick
%38 = call i32 @gettimeofday(%struct.timeval* @twiddler_key.tv1, %struct.timezone* null)
br label %39
39: ; preds = %37, %25
%40 = load i32, i32* @twiddler_key.last_message
store i32 %40, i32* @twiddler_key.last_pressed
br label %41
41: ; preds = %39, %22, %11
%42 = load i64, i64* %3
%43 = load i32, i32* @twiddler_key.last_message
%44 = sext i32 %43 to i64
%45 = icmp ult i64 %42, %44
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i64, i64* %3
%48 = trunc i64 %47 to i32
store i32 %48, i32* @twiddler_key.last_message
store i32 0, i32* %2
br label %106
49: ; preds = %41
%50 = load i64, i64* %3
%51 = load i32, i32* @twiddler_key.last_pressed
%52 = sext i32 %51 to i64
%53 = icmp ne i64 %50, %52
br i1 %53, label %54, label %57
54: ; preds = %49
store i32 0, i32* @twiddler_key.marked
%55 = load i64, i64* %3
%56 = trunc i64 %55 to i32
store i32 %56, i32* @twiddler_key.last_message
store i32 0, i32* %2
br label %106
57: ; preds = %49
%58 = load i64, i64* %3
%59 = load i32, i32* @twiddler_key.last_message
%60 = sext i32 %59 to i64
%61 = icmp ugt i64 %58, %60
br i1 %61, label %62, label %83
62: ; preds = %57
store i32 0, i32* @twiddler_key.marked
%63 = call i32 @gettimeofday(%struct.timeval* @twiddler_key.tv2, %struct.timezone* null)
%64 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv2, i32 0, i32 0)
%65 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv1, i32 0, i32 0)
%66 = sub nsw i32 %64, %65
%67 = mul nsw i32 %66, 1000
%68 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv2, i32 0, i32 1)
%69 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv1, i32 0, i32 1)
%70 = sub nsw i32 %68, %69
%71 = sdiv i32 %70, 1000
%72 = add nsw i32 %67, %71
%73 = icmp slt i32 %72, 300
%74 = zext i1 %73 to i32
%75 = add nsw i32 1, %74
store i32 %75, i32* @twiddler_key.nclick
%76 = load i64, i64* %3
%77 = trunc i64 %76 to i32
store i32 %77, i32* @twiddler_key.last_message
%78 = load i32, i32* @twiddler_key.nclick
%79 = icmp eq i32 %78, 1
br i1 %79, label %80, label %82
80: ; preds = %62
%81 = call i32 @gettimeofday(%struct.timeval* @twiddler_key.tv1, %struct.timezone* null)
br label %82
82: ; preds = %80, %62
store i32 1, i32* %2
br label %106
83: ; preds = %57
%84 = load i32, i32* @twiddler_key.nclick
%85 = icmp eq i32 %84, 2
br i1 %85, label %86, label %105
86: ; preds = %83
%87 = call i32 @gettimeofday(%struct.timeval* @twiddler_key.tv1, %struct.timezone* null)
%88 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv1, i32 0, i32 0)
%89 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv2, i32 0, i32 0)
%90 = sub nsw i32 %88, %89
%91 = mul nsw i32 %90, 1000
%92 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv1, i32 0, i32 1)
%93 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv2, i32 0, i32 1)
%94 = sub nsw i32 %92, %93
%95 = sdiv i32 %94, 1000
%96 = add nsw i32 %91, %95
%97 = icmp sgt i32 %96, 500
br i1 %97, label %98, label %104
98: ; preds = %86
%99 = load i8**, i8*** %4
%100 = load i64, i64* %3
%101 = getelementptr inbounds i8*, i8** %99, i64 %100
%102 = load i8*, i8** %101
%103 = call i32 @twiddler_use_item(i8* %102)
br label %104
104: ; preds = %98, %86
store i32 1, i32* %2
br label %106
105: ; preds = %83
store i32 0, i32* %2
br label %106
106: ; preds = %105, %104, %82, %54, %46, %10
%107 = load i32, i32* %2
ret i32 %107
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.timezone = type { i32 }
@twiddler_key.last_message = internal unnamed_addr global i32 0
@twiddler_key.marked = internal unnamed_addr global i1 false
@twiddler_key.tv1 = internal global %struct.timeval zeroinitializer
@twiddler_key.tv2 = internal global %struct.timeval zeroinitializer
@twiddler_key.nclick = internal unnamed_addr global i32 0
@twiddler_key.last_pressed = internal unnamed_addr global i32 0
define i32 @twiddler_key(i64 %0) {
%2 = tail call i8** @twiddler_get_table(i64 %0)
%3 = icmp eq i8** %2, null
br i1 %3, label %72, label %4
4: ; preds = %1
%5 = and i64 %0, 255
%6 = getelementptr inbounds i8*, i8** %2, i64 %5
%7 = load i32, i32* @twiddler_key.last_message
%8 = zext i32 %7 to i64
%9 = icmp uge i64 %5, %8
%10 = load i1, i1* @twiddler_key.marked
%11 = select i1 %9, i1 true, i1 %10
br i1 %11, label %25, label %12
12: ; preds = %4
store i1 true, i1* @twiddler_key.marked
%13 = getelementptr inbounds i8*, i8** %2, i64 %8
%14 = load i8*, i8** %13
%15 = tail call i32 @twiddler_use_item(i8* %14)
%16 = load i32, i32* @twiddler_key.last_pressed
%17 = load i32, i32* @twiddler_key.last_message
%18 = icmp eq i32 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %12
store i32 1, i32* @twiddler_key.nclick
%20 = tail call i32 @gettimeofday(%struct.timeval* @twiddler_key.tv1, %struct.timezone* null)
%21 = load i32, i32* @twiddler_key.last_message
br label %22
22: ; preds = %19, %12
%23 = phi i32 [ %21, %19 ], [ %16, %12 ]
store i32 %23, i32* @twiddler_key.last_pressed
%24 = zext i32 %23 to i64
br label %25
25: ; preds = %22, %4
%26 = phi i64 [ %24, %22 ], [ %8, %4 ]
%27 = icmp ult i64 %5, %26
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = trunc i64 %5 to i32
store i32 %29, i32* @twiddler_key.last_message
br label %72
30: ; preds = %25
%31 = load i32, i32* @twiddler_key.last_pressed
%32 = zext i32 %31 to i64
%33 = icmp eq i64 %5, %32
br i1 %33, label %36, label %34
34: ; preds = %30
store i1 false, i1* @twiddler_key.marked
%35 = trunc i64 %5 to i32
store i32 %35, i32* @twiddler_key.last_message
br label %72
36: ; preds = %30
%37 = icmp ugt i64 %5, %26
br i1 %37, label %38, label %54
38: ; preds = %36
store i1 false, i1* @twiddler_key.marked
%39 = tail call i32 @gettimeofday(%struct.timeval* @twiddler_key.tv2, %struct.timezone* null)
%40 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv2, i64 0, i32 0)
%41 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv1, i64 0, i32 0)
%42 = sub nsw i32 %40, %41
%43 = mul nsw i32 %42, 1000
%44 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv2, i64 0, i32 1)
%45 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv1, i64 0, i32 1)
%46 = sub nsw i32 %44, %45
%47 = sdiv i32 %46, 1000
%48 = add nsw i32 %47, %43
%49 = icmp slt i32 %48, 300
%50 = select i1 %49, i32 2, i32 1
store i32 %50, i32* @twiddler_key.nclick
%51 = trunc i64 %5 to i32
store i32 %51, i32* @twiddler_key.last_message
br i1 %49, label %72, label %52
52: ; preds = %38
%53 = tail call i32 @gettimeofday(%struct.timeval* @twiddler_key.tv1, %struct.timezone* null)
br label %72
54: ; preds = %36
%55 = load i32, i32* @twiddler_key.nclick
%56 = icmp eq i32 %55, 2
br i1 %56, label %57, label %72
57: ; preds = %54
%58 = tail call i32 @gettimeofday(%struct.timeval* @twiddler_key.tv1, %struct.timezone* null)
%59 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv1, i64 0, i32 0)
%60 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv2, i64 0, i32 0)
%61 = sub nsw i32 %59, %60
%62 = mul nsw i32 %61, 1000
%63 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv1, i64 0, i32 1)
%64 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @twiddler_key.tv2, i64 0, i32 1)
%65 = sub nsw i32 %63, %64
%66 = sdiv i32 %65, 1000
%67 = add nsw i32 %66, %62
%68 = icmp sgt i32 %67, 500
br i1 %68, label %69, label %72
69: ; preds = %57
%70 = load i8*, i8** %6
%71 = tail call i32 @twiddler_use_item(i8* %70)
br label %72
72: ; preds = %54, %57, %69, %38, %52, %1, %34, %28
%73 = phi i32 [ 0, %28 ], [ 0, %34 ], [ 0, %1 ], [ 1, %52 ], [ 1, %38 ], [ 1, %69 ], [ 1, %57 ], [ 0, %54 ]
ret i32 %73
}