#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 /*<<< orphan*/ kb_readkey(unsigned char *);
unsigned int kb_toascii(unsigned char *);
scalar_t__ strchr(char *, unsigned int);
unsigned int transl_input(unsigned char *, int);
int kb_getpress(int flags) {
  unsigned char s[5] = "";
  unsigned c;
  if (!kb_readkey(s))
    return 0;
  if (c = transl_input(s, flags))
    return c;
  if (!s[1])
    c = s[0];
  else if (flags & 0x100)
    c = '\033' + 1;
  else
    c = kb_toascii(s);
  if (!(flags & 0x100) || c <= 4)
    return c;
  if (strchr("\033\b \npq", c))
    return c;
  return kb_getpress(flags);
}


x86 assembly (clang, -O0):
<kb_getpress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0xd(%rbp),%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1050 <memset@plt>
lea    -0xd(%rbp),%rdi
call   1080 <kb_readkey@plt>
cmp    $0x0,%eax
jne    1199 <kb_getpress+0x39>
movl   $0x0,-0x4(%rbp)
jmp    1255 <kb_getpress+0xf5>
lea    -0xd(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <transl_input@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     11bc <kb_getpress+0x5c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1255 <kb_getpress+0xf5>
cmpb   $0x0,-0xc(%rbp)
jne    11d2 <kb_getpress+0x72>
movzbl -0xd(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1200 <kb_getpress+0xa0>
mov    -0x8(%rbp),%eax
and    $0x100,%eax
cmp    $0x0,%eax
je     11ef <kb_getpress+0x8f>
movl   $0x1c,-0x14(%rbp)
jmp    11fb <kb_getpress+0x9b>
lea    -0xd(%rbp),%rdi
call   1060 <kb_toascii@plt>
mov    %eax,-0x14(%rbp)
jmp    1200 <kb_getpress+0xa0>
mov    -0x8(%rbp),%eax
and    $0x100,%eax
cmp    $0x0,%eax
je     121b <kb_getpress+0xbb>
cmpl   $0x4,-0x14(%rbp)
ja     1226 <kb_getpress+0xc6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1255 <kb_getpress+0xf5>
mov    -0x14(%rbp),%esi
lea    0xdd0(%rip),%rdi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     124a <kb_getpress+0xea>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1255 <kb_getpress+0xf5>
mov    -0x8(%rbp),%edi
call   1070 <kb_getpress@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kb_getpress>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
movb   $0x0,0x4(%rsp)
movl   $0x0,(%rsp)
mov    %rsp,%rdi
call   1070 <kb_readkey@plt>
test   %eax,%eax
je     119c <kb_getpress+0x4c>
mov    %rsp,%rdi
mov    %ebx,%esi
call   1030 <transl_input@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11d0 <kb_getpress+0x80>
cmpb   $0x0,0x1(%rsp)
je     11a0 <kb_getpress+0x50>
mov    %ebx,%eax
mov    $0x1c,%ebp
and    $0x100,%eax
jne    11ab <kb_getpress+0x5b>
mov    %rsp,%rdi
call   1050 <kb_toascii@plt>
jmp    11ce <kb_getpress+0x7e>
xor    %ebp,%ebp
jmp    11d0 <kb_getpress+0x80>
movzbl (%rsp),%ebp
mov    %ebx,%eax
and    $0x100,%eax
test   %eax,%eax
je     11d0 <kb_getpress+0x80>
cmp    $0x5,%ebp
jb     11d0 <kb_getpress+0x80>
lea    0xe45(%rip),%rdi
mov    %ebp,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    11d0 <kb_getpress+0x80>
mov    %ebx,%edi
call   1060 <kb_getpress@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"\1B\08 \0Apq\00"
define i32 @kb_getpress(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [5 x i8]
%5 = alloca i32
store i32 %0, i32* %3
%6 = bitcast [5 x i8]* %4 to i8*
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%8 = call i32 @kb_readkey(i8* %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %1
store i32 0, i32* %2
br label %53
11: ; preds = %1
%12 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%13 = load i32, i32* %3
%14 = call i32 @transl_input(i8* %12, i32 %13)
store i32 %14, i32* %5
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = load i32, i32* %5
store i32 %17, i32* %2
br label %53
18: ; preds = %11
%19 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 1
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
store i32 %25, i32* %5
br label %35
26: ; preds = %18
%27 = load i32, i32* %3
%28 = and i32 %27, 256
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 28, i32* %5
br label %34
31: ; preds = %26
%32 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%33 = call i32 @kb_toascii(i8* %32)
store i32 %33, i32* %5
br label %34
34: ; preds = %31, %30
br label %35
35: ; preds = %34, %22
%36 = load i32, i32* %3
%37 = and i32 %36, 256
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i32, i32* %5
%41 = icmp ule i32 %40, 4
br i1 %41, label %42, label %44
42: ; preds = %39, %35
%43 = load i32, i32* %5
store i32 %43, i32* %2
br label %53
44: ; preds = %39
%45 = load i32, i32* %5
%46 = call i64 @strchr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = load i32, i32* %5
store i32 %49, i32* %2
br label %53
50: ; preds = %44
%51 = load i32, i32* %3
%52 = call i32 @kb_getpress(i32 %51)
store i32 %52, i32* %2
br label %53
53: ; preds = %50, %48, %42, %16, %10
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
@.str = [7 x i8] c"\1B\08 \0Apq\00"
define i32 @kb_getpress(i32 %0) {
%2 = alloca [5 x i8]
%3 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%4 = call i32 @kb_readkey(i8* %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %33, label %6
6: ; preds = %1
%7 = call i32 @transl_input(i8* %3, i32 %0)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %33
9: ; preds = %6
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 1
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i8, i8* %3
%15 = zext i8 %14 to i32
%16 = and i32 %0, 256
br label %22
17: ; preds = %9
%18 = and i32 %0, 256
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @kb_toascii(i8* %3)
br label %33
22: ; preds = %17, %13
%23 = phi i32 [ %18, %17 ], [ %16, %13 ]
%24 = phi i32 [ 28, %17 ], [ %15, %13 ]
%25 = icmp eq i32 %23, 0
%26 = icmp ult i32 %24, 5
%27 = select i1 %25, i1 true, i1 %26
br i1 %27, label %33, label %28
28: ; preds = %22
%29 = call i64 @strchr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %24)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @kb_getpress(i32 %0)
br label %33
33: ; preds = %20, %28, %22, %6, %1, %31
%34 = phi i32 [ %32, %31 ], [ 0, %1 ], [ %7, %6 ], [ %24, %22 ], [ %24, %28 ], [ %21, %20 ]
ret i32 %34
}