#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 termio {
  int c_iflag;
  int *c_cc;
  scalar_t__ c_lflag;
};

/* Variables and functions */
int BRKINT;
int ISTRIP;
int IXON;
int /*<<< orphan*/ TCGETA;
int /*<<< orphan*/ TCSETA;
size_t VEOF;
int /*<<< orphan*/ ioctl(int /*<<< orphan*/, int /*<<< orphan*/,
                         struct termio *);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int TEK_getkey() {
  struct termio oldtty, newtty;
  char c;
  ioctl(0, TCGETA, &oldtty);
  newtty = oldtty;
  newtty.c_iflag = BRKINT | IXON | ISTRIP;
  newtty.c_lflag = 0;
  newtty.c_cc[VEOF] = 1;
  ioctl(0, TCSETA, &newtty);
  read(0, &c, 1);
  ioctl(0, TCSETA, &oldtty);
  return (c);
}


x86 assembly (clang, -O0):
<TEK_getkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2eb9(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    0x2e7c(%rip),%rcx
or     (%rcx),%eax
mov    0x2e5b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
xor    %edi,%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
movsbl -0x31(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TEK_getkey>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    0x2eba(%rip),%rax
mov    (%rax),%esi
lea    0x30(%rsp),%r14
xor    %edi,%edi
mov    %r14,%rdx
call   1030 <ioctl@plt>
movups 0x30(%rsp),%xmm0
mov    0x2e8d(%rip),%rax
mov    0x2e8e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
movaps %xmm0,0x10(%rsp)
mov    0x2e66(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x10(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x18(%rsp),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e32(%rip),%rbx
mov    (%rbx),%esi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1030 <ioctl@plt>
lea    0xf(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <read@plt>
mov    (%rbx),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1030 <ioctl@plt>
movsbl 0xf(%rsp),%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termio = type { i32, i32*, i64 }
@TCGETA = global i32 0
@BRKINT = global i32 0
@IXON = global i32 0
@ISTRIP = global i32 0
@VEOF = global i64 0
@TCSETA = global i32 0
define i32 @TEK_getkey() {
%1 = alloca %struct.termio
%2 = alloca %struct.termio
%3 = alloca i8
%4 = load i32, i32* @TCGETA
%5 = call i32 @ioctl(i32 0, i32 %4, %struct.termio* %1)
%6 = bitcast %struct.termio* %2 to i8*
%7 = bitcast %struct.termio* %1 to i8*
%8 = load i32, i32* @BRKINT
%9 = load i32, i32* @IXON
%10 = or i32 %8, %9
%11 = load i32, i32* @ISTRIP
%12 = or i32 %10, %11
%13 = getelementptr inbounds %struct.termio, %struct.termio* %2, i32 0, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.termio, %struct.termio* %2, i32 0, i32 2
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.termio, %struct.termio* %2, i32 0, i32 1
%16 = load i32*, i32** %15
%17 = load i64, i64* @VEOF
%18 = getelementptr inbounds i32, i32* %16, i64 %17
store i32 1, i32* %18
%19 = load i32, i32* @TCSETA
%20 = call i32 @ioctl(i32 0, i32 %19, %struct.termio* %2)
%21 = call i32 @read(i32 0, i8* %3, i32 1)
%22 = load i32, i32* @TCSETA
%23 = call i32 @ioctl(i32 0, i32 %22, %struct.termio* %1)
%24 = load i8, i8* %3
%25 = sext i8 %24 to i32
ret i32 %25
}

LLVM IR (-O2):
%struct.termio = type { i32, i32*, i64 }
@TCGETA = global i32 0
@BRKINT = global i32 0
@IXON = global i32 0
@ISTRIP = global i32 0
@VEOF = global i64 0
@TCSETA = global i32 0
define i32 @TEK_getkey() {
%1 = alloca %struct.termio
%2 = alloca %struct.termio
%3 = alloca i8
%4 = bitcast %struct.termio* %1 to i8*
%5 = bitcast %struct.termio* %2 to i8*
%6 = load i32, i32* @TCGETA
%7 = call i32 @ioctl(i32 0, i32 %6, %struct.termio* %1)
%8 = load i32, i32* @BRKINT
%9 = load i32, i32* @IXON
%10 = or i32 %9, %8
%11 = load i32, i32* @ISTRIP
%12 = or i32 %10, %11
%13 = getelementptr inbounds %struct.termio, %struct.termio* %2, i64 0, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.termio, %struct.termio* %2, i64 0, i32 2
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.termio, %struct.termio* %2, i64 0, i32 1
%16 = load i32*, i32** %15
%17 = load i64, i64* @VEOF
%18 = getelementptr inbounds i32, i32* %16, i64 %17
store i32 1, i32* %18
%19 = load i32, i32* @TCSETA
%20 = call i32 @ioctl(i32 0, i32 %19, %struct.termio* %2)
%21 = call i32 @read(i32 0, i8* %3, i32 1)
%22 = load i32, i32* @TCSETA
%23 = call i32 @ioctl(i32 0, i32 %22, %struct.termio* %1)
%24 = load i8, i8* %3
%25 = sext i8 %24 to i32
ret i32 %25
}