#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 */

/* Variables and functions */
int getchar();
int gettime();
scalar_t__ kbd_pending();
int getchar_timeout(void) {
  int cnt = 30;
  int t0, t1;
  t0 = gettime();
  while (cnt) {
    if (kbd_pending())
      return getchar();
    t1 = gettime();
    if (t0 != t1) {
      cnt--;
      t0 = t1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<getchar_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x1e,-0x8(%rbp)
mov    $0x0,%al
call   1050 <gettime@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     119d <getchar_timeout+0x6d>
mov    $0x0,%al
call   1030 <kbd_pending@plt>
cmp    $0x0,%rax
je     1173 <getchar_timeout+0x43>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x4(%rbp)
jmp    11a4 <getchar_timeout+0x74>
mov    $0x0,%al
call   1050 <gettime@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     1198 <getchar_timeout+0x68>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1149 <getchar_timeout+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getchar_timeout>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <gettime@plt>
mov    %eax,%ebx
mov    $0x1e,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <kbd_pending@plt>
test   %rax,%rax
jne    1179 <getchar_timeout+0x49>
xor    %eax,%eax
call   1050 <gettime@plt>
xor    %ecx,%ecx
cmp    %eax,%ebx
setne  %cl
mov    %eax,%ebx
sub    %ecx,%ebp
jne    1150 <getchar_timeout+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <getchar@plt>

LLVM IR (-O0):
define i32 @getchar_timeout() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 30, i32* %2
%5 = call i32 (...) @gettime()
store i32 %5, i32* %3
br label %6
6: ; preds = %23, %0
%7 = load i32, i32* %2
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %6
%10 = call i64 (...) @kbd_pending()
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 (...) @getchar()
store i32 %13, i32* %1
br label %25
14: ; preds = %9
%15 = call i32 (...) @gettime()
store i32 %15, i32* %4
%16 = load i32, i32* %3
%17 = load i32, i32* %4
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* %2
%21 = add nsw i32 %20, -1
store i32 %21, i32* %2
%22 = load i32, i32* %4
store i32 %22, i32* %3
br label %23
23: ; preds = %19, %14
br label %6
24: ; preds = %6
store i32 0, i32* %1
br label %25
25: ; preds = %24, %12
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
define i32 @getchar_timeout() {
%1 = tail call i32 (...) @gettime()
br label %2
2: ; preds = %0, %9
%3 = phi i32 [ %1, %0 ], [ %10, %9 ]
%4 = phi i32 [ 30, %0 ], [ %13, %9 ]
%5 = tail call i64 (...) @kbd_pending()
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 (...) @getchar()
br label %15
9: ; preds = %2
%10 = tail call i32 (...) @gettime()
%11 = icmp ne i32 %3, %10
%12 = sext i1 %11 to i32
%13 = add nsw i32 %4, %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %2
15: ; preds = %9, %7
%16 = phi i32 [ %8, %7 ], [ 0, %9 ]
ret i32 %16
}