#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 */
typedef int /*<<< orphan*/ tcflag_t;
struct timespec {
  int dummy;
};
struct termios {
  int /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
char read_character(struct timespec *);
int /*<<< orphan*/ *strchr(char const *, char);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
char wait_for_character(const char *charset, struct timespec *timeout) {
  struct termios term;
  tcflag_t lflag;
  char c;
  (void)tcgetattr(STDIN_FILENO, &term);
  lflag = term.c_lflag;
  term.c_lflag &= ~ICANON;
  (void)tcsetattr(STDIN_FILENO, TCSANOW, &term);
  for (;;) {
    c = read_character(timeout);
    if (c == 0 || charset == NULL)
      break;
    else if (strchr(charset, c) != NULL)
      break;
  }
  term.c_lflag = lflag;
  (void)tcsetattr(STDIN_FILENO, TCSANOW, &term);
  return c;
}


x86 assembly (clang, -O0):
<wait_for_character>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x10(%rbp),%rdi
call   1040 <read_character@plt>
mov    %al,-0x1d(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0x0,%eax
je     11b9 <wait_for_character+0x79>
cmpq   $0x0,-0x8(%rbp)
jne    11be <wait_for_character+0x7e>
jmp    11e4 <wait_for_character+0xa4>
mov    -0x8(%rbp),%rdi
movsbl -0x1d(%rbp),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11da <wait_for_character+0x9a>
jmp    11e4 <wait_for_character+0xa4>
jmp    11df <wait_for_character+0x9f>
jmp    1195 <wait_for_character+0x55>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
movsbl -0x1d(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_character>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e80(%rip),%r12
mov    (%r12),%edi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1050 <tcgetattr@plt>
mov    (%rsp),%ebp
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ebp,%eax
mov    %eax,(%rsp)
mov    (%r12),%edi
mov    0x2e63(%rip),%r13
mov    0x0(%r13),%esi
mov    %rbx,%rdx
call   1060 <tcsetattr@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1040 <read_character@plt>
mov    %eax,%ebx
test   %r15,%r15
je     11c3 <wait_for_character+0x83>
test   %bl,%bl
je     11c3 <wait_for_character+0x83>
movsbl %bl,%esi
mov    %r15,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
je     11a0 <wait_for_character+0x60>
mov    %ebp,(%rsp)
mov    (%r12),%edi
mov    0x0(%r13),%esi
mov    %rsp,%rdx
call   1060 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@TCSANOW = global i32 0
define signext i8 @wait_for_character(i8* %0, %struct.timespec* %1) {
%3 = alloca i8*
%4 = alloca %struct.timespec*
%5 = alloca %struct.termios
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %3
store %struct.timespec* %1, %struct.timespec** %4
%8 = load i32, i32* @STDIN_FILENO
%9 = call i32 @tcgetattr(i32 %8, %struct.termios* %5)
%10 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%11 = load i32, i32* %10
store i32 %11, i32* %6
%12 = load i32, i32* @ICANON
%13 = xor i32 %12, -1
%14 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = and i32 %15, %13
store i32 %16, i32* %14
%17 = load i32, i32* @STDIN_FILENO
%18 = load i32, i32* @TCSANOW
%19 = call i32 @tcsetattr(i32 %17, i32 %18, %struct.termios* %5)
br label %20
20: ; preds = %37, %2
%21 = load %struct.timespec*, %struct.timespec** %4
%22 = call signext i8 @read_character(%struct.timespec* %21)
store i8 %22, i8* %7
%23 = load i8, i8* %7
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %20
%27 = load i8*, i8** %3
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %26, %20
br label %38
30: ; preds = %26
%31 = load i8*, i8** %3
%32 = load i8, i8* %7
%33 = call i32* @strchr(i8* %31, i8 signext %32)
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %36
35: ; preds = %30
br label %38
36: ; preds = %30
br label %37
37: ; preds = %36
br label %20
38: ; preds = %35, %29
%39 = load i32, i32* %6
%40 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
store i32 %39, i32* %40
%41 = load i32, i32* @STDIN_FILENO
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 %41, i32 %42, %struct.termios* %5)
%44 = load i8, i8* %7
ret i8 %44
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@TCSANOW = global i32 0
define signext i8 @wait_for_character(i8* %0, %struct.timespec* %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = load i32, i32* @STDIN_FILENO
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %3)
%7 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%8 = load i32, i32* %7
%9 = load i32, i32* @ICANON
%10 = xor i32 %9, -1
%11 = and i32 %8, %10
store i32 %11, i32* %7
%12 = load i32, i32* @STDIN_FILENO
%13 = load i32, i32* @TCSANOW
%14 = call i32 @tcsetattr(i32 %12, i32 %13, %struct.termios* %3)
%15 = icmp eq i8* %0, null
br label %16
16: ; preds = %20, %2
%17 = call signext i8 @read_character(%struct.timespec* %1)
%18 = icmp eq i8 %17, 0
%19 = or i1 %15, %18
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = call i32* @strchr(i8* %0, i8 signext %17)
%22 = icmp eq i32* %21, null
br i1 %22, label %16, label %23
23: ; preds = %20, %16
store i32 %8, i32* %7
%24 = load i32, i32* @STDIN_FILENO
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 %24, i32 %25, %struct.termios* %3)
ret i8 %17
}