#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 */
char fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ fputc(char, int);
int /*<<< orphan*/ printf(char *);
void readline(char *s, int maxlength) {
  int i = 0;
  while (1) {
    char c = fgetc(0);
    if (c == '\n') {
      fputc('\n', 1);
      s[i] = 0;
      return;
    } else if (c == '\b') {
      if (i == 0) {
        fputc(0x07, 1);
      } else {
        printf("\b \b");
        i--;
      }
    } else if (c < 0x20 || i + 1 == maxlength) {
      fputc(0x07, 1);
    } else {
      s[i++] = c;
      fputc(c, 1);
    }
  }
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
xor    %edi,%edi
call   1040 <fgetc@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    117e <readline+0x4e>
mov    $0xa,%edi
mov    $0x1,%esi
call   1050 <fputc@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret
movsbl -0x11(%rbp),%eax
cmp    $0x8,%eax
jne    11c3 <readline+0x93>
cmpl   $0x0,-0x10(%rbp)
jne    11a9 <readline+0x79>
mov    $0x7,%edi
mov    $0x1,%esi
call   1050 <fputc@plt>
jmp    11be <readline+0x8e>
lea    0xe50(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    121e <readline+0xee>
movsbl -0x11(%rbp),%eax
cmp    $0x20,%eax
jl     11df <readline+0xaf>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cmp    -0xc(%rbp),%eax
jne    11f3 <readline+0xc3>
mov    $0x7,%edi
mov    $0x1,%esi
call   1050 <fputc@plt>
jmp    1219 <readline+0xe9>
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    $0x1,%esi
movsbl -0x11(%rbp),%edi
call   1050 <fputc@plt>
jmp    121e <readline+0xee>
jmp    1223 <readline+0xf3>
jmp    1146 <readline+0x16>

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r15
xor    %r12d,%r12d
lea    0xeb9(%rip),%r14
lea    0x1(%r12),%ebx
jmp    115f <readline+0x2f>
xchg   %ax,%ax
mov    $0x7,%edi
mov    $0x1,%esi
call   1050 <fputc@plt>
xor    %edi,%edi
call   1040 <fgetc@plt>
cmp    $0xa,%al
je     11b7 <readline+0x87>
cmp    $0x8,%al
je     1190 <readline+0x60>
cmp    $0x20,%al
jl     1150 <readline+0x20>
cmp    %ebp,%ebx
je     1150 <readline+0x20>
movslq %r12d,%rcx
mov    %al,(%r15,%rcx,1)
movsbl %al,%edi
mov    $0x1,%esi
call   1050 <fputc@plt>
mov    %ebx,%r12d
jmp    1147 <readline+0x17>
nop
test   %r12d,%r12d
je     11a3 <readline+0x73>
mov    %r14,%rdi
call   1030 <printf@plt>
add    $0xffffffff,%r12d
jmp    1147 <readline+0x17>
mov    $0x7,%edi
mov    $0x1,%esi
call   1050 <fputc@plt>
xor    %r12d,%r12d
jmp    1147 <readline+0x17>
mov    $0xa,%edi
mov    $0x1,%esi
call   1050 <fputc@plt>
movslq %r12d,%rax
movb   $0x0,(%r15,%rax,1)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"\08 \08\00"
define void @readline(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 = %2, %54
%8 = call signext i8 @fgetc(i32 0)
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 %18
12: ; preds = %7
%13 = call i32 @fputc(i8 signext 10, i32 1)
%14 = load i8*, i8** %3
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8 0, i8* %17
ret void
18: ; preds = %7
%19 = load i8, i8* %6
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 8
br i1 %21, label %22, label %32
22: ; preds = %18
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @fputc(i8 signext 7, i32 1)
br label %31
27: ; preds = %22
%28 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%29 = load i32, i32* %5
%30 = add nsw i32 %29, -1
store i32 %30, i32* %5
br label %31
31: ; preds = %27, %25
br label %53
32: ; preds = %18
%33 = load i8, i8* %6
%34 = sext i8 %33 to i32
%35 = icmp slt i32 %34, 32
br i1 %35, label %41, label %36
36: ; preds = %32
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
%39 = load i32, i32* %4
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %36, %32
%42 = call i32 @fputc(i8 signext 7, i32 1)
br label %52
43: ; preds = %36
%44 = load i8, i8* %6
%45 = load i8*, i8** %3
%46 = load i32, i32* %5
%47 = add nsw i32 %46, 1
store i32 %47, i32* %5
%48 = sext i32 %46 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
store i8 %44, i8* %49
%50 = load i8, i8* %6
%51 = call i32 @fputc(i8 signext %50, i32 1)
br label %52
52: ; preds = %43, %41
br label %53
53: ; preds = %52, %31
br label %54
54: ; preds = %53
br label %7
}

LLVM IR (-O2):
@.str = [4 x i8] c"\08 \08\00"
define void @readline(i8* nocapture writeonly %0, i32 %1) {
br label %3
3: ; preds = %13, %2
%4 = phi i32 [ 0, %2 ], [ %14, %13 ]
%5 = add nsw i32 %4, 1
%6 = icmp eq i32 %5, %1
br label %7
7: ; preds = %3, %21
%8 = tail call signext i8 @fgetc(i32 0)
switch i8 %8, label %18 [
i8 10, label %27
i8 8, label %9
]
9: ; preds = %7
%10 = icmp eq i32 %4, 0
br i1 %10, label %11, label %15
11: ; preds = %9
%12 = tail call i32 @fputc(i8 signext 7, i32 1)
br label %13
13: ; preds = %11, %15, %23
%14 = phi i32 [ %5, %23 ], [ %17, %15 ], [ 0, %11 ]
br label %3
15: ; preds = %9
%16 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%17 = add nsw i32 %4, -1
br label %13
18: ; preds = %7
%19 = icmp slt i8 %8, 32
%20 = select i1 %19, i1 true, i1 %6
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @fputc(i8 signext 7, i32 1)
br label %7
23: ; preds = %18
%24 = sext i32 %4 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 %8, i8* %25
%26 = tail call i32 @fputc(i8 signext %8, i32 1)
br label %13
27: ; preds = %7
%28 = tail call i32 @fputc(i8 signext 10, i32 1)
%29 = sext i32 %4 to i64
%30 = getelementptr inbounds i8, i8* %0, i64 %29
store i8 0, i8* %30
ret void
}