#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 STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int getch(void) {
  struct termios tm;
  struct termios tm_old;
  int fd = STDIN_FILENO, c;
  if (tcgetattr(fd, &tm) < 0)
    return -1;
  tm_old = tm;
  cfmakeraw(&tm);
  if (tcsetattr(fd, TCSANOW, &tm) < 0)
    return -1;
  c = fgetc(stdin);
  if (tcsetattr(fd, TCSANOW, &tm_old) < 0)
    return -1;
  return c;
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1176 <getch+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <getch+0xb2>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
call   1040 <cfmakeraw@plt>
mov    -0x14(%rbp),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    11b0 <getch+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <getch+0xb2>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1030 <fgetc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    11ec <getch+0xac>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <getch+0xb2>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e7f(%rip),%rax
mov    (%rax),%ebx
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1050 <tcgetattr@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     11c3 <getch+0x83>
mov    0x8(%rsp),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1040 <cfmakeraw@plt>
mov    0x2e5b(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
js     11c3 <getch+0x83>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1030 <fgetc@plt>
mov    %eax,%r14d
mov    (%r15),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1060 <tcsetattr@plt>
test   %rax,%rax
mov    $0xffffffff,%ebp
cmovns %r14d,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@TCSANOW = global i32 0
@stdin = global i32 0
define i32 @getch() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = alloca i32
%5 = alloca i32
%6 = load i32, i32* @STDIN_FILENO
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = call i64 @tcgetattr(i32 %7, %struct.termios* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
store i32 -1, i32* %1
br label %30
11: ; preds = %0
%12 = bitcast %struct.termios* %3 to i8*
%13 = bitcast %struct.termios* %2 to i8*
%14 = call i32 @cfmakeraw(%struct.termios* %2)
%15 = load i32, i32* %4
%16 = load i32, i32* @TCSANOW
%17 = call i64 @tcsetattr(i32 %15, i32 %16, %struct.termios* %2)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %11
store i32 -1, i32* %1
br label %30
20: ; preds = %11
%21 = load i32, i32* @stdin
%22 = call i32 @fgetc(i32 %21)
store i32 %22, i32* %5
%23 = load i32, i32* %4
%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 %28
27: ; preds = %20
store i32 -1, i32* %1
br label %30
28: ; preds = %20
%29 = load i32, i32* %5
store i32 %29, i32* %1
br label %30
30: ; preds = %28, %27, %19, %10
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@TCSANOW = global i32 0
@stdin = global i32 0
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 %23, label %8
8: ; preds = %0
%9 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%10 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%11 = load i32, i32* %9
store i32 %11, i32* %10
%12 = call i32 @cfmakeraw(%struct.termios* %1)
%13 = load i32, i32* @TCSANOW
%14 = call i64 @tcsetattr(i32 %5, i32 %13, %struct.termios* %1)
%15 = icmp slt i64 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %8
%17 = load i32, i32* @stdin
%18 = call i32 @fgetc(i32 %17)
%19 = load i32, i32* @TCSANOW
%20 = call i64 @tcsetattr(i32 %5, i32 %19, %struct.termios* %2)
%21 = icmp slt i64 %20, 0
%22 = select i1 %21, i32 -1, i32 %18
br label %23
23: ; preds = %16, %8, %0
%24 = phi i32 [ -1, %0 ], [ -1, %8 ], [ %22, %16 ]
ret i32 %24
}