#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 */
scalar_t__ poll_char(char *);
int /*<<< orphan*/ vga_printf(char *, ...);
int poll_string(char *string) {
  char *cur = string;
  while (*cur)
    cur++;
  char c;
  while (poll_char(&c)) {
    if (c == '\b') {
      if (cur != string) {
        cur--;
        vga_printf("\b \b");
      }
      *cur = 0;
    } else if (c == '\n') {
      vga_printf("\n");
      return 1;
    } else {
      *cur = c;
      cur++;
      *cur = 0;
      vga_printf("%c", c);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<poll_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1152 <poll_string+0x32>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1134 <poll_string+0x14>
jmp    1157 <poll_string+0x37>
lea    -0x19(%rbp),%rdi
call   1040 <poll_char@plt>
cmp    $0x0,%rax
je     120a <poll_string+0xea>
movsbl -0x19(%rbp),%eax
cmp    $0x8,%eax
jne    11ab <poll_string+0x8b>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     119f <poll_string+0x7f>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1030 <vga_printf@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    1205 <poll_string+0xe5>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    11d2 <poll_string+0xb2>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1030 <vga_printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1211 <poll_string+0xf1>
mov    -0x19(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movsbl -0x19(%rbp),%esi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1030 <vga_printf@plt>
jmp    1205 <poll_string+0xe5>
jmp    1157 <poll_string+0x37>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poll_string>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r13
lea    -0x1(%rdi),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    1140 <poll_string+0x20>
lea    0xf(%rsp),%rdi
call   1040 <poll_char@plt>
test   %rax,%rax
je     11cb <poll_string+0xab>
lea    0xea0(%rip),%r14
lea    0xf(%rsp),%r15
lea    0xe9a(%rip),%r12
jmp    1183 <poll_string+0x63>
xchg   %ax,%ax
movb   $0x0,(%rax)
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <poll_char@plt>
test   %rax,%rax
je     11cb <poll_string+0xab>
movzbl 0xf(%rsp),%eax
cmp    $0x8,%al
je     11b0 <poll_string+0x90>
cmp    $0xa,%al
je     11cf <poll_string+0xaf>
mov    %al,(%rbx)
movb   $0x0,0x1(%rbx)
add    $0x1,%rbx
movsbl 0xf(%rsp),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1030 <vga_printf@plt>
jmp    1176 <poll_string+0x56>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rax
cmp    %r13,%rbx
je     1170 <poll_string+0x50>
add    $0xffffffffffffffff,%rbx
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <vga_printf@plt>
mov    %rbx,%rax
jmp    1170 <poll_string+0x50>
xor    %eax,%eax
jmp    11e2 <poll_string+0xc2>
lea    0xe2e(%rip),%rdi
xor    %eax,%eax
call   1030 <vga_printf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"\08 \08\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [3 x i8] c"%c\00"
define i32 @poll_string(i8* %0) {
%2 = alloca i8
br label %3
3: ; preds = %3, %1
%4 = phi i8* [ %0, %1 ], [ %7, %3 ]
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 0
%7 = getelementptr inbounds i8, i8* %4, i64 1
br i1 %6, label %8, label %3
8: ; preds = %3
%9 = call i64 @poll_char(i8* %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %32, label %11
11: ; preds = %8, %28
%12 = phi i8* [ %29, %28 ], [ %4, %8 ]
%13 = load i8, i8* %2
switch i8 %13, label %23 [
i8 8, label %14
i8 10, label %21
]
14: ; preds = %11
%15 = icmp eq i8* %12, %0
br i1 %15, label %19, label %16
16: ; preds = %14
%17 = getelementptr inbounds i8, i8* %12, i64 -1
%18 = call i32 (i8*, ...) @vga_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %16, %14
%20 = phi i8* [ %17, %16 ], [ %0, %14 ]
store i8 0, i8* %20
br label %28
21: ; preds = %11
%22 = call i32 (i8*, ...) @vga_printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %32
23: ; preds = %11
store i8 %13, i8* %12
%24 = getelementptr inbounds i8, i8* %12, i64 1
store i8 0, i8* %24
%25 = load i8, i8* %2
%26 = sext i8 %25 to i32
%27 = call i32 (i8*, ...) @vga_printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %23, %19
%29 = phi i8* [ %20, %19 ], [ %24, %23 ]
%30 = call i64 @poll_char(i8* %2)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %11
32: ; preds = %28, %8, %21
%33 = phi i32 [ 1, %21 ], [ 0, %8 ], [ 0, %28 ]
ret i32 %33
}