#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*/ ttysave;
struct termios {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ raw;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int read(int, char *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int getch(int fd) {
  char buf[16];
  ssize_t n;
  struct termios ttysave;
  struct termios raw;
  memset(&ttysave, 0, sizeof(ttysave));
  memset(&raw, 0, sizeof(raw));
  tcgetattr(fd, &ttysave);
  tcgetattr(fd, &raw);
  cfmakeraw(&raw);
  tcsetattr(fd, TCSAFLUSH, &raw);
  n = read(fd, buf, sizeof(buf));
  tcsetattr(fd, TCSAFLUSH, &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
mov    %edi,-0x8(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1060 <tcgetattr@plt>
lea    -0x30(%rbp),%rdi
call   1050 <cfmakeraw@plt>
mov    -0x8(%rbp),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmpl   $0x0,-0x24(%rbp)
jg     11f0 <getch+0xa0>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <getch+0xbe>
cmpl   $0x1,-0x24(%rbp)
jle    1207 <getch+0xb7>
xor    %eax,%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <getch+0xbe>
movsbl -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <tcgetattr@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <tcgetattr@plt>
mov    %rbp,%rdi
call   1050 <cfmakeraw@plt>
mov    0x2e43(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1070 <tcsetattr@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1040 <read@plt>
mov    %eax,%ebp
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
test   %ebp,%ebp
jle    11e2 <getch+0x92>
cmp    $0x1,%ebp
jne    11e9 <getch+0x99>
movsbl 0x10(%rsp),%ebp
jmp    11eb <getch+0x9b>
mov    $0xffffffff,%ebp
jmp    11eb <getch+0x9b>
neg    %ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@TCSAFLUSH = global i32 0
define i32 @getch(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16 x i8]
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca %struct.termios
store i32 %0, i32* %3
%8 = call i32 @memset(%struct.termios* %6, i32 0, i32 4)
%9 = call i32 @memset(%struct.termios* %7, i32 0, i32 4)
%10 = load i32, i32* %3
%11 = call i32 @tcgetattr(i32 %10, %struct.termios* %6)
%12 = load i32, i32* %3
%13 = call i32 @tcgetattr(i32 %12, %struct.termios* %7)
%14 = call i32 @cfmakeraw(%struct.termios* %7)
%15 = load i32, i32* %3
%16 = load i32, i32* @TCSAFLUSH
%17 = call i32 @tcsetattr(i32 %15, i32 %16, %struct.termios* %7)
%18 = load i32, i32* %3
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 16)
store i32 %20, i32* %5
%21 = load i32, i32* %3
%22 = load i32, i32* @TCSAFLUSH
%23 = call i32 @tcsetattr(i32 %21, i32 %22, %struct.termios* %6)
%24 = load i32, i32* %5
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %1
store i32 -1, i32* %2
br label %37
27: ; preds = %1
%28 = load i32, i32* %5
%29 = icmp sgt i32 %28, 1
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* %5
%32 = sub nsw i32 0, %31
store i32 %32, i32* %2
br label %37
33: ; preds = %27
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
store i32 %36, i32* %2
br label %37
37: ; preds = %33, %30, %26
%38 = load i32, i32* %2
ret i32 %38
}

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