#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 console_getc();
int /*<<< orphan*/ console_puts(char *);
int /*<<< orphan*/ screen_putch(char);
int console_gets(char *s) {
  int cnt = 0;
  char c;
  while ((c = console_getc()) != 0x0d) {
    if (c != 0x08) {
      cnt++;
      *s++ = c;
      screen_putch(c);
    } else {
      if (cnt > 0) {
        cnt--;
        *--s = '\0';
        console_puts("\b \b");
      }
    }
  }
  *s = 0;
  return cnt;
}


x86 assembly (clang, -O0):
<console_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1040 <console_getc@plt>
mov    %al,-0xd(%rbp)
movsbl %al,%eax
cmp    $0xd,%eax
je     11cd <console_gets+0x9d>
movsbl -0xd(%rbp),%eax
cmp    $0x8,%eax
je     1191 <console_gets+0x61>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xd(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
movsbl -0xd(%rbp),%edi
call   1050 <screen_putch@plt>
jmp    11c8 <console_gets+0x98>
cmpl   $0x0,-0xc(%rbp)
jle    11c3 <console_gets+0x93>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,-0x1(%rax)
lea    0xe42(%rip),%rdi
call   1030 <console_puts@plt>
jmp    11c8 <console_gets+0x98>
jmp    1143 <console_gets+0x13>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<console_gets>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xec2(%rip),%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <console_getc@plt>
cmp    $0x8,%al
jne    1164 <console_gets+0x34>
test   %ebp,%ebp
jle    1140 <console_gets+0x10>
add    $0xffffffff,%ebp
movb   $0x0,-0x1(%rbx)
add    $0xffffffffffffffff,%rbx
mov    %r14,%rdi
call   1030 <console_puts@plt>
jmp    1140 <console_gets+0x10>
cmp    $0xd,%al
je     117b <console_gets+0x4b>
add    $0x1,%ebp
mov    %al,(%rbx)
add    $0x1,%rbx
movsbl %al,%edi
call   1050 <screen_putch@plt>
jmp    1140 <console_gets+0x10>
movb   $0x0,(%rbx)
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"\08 \08\00"
define i32 @console_gets(i8* nocapture writeonly %0) {
br label %2
2: ; preds = %12, %1
%3 = phi i32 [ 0, %1 ], [ %13, %12 ]
%4 = phi i8* [ %0, %1 ], [ %14, %12 ]
%5 = icmp sgt i32 %3, 0
br label %6
6: ; preds = %2, %15
%7 = tail call signext i8 (...) @console_getc()
switch i8 %7, label %8 [
i8 13, label %20
i8 8, label %15
]
8: ; preds = %6
%9 = add nsw i32 %3, 1
%10 = getelementptr inbounds i8, i8* %4, i64 1
store i8 %7, i8* %4
%11 = tail call i32 @screen_putch(i8 signext %7)
br label %12
12: ; preds = %8, %16
%13 = phi i32 [ %17, %16 ], [ %9, %8 ]
%14 = phi i8* [ %18, %16 ], [ %10, %8 ]
br label %2
15: ; preds = %6
br i1 %5, label %16, label %6
16: ; preds = %15
%17 = add nsw i32 %3, -1
%18 = getelementptr inbounds i8, i8* %4, i64 -1
store i8 0, i8* %18
%19 = tail call i32 @console_puts(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %12
20: ; preds = %6
store i8 0, i8* %4
ret i32 %3
}