#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 dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ TCIOFLUSH;
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ tcflush(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ termraw(struct termios *);
int /*<<< orphan*/ termset(struct termios *);
int getch(void) {
  char buf[16];
  ssize_t n;
  struct termios ttysave;
  termraw(&ttysave);
  tcflush(0, TCIOFLUSH);
  n = read(0, buf, sizeof(buf));
  termset(&ttysave);
  if (n <= 0)
    return (-1);
  if (n > 1)
    return (-n);
  return (buf[0]);
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x28(%rbp),%rdi
call   1060 <termraw@plt>
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1030 <tcflush@plt>
lea    -0x20(%rbp),%rsi
xor    %edi,%edi
mov    $0x10,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
call   1040 <termset@plt>
cmpl   $0x0,-0x24(%rbp)
jg     1193 <getch+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <getch+0x71>
cmpl   $0x1,-0x24(%rbp)
jle    11aa <getch+0x6a>
xor    %eax,%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b1 <getch+0x71>
movsbl -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1060 <termraw@plt>
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1030 <tcflush@plt>
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    $0x10,%edx
call   1050 <read@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <termset@plt>
test   %ebx,%ebx
jle    118f <getch+0x4f>
cmp    $0x1,%ebx
jne    1196 <getch+0x56>
movsbl 0x10(%rsp),%ebx
jmp    1198 <getch+0x58>
mov    $0xffffffff,%ebx
jmp    1198 <getch+0x58>
neg    %ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@TCIOFLUSH = global i32 0
define i32 @getch() {
%1 = alloca i32
%2 = alloca [16 x i8]
%3 = alloca i32
%4 = alloca %struct.termios
%5 = call i32 @termraw(%struct.termios* %4)
%6 = load i32, i32* @TCIOFLUSH
%7 = call i32 @tcflush(i32 0, i32 %6)
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%9 = call i32 @read(i32 0, i8* %8, i32 16)
store i32 %9, i32* %3
%10 = call i32 @termset(%struct.termios* %4)
%11 = load i32, i32* %3
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 -1, i32* %1
br label %24
14: ; preds = %0
%15 = load i32, i32* %3
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %3
%19 = sub nsw i32 0, %18
store i32 %19, i32* %1
br label %24
20: ; preds = %14
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
store i32 %23, i32* %1
br label %24
24: ; preds = %20, %17, %13
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@TCIOFLUSH = global i32 0
define i32 @getch() {
%1 = alloca [16 x i8]
%2 = alloca %struct.termios
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.termios* %2 to i8*
%5 = call i32 @termraw(%struct.termios* %2)
%6 = load i32, i32* @TCIOFLUSH
%7 = call i32 @tcflush(i32 0, i32 %6)
%8 = call i32 @read(i32 0, i8* %3, i32 16)
%9 = call i32 @termset(%struct.termios* %2)
%10 = icmp slt i32 %8, 1
br i1 %10, label %18, label %11
11: ; preds = %0
%12 = icmp eq i32 %8, 1
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = sub nsw i32 0, %8
br label %18
15: ; preds = %11
%16 = load i8, i8* %3
%17 = sext i8 %16 to i32
br label %18
18: ; preds = %0, %15, %13
%19 = phi i32 [ %14, %13 ], [ %17, %15 ], [ -1, %0 ]
ret i32 %19
}