#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 getchar();
int /*<<< orphan*/ putchar(char);
char *gets(char *s) {
  char *p = s;
  while ((*p = getchar()) != '\n') {
    if (*p != '\b')
      putchar(*p++);
    else if (p > s)
      putchar(*p--);
  }
  *p = '\0';
  putchar('\n');
  return s;
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    -0x10(%rbp),%rcx
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0xa,%eax
je     11a8 <gets+0x88>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x8,%eax
je     1179 <gets+0x59>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%edi
call   1030 <putchar@plt>
jmp    11a3 <gets+0x83>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    119e <gets+0x7e>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%edi
call   1030 <putchar@plt>
jmp    11a3 <gets+0x83>
jmp    1134 <gets+0x14>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rdi,%rbx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %al,(%rbx)
cmp    $0x8,%al
jne    114d <gets+0x2d>
cmp    %r14,%rbx
jbe    1130 <gets+0x10>
mov    $0x8,%al
mov    $0xffffffffffffffff,%rcx
jmp    1156 <gets+0x36>
cmp    $0xa,%al
je     1163 <gets+0x43>
mov    $0x1,%ecx
add    %rcx,%rbx
movsbl %al,%edi
call   1030 <putchar@plt>
jmp    1130 <gets+0x10>
movb   $0x0,(%rbx)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @gets(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
store i8* %4, i8** %3
br label %5
5: ; preds = %30, %1
%6 = call signext i8 (...) @getchar()
%7 = load i8*, i8** %3
store i8 %6, i8* %7
%8 = sext i8 %6 to i32
%9 = icmp ne i32 %8, 10
br i1 %9, label %10, label %31
10: ; preds = %5
%11 = load i8*, i8** %3
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 8
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %3
%18 = load i8, i8* %16
%19 = call i32 @putchar(i8 signext %18)
br label %30
20: ; preds = %10
%21 = load i8*, i8** %3
%22 = load i8*, i8** %2
%23 = icmp ugt i8* %21, %22
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i32 -1
store i8* %26, i8** %3
%27 = load i8, i8* %25
%28 = call i32 @putchar(i8 signext %27)
br label %29
29: ; preds = %24, %20
br label %30
30: ; preds = %29, %15
br label %5
31: ; preds = %5
%32 = load i8*, i8** %3
store i8 0, i8* %32
%33 = call i32 @putchar(i8 signext 10)
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
define i8* @gets(i8* returned writeonly %0) {
br label %2
2: ; preds = %8, %1
%3 = phi i8* [ %11, %8 ], [ %0, %1 ]
%4 = icmp ugt i8* %3, %0
br label %5
5: ; preds = %2, %7
%6 = tail call signext i8 (...) @getchar()
store i8 %6, i8* %3
switch i8 %6, label %8 [
i8 10, label %13
i8 8, label %7
]
7: ; preds = %5
br i1 %4, label %8, label %5
8: ; preds = %7, %5
%9 = phi i64 [ 1, %5 ], [ -1, %7 ]
%10 = phi i8 [ %6, %5 ], [ 8, %7 ]
%11 = getelementptr inbounds i8, i8* %3, i64 %9
%12 = tail call i32 @putchar(i8 signext %10)
br label %2
13: ; preds = %5
store i8 0, i8* %3
%14 = tail call i32 @putchar(i8 signext 10)
ret i8* %0
}