#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*/ addstr(char *);
int /*<<< orphan*/ beep();
int /*<<< orphan*/ clrtoeol();
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int getch();
int /*<<< orphan*/ move(int, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ stdscr;
int /*<<< orphan*/ wrefresh(int /*<<< orphan*/);
void startup_getstr(int y, int x, char *buf, size_t max) {
  size_t pos = 0;
  int ch;
  for (;;) {
    buf[pos] = 0;
    move(y, x);
    addstr(buf);
    clrtoeol();
    wrefresh(stdscr);
    fflush(stdout);
    ch = getch();
    switch (ch) {
    case '\n':
    case '\r':
      return;
    case '\b':
      if (pos > 0) {
        pos--;
      }
      break;
    default:
      if (ch >= ' ' && ch < 0x7f && pos < max - 1) {
        buf[pos++] = ch;
      } else {
        beep();
      }
    }
  }
}


x86 assembly (clang, -O0):
<startup_getstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1090 <move@plt>
mov    -0x10(%rbp),%rdi
call   1080 <addstr@plt>
xor    %eax,%eax
mov    %al,-0x29(%rbp)
call   1070 <clrtoeol@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1030 <wrefresh@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    -0x29(%rbp),%al
call   1060 <getch@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x8,%eax
je     121b <startup_getstr+0xab>
jmp    11f3 <startup_getstr+0x83>
mov    -0x28(%rbp),%eax
sub    $0xa,%eax
je     1215 <startup_getstr+0xa5>
jmp    1204 <startup_getstr+0x94>
mov    -0x28(%rbp),%eax
sub    $0xd,%eax
jne    1237 <startup_getstr+0xc7>
jmp    1215 <startup_getstr+0xa5>
add    $0x30,%rsp
pop    %rbp
ret
cmpq   $0x0,-0x20(%rbp)
jbe    1232 <startup_getstr+0xc2>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    128c <startup_getstr+0x11c>
cmpl   $0x20,-0x24(%rbp)
jl     1280 <startup_getstr+0x110>
cmpl   $0x7f,-0x24(%rbp)
jge    1280 <startup_getstr+0x110>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    1280 <startup_getstr+0x110>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    1287 <startup_getstr+0x117>
mov    $0x0,%al
call   1040 <beep@plt>
jmp    128c <startup_getstr+0x11c>
jmp    118e <startup_getstr+0x1e>

x86 assembly (clang, -O2):
<startup_getstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %edi,%ebp
add    $0xffffffffffffffff,%r14
mov    0x2e47(%rip),%r13
xor    %r12d,%r12d
jmp    11a7 <startup_getstr+0x37>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <beep@plt>
movb   $0x0,(%rbx,%r12,1)
mov    %ebp,%edi
mov    %r15d,%esi
call   1090 <move@plt>
mov    %rbx,%rdi
call   1080 <addstr@plt>
xor    %eax,%eax
call   1070 <clrtoeol@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1030 <wrefresh@plt>
mov    0x0(%r13),%edi
call   1050 <fflush@plt>
xor    %eax,%eax
call   1060 <getch@plt>
cmp    $0x8,%eax
je     1209 <startup_getstr+0x99>
cmp    $0xd,%eax
je     1218 <startup_getstr+0xa8>
cmp    $0xa,%eax
je     1218 <startup_getstr+0xa8>
lea    -0x20(%rax),%ecx
cmp    $0x5e,%ecx
ja     11a0 <startup_getstr+0x30>
cmp    %r14,%r12
jae    11a0 <startup_getstr+0x30>
mov    %al,(%rbx,%r12,1)
add    $0x1,%r12
jmp    11a7 <startup_getstr+0x37>
sub    $0x1,%r12
mov    $0x0,%eax
cmovb  %rax,%r12
jmp    11a7 <startup_getstr+0x37>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdscr = global i32 0
@stdout = global i32 0
define void @startup_getstr(i32 %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
store i64 0, i64* %9
br label %11
11: ; preds = %56, %4
%12 = load i8*, i8** %7
%13 = load i64, i64* %9
%14 = getelementptr inbounds i8, i8* %12, i64 %13
store i8 0, i8* %14
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = call i32 @move(i32 %15, i32 %16)
%18 = load i8*, i8** %7
%19 = call i32 @addstr(i8* %18)
%20 = call i32 (...) @clrtoeol()
%21 = load i32, i32* @stdscr
%22 = call i32 @wrefresh(i32 %21)
%23 = load i32, i32* @stdout
%24 = call i32 @fflush(i32 %23)
%25 = call i32 (...) @getch()
store i32 %25, i32* %10
%26 = load i32, i32* %10
switch i32 %26, label %35 [
i32 10, label %27
i32 13, label %27
i32 8, label %28
]
27: ; preds = %11, %11
ret void
28: ; preds = %11
%29 = load i64, i64* %9
%30 = icmp ugt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i64, i64* %9
%33 = add i64 %32, -1
store i64 %33, i64* %9
br label %34
34: ; preds = %31, %28
br label %56
35: ; preds = %11
%36 = load i32, i32* %10
%37 = icmp sge i32 %36, 32
br i1 %37, label %38, label %53
38: ; preds = %35
%39 = load i32, i32* %10
%40 = icmp slt i32 %39, 127
br i1 %40, label %41, label %53
41: ; preds = %38
%42 = load i64, i64* %9
%43 = load i64, i64* %8
%44 = sub i64 %43, 1
%45 = icmp ult i64 %42, %44
br i1 %45, label %46, label %53
46: ; preds = %41
%47 = load i32, i32* %10
%48 = trunc i32 %47 to i8
%49 = load i8*, i8** %7
%50 = load i64, i64* %9
%51 = add i64 %50, 1
store i64 %51, i64* %9
%52 = getelementptr inbounds i8, i8* %49, i64 %50
store i8 %48, i8* %52
br label %55
53: ; preds = %41, %38, %35
%54 = call i32 (...) @beep()
br label %55
55: ; preds = %53, %46
br label %56
56: ; preds = %55, %34
br label %11
}

LLVM IR (-O2):
@stdscr = global i32 0
@stdout = global i32 0
define void @startup_getstr(i32 %0, i32 %1, i8* %2, i64 %3) {
%5 = add i64 %3, -1
br label %6
6: ; preds = %32, %4
%7 = phi i64 [ 0, %4 ], [ %33, %32 ]
%8 = getelementptr inbounds i8, i8* %2, i64 %7
%9 = icmp ult i64 %7, %5
br label %10
10: ; preds = %6, %34
store i8 0, i8* %8
%11 = tail call i32 @move(i32 %0, i32 %1)
%12 = tail call i32 @addstr(i8* %2)
%13 = tail call i32 (...) @clrtoeol()
%14 = load i32, i32* @stdscr
%15 = tail call i32 @wrefresh(i32 %14)
%16 = load i32, i32* @stdout
%17 = tail call i32 @fflush(i32 %16)
%18 = tail call i32 (...) @getch()
switch i32 %18, label %24 [
i32 10, label %19
i32 13, label %19
i32 8, label %20
]
19: ; preds = %10, %10
ret void
20: ; preds = %10
%21 = icmp eq i64 %7, 0
%22 = add i64 %7, -1
%23 = select i1 %21, i64 0, i64 %22
br label %32
24: ; preds = %10
%25 = add i32 %18, -32
%26 = icmp ult i32 %25, 95
%27 = select i1 %26, i1 %9, i1 false
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = getelementptr inbounds i8, i8* %2, i64 %7
%30 = trunc i32 %18 to i8
%31 = add nuw i64 %7, 1
store i8 %30, i8* %29
br label %32
32: ; preds = %28, %20
%33 = phi i64 [ %23, %20 ], [ %31, %28 ]
br label %6
34: ; preds = %24
%35 = tail call i32 (...) @beep()
br label %10
}