#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*/ putchar(char);
int /*<<< orphan*/ puts(char *);
char *gets(char *buffer) {
  char *ptr = buffer;
  do {
    char c = getc();
    if (c == '\n')
      break;
    *ptr = c;
    if (c == '\b') {
      if (ptr > buffer) {
        ptr--;
        puts("\b ");
      } else
        continue;
    } else
      ptr++;
    putchar(c);
  } while (1);
  putchar('\n');
  *ptr = 0;
  return buffer;
}


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 <getc@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    1160 <gets+0x30>
jmp    11cf <gets+0x9f>
mov    -0x11(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movsbl -0x11(%rbp),%eax
cmp    $0x8,%eax
jne    11ab <gets+0x7b>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    11a1 <gets+0x71>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
lea    0xe69(%rip),%rdi
call   1040 <puts@plt>
jmp    11a6 <gets+0x76>
jmp    11c0 <gets+0x90>
jmp    11b7 <gets+0x87>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
movsbl -0x11(%rbp),%edi
call   1030 <putchar@plt>
mov    $0x1,%al
test   $0x1,%al
jne    1144 <gets+0x14>
jmp    11cf <gets+0x9f>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
xor    %eax,%eax
call   1050 <getc@plt>
mov    %r15,%r12
cmp    $0xa,%al
jne    1167 <gets+0x37>
mov    $0xa,%edi
call   1030 <putchar@plt>
movb   $0x0,(%r12)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %eax,%ebx
lea    0xe90(%rip),%r14
mov    %r15,%r12
jmp    1199 <gets+0x69>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r12
movsbl %bl,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   1050 <getc@plt>
mov    %eax,%ebx
cmp    $0xa,%al
je     1149 <gets+0x19>
mov    %bl,(%r12)
cmp    $0x8,%bl
jne    1180 <gets+0x50>
cmp    %r15,%r12
jbe    118c <gets+0x5c>
add    $0xffffffffffffffff,%r12
mov    %r14,%rdi
call   1040 <puts@plt>
jmp    1184 <gets+0x54>

LLVM IR (-O0):
@.str = [3 x i8] c"\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** %3
br label %6
6: ; preds = %34, %1
%7 = call signext i8 (...) @getc()
store i8 %7, i8* %4
%8 = load i8, i8* %4
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, 10
br i1 %10, label %11, label %12
11: ; preds = %6
br label %35
12: ; preds = %6
%13 = load i8, i8* %4
%14 = load i8*, i8** %3
store i8 %13, i8* %14
%15 = load i8, i8* %4
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 8
br i1 %17, label %18, label %28
18: ; preds = %12
%19 = load i8*, i8** %3
%20 = load i8*, i8** %2
%21 = icmp ugt i8* %19, %20
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i32 -1
store i8* %24, i8** %3
%25 = call i32 @puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %27
26: ; preds = %18
br label %34
27: ; preds = %22
br label %31
28: ; preds = %12
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %3
br label %31
31: ; preds = %28, %27
%32 = load i8, i8* %4
%33 = call i32 @putchar(i8 signext %32)
br label %34
34: ; preds = %31, %26
br i1 true, label %6, label %35
35: ; preds = %34, %11
%36 = call i32 @putchar(i8 signext 10)
%37 = load i8*, i8** %3
store i8 0, i8* %37
%38 = load i8*, i8** %2
ret i8* %38
}

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