#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*/ printf(char *);
int /*<<< orphan*/ putc(char);
char *gets(char *buf) {
  int i = 0, done = 0;
  char c;
  while (!done) {
    c = getc();
    switch (c) {
    case ('\r'):
      printf("\r\n");
      *(buf + i) = '\0';
      done = 1;
      break;
    case ('\b'):
      if (i > 0) {
        printf("\b \b");
        *(buf + i) = '\0';
        i--;
      }
      i--;
      break;
    default:
      putc(c);
      *(buf + i) = c;
      break;
    }
    i++;
  }
  return (buf);
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1160 <gets+0x30>
jmp    1212 <gets+0xe2>
xor    %eax,%eax
call   1050 <getc@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0x8,%eax
je     11b4 <gets+0x84>
jmp    117f <gets+0x4f>
mov    -0x18(%rbp),%eax
sub    $0xd,%eax
jne    11ed <gets+0xbd>
jmp    1190 <gets+0x60>
lea    0xe69(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x10(%rbp)
jmp    1204 <gets+0xd4>
cmpl   $0x0,-0xc(%rbp)
jle    11df <gets+0xaf>
lea    0xe3e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1204 <gets+0xd4>
movsbl -0x11(%rbp),%edi
call   1040 <putc@plt>
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    114a <gets+0x1a>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
lea    0xeae(%rip),%r14
xor    %ebx,%ebx
jmp    1166 <gets+0x26>
cs nopw 0x0(%rax,%rax,1)
add    $0xffffffff,%ebx
add    $0x1,%ebx
xor    %eax,%eax
call   1060 <getc@plt>
movsbl %al,%ebp
cmp    $0x8,%ebp
je     1190 <gets+0x50>
movslq %ebx,%r12
cmp    $0xd,%ebp
je     11a8 <gets+0x68>
mov    %ebp,%edi
call   1050 <putc@plt>
mov    %bpl,(%r15,%r12,1)
add    $0x1,%ebx
jmp    1166 <gets+0x26>
nopl   (%rax)
test   %ebx,%ebx
jle    1160 <gets+0x20>
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
add    $0xffffffff,%ebx
jmp    1160 <gets+0x20>
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
movb   $0x0,(%r15,%r12,1)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\0D\0A\00"
@.str.1 = [4 x i8] c"\08 \08\00"
define i8* @gets(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
br label %6
6: ; preds = %42, %1
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 0
%9 = xor i1 %8, true
br i1 %9, label %10, label %45
10: ; preds = %6
%11 = call signext i8 (...) @getc()
store i8 %11, i8* %5
%12 = load i8, i8* %5
%13 = sext i8 %12 to i32
switch i32 %13, label %34 [
i32 13, label %14
i32 8, label %20
]
14: ; preds = %10
%15 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%16 = load i8*, i8** %2
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
store i8 0, i8* %19
store i32 1, i32* %4
br label %42
20: ; preds = %10
%21 = load i32, i32* %3
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%25 = load i8*, i8** %2
%26 = load i32, i32* %3
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i32, i32* %3
%30 = add nsw i32 %29, -1
store i32 %30, i32* %3
br label %31
31: ; preds = %23, %20
%32 = load i32, i32* %3
%33 = add nsw i32 %32, -1
store i32 %33, i32* %3
br label %42
34: ; preds = %10
%35 = load i8, i8* %5
%36 = call i32 @putc(i8 signext %35)
%37 = load i8, i8* %5
%38 = load i8*, i8** %2
%39 = load i32, i32* %3
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 %37, i8* %41
br label %42
42: ; preds = %34, %31, %14
%43 = load i32, i32* %3
%44 = add nsw i32 %43, 1
store i32 %44, i32* %3
br label %6
45: ; preds = %6
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"\08 \08\00"
@str = [2 x i8] c"\0D\00"
define i8* @gets(i8* returned writeonly %0) {
br label %2
2: ; preds = %20, %1
%3 = phi i32 [ 0, %1 ], [ %22, %20 ]
%4 = tail call signext i8 (...) @getc()
%5 = sext i8 %4 to i32
switch i32 %5, label %16 [
i32 13, label %23
i32 8, label %6
]
6: ; preds = %2
%7 = icmp sgt i32 %3, 0
br i1 %7, label %8, label %13
8: ; preds = %6
%9 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%10 = zext i32 %3 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %11
%12 = add nsw i32 %3, -1
br label %13
13: ; preds = %8, %6
%14 = phi i32 [ %12, %8 ], [ %3, %6 ]
%15 = add nsw i32 %14, -1
br label %20
16: ; preds = %2
%17 = tail call i32 @putc(i8 signext %4)
%18 = sext i32 %3 to i64
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 %4, i8* %19
br label %20
20: ; preds = %16, %13
%21 = phi i32 [ %3, %16 ], [ %15, %13 ]
%22 = add nsw i32 %21, 1
br label %2
23: ; preds = %2
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%25 = sext i32 %3 to i64
%26 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 0, i8* %26
ret i8* %0
}