#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 uint32_t;
struct termios {
  int c_lflag;
};
typedef int /*<<< orphan*/ ch;

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ putchar(char);
int read(int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
uint32_t getch(void) {
  struct termios oldt, newt;
  uint32_t ch = 0;
  uint32_t val = 0;
  int n;
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  n = read(STDIN_FILENO, &ch, sizeof(ch));
  if (!n)
    return 0;
  putchar('\n');
  while (ch) {
    val <<= 8;
    val += ch & 0xFF;
    ch >>= 8;
  }
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  return val;
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    0x2e62(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11d4 <getch+0x94>
movl   $0x0,-0x4(%rbp)
jmp    122e <getch+0xee>
mov    $0xa,%edi
call   1030 <putchar@plt>
cmpl   $0x0,-0x14(%rbp)
je     120d <getch+0xcd>
mov    -0x18(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x14(%rbp)
jmp    11de <getch+0x9e>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0x4(%rsp)
mov    0x2e78(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e5e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    (%rsp),%ecx
mov    %ecx,0x8(%rsp)
mov    (%r14),%edi
mov    0x2e60(%rip),%r15
mov    (%r15),%esi
lea    0x8(%rsp),%rdx
call   1060 <tcsetattr@plt>
mov    (%r14),%edi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11f4 <getch+0xb4>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x4(%rsp),%eax
xor    %ebx,%ebx
test   %eax,%eax
je     11e4 <getch+0xa4>
mov    %eax,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
shl    $0x8,%ebx
movzbl %al,%edx
or     %edx,%ebx
sar    $0x8,%ecx
cmp    $0xff,%eax
mov    %ecx,%eax
ja     11d0 <getch+0x90>
mov    (%r14),%edi
mov    (%r15),%esi
mov    %rsp,%rdx
call   1060 <tcsetattr@plt>
jmp    11f6 <getch+0xb6>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @getch() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %4
store i32 0, i32* %5
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %2)
%9 = bitcast %struct.termios* %3 to i8*
%10 = bitcast %struct.termios* %2 to i8*
%11 = load i32, i32* @ICANON
%12 = load i32, i32* @ECHO
%13 = or i32 %11, %12
%14 = xor i32 %13, -1
%15 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%16 = load i32, i32* %15
%17 = and i32 %16, %14
store i32 %17, i32* %15
%18 = load i32, i32* @STDIN_FILENO
%19 = load i32, i32* @TCSANOW
%20 = call i32 @tcsetattr(i32 %18, i32 %19, %struct.termios* %3)
%21 = load i32, i32* @STDIN_FILENO
%22 = call i32 @read(i32 %21, i32* %4, i32 4)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %0
store i32 0, i32* %1
br label %45
26: ; preds = %0
%27 = call i32 @putchar(i8 signext 10)
br label %28
28: ; preds = %31, %26
%29 = load i32, i32* %4
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %28
%32 = load i32, i32* %5
%33 = shl i32 %32, 8
store i32 %33, i32* %5
%34 = load i32, i32* %4
%35 = and i32 %34, 255
%36 = load i32, i32* %5
%37 = add nsw i32 %36, %35
store i32 %37, i32* %5
%38 = load i32, i32* %4
%39 = ashr i32 %38, 8
store i32 %39, i32* %4
br label %28
40: ; preds = %28
%41 = load i32, i32* @STDIN_FILENO
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 %41, i32 %42, %struct.termios* %2)
%44 = load i32, i32* %5
store i32 %44, i32* %1
br label %45
45: ; preds = %40, %25
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @getch() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i32
%4 = bitcast %struct.termios* %1 to i8*
%5 = bitcast %struct.termios* %2 to i8*
%6 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %1)
%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
%12 = load i32, i32* @ICANON
%13 = load i32, i32* @ECHO
%14 = or i32 %13, %12
%15 = xor i32 %14, -1
%16 = and i32 %11, %15
store i32 %16, i32* %10
%17 = load i32, i32* @STDIN_FILENO
%18 = load i32, i32* @TCSANOW
%19 = call i32 @tcsetattr(i32 %17, i32 %18, %struct.termios* %2)
%20 = load i32, i32* @STDIN_FILENO
%21 = call i32 @read(i32 %20, i32* %3, i32 4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %40, label %23
23: ; preds = %0
%24 = call i32 @putchar(i8 signext 10)
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, 0
br i1 %26, label %35, label %27
27: ; preds = %23, %27
%28 = phi i32 [ %32, %27 ], [ 0, %23 ]
%29 = phi i32 [ %33, %27 ], [ %25, %23 ]
%30 = shl i32 %28, 8
%31 = and i32 %29, 255
%32 = or i32 %30, %31
%33 = ashr i32 %29, 8
%34 = icmp ult i32 %29, 256
br i1 %34, label %35, label %27
35: ; preds = %27, %23
%36 = phi i32 [ 0, %23 ], [ %32, %27 ]
%37 = load i32, i32* @STDIN_FILENO
%38 = load i32, i32* @TCSANOW
%39 = call i32 @tcsetattr(i32 %37, i32 %38, %struct.termios* %1)
br label %40
40: ; preds = %0, %35
%41 = phi i32 [ %36, %35 ], [ 0, %0 ]
ret i32 %41
}