#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 getch();
void gets(char *str) {
  char b = getch();
  while (b != '\n') {
    if (b == 0x08)
      str--;
    else if (b > 0)
      *str++ = b;
    b = getch();
  }
  *str = '\0';
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getch@plt>
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0xa,%eax
je     1186 <gets+0x76>
movsbl -0x9(%rbp),%eax
cmp    $0x8,%eax
jne    1151 <gets+0x41>
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1177 <gets+0x67>
movsbl -0x9(%rbp),%eax
cmp    $0x0,%eax
jle    1172 <gets+0x62>
mov    -0x9(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
jmp    1177 <gets+0x67>
mov    $0x0,%al
call   1030 <getch@plt>
mov    %al,-0x9(%rbp)
jmp    1126 <gets+0x16>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %rbx
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1030 <getch@plt>
cmp    $0xa,%al
je     1141 <gets+0x31>
cmp    $0x8,%al
je     113b <gets+0x2b>
test   %al,%al
jle    1120 <gets+0x10>
mov    %al,(%rbx)
add    $0x1,%rbx
jmp    1120 <gets+0x10>
add    $0xffffffffffffffff,%rbx
jmp    1120 <gets+0x10>
movb   $0x0,(%rbx)
pop    %rbx
ret

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

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