#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 getchar();
int /*<<< orphan*/ putch(char);
int /*<<< orphan*/ puts0(char *);
char *getsn(char *str, int len) {
  char c;
  int i;
  i = 0;
  while (1) {
    c = getchar();
    putch(c);
    if (c == 0x0DU) {
      *str = 0;
      break;
    }
    if (c == 8) {
      if (i > 0) {
        i--;
        str--;
      }
    } else {
      *str = c;
      if (++i >= (len - 1)) {
        *str = 0;
        break;
      }
      str++;
    }
  }
  puts0("\r\n");
  return str;
}


x86 assembly (clang, -O0):
<getsn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0xd(%rbp)
movsbl -0xd(%rbp),%edi
call   1050 <putch@plt>
movsbl -0xd(%rbp),%eax
cmp    $0xd,%eax
jne    1172 <getsn+0x42>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    11e0 <getsn+0xb0>
movsbl -0xd(%rbp),%eax
cmp    $0x8,%eax
jne    11a3 <getsn+0x73>
cmpl   $0x0,-0x14(%rbp)
jle    119e <getsn+0x6e>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    11db <getsn+0xab>
mov    -0xd(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     11cf <getsn+0x9f>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    11e0 <getsn+0xb0>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1146 <getsn+0x16>
lea    0xe19(%rip),%rdi
call   1040 <puts0@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsn>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
add    $0xffffffff,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebx
movsbl %al,%edi
call   1050 <putch@plt>
cmp    $0x8,%bl
jne    1173 <getsn+0x43>
test   %ebp,%ebp
jle    1150 <getsn+0x20>
add    $0xffffffff,%ebp
add    $0xffffffffffffffff,%r15
jmp    1150 <getsn+0x20>
cmp    $0xd,%bl
je     1189 <getsn+0x59>
mov    %bl,(%r15)
add    $0x1,%ebp
cmp    %r14d,%ebp
jge    1189 <getsn+0x59>
add    $0x1,%r15
jmp    1150 <getsn+0x20>
movb   $0x0,(%r15)
lea    0xe6c(%rip),%rdi
call   1040 <puts0@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\0D\0A\00"
define i8* @getsn(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
store i32 0, i32* %6
br label %7
7: ; preds = %2, %42
%8 = call signext i8 (...) @getchar()
store i8 %8, i8* %5
%9 = load i8, i8* %5
%10 = call i32 @putch(i8 signext %9)
%11 = load i8, i8* %5
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 13
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = load i8*, i8** %3
store i8 0, i8* %15
br label %43
16: ; preds = %7
%17 = load i8, i8* %5
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 8
br i1 %19, label %20, label %29
20: ; preds = %16
%21 = load i32, i32* %6
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i32, i32* %6
%25 = add nsw i32 %24, -1
store i32 %25, i32* %6
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i32 -1
store i8* %27, i8** %3
br label %28
28: ; preds = %23, %20
br label %42
29: ; preds = %16
%30 = load i8, i8* %5
%31 = load i8*, i8** %3
store i8 %30, i8* %31
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
%34 = load i32, i32* %4
%35 = sub nsw i32 %34, 1
%36 = icmp sge i32 %33, %35
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = load i8*, i8** %3
store i8 0, i8* %38
br label %43
39: ; preds = %29
%40 = load i8*, i8** %3
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %3
br label %42
42: ; preds = %39, %28
br label %7
43: ; preds = %37, %14
%44 = call i32 @puts0(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%45 = load i8*, i8** %3
ret i8* %45
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0D\0A\00"
define i8* @getsn(i8* writeonly %0, i32 %1) {
%3 = add nsw i32 %1, -1
br label %4
4: ; preds = %15, %2
%5 = phi i8* [ %0, %2 ], [ %16, %15 ]
%6 = phi i32 [ 0, %2 ], [ %17, %15 ]
%7 = icmp sgt i32 %6, 0
br label %8
8: ; preds = %4, %11
%9 = tail call signext i8 (...) @getchar()
%10 = tail call i32 @putch(i8 signext %9)
switch i8 %9, label %18 [
i8 13, label %23
i8 8, label %11
]
11: ; preds = %8
br i1 %7, label %12, label %8
12: ; preds = %11
%13 = add nsw i32 %6, -1
%14 = getelementptr inbounds i8, i8* %5, i64 -1
br label %15
15: ; preds = %12, %21
%16 = phi i8* [ %22, %21 ], [ %14, %12 ]
%17 = phi i32 [ %19, %21 ], [ %13, %12 ]
br label %4
18: ; preds = %8
store i8 %9, i8* %5
%19 = add nsw i32 %6, 1
%20 = icmp slt i32 %19, %3
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = getelementptr inbounds i8, i8* %5, i64 1
br label %15
23: ; preds = %18, %8
store i8 0, i8* %5
%24 = tail call i32 @puts0(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
ret i8* %5
}