#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_usec;
  int /*<<< orphan*/ tv_sec;
};
struct timespec {
  int tv_nsec;
  int /*<<< orphan*/ tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ STDIN_FILENO;
struct timeval *calloc(int, int);
int /*<<< orphan*/ free(struct timeval *);
int /*<<< orphan*/ read(scalar_t__, char *, int);
int select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
char read_character(struct timespec *timeout) {
  char c = 0;
  struct timeval *timeout_val = NULL;
  fd_set readfds;
  if (timeout != NULL) {
    timeout_val = calloc(sizeof *timeout_val, 1);
    if (timeout_val != NULL) {
      timeout_val->tv_sec = timeout->tv_sec;
      timeout_val->tv_usec = timeout->tv_nsec / 1000;
    }
  }
  FD_ZERO(&readfds);
  FD_SET(STDIN_FILENO, &readfds);
  if (select(STDIN_FILENO + 1, &readfds, NULL, NULL, timeout_val) != 1)
    goto out;
  (void)read(STDIN_FILENO, &c, 1);
out:
  free(timeout_val);
  return c;
}


x86 assembly (clang, -O0):
<read_character>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movb   $0x0,-0x9(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11ca <read_character+0x6a>
mov    $0x8,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c5 <read_character+0x65>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    11ca <read_character+0x6a>
lea    -0x1c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%rdi
lea    -0x1c(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%r8
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <select@plt>
cmp    $0x1,%eax
je     1216 <read_character+0xb6>
jmp    122e <read_character+0xce>
mov    0x2dc3(%rip),%rax
mov    (%rax),%rdi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movsbl -0x9(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_character>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
movb   $0x0,0x3(%rsp)
test   %rdi,%rdi
je     11af <read_character+0x4f>
mov    %rdi,%rbx
mov    $0x8,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     11af <read_character+0x4f>
mov    %rax,%r14
mov    0x4(%rbx),%eax
mov    %eax,0x4(%r14)
movslq (%rbx),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
mov    %eax,(%r14)
jmp    11b2 <read_character+0x52>
xor    %r14d,%r14d
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1060 <FD_ZERO@plt>
mov    0x2e1a(%rip),%r12
mov    (%r12),%rdi
mov    %r15,%rsi
call   1080 <FD_SET@plt>
mov    (%r12),%rdi
add    $0x1,%rdi
xor    %ebx,%ebx
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1070 <select@plt>
cmp    $0x1,%eax
jne    1207 <read_character+0xa7>
mov    (%r12),%rdi
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    0x3(%rsp),%bl
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
@STDIN_FILENO = global i64 0
define signext i8 @read_character(%struct.timespec* %0) {
%2 = alloca %struct.timespec*
%3 = alloca i8
%4 = alloca %struct.timeval*
%5 = alloca i32
store %struct.timespec* %0, %struct.timespec** %2
store i8 0, i8* %3
store %struct.timeval* null, %struct.timeval** %4
%6 = load %struct.timespec*, %struct.timespec** %2
%7 = icmp ne %struct.timespec* %6, null
br i1 %7, label %8, label %25
8: ; preds = %1
%9 = call %struct.timeval* @calloc(i32 8, i32 1)
store %struct.timeval* %9, %struct.timeval** %4
%10 = load %struct.timeval*, %struct.timeval** %4
%11 = icmp ne %struct.timeval* %10, null
br i1 %11, label %12, label %24
12: ; preds = %8
%13 = load %struct.timespec*, %struct.timespec** %2
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 1
%15 = load i32, i32* %14
%16 = load %struct.timeval*, %struct.timeval** %4
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
store i32 %15, i32* %17
%18 = load %struct.timespec*, %struct.timespec** %2
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = sdiv i32 %20, 1000
%22 = load %struct.timeval*, %struct.timeval** %4
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 0
store i32 %21, i32* %23
br label %24
24: ; preds = %12, %8
br label %25
25: ; preds = %24, %1
%26 = call i32 @FD_ZERO(i32* %5)
%27 = load i64, i64* @STDIN_FILENO
%28 = call i32 @FD_SET(i64 %27, i32* %5)
%29 = load i64, i64* @STDIN_FILENO
%30 = add nsw i64 %29, 1
%31 = load %struct.timeval*, %struct.timeval** %4
%32 = call i32 @select(i64 %30, i32* %5, i32* null, i32* null, %struct.timeval* %31)
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %25
br label %38
35: ; preds = %25
%36 = load i64, i64* @STDIN_FILENO
%37 = call i32 @read(i64 %36, i8* %3, i32 1)
br label %38
38: ; preds = %35, %34
%39 = load %struct.timeval*, %struct.timeval** %4
%40 = call i32 @free(%struct.timeval* %39)
%41 = load i8, i8* %3
ret i8 %41
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
@STDIN_FILENO = global i64 0
define signext i8 @read_character(%struct.timespec* %0) {
%2 = alloca i8
%3 = alloca i32
store i8 0, i8* %2
%4 = bitcast i32* %3 to i8*
%5 = icmp eq %struct.timespec* %0, null
br i1 %5, label %17, label %6
6: ; preds = %1
%7 = tail call %struct.timeval* @calloc(i32 8, i32 1)
%8 = icmp eq %struct.timeval* %7, null
br i1 %8, label %17, label %9
9: ; preds = %6
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%14 = load i32, i32* %13
%15 = sdiv i32 %14, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i32 %15, i32* %16
br label %17
17: ; preds = %6, %9, %1
%18 = phi %struct.timeval* [ %7, %9 ], [ null, %6 ], [ null, %1 ]
%19 = call i32 @FD_ZERO(i32* %3)
%20 = load i64, i64* @STDIN_FILENO
%21 = call i32 @FD_SET(i64 %20, i32* %3)
%22 = load i64, i64* @STDIN_FILENO
%23 = add nsw i64 %22, 1
%24 = call i32 @select(i64 %23, i32* %3, i32* null, i32* null, %struct.timeval* %18)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %30
26: ; preds = %17
%27 = load i64, i64* @STDIN_FILENO
%28 = call i32 @read(i64 %27, i8* %2, i32 1)
%29 = load i8, i8* %2
br label %30
30: ; preds = %17, %26
%31 = phi i8 [ 0, %17 ], [ %29, %26 ]
%32 = call i32 @free(%struct.timeval* %18)
ret i8 %31
}