#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 mygetch();
int /*<<< orphan*/ printf(char *, ...);
int main() {
  char a[20];
  int x = 2;
  int i = 0;
  do {
    a[i] = mygetch();
    if (127 == a[i])
      x = 1;
    if (10 == a[i])
      break;
    switch (x) {
    case 1:
      printf("\b \b");
      i--;
      x = 2;
      break;
    case 2:
      printf("*");
      i++;
      break;
    }
  } while (20 != i);
  a[i] = '\0';
  printf("%s", a);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x2,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1030 <mygetch@plt>
mov    %al,%cl
movslq -0x28(%rbp),%rax
mov    %cl,-0x20(%rbp,%rax,1)
movslq -0x28(%rbp),%rax
movsbl -0x20(%rbp,%rax,1),%ecx
mov    $0x7f,%eax
cmp    %ecx,%eax
jne    116b <main+0x4b>
movl   $0x1,-0x24(%rbp)
movslq -0x28(%rbp),%rax
movsbl -0x20(%rbp,%rax,1),%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
jne    1186 <main+0x66>
jmp    11f8 <main+0xd8>
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x1,%eax
je     11ab <main+0x8b>
jmp    119a <main+0x7a>
mov    -0x2c(%rbp),%eax
sub    $0x2,%eax
je     11ce <main+0xae>
jmp    11e5 <main+0xc5>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
movl   $0x2,-0x24(%rbp)
jmp    11e5 <main+0xc5>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11ea <main+0xca>
mov    $0x14,%eax
cmp    -0x28(%rbp),%eax
jne    113d <main+0x1d>
movslq -0x28(%rbp),%rax
movb   $0x0,-0x20(%rbp,%rax,1)
lea    -0x20(%rbp),%rsi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0x2,%ebx
mov    $0x1,%r15d
lea    0xeb4(%rip),%r14
xor    %ebp,%ebp
jmp    1167 <main+0x37>
mov    $0x2a,%edi
call   1030 <putchar@plt>
add    $0x1,%ebp
mov    $0x2,%ebx
cmp    $0x14,%ebp
je     1197 <main+0x67>
xor    %eax,%eax
call   1040 <mygetch@plt>
movslq %ebp,%rcx
mov    %al,(%rsp,%rcx,1)
cmp    $0x7f,%al
cmove  %r15d,%ebx
cmp    $0xa,%al
je     119c <main+0x6c>
cmp    $0x2,%ebx
je     1150 <main+0x20>
cmp    $0x1,%ebx
jne    1162 <main+0x32>
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffff,%ebp
jmp    115d <main+0x2d>
mov    $0x14,%ebp
movslq %ebp,%rax
movb   $0x0,(%rsp,%rax,1)
lea    0xe5a(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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