#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 getchar();
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ putstr(char *);
char *safe_gets(char *origBuf, int count) {
  char *buf = origBuf;
  while (--count > 0) {
    int ch = getchar();
    if (ch == 8 || ch == 127) {
      if (buf > origBuf) {
        putstr("\010 \010");
        count += 1;
        buf--;
      }
      count += 1;
      continue;
    }
    putchar(ch);
    if (ch == '\r')
      putchar('\n');
    if (ch == '\r' || ch == '\n')
      break;
    *buf++ = ch;
  }
  *buf = 0;
  return (origBuf);
}


x86 assembly (clang, -O0):
<safe_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    1207 <safe_gets+0xd7>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x8,-0x1c(%rbp)
je     1177 <safe_gets+0x47>
cmpl   $0x7f,-0x1c(%rbp)
jne    11b4 <safe_gets+0x84>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    11a6 <safe_gets+0x76>
lea    0xe74(%rip),%rdi
call   1050 <putstr@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1147 <safe_gets+0x17>
mov    -0x1c(%rbp),%eax
movsbl %al,%edi
call   1030 <putchar@plt>
cmpl   $0xd,-0x1c(%rbp)
jne    11d3 <safe_gets+0xa3>
mov    $0xa,%edi
call   1030 <putchar@plt>
cmpl   $0xd,-0x1c(%rbp)
je     11e7 <safe_gets+0xb7>
cmpl   $0xa,-0x1c(%rbp)
jne    11ec <safe_gets+0xbc>
jmp    1207 <safe_gets+0xd7>
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1147 <safe_gets+0x17>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_gets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    %rdi,%r12
cmp    $0x2,%esi
jl     11c7 <safe_gets+0x97>
mov    %esi,%ebx
lea    0xead(%rip),%r14
mov    %r15,%r12
jmp    117b <safe_gets+0x4b>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1050 <putstr@plt>
add    $0xffffffffffffffff,%r12
add    $0x1,%ebx
mov    %ebx,%r13d
mov    %r13d,%ebx
cmp    $0x1,%r13d
jle    11c7 <safe_gets+0x97>
lea    -0x1(%rbx),%r13d
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0x7f,%eax
je     1192 <safe_gets+0x62>
mov    %eax,%ebp
cmp    $0x8,%eax
jne    11a0 <safe_gets+0x70>
cmp    %r15,%r12
ja     1160 <safe_gets+0x30>
mov    %r13d,%ebx
jmp    116c <safe_gets+0x3c>
nopl   0x0(%rax)
movsbl %bpl,%edi
call   1030 <putchar@plt>
cmp    $0xa,%ebp
je     11c7 <safe_gets+0x97>
cmp    $0xd,%ebp
je     11bd <safe_gets+0x8d>
mov    %bpl,(%r12)
add    $0x1,%r12
jmp    1172 <safe_gets+0x42>
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 = [4 x i8] c"\08 \08\00"
define i8* @safe_gets(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
store i8* %7, i8** %5
br label %8
8: ; preds = %47, %29, %2
%9 = load i32, i32* %4
%10 = add nsw i32 %9, -1
store i32 %10, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %52
12: ; preds = %8
%13 = call i32 (...) @getchar()
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 8
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, 127
br i1 %18, label %19, label %32
19: ; preds = %16, %12
%20 = load i8*, i8** %5
%21 = load i8*, i8** %3
%22 = icmp ugt i8* %20, %21
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = call i32 @putstr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i32 -1
store i8* %28, i8** %5
br label %29
29: ; preds = %23, %19
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
br label %8
32: ; preds = %16
%33 = load i32, i32* %6
%34 = trunc i32 %33 to i8
%35 = call i32 @putchar(i8 signext %34)
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, 13
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @putchar(i8 signext 10)
br label %40
40: ; preds = %38, %32
%41 = load i32, i32* %6
%42 = icmp eq i32 %41, 13
br i1 %42, label %46, label %43
43: ; preds = %40
%44 = load i32, i32* %6
%45 = icmp eq i32 %44, 10
br i1 %45, label %46, label %47
46: ; preds = %43, %40
br label %52
47: ; preds = %43
%48 = load i32, i32* %6
%49 = trunc i32 %48 to i8
%50 = load i8*, i8** %5
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %5
store i8 %49, i8* %50
br label %8
52: ; preds = %46, %8
%53 = load i8*, i8** %5
store i8 0, i8* %53
%54 = load i8*, i8** %3
ret i8* %54
}

LLVM IR (-O2):
@.str = [4 x i8] c"\08 \08\00"
define i8* @safe_gets(i8* returned writeonly %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %29
4: ; preds = %2, %25
%5 = phi i32 [ %27, %25 ], [ %1, %2 ]
%6 = phi i8* [ %26, %25 ], [ %0, %2 ]
%7 = add nsw i32 %5, -1
%8 = tail call i32 (...) @getchar()
switch i32 %8, label %18 [
i32 127, label %9
i32 8, label %9
]
9: ; preds = %4, %4
%10 = icmp ugt i8* %6, %0
br i1 %10, label %11, label %14
11: ; preds = %9
%12 = tail call i32 @putstr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds i8, i8* %6, i64 -1
br label %14
14: ; preds = %11, %9
%15 = phi i8* [ %13, %11 ], [ %6, %9 ]
%16 = phi i32 [ %5, %11 ], [ %7, %9 ]
%17 = add nuw nsw i32 %16, 1
br label %25
18: ; preds = %4
%19 = trunc i32 %8 to i8
%20 = tail call i32 @putchar(i8 signext %19)
switch i32 %8, label %23 [
i32 13, label %21
i32 10, label %29
]
21: ; preds = %18
%22 = tail call i32 @putchar(i8 signext 10)
br label %29
23: ; preds = %18
%24 = getelementptr inbounds i8, i8* %6, i64 1
store i8 %19, i8* %6
br label %25
25: ; preds = %23, %14
%26 = phi i8* [ %15, %14 ], [ %24, %23 ]
%27 = phi i32 [ %17, %14 ], [ %7, %23 ]
%28 = icmp sgt i32 %27, 1
br i1 %28, label %4, label %29
29: ; preds = %25, %18, %2, %21
%30 = phi i8* [ %6, %21 ], [ %0, %2 ], [ %26, %25 ], [ %6, %18 ]
store i8 0, i8* %30
ret i8* %0
}