#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*/ addch(char);
int /*<<< orphan*/ addstr(char *);
char getch();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ refresh();
void readstring(char *str, int size) {
  int count = 0;
  char ch;
  for (;;) {
    ch = getch();
    if (ch == '\n') {
      *str = '\0';
      break;
    }
    if ((ch == '\010') || (ch == '\177')) {
      if (count == 0)
        continue;
      str--;
      count--;
      addstr("\b \b");
      refresh();
      continue;
    }
    if (count == size) {
      printf("\007");
      continue;
    }
    addch(ch);
    *str = ch;
    str++;
    count++;
    refresh();
  }
}


x86 assembly (clang, -O0):
<readstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1060 <getch@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    1189 <readstring+0x39>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    122f <readstring+0xdf>
movsbl -0x11(%rbp),%eax
cmp    $0x8,%eax
je     11a3 <readstring+0x53>
movsbl -0x11(%rbp),%eax
cmp    $0x7f,%eax
jne    11df <readstring+0x8f>
cmpl   $0x0,-0x10(%rbp)
jne    11b2 <readstring+0x62>
jmp    1166 <readstring+0x16>
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
lea    0xe32(%rip),%rdi
call   1070 <addstr@plt>
mov    $0x0,%al
call   1050 <refresh@plt>
jmp    1166 <readstring+0x16>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    11fc <readstring+0xac>
lea    0xe12(%rip),%rdi
call   1030 <printf@plt>
jmp    1166 <readstring+0x16>
movsbl -0x11(%rbp),%edi
call   1040 <addch@plt>
mov    -0x11(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <refresh@plt>
jmp    1166 <readstring+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readstring>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r12d
mov    %rdi,%r15
lea    0xe9b(%rip),%r14
xor    %ebx,%ebx
jmp    1176 <readstring+0x26>
nopl   0x0(%rax)
xor    %ebx,%ebx
test   %ebp,%ebp
jne    11cd <readstring+0x7d>
mov    %ebx,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <getch@plt>
cmp    $0x8,%al
je     1170 <readstring+0x20>
mov    %eax,%ebx
cmp    $0x7f,%al
je     1170 <readstring+0x20>
cmp    $0xa,%bl
je     11de <readstring+0x8e>
cmp    %r12d,%ebp
jne    11b0 <readstring+0x60>
mov    $0x7,%edi
call   1030 <putchar@plt>
jmp    1180 <readstring+0x30>
nopw   0x0(%rax,%rax,1)
movsbl %bl,%edi
call   1040 <addch@plt>
mov    %bl,(%r15)
add    $0x1,%r15
add    $0x1,%ebp
mov    %ebp,%ebx
xor    %eax,%eax
call   1050 <refresh@plt>
jmp    1176 <readstring+0x26>
add    $0xffffffffffffffff,%r15
add    $0xffffffff,%ebp
mov    %r14,%rdi
call   1070 <addstr@plt>
jmp    11c2 <readstring+0x72>
movb   $0x0,(%r15)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"\08 \08\00"
@.str.1 = [2 x i8] c"\07\00"
define void @readstring(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %39, %37, %26, %25, %2
%8 = call signext i8 (...) @getch()
store i8 %8, i8* %6
%9 = load i8, i8* %6
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 10
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = load i8*, i8** %3
store i8 0, i8* %13
br label %49
14: ; preds = %7
%15 = load i8, i8* %6
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 8
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = load i8, i8* %6
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 127
br i1 %21, label %22, label %33
22: ; preds = %18, %14
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22
br label %7
26: ; preds = %22
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i32 -1
store i8* %28, i8** %3
%29 = load i32, i32* %5
%30 = add nsw i32 %29, -1
store i32 %30, i32* %5
%31 = call i32 @addstr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%32 = call i32 (...) @refresh()
br label %7
33: ; preds = %18
%34 = load i32, i32* %5
%35 = load i32, i32* %4
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %7
39: ; preds = %33
%40 = load i8, i8* %6
%41 = call i32 @addch(i8 signext %40)
%42 = load i8, i8* %6
%43 = load i8*, i8** %3
store i8 %42, i8* %43
%44 = load i8*, i8** %3
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %3
%46 = load i32, i32* %5
%47 = add nsw i32 %46, 1
store i32 %47, i32* %5
%48 = call i32 (...) @refresh()
br label %7
49: ; preds = %12
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"\08 \08\00"
define void @readstring(i8* nocapture writeonly %0, i32 %1) {
br label %3
3: ; preds = %25, %2
%4 = phi i32 [ 0, %2 ], [ %26, %25 ]
%5 = phi i8* [ %0, %2 ], [ %27, %25 ]
br label %6
6: ; preds = %3, %12
%7 = phi i32 [ 0, %12 ], [ %4, %3 ]
%8 = icmp eq i32 %7, %1
br label %9
9: ; preds = %6, %19
%10 = tail call signext i8 (...) @getch()
switch i8 %10, label %18 [
i8 10, label %11
i8 127, label %12
i8 8, label %12
]
11: ; preds = %9
store i8 0, i8* %5
ret void
12: ; preds = %9, %9
%13 = icmp eq i32 %7, 0
br i1 %13, label %6, label %14
14: ; preds = %12
%15 = getelementptr inbounds i8, i8* %5, i64 -1
%16 = add nsw i32 %7, -1
%17 = tail call i32 @addstr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %25
18: ; preds = %9
br i1 %8, label %19, label %21
19: ; preds = %18
%20 = tail call i32 @putchar(i32 7)
br label %9
21: ; preds = %18
%22 = tail call i32 @addch(i8 signext %10)
store i8 %10, i8* %5
%23 = getelementptr inbounds i8, i8* %5, i64 1
%24 = add nsw i32 %7, 1
br label %25
25: ; preds = %21, %14
%26 = phi i32 [ %16, %14 ], [ %24, %21 ]
%27 = phi i8* [ %15, %14 ], [ %23, %21 ]
%28 = tail call i32 (...) @refresh()
br label %3
}