#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 */
int getch();
int /*<<< orphan*/ putc(int);
int /*<<< orphan*/ puts(char *);
void gets(char *buf) {
  char *t;
  int i;
  t = buf;
  while (1) {
    i = getch();
    if ((i == '\b') || (i == 127)) {
      if (t > buf) {
        puts("\b \b");
        t--;
      }
      *t = 0;
      continue;
    }
    if (i == '\r') {
      putc('\n');
      break;
    }
    if (i == '\n') {
      putc('\n');
      break;
    }
    putc(i);
    *t++ = i;
  }
  *t = 0;
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getch@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x8,-0x14(%rbp)
je     1162 <gets+0x32>
cmpl   $0x7f,-0x14(%rbp)
jne    1194 <gets+0x64>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    1188 <gets+0x58>
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    1144 <gets+0x14>
cmpl   $0xd,-0x14(%rbp)
jne    11ad <gets+0x7d>
mov    $0xa,%edi
call   1040 <putc@plt>
jmp    11e9 <gets+0xb9>
cmpl   $0xa,-0x14(%rbp)
jne    11c6 <gets+0x96>
mov    $0xa,%edi
call   1040 <putc@plt>
jmp    11e9 <gets+0xb9>
mov    -0x14(%rbp),%edi
call   1040 <putc@plt>
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    1144 <gets+0x14>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
lea    0xebf(%rip),%r14
mov    %rdi,%rbx
jmp    1153 <gets+0x23>
cs nopw 0x0(%rax,%rax,1)
movb   $0x0,(%rbx)
xor    %eax,%eax
call   1050 <getch@plt>
mov    %eax,%ebp
cmp    $0xc,%eax
jg     1170 <gets+0x40>
cmp    $0x8,%ebp
je     1175 <gets+0x45>
cmp    $0xa,%ebp
jne    118d <gets+0x5d>
jmp    119d <gets+0x6d>
nopl   (%rax)
cmp    $0x7f,%ebp
jne    1188 <gets+0x58>
cmp    %r15,%rbx
jbe    1150 <gets+0x20>
mov    %r14,%rdi
call   1030 <puts@plt>
add    $0xffffffffffffffff,%rbx
jmp    1150 <gets+0x20>
cmp    $0xd,%ebp
je     119d <gets+0x6d>
mov    %ebp,%edi
call   1040 <putc@plt>
mov    %bpl,(%rbx)
add    $0x1,%rbx
jmp    1153 <gets+0x23>
mov    $0xa,%edi
call   1040 <putc@plt>
movb   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"\08 \08\00"
define void @gets(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
store i8* %5, i8** %3
br label %6
6: ; preds = %1, %21, %33
%7 = call i32 (...) @getch()
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 8
br i1 %9, label %13, label %10
10: ; preds = %6
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 127
br i1 %12, label %13, label %23
13: ; preds = %10, %6
%14 = load i8*, i8** %3
%15 = load i8*, i8** %2
%16 = icmp ugt i8* %14, %15
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = call i32 @puts(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i32 -1
store i8* %20, i8** %3
br label %21
21: ; preds = %17, %13
%22 = load i8*, i8** %3
store i8 0, i8* %22
br label %6
23: ; preds = %10
%24 = load i32, i32* %4
%25 = icmp eq i32 %24, 13
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @putc(i32 10)
br label %40
28: ; preds = %23
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 10
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @putc(i32 10)
br label %40
33: ; preds = %28
%34 = load i32, i32* %4
%35 = call i32 @putc(i32 %34)
%36 = load i32, i32* %4
%37 = trunc i32 %36 to i8
%38 = load i8*, i8** %3
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %3
store i8 %37, i8* %38
br label %6
40: ; preds = %31, %26
%41 = load i8*, i8** %3
store i8 0, i8* %41
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"\08 \08\00"
define void @gets(i8* writeonly %0) {
br label %2
2: ; preds = %12, %1
%3 = phi i8* [ %0, %1 ], [ %13, %12 ]
%4 = tail call i32 (...) @getch()
switch i32 %4, label %14 [
i32 127, label %5
i32 8, label %5
i32 13, label %18
i32 10, label %18
]
5: ; preds = %2, %2
%6 = icmp ugt i8* %3, %0
br i1 %6, label %7, label %10
7: ; preds = %5
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds i8, i8* %3, i64 -1
br label %10
10: ; preds = %7, %5
%11 = phi i8* [ %9, %7 ], [ %3, %5 ]
store i8 0, i8* %11
br label %12
12: ; preds = %10, %14
%13 = phi i8* [ %11, %10 ], [ %17, %14 ]
br label %2
14: ; preds = %2
%15 = tail call i32 @putc(i32 %4)
%16 = trunc i32 %4 to i8
%17 = getelementptr inbounds i8, i8* %3, i64 1
store i8 %16, i8* %3
br label %12
18: ; preds = %2, %2
%19 = tail call i32 @putc(i32 10)
store i8 0, i8* %3
ret void
}