#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 */

/* Variables and functions */
int control(int, int, int /*<<< orphan*/, int /*<<< orphan*/);
int getc(int);
int getch(int dev, int *buf) {
  if (*buf != (-2) && *buf != (-1)) {
    *buf = getc(dev);
  }
  if (*buf != (-2)) {
    *buf = control(dev, 0x0028, 0, 0);
  }
  return (*buf);
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0xfffffffe,(%rax)
je     1159 <getch+0x39>
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
je     1159 <getch+0x39>
mov    -0x4(%rbp),%edi
call   1040 <getc@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0xfffffffe,(%rax)
je     117f <getch+0x5f>
mov    -0x4(%rbp),%edi
mov    $0x28,%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1030 <control@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rsi),%eax
cmp    $0xfffffffd,%eax
ja     1138 <getch+0x18>
mov    %ebp,%edi
call   1040 <getc@plt>
mov    %eax,(%rbx)
mov    $0xfffffffe,%ecx
cmp    $0xfffffffe,%eax
je     1156 <getch+0x36>
mov    %ebp,%edi
mov    $0x28,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <control@plt>
mov    %eax,%ecx
mov    %eax,(%rbx)
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @getch(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
store i32 %0, i32* %3
store i32* %1, i32** %4
%5 = load i32*, i32** %4
%6 = load i32, i32* %5
%7 = icmp ne i32 %6, -2
br i1 %7, label %8, label %16
8: ; preds = %2
%9 = load i32*, i32** %4
%10 = load i32, i32* %9
%11 = icmp ne i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = load i32, i32* %3
%14 = call i32 @getc(i32 %13)
%15 = load i32*, i32** %4
store i32 %14, i32* %15
br label %16
16: ; preds = %12, %8, %2
%17 = load i32*, i32** %4
%18 = load i32, i32* %17
%19 = icmp ne i32 %18, -2
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i32, i32* %3
%22 = call i32 @control(i32 %21, i32 40, i32 0, i32 0)
%23 = load i32*, i32** %4
store i32 %22, i32* %23
br label %24
24: ; preds = %20, %16
%25 = load i32*, i32** %4
%26 = load i32, i32* %25
ret i32 %26
}

LLVM IR (-O2):
define i32 @getch(i32 %0, i32* nocapture %1) {
%3 = load i32, i32* %1
%4 = icmp ugt i32 %3, -3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @getc(i32 %0)
store i32 %6, i32* %1
br label %7
7: ; preds = %2, %5
%8 = phi i32 [ %3, %2 ], [ %6, %5 ]
%9 = icmp eq i32 %8, -2
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @control(i32 %0, i32 40, i32 0, i32 0)
store i32 %11, i32* %1
br label %12
12: ; preds = %10, %7
%13 = phi i32 [ %11, %10 ], [ -2, %7 ]
ret i32 %13
}