#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;
};

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ exit(int);
int getchar();
int /*<<< orphan*/ perror(char *);
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
int getch(void) {
  int ch;
  struct termios tm, tm_bak;
  if (tcgetattr(STDIN_FILENO, &tm) < 0) {
    perror("tcgetattr");
    exit(-1);
  }
  tm_bak = tm;
  cfmakeraw(&tm);
  if (tcsetattr(STDIN_FILENO, TCSANOW, &tm) < 0) {
    perror("tcsetattr");
    exit(-1);
  }
  ch = getchar();
  if (tcsetattr(STDIN_FILENO, TCSANOW, &tm_bak) < 0) {
    perror("tcsetattr");
    exit(-1);
  }
  return ch;
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
jge    119a <getch+0x3a>
lea    0xe75(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
call   1040 <cfmakeraw@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    11e4 <getch+0x84>
lea    0xe35(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x4(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1229 <getch+0xc9>
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e68(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1050 <tcgetattr@plt>
test   %rax,%rax
js     11df <getch+0x7f>
mov    (%rsp),%eax
mov    %eax,0x8(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <cfmakeraw@plt>
mov    (%r14),%edi
mov    0x2e4c(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
js     11d6 <getch+0x76>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebx
mov    (%r14),%edi
mov    (%r15),%esi
lea    0x8(%rsp),%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
js     11d6 <getch+0x76>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe2d(%rip),%rdi
jmp    11e6 <getch+0x86>
lea    0xe1a(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@.str = [10 x i8] c"tcgetattr\00"
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define i32 @getch() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = load i32, i32* @STDIN_FILENO
%5 = call i64 @tcgetattr(i32 %4, %struct.termios* %2)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = bitcast %struct.termios* %3 to i8*
%12 = bitcast %struct.termios* %2 to i8*
%13 = call i32 @cfmakeraw(%struct.termios* %2)
%14 = load i32, i32* @STDIN_FILENO
%15 = load i32, i32* @TCSANOW
%16 = call i64 @tcsetattr(i32 %14, i32 %15, %struct.termios* %2)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %10
%22 = call i32 (...) @getchar()
store i32 %22, i32* %1
%23 = load i32, i32* @STDIN_FILENO
%24 = load i32, i32* @TCSANOW
%25 = call i64 @tcsetattr(i32 %23, i32 %24, %struct.termios* %3)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %21
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@.str = [10 x i8] c"tcgetattr\00"
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define i32 @getch() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %1 to i8*
%4 = bitcast %struct.termios* %2 to i8*
%5 = load i32, i32* @STDIN_FILENO
%6 = call i64 @tcgetattr(i32 %5, %struct.termios* %1)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%13 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%14 = load i32, i32* %12
store i32 %14, i32* %13
%15 = call i32 @cfmakeraw(%struct.termios* %1)
%16 = load i32, i32* @STDIN_FILENO
%17 = load i32, i32* @TCSANOW
%18 = call i64 @tcsetattr(i32 %16, i32 %17, %struct.termios* %1)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %11
%21 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %11
%24 = call i32 (...) @getchar()
%25 = load i32, i32* @STDIN_FILENO
%26 = load i32, i32* @TCSANOW
%27 = call i64 @tcsetattr(i32 %25, i32 %26, %struct.termios* %2)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %23
ret i32 %24
}