#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 getc();
int /*<<< orphan*/ putc(char);
char *gets(char *buff) {
  char c;
  int counter = 0;
  c = ' ';
  while (c != '\n') {
    c = getc();
    if (c == 0x08) {
      if (counter < 1)
        continue;
      buff--;
      counter--;
    } else {
      *(buff++) = c;
      counter++;
    }
    putc(c);
  }
  *(buff++) = '\0';
  return buff;
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movb   $0x20,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0xa,%eax
je     11af <gets+0x8f>
mov    $0x0,%al
call   1040 <getc@plt>
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0x8,%eax
jne    1184 <gets+0x64>
cmpl   $0x1,-0x10(%rbp)
jge    116a <gets+0x4a>
jmp    1137 <gets+0x17>
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    11a1 <gets+0x81>
mov    -0x9(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
movsbl -0x9(%rbp),%edi
call   1030 <putc@plt>
jmp    1137 <gets+0x17>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x20,%bl
xor    %ebp,%ebp
mov    %ebx,%eax
nopl   (%rax)
cmp    $0xa,%al
je     1178 <gets+0x58>
xor    %eax,%eax
call   1040 <getc@plt>
mov    %eax,%ebx
cmp    $0x8,%al
jne    1160 <gets+0x40>
mov    $0x8,%al
test   %ebp,%ebp
jle    1130 <gets+0x10>
add    $0xffffffffffffffff,%r14
mov    $0xffffffff,%eax
jmp    116c <gets+0x4c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %bl,(%r14)
add    $0x1,%r14
mov    $0x1,%eax
add    %eax,%ebp
movsbl %bl,%edi
call   1030 <putc@plt>
jmp    112b <gets+0xb>
movb   $0x0,(%r14)
add    $0x1,%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @gets(i8* writeonly %0) {
br label %2
2: ; preds = %18, %1
%3 = phi i8* [ %20, %18 ], [ %0, %1 ]
%4 = phi i8 [ %11, %18 ], [ 32, %1 ]
%5 = phi i32 [ %21, %18 ], [ 0, %1 ]
%6 = icmp slt i32 %5, 1
br label %7
7: ; preds = %2, %13
%8 = phi i8 [ 8, %13 ], [ %4, %2 ]
%9 = icmp eq i8 %8, 10
br i1 %9, label %23, label %10
10: ; preds = %7
%11 = tail call signext i8 (...) @getc()
%12 = icmp eq i8 %11, 8
br i1 %12, label %13, label %16
13: ; preds = %10
br i1 %6, label %7, label %14
14: ; preds = %13
%15 = getelementptr inbounds i8, i8* %3, i64 -1
br label %18
16: ; preds = %10
%17 = getelementptr inbounds i8, i8* %3, i64 1
store i8 %11, i8* %3
br label %18
18: ; preds = %16, %14
%19 = phi i32 [ 1, %16 ], [ -1, %14 ]
%20 = phi i8* [ %17, %16 ], [ %15, %14 ]
%21 = add nsw i32 %5, %19
%22 = tail call i32 @putc(i8 signext %11)
br label %2
23: ; preds = %7
%24 = getelementptr inbounds i8, i8* %3, i64 1
store i8 0, i8* %3
ret i8* %24
}