#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 timeval {
  int tv_sec;
  int /*<<< orphan*/ tv_usec;
};

/* Variables and functions */
int COLS;
int EOF;
int LINES;
int /*<<< orphan*/ clear();
int getch();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
int /*<<< orphan*/ move(int, int);
int /*<<< orphan*/ printChar(char);
int /*<<< orphan*/ printw(char *, int);
size_t rand();
int /*<<< orphan*/ refresh();
int /*<<< orphan*/ sleep(int);
size_t strlen(char *);
int testReaction(int roundNum) {
  int timeRange = 2;
  char charset[] = "0123456789";
  int characterInput, reactionTime;
  struct timeval startTime, endTime;
  char target = charset[rand() % strlen(charset)];
  move(LINES / 2, COLS / 2 - 4);
  printw("Round %2d", roundNum);
  move(LINES - 1, COLS - 1);
  refresh();
  sleep(2);
  clear();
  refresh();
  printChar(target);
  gettimeofday(&startTime, 0);
  while ((characterInput = getch()) != EOF && characterInput != target)
    ;
  gettimeofday(&endTime, 0);
  printChar(' ');
  if (characterInput == EOF) {
    reactionTime = -1L;
  } else {
    reactionTime = 1000000 * (endTime.tv_sec - startTime.tv_sec) +
                   (endTime.tv_usec = startTime.tv_usec);
  }
  return reactionTime;
}


x86 assembly (clang, -O0):
<testReaction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x2,-0x8(%rbp)
mov    0xe47(%rip),%rax
mov    %rax,-0x13(%rbp)
mov    0xe44(%rip),%ax
mov    %ax,-0xb(%rbp)
mov    0xe3c(%rip),%al
mov    %al,-0x9(%rbp)
mov    $0x0,%al
call   10b0 <rand@plt>
mov    %rax,-0x40(%rbp)
lea    -0x13(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
xor    %edx,%edx
div    %rcx
mov    -0x13(%rbp,%rdx,1),%al
mov    %al,-0x31(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
sub    $0x4,%esi
call   10c0 <move@plt>
mov    -0x4(%rbp),%esi
lea    0xddb(%rip),%rdi
call   1050 <printw@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
sub    $0x1,%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
sub    $0x1,%esi
call   10c0 <move@plt>
mov    $0x0,%al
call   1070 <refresh@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    $0x0,%al
call   1090 <clear@plt>
mov    $0x0,%al
call   1070 <refresh@plt>
movsbl -0x31(%rbp),%edi
call   1060 <printChar@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    $0x0,%al
call   1080 <getch@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
mov    0x2d46(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x41(%rbp)
je     12b4 <testReaction+0x114>
mov    -0x18(%rbp),%eax
movsbl -0x31(%rbp),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    12c4 <testReaction+0x124>
jmp    12c9 <testReaction+0x129>
jmp    1285 <testReaction+0xe5>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    $0x20,%edi
call   1060 <printChar@plt>
mov    -0x18(%rbp),%eax
mov    0x2cf8(%rip),%rcx
cmp    (%rcx),%eax
jne    12fc <testReaction+0x15c>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1313 <testReaction+0x173>
mov    -0x30(%rbp),%eax
sub    -0x28(%rbp),%eax
imul   $0xf4240,%eax,%eax
mov    -0x24(%rbp),%ecx
mov    %ecx,-0x2c(%rbp)
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testReaction>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
xor    %eax,%eax
call   10a0 <rand@plt>
mov    %rax,%rcx
movabs $0xcccccccccccccccd,%rdx
cc cc cc
mul    %rdx
shr    $0x2,%rdx
and    $0xfffffffffffffffe,%rdx
lea    (%rdx,%rdx,4),%rax
sub    %rax,%rcx
lea    0xe36(%rip),%rax
movsbl (%rax,%rcx,1),%ebx
mov    0x2e13(%rip),%r14
mov    (%r14),%eax
mov    %eax,%edi
shr    $0x1f,%edi
add    %eax,%edi
sar    %edi
mov    0x2df0(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,%esi
shr    $0x1f,%esi
add    %eax,%esi
sar    %esi
add    $0xfffffffc,%esi
call   10b0 <move@plt>
lea    0xe08(%rip),%rdi
mov    %r15d,%esi
call   1040 <printw@plt>
mov    (%r14),%edi
add    $0xffffffff,%edi
mov    0x0(%rbp),%esi
add    $0xffffffff,%esi
call   10b0 <move@plt>
xor    %eax,%eax
call   1060 <refresh@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1080 <clear@plt>
xor    %eax,%eax
call   1060 <refresh@plt>
mov    %ebx,%edi
call   1050 <printChar@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x2d8b(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1070 <getch@plt>
mov    %eax,%ebp
cmp    (%r14),%eax
je     1272 <testReaction+0xe2>
cmp    %ebx,%ebp
jne    1260 <testReaction+0xd0>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    $0x20,%edi
call   1050 <printChar@plt>
mov    $0xffffffff,%eax
cmp    (%r14),%ebp
je     12a4 <testReaction+0x114>
mov    0x10(%rsp),%eax
sub    0x8(%rsp),%eax
imul   $0xf4240,%eax,%eax
add    0xc(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@__const.testReaction.charset = [11 x i8] c"0123456789\00"
@LINES = global i32 0
@COLS = global i32 0
@.str = [10 x i8] c"Round %2d\00"
@EOF = global i32 0
define i32 @testReaction(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [11 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca %struct.timeval
%9 = alloca i8
store i32 %0, i32* %2
store i32 2, i32* %3
%10 = bitcast [11 x i8]* %4 to i8*
%11 = call i64 (...) @rand()
%12 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 0
%13 = call i64 @strlen(i8* %12)
%14 = urem i64 %11, %13
%15 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 %14
%16 = load i8, i8* %15
store i8 %16, i8* %9
%17 = load i32, i32* @LINES
%18 = sdiv i32 %17, 2
%19 = load i32, i32* @COLS
%20 = sdiv i32 %19, 2
%21 = sub nsw i32 %20, 4
%22 = call i32 @move(i32 %18, i32 %21)
%23 = load i32, i32* %2
%24 = call i32 @printw(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @LINES
%26 = sub nsw i32 %25, 1
%27 = load i32, i32* @COLS
%28 = sub nsw i32 %27, 1
%29 = call i32 @move(i32 %26, i32 %28)
%30 = call i32 (...) @refresh()
%31 = call i32 @sleep(i32 2)
%32 = call i32 (...) @clear()
%33 = call i32 (...) @refresh()
%34 = load i8, i8* %9
%35 = call i32 @printChar(i8 signext %34)
%36 = call i32 @gettimeofday(%struct.timeval* %7, i32 0)
br label %37
37: ; preds = %48, %1
%38 = call i32 (...) @getch()
store i32 %38, i32* %5
%39 = load i32, i32* @EOF
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i32, i32* %5
%43 = load i8, i8* %9
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %42, %44
br label %46
46: ; preds = %41, %37
%47 = phi i1 [ false, %37 ], [ %45, %41 ]
br i1 %47, label %48, label %49
48: ; preds = %46
br label %37
49: ; preds = %46
%50 = call i32 @gettimeofday(%struct.timeval* %8, i32 0)
%51 = call i32 @printChar(i8 signext 32)
%52 = load i32, i32* %5
%53 = load i32, i32* @EOF
%54 = icmp eq i32 %52, %53
br i1 %54, label %55, label %56
55: ; preds = %49
store i32 -1, i32* %6
br label %67
56: ; preds = %49
%57 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%58 = load i32, i32* %57
%59 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%60 = load i32, i32* %59
%61 = sub nsw i32 %58, %60
%62 = mul nsw i32 1000000, %61
%63 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%64 = load i32, i32* %63
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i32 %64, i32* %65
%66 = add nsw i32 %62, %64
store i32 %66, i32* %6
br label %67
67: ; preds = %56, %55
%68 = load i32, i32* %6
ret i32 %68
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@__const.testReaction.charset = [11 x i8] c"0123456789\00"
@LINES = global i32 0
@COLS = global i32 0
@.str = [10 x i8] c"Round %2d\00"
@EOF = global i32 0
define i32 @testReaction(i32 %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = tail call i64 (...) @rand()
%7 = urem i64 %6, 10
%8 = getelementptr inbounds [11 x i8], [11 x i8]* @__const.testReaction.charset, i64 0, i64 %7
%9 = load i8, i8* %8
%10 = load i32, i32* @LINES
%11 = sdiv i32 %10, 2
%12 = load i32, i32* @COLS
%13 = sdiv i32 %12, 2
%14 = add nsw i32 %13, -4
%15 = tail call i32 @move(i32 %11, i32 %14)
%16 = tail call i32 @printw(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %0)
%17 = load i32, i32* @LINES
%18 = add nsw i32 %17, -1
%19 = load i32, i32* @COLS
%20 = add nsw i32 %19, -1
%21 = tail call i32 @move(i32 %18, i32 %20)
%22 = tail call i32 (...) @refresh()
%23 = tail call i32 @sleep(i32 2)
%24 = tail call i32 (...) @clear()
%25 = tail call i32 (...) @refresh()
%26 = tail call i32 @printChar(i8 signext %9)
%27 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%28 = sext i8 %9 to i32
br label %29
29: ; preds = %29, %1
%30 = call i32 (...) @getch()
%31 = load i32, i32* @EOF
%32 = icmp ne i32 %30, %31
%33 = icmp ne i32 %30, %28
%34 = select i1 %32, i1 %33, i1 false
br i1 %34, label %29, label %35
35: ; preds = %29
%36 = call i32 @gettimeofday(%struct.timeval* %3, i32 0)
%37 = call i32 @printChar(i8 signext 32)
%38 = load i32, i32* @EOF
%39 = icmp eq i32 %30, %38
br i1 %39, label %50, label %40
40: ; preds = %35
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%44 = load i32, i32* %43
%45 = sub nsw i32 %42, %44
%46 = mul nsw i32 %45, 1000000
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%48 = load i32, i32* %47
%49 = add nsw i32 %46, %48
br label %50
50: ; preds = %35, %40
%51 = phi i32 [ %49, %40 ], [ -1, %35 ]
ret i32 %51
}