#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 asm_getchar();
char asm_getdisppage();
int /*<<< orphan*/ asm_getpos(char *, char *, char);
int /*<<< orphan*/ asm_gotoxy(char, char, char);
int /*<<< orphan*/ asm_putchar(char, int, char);
int /*<<< orphan*/ csprint(char *, int);
void getstring(char *str, unsigned int size) {
  char c;
  char *p = str;
  char page = asm_getdisppage();
  char row, col;
  while ((c = asm_getchar()) != '\r') {
    switch (c) {
    case '\0':
      asm_getchar();
      break;
    case '\b':
      if (p > str) {
        p--;
        csprint("\b \b", 0x07);
      }
      break;
    case '\x15':
      while (p > str) {
        p--;
        csprint("\b \b", 0x07);
      }
      break;
    default:
      if (c >= ' ' && (unsigned int)(p - str) < size - 1) {
        *p++ = c;
        asm_getpos(&row, &col, page);
        asm_putchar(c, 0x07, page);
        asm_gotoxy(row, col + 1, page);
      }
      break;
    }
  }
  *p = '\0';
  csprint("\r\n", 0x07);
}


x86 assembly (clang, -O0):
<getstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <asm_getdisppage@plt>
mov    %al,-0x19(%rbp)
mov    $0x0,%al
call   1080 <asm_getchar@plt>
mov    %al,-0xd(%rbp)
movsbl %al,%eax
cmp    $0xd,%eax
je     12c5 <getstring+0x165>
movsbl -0xd(%rbp),%eax
mov    %eax,-0x20(%rbp)
test   %eax,%eax
je     11cd <getstring+0x6d>
jmp    11ab <getstring+0x4b>
mov    -0x20(%rbp),%eax
sub    $0x8,%eax
je     11d9 <getstring+0x79>
jmp    11bc <getstring+0x5c>
mov    -0x20(%rbp),%eax
sub    $0x15,%eax
je     1209 <getstring+0xa9>
jmp    1243 <getstring+0xe3>
mov    $0x0,%al
call   1080 <asm_getchar@plt>
jmp    12c0 <getstring+0x160>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    1204 <getstring+0xa4>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
lea    0xe06(%rip),%rdi
mov    $0x7,%esi
call   1070 <csprint@plt>
jmp    12c0 <getstring+0x160>
jmp    120e <getstring+0xae>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    123e <getstring+0xde>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
lea    0xdd1(%rip),%rdi
mov    $0x7,%esi
call   1070 <csprint@plt>
jmp    120e <getstring+0xae>
jmp    12c0 <getstring+0x160>
movsbl -0xd(%rbp),%eax
cmp    $0x20,%eax
jl     12bb <getstring+0x15b>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jae    12bb <getstring+0x15b>
mov    -0xd(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
lea    -0x1a(%rbp),%rdi
lea    -0x1b(%rbp),%rsi
movsbl -0x19(%rbp),%edx
call   1050 <asm_getpos@plt>
mov    -0xd(%rbp),%al
mov    $0x7,%esi
movsbl %al,%edi
movsbl -0x19(%rbp),%edx
call   1060 <asm_putchar@plt>
mov    -0x1a(%rbp),%cl
movsbl -0x1b(%rbp),%eax
add    $0x1,%eax
movsbl %cl,%edi
movsbl %al,%esi
movsbl -0x19(%rbp),%edx
call   1030 <asm_gotoxy@plt>
jmp    12c0 <getstring+0x160>
jmp    1181 <getstring+0x21>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    0xd31(%rip),%rdi
mov    $0x7,%esi
call   1070 <csprint@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstring>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <asm_getdisppage@plt>
mov    %al,0x5(%rsp)
xor    %eax,%eax
call   1080 <asm_getchar@plt>
cmp    $0xd,%al
jne    11ad <getstring+0x4d>
mov    %rbx,%r14
movb   $0x0,(%r14)
lea    0xe70(%rip),%rdi
mov    $0x7,%esi
call   1070 <csprint@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0xffffffff,%ebp
lea    0xe49(%rip),%r15
mov    %rbx,%r14
jmp    11d2 <getstring+0x72>
nopl   0x0(%rax)
xor    %eax,%eax
call   1080 <asm_getchar@plt>
xor    %eax,%eax
call   1080 <asm_getchar@plt>
cmp    $0xd,%al
je     1189 <getstring+0x29>
movsbl %al,%r12d
test   %r12d,%r12d
je     11c0 <getstring+0x60>
cmp    $0x8,%r12d
je     1230 <getstring+0xd0>
cmp    $0x15,%r12d
jne    1250 <getstring+0xf0>
cmp    %rbx,%r14
jbe    11c7 <getstring+0x67>
mov    %ebp,%r13d
mov    %r14,%r12
neg    %r12
mov    %r14,%rbp
nopl   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%rbp
mov    %r15,%rdi
mov    $0x7,%esi
call   1070 <csprint@plt>
cmp    %rbx,%rbp
ja     1200 <getstring+0xa0>
add    %rbx,%r14
add    %r12,%r14
mov    %r13d,%ebp
jmp    11c7 <getstring+0x67>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %rbx,%r14
jbe    11c7 <getstring+0x67>
add    $0xffffffffffffffff,%r14
mov    %r15,%rdi
mov    $0x7,%esi
call   1070 <csprint@plt>
jmp    11c7 <getstring+0x67>
nopl   0x0(%rax,%rax,1)
cmp    $0x20,%al
jl     11c7 <getstring+0x67>
mov    %r14d,%ecx
sub    %ebx,%ecx
cmp    %ecx,%ebp
jbe    11c7 <getstring+0x67>
mov    %al,(%r14)
add    $0x1,%r14
movsbl 0x5(%rsp),%r13d
lea    0x7(%rsp),%rdi
lea    0x6(%rsp),%rsi
mov    %r13d,%edx
call   1050 <asm_getpos@plt>
mov    %r12d,%edi
mov    $0x7,%esi
mov    %r13d,%edx
call   1060 <asm_putchar@plt>
movsbl 0x7(%rsp),%edi
mov    0x6(%rsp),%al
add    $0x1,%al
movsbl %al,%esi
mov    %r13d,%edx
call   1030 <asm_gotoxy@plt>
jmp    11c7 <getstring+0x67>

LLVM IR (-O0):
@.str = [4 x i8] c"\08 \08\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define void @getstring(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i8*, i8** %3
store i8* %10, i8** %6
%11 = call signext i8 (...) @asm_getdisppage()
store i8 %11, i8* %7
br label %12
12: ; preds = %71, %2
%13 = call signext i8 (...) @asm_getchar()
store i8 %13, i8* %5
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 13
br i1 %15, label %16, label %72
16: ; preds = %12
%17 = load i8, i8* %5
%18 = sext i8 %17 to i32
switch i32 %18, label %40 [
i32 0, label %19
i32 8, label %21
i32 21, label %30
]
19: ; preds = %16
%20 = call signext i8 (...) @asm_getchar()
br label %71
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = load i8*, i8** %3
%24 = icmp ugt i8* %22, %23
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i8*, i8** %6
%27 = getelementptr inbounds i8, i8* %26, i32 -1
store i8* %27, i8** %6
%28 = call i32 @csprint(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 7)
br label %29
29: ; preds = %25, %21
br label %71
30: ; preds = %16
br label %31
31: ; preds = %35, %30
%32 = load i8*, i8** %6
%33 = load i8*, i8** %3
%34 = icmp ugt i8* %32, %33
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = load i8*, i8** %6
%37 = getelementptr inbounds i8, i8* %36, i32 -1
store i8* %37, i8** %6
%38 = call i32 @csprint(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 7)
br label %31
39: ; preds = %31
br label %71
40: ; preds = %16
%41 = load i8, i8* %5
%42 = sext i8 %41 to i32
%43 = icmp sge i32 %42, 32
br i1 %43, label %44, label %70
44: ; preds = %40
%45 = load i8*, i8** %6
%46 = load i8*, i8** %3
%47 = ptrtoint i8* %45 to i64
%48 = ptrtoint i8* %46 to i64
%49 = sub i64 %47, %48
%50 = trunc i64 %49 to i32
%51 = load i32, i32* %4
%52 = sub i32 %51, 1
%53 = icmp ult i32 %50, %52
br i1 %53, label %54, label %70
54: ; preds = %44
%55 = load i8, i8* %5
%56 = load i8*, i8** %6
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %6
store i8 %55, i8* %56
%58 = load i8, i8* %7
%59 = call i32 @asm_getpos(i8* %8, i8* %9, i8 signext %58)
%60 = load i8, i8* %5
%61 = load i8, i8* %7
%62 = call i32 @asm_putchar(i8 signext %60, i32 7, i8 signext %61)
%63 = load i8, i8* %8
%64 = load i8, i8* %9
%65 = sext i8 %64 to i32
%66 = add nsw i32 %65, 1
%67 = trunc i32 %66 to i8
%68 = load i8, i8* %7
%69 = call i32 @asm_gotoxy(i8 signext %63, i8 signext %67, i8 signext %68)
br label %70
70: ; preds = %54, %44, %40
br label %71
71: ; preds = %70, %39, %29, %19
br label %12
72: ; preds = %12
%73 = load i8*, i8** %6
store i8 0, i8* %73
%74 = call i32 @csprint(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 7)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"\08 \08\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define void @getstring(i8* %0, i32 %1) {
%3 = ptrtoint i8* %0 to i64
%4 = alloca i8
%5 = alloca i8
%6 = tail call signext i8 (...) @asm_getdisppage()
%7 = call signext i8 (...) @asm_getchar()
%8 = icmp eq i8 %7, 13
br i1 %8, label %53, label %9
9: ; preds = %2
%10 = add i32 %1, -1
br label %11
11: ; preds = %9, %49
%12 = phi i8 [ %7, %9 ], [ %51, %49 ]
%13 = phi i8* [ %0, %9 ], [ %50, %49 ]
%14 = ptrtoint i8* %13 to i64
%15 = sext i8 %12 to i32
switch i32 %15, label %32 [
i32 0, label %20
i32 8, label %22
i32 21, label %16
]
16: ; preds = %11
%17 = icmp ugt i8* %13, %0
br i1 %17, label %18, label %49
18: ; preds = %16
%19 = sub i64 0, %14
br label %27
20: ; preds = %11
%21 = call signext i8 (...) @asm_getchar()
br label %49
22: ; preds = %11
%23 = icmp ugt i8* %13, %0
br i1 %23, label %24, label %49
24: ; preds = %22
%25 = getelementptr inbounds i8, i8* %13, i64 -1
%26 = call i32 @csprint(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 7)
br label %49
27: ; preds = %18, %27
%28 = phi i8* [ %29, %27 ], [ %13, %18 ]
%29 = getelementptr inbounds i8, i8* %28, i64 -1
%30 = call i32 @csprint(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 7)
%31 = icmp ugt i8* %29, %0
br i1 %31, label %27, label %46
32: ; preds = %11
%33 = icmp sgt i8 %12, 31
%34 = sub i64 %14, %3
%35 = trunc i64 %34 to i32
%36 = icmp ugt i32 %10, %35
%37 = select i1 %33, i1 %36, i1 false
br i1 %37, label %38, label %49
38: ; preds = %32
%39 = getelementptr inbounds i8, i8* %13, i64 1
store i8 %12, i8* %13
%40 = call i32 @asm_getpos(i8* %4, i8* %5, i8 signext %6)
%41 = call i32 @asm_putchar(i8 signext %12, i32 7, i8 signext %6)
%42 = load i8, i8* %4
%43 = load i8, i8* %5
%44 = add i8 %43, 1
%45 = call i32 @asm_gotoxy(i8 signext %42, i8 signext %44, i8 signext %6)
br label %49
46: ; preds = %27
%47 = getelementptr i8, i8* %13, i64 %3
%48 = getelementptr i8, i8* %47, i64 %19
br label %49
49: ; preds = %46, %16, %32, %38, %22, %24, %20
%50 = phi i8* [ %39, %38 ], [ %13, %32 ], [ %25, %24 ], [ %13, %22 ], [ %13, %20 ], [ %13, %16 ], [ %48, %46 ]
%51 = call signext i8 (...) @asm_getchar()
%52 = icmp eq i8 %51, 13
br i1 %52, label %53, label %11
53: ; preds = %49, %2
%54 = phi i8* [ %0, %2 ], [ %50, %49 ]
store i8 0, i8* %54
%55 = call i32 @csprint(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 7)
ret void
}