#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 termios {
  int *c_cc;
  int c_lflag;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ IXOFF;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ printf(char *, char const *);
int read(int /*<<< orphan*/, void *, int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int PressAnyKey(const char *prompt) {
  struct termios initial_settings;
  struct termios settings;
  unsigned char keycodes[18];
  int count;
  tcgetattr(STDIN_FILENO, &initial_settings);
  settings = initial_settings;
  settings.c_cc[VTIME] = 1;
  settings.c_cc[VMIN] = 18;
  settings.c_iflag &= ~(IXOFF);
  settings.c_lflag &= ~(ECHO | ICANON);
  tcsetattr(STDIN_FILENO, TCSANOW, &settings);
  printf("%s", prompt ? prompt : "Press a key to continue...");
  count = read(STDIN_FILENO, (void *)keycodes, 18);
  tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
  return (count == 1) ? keycodes[0] : -(int)(keycodes[count - 1]);
}


x86 assembly (clang, -O0):
<PressAnyKey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x12,(%rax,%rcx,4)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    0x2df6(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmpq   $0x0,-0x8(%rbp)
je     11f8 <PressAnyKey+0xb8>
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1208 <PressAnyKey+0xc8>
lea    0xe04(%rip),%rax
mov    %rax,-0x50(%rbp)
jmp    1208 <PressAnyKey+0xc8>
mov    -0x50(%rbp),%rsi
lea    0xded(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
mov    $0x12,%edx
call   1040 <read@plt>
mov    %eax,-0x44(%rbp)
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmpl   $0x1,-0x44(%rbp)
jne    1263 <PressAnyKey+0x123>
movzbl -0x40(%rbp),%eax
mov    %eax,-0x54(%rbp)
jmp    1277 <PressAnyKey+0x137>
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
cltq
movzbl -0x40(%rbp,%rax,1),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PressAnyKey>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rdi,%rbx
mov    0x2e62(%rip),%r15
mov    (%r15),%edi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
movups 0x10(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    (%rsp),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e45(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x12,(%rax,%rcx,4)
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0xc(%rsp)
mov    0x2e2d(%rip),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    (%r15),%edi
mov    0x2e1a(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%rdx
call   1060 <tcsetattr@plt>
test   %rbx,%rbx
lea    0xe27(%rip),%rsi
cmovne %rbx,%rsi
lea    0xe19(%rip),%rdi
call   1030 <printf@plt>
mov    (%r15),%edi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x12,%edx
call   1040 <read@plt>
mov    %eax,%ebp
mov    (%r15),%edi
mov    (%r12),%esi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
cmp    $0x1,%ebp
jne    121e <PressAnyKey+0xde>
movzbl 0x20(%rsp),%eax
jmp    1228 <PressAnyKey+0xe8>
movslq %ebp,%rax
movzbl -0x1(%rax,%rbx,1),%eax
neg    %eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32, i32 }
@STDIN_FILENO = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@IXOFF = global i32 0
@ECHO = global i32 0
@ICANON = global i32 0
@TCSANOW = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [27 x i8] c"Press a key to continue...\00"
define i32 @PressAnyKey(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca [18 x i8]
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %3)
%9 = bitcast %struct.termios* %4 to i8*
%10 = bitcast %struct.termios* %3 to i8*
%11 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%12 = load i32*, i32** %11
%13 = load i64, i64* @VTIME
%14 = getelementptr inbounds i32, i32* %12, i64 %13
store i32 1, i32* %14
%15 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%16 = load i32*, i32** %15
%17 = load i64, i64* @VMIN
%18 = getelementptr inbounds i32, i32* %16, i64 %17
store i32 18, i32* %18
%19 = load i32, i32* @IXOFF
%20 = xor i32 %19, -1
%21 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 2
%22 = load i32, i32* %21
%23 = and i32 %22, %20
store i32 %23, i32* %21
%24 = load i32, i32* @ECHO
%25 = load i32, i32* @ICANON
%26 = or i32 %24, %25
%27 = xor i32 %26, -1
%28 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%29 = load i32, i32* %28
%30 = and i32 %29, %27
store i32 %30, i32* %28
%31 = load i32, i32* @STDIN_FILENO
%32 = load i32, i32* @TCSANOW
%33 = call i32 @tcsetattr(i32 %31, i32 %32, %struct.termios* %4)
%34 = load i8*, i8** %2
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %38
36: ; preds = %1
%37 = load i8*, i8** %2
br label %39
38: ; preds = %1
br label %39
39: ; preds = %38, %36
%40 = phi i8* [ %37, %36 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), %38 ]
%41 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %40)
%42 = load i32, i32* @STDIN_FILENO
%43 = getelementptr inbounds [18 x i8], [18 x i8]* %5, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 18)
store i32 %44, i32* %6
%45 = load i32, i32* @STDIN_FILENO
%46 = load i32, i32* @TCSANOW
%47 = call i32 @tcsetattr(i32 %45, i32 %46, %struct.termios* %3)
%48 = load i32, i32* %6
%49 = icmp eq i32 %48, 1
br i1 %49, label %50, label %54
50: ; preds = %39
%51 = getelementptr inbounds [18 x i8], [18 x i8]* %5, i64 0, i64 0
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
br label %62
54: ; preds = %39
%55 = load i32, i32* %6
%56 = sub nsw i32 %55, 1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [18 x i8], [18 x i8]* %5, i64 0, i64 %57
%59 = load i8, i8* %58
%60 = zext i8 %59 to i32
%61 = sub nsw i32 0, %60
br label %62
62: ; preds = %54, %50
%63 = phi i32 [ %53, %50 ], [ %61, %54 ]
ret i32 %63
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32, i32 }
@STDIN_FILENO = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@IXOFF = global i32 0
@ECHO = global i32 0
@ICANON = global i32 0
@TCSANOW = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [27 x i8] c"Press a key to continue...\00"
define i32 @PressAnyKey(i8* %0) {
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = alloca [18 x i8]
%5 = bitcast %struct.termios* %2 to i8*
%6 = bitcast %struct.termios* %3 to i8*
%7 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* @STDIN_FILENO
%9 = call i32 @tcgetattr(i32 %8, %struct.termios* %2)
%10 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%11 = load i32*, i32** %10
%12 = load i64, i64* @VTIME
%13 = getelementptr inbounds i32, i32* %11, i64 %12
store i32 1, i32* %13
%14 = load i64, i64* @VMIN
%15 = getelementptr inbounds i32, i32* %11, i64 %14
store i32 18, i32* %15
%16 = load i32, i32* @IXOFF
%17 = xor i32 %16, -1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%19 = load i32, i32* %18
%20 = and i32 %19, %17
store i32 %20, i32* %18
%21 = load i32, i32* @ECHO
%22 = load i32, i32* @ICANON
%23 = or i32 %22, %21
%24 = xor i32 %23, -1
%25 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%26 = load i32, i32* %25
%27 = and i32 %26, %24
store i32 %27, i32* %25
%28 = load i32, i32* @STDIN_FILENO
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %28, i32 %29, %struct.termios* %3)
%31 = icmp eq i8* %0, null
%32 = select i1 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0
%33 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @STDIN_FILENO
%35 = call i32 @read(i32 %34, i8* %7, i32 18)
%36 = load i32, i32* @STDIN_FILENO
%37 = load i32, i32* @TCSANOW
%38 = call i32 @tcsetattr(i32 %36, i32 %37, %struct.termios* %2)
%39 = icmp eq i32 %35, 1
br i1 %39, label %40, label %43
40: ; preds = %1
%41 = load i8, i8* %7
%42 = zext i8 %41 to i32
br label %50
43: ; preds = %1
%44 = add nsw i32 %35, -1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = sub nsw i32 0, %48
br label %50
50: ; preds = %43, %40
%51 = phi i32 [ %42, %40 ], [ %49, %43 ]
ret i32 %51
}