#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();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
char *gets(char *pszBuf) {
  char c;
  char *pc = pszBuf;
  while (1) {
    c = getch();
    if (c == '\r') {
      printf("\n");
      break;
    }
    if ((c == '\x08') && (pc > pszBuf)) {
      printf("\x08 \x08");
      pc -= 1;
      continue;
    }
    putchar(c);
    *pc = c;
    pc += 1;
  }
  *pc = '\0';
  return pszBuf;
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getch@plt>
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0xd,%eax
jne    116c <gets+0x3c>
lea    0xe9e(%rip),%rdi
call   1040 <printf@plt>
jmp    11c7 <gets+0x97>
movsbl -0x9(%rbp),%eax
cmp    $0x8,%eax
jne    11a4 <gets+0x74>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    11a4 <gets+0x74>
lea    0xe74(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1144 <gets+0x14>
movsbl -0x9(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x9(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1144 <gets+0x14>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
lea    0xebb(%rip),%r14
mov    $0xffffffffffffffff,%r13
mov    $0x1,%ebp
mov    %rdi,%r12
jmp    1174 <gets+0x44>
cs nopw 0x0(%rax,%rax,1)
cmp    $0xd,%bl
je     1193 <gets+0x63>
movsbl %bl,%edi
call   1030 <putchar@plt>
mov    %bl,(%r12)
add    %rbp,%r12
xor    %eax,%eax
call   1050 <getch@plt>
mov    %eax,%ebx
cmp    $0x8,%al
jne    1160 <gets+0x30>
cmp    %r15,%r12
jbe    1165 <gets+0x35>
mov    %r14,%rdi
call   1040 <printf@plt>
add    %r13,%r12
jmp    1174 <gets+0x44>
mov    $0xa,%edi
call   1030 <putchar@plt>
movb   $0x0,(%r12)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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