#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 timespec {
  int tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int EOF;
int getchar();
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
char btktermio_get_char(void) {
  struct timespec ts;
  int t = 50;
  int c;
  ts.tv_sec = t / 1000;
  ts.tv_nsec = (t % 1000) * 1000000;
  while ((c = getchar()) == EOF) {
    nanosleep(&ts, NULL);
  }
  return (char)c;
}


x86 assembly (clang, -O0):
<btktermio_get_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x32,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0xf4240,%edx,%eax
mov    %eax,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e86(%rip),%rcx
cmp    (%rcx),%eax
jne    117c <btktermio_get_char+0x5c>
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
jmp    1151 <btktermio_get_char+0x31>
mov    -0x10(%rbp),%eax
movsbl %al,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<btktermio_get_char>:
push   %r14
push   %rbx
push   %rax
movabs $0x2faf08000000000,%rax
f0 fa 02
mov    %rax,(%rsp)
xor    %eax,%eax
call   1040 <getchar@plt>
mov    0x2ea8(%rip),%r14
cmp    (%r14),%eax
jne    1166 <btktermio_get_char+0x46>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    (%r14),%eax
je     1150 <btktermio_get_char+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@EOF = global i32 0
define signext i8 @btktermio_get_char() {
%1 = alloca %struct.timespec
%2 = alloca i32
%3 = alloca i32
store i32 50, i32* %2
%4 = load i32, i32* %2
%5 = sdiv i32 %4, 1000
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 0
store i32 %5, i32* %6
%7 = load i32, i32* %2
%8 = srem i32 %7, 1000
%9 = mul nsw i32 %8, 1000000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 1
store i32 %9, i32* %10
br label %11
11: ; preds = %15, %0
%12 = call i32 (...) @getchar()
store i32 %12, i32* %3
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @nanosleep(%struct.timespec* %1, i32* null)
br label %11
17: ; preds = %11
%18 = load i32, i32* %3
%19 = trunc i32 %18 to i8
ret i8 %19
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@EOF = global i32 0
define signext i8 @btktermio_get_char() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 0, i32* %3
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i32 50000000, i32* %4
%5 = call i32 (...) @getchar()
%6 = load i32, i32* @EOF
%7 = icmp eq i32 %5, %6
br i1 %7, label %8, label %13
8: ; preds = %0, %8
%9 = call i32 @nanosleep(%struct.timespec* %1, i32* null)
%10 = call i32 (...) @getchar()
%11 = load i32, i32* @EOF
%12 = icmp eq i32 %10, %11
br i1 %12, label %8, label %13
13: ; preds = %8, %0
%14 = phi i32 [ %5, %0 ], [ %10, %8 ]
%15 = trunc i32 %14 to i8
ret i8 %15
}