void readstring(str, size) 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    1235 <readstring+0xe5>
movsbl -0x11(%rbp),%eax
cmp    $0x8,%eax
je     11a3 <readstring+0x53>
movsbl -0x11(%rbp),%eax
cmp    $0x7f,%eax
jne    11e1 <readstring+0x91>
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
mov    $0x0,%al
call   1070 <addstr@plt>
mov    $0x0,%al
call   1050 <refresh@plt>
jmp    1166 <readstring+0x16>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    1200 <readstring+0xb0>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1166 <readstring+0x16>
movsbl -0x11(%rbp),%edi
mov    $0x0,%al
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    11e1 <readstring+0x91>
mov    %ebx,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <getch@plt>
mov    %eax,%edi
shl    $0x18,%edi
cmp    $0x8000000,%edi
je     1170 <readstring+0x20>
cmp    $0x7f000000,%edi
je     1170 <readstring+0x20>
cmp    $0xa000000,%edi
je     11f4 <readstring+0xa4>
cmp    %r12d,%ebp
jne    11c0 <readstring+0x70>
mov    $0x7,%edi
call   1030 <putchar@plt>
jmp    1180 <readstring+0x30>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%ebx
sar    $0x18,%edi
xor    %eax,%eax
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
xor    %eax,%eax
call   1070 <addstr@plt>
jmp    11d6 <readstring+0x86>
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 = %40, %38, %27, %26, %2
%8 = call i32 (...) @getch()
%9 = trunc i32 %8 to i8
store i8 %9, i8* %6
%10 = load i8, i8* %6
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 10
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = load i8*, i8** %3
store i8 0, i8* %14
br label %51
15: ; preds = %7
%16 = load i8, i8* %6
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 8
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i8, i8* %6
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 127
br i1 %22, label %23, label %34
23: ; preds = %19, %15
%24 = load i32, i32* %5
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
br label %7
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i32 -1
store i8* %29, i8** %3
%30 = load i32, i32* %5
%31 = add nsw i32 %30, -1
store i32 %31, i32* %5
%32 = call i32 (i8*, ...) bitcast (i32 (...)* @addstr to i32 (i8*, ...)*)(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%33 = call i32 (...) @refresh()
br label %7
34: ; preds = %19
%35 = load i32, i32* %5
%36 = load i32, i32* %4
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %7
40: ; preds = %34
%41 = load i8, i8* %6
%42 = sext i8 %41 to i32
%43 = call i32 (i32, ...) bitcast (i32 (...)* @addch to i32 (i32, ...)*)(i32 %42)
%44 = load i8, i8* %6
%45 = load i8*, i8** %3
store i8 %44, i8* %45
%46 = load i8*, i8** %3
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %3
%48 = load i32, i32* %5
%49 = add nsw i32 %48, 1
store i32 %49, i32* %5
%50 = call i32 (...) @refresh()
br label %7
51: ; preds = %13
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 = %28, %2
%4 = phi i32 [ 0, %2 ], [ %29, %28 ]
%5 = phi i8* [ %0, %2 ], [ %30, %28 ]
br label %6
6: ; preds = %3, %13
%7 = phi i32 [ 0, %13 ], [ %4, %3 ]
%8 = icmp eq i32 %7, %1
br label %9
9: ; preds = %6, %20
%10 = tail call i32 (...) @getch()
%11 = shl i32 %10, 24
switch i32 %11, label %19 [
i32 167772160, label %12
i32 2130706432, label %13
i32 134217728, label %13
]
12: ; preds = %9
store i8 0, i8* %5
ret void
13: ; preds = %9, %9
%14 = icmp eq i32 %7, 0
br i1 %14, label %6, label %15
15: ; preds = %13
%16 = getelementptr inbounds i8, i8* %5, i64 -1
%17 = add nsw i32 %7, -1
%18 = tail call i32 (i8*, ...) bitcast (i32 (...)* @addstr to i32 (i8*, ...)*)(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %28
19: ; preds = %9
br i1 %8, label %20, label %22
20: ; preds = %19
%21 = tail call i32 @putchar(i32 7)
br label %9
22: ; preds = %19
%23 = trunc i32 %10 to i8
%24 = ashr exact i32 %11, 24
%25 = tail call i32 (i32, ...) bitcast (i32 (...)* @addch to i32 (i32, ...)*)(i32 %24)
store i8 %23, i8* %5
%26 = getelementptr inbounds i8, i8* %5, i64 1
%27 = add nsw i32 %7, 1
br label %28
28: ; preds = %22, %15
%29 = phi i32 [ %17, %15 ], [ %27, %22 ]
%30 = phi i8* [ %16, %15 ], [ %26, %22 ]
%31 = tail call i32 (...) @refresh()
br label %3
}