#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__ getchar();
char *gets(char *str) {
  char const *startstr = str;
  while ((*str = (char)getchar()) != '\n') {
    if (*str == '\b') {
      if (str != startstr) {
        --str;
      }
    } else {
      ++str;
    }
  }
  *str = 0;
  return str;
}


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   1030 <getchar@plt>
mov    -0x8(%rbp),%rcx
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0xa,%eax
je     117d <gets+0x6d>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x8,%eax
jne    116c <gets+0x5c>
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1167 <gets+0x57>
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1178 <gets+0x68>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1124 <gets+0x14>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
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
jmp    112e <gets+0x1e>
nopl   0x0(%rax)
xor    %ecx,%ecx
cmp    %r14,%rbx
setne  %cl
neg    %rcx
add    %rcx,%rbx
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %al,(%rbx)
cmp    $0x8,%al
je     1120 <gets+0x10>
mov    $0x1,%ecx
cmp    $0xa,%al
jne    112b <gets+0x1b>
movb   $0x0,(%rbx)
mov    %rbx,%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 = %27, %1
%6 = call i64 (...) @getchar()
%7 = trunc i64 %6 to i8
%8 = load i8*, i8** %2
store i8 %7, i8* %8
%9 = sext i8 %7 to i32
%10 = icmp ne i32 %9, 10
br i1 %10, label %11, label %28
11: ; preds = %5
%12 = load i8*, i8** %2
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 8
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = load i8*, i8** %2
%18 = load i8*, i8** %3
%19 = icmp ne i8* %17, %18
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i8*, i8** %2
%22 = getelementptr inbounds i8, i8* %21, i32 -1
store i8* %22, i8** %2
br label %23
23: ; preds = %20, %16
br label %27
24: ; preds = %11
%25 = load i8*, i8** %2
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %2
br label %27
27: ; preds = %24, %23
br label %5
28: ; preds = %5
%29 = load i8*, i8** %2
store i8 0, i8* %29
%30 = load i8*, i8** %2
ret i8* %30
}

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