#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*/ putch3(char);
char *getsn(char *str, int len) {
  char c;
  int i;
  i = 0;
  while (1) {
    c = getch();
    putch3(c);
    if (c == 0x0DU) {
      *str = 0;
      return str;
    }
    *str = c;
    if (++i >= (len - 1)) {
      *str = 0;
      return str;
    }
    str++;
  }
}


x86 assembly (clang, -O0):
<getsn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <getch@plt>
mov    %al,-0x15(%rbp)
movsbl -0x15(%rbp),%edi
call   1030 <putch3@plt>
movsbl -0x15(%rbp),%eax
cmp    $0xd,%eax
jne    116a <getsn+0x4a>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11af <getsn+0x8f>
mov    -0x15(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     119e <getsn+0x7e>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11af <getsn+0x8f>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1136 <getsn+0x16>
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,%ebp
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <getch@plt>
mov    %eax,%r15d
movsbl %al,%edi
call   1030 <putch3@plt>
cmp    $0xd,%r15b
je     1186 <getsn+0x66>
add    $0xffffffff,%ebp
cmp    $0x2,%ebp
mov    $0x1,%ebx
cmovge %ebp,%ebx
add    $0xffffffff,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15b,(%r14)
sub    $0x1,%ebx
jb     1186 <getsn+0x66>
add    $0x1,%r14
xor    %eax,%eax
call   1040 <getch@plt>
mov    %eax,%r15d
movsbl %al,%ebp
mov    %ebp,%edi
call   1030 <putch3@plt>
cmp    $0xd,%bpl
jne    1160 <getsn+0x40>
movb   $0x0,(%r14)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @getsn(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %7
br label %8
8: ; preds = %2, %29
%9 = call signext i8 (...) @getch()
store i8 %9, i8* %6
%10 = load i8, i8* %6
%11 = call i32 @putch3(i8 signext %10)
%12 = load i8, i8* %6
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 13
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = load i8*, i8** %4
store i8 0, i8* %16
%17 = load i8*, i8** %4
store i8* %17, i8** %3
br label %32
18: ; preds = %8
%19 = load i8, i8* %6
%20 = load i8*, i8** %4
store i8 %19, i8* %20
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 1
store i32 %22, i32* %7
%23 = load i32, i32* %5
%24 = sub nsw i32 %23, 1
%25 = icmp sge i32 %22, %24
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = load i8*, i8** %4
store i8 0, i8* %27
%28 = load i8*, i8** %4
store i8* %28, i8** %3
br label %32
29: ; preds = %18
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %4
br label %8
32: ; preds = %26, %15
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
define i8* @getsn(i8* writeonly %0, i32 %1) {
%3 = tail call signext i8 (...) @getch()
%4 = tail call i32 @putch3(i8 signext %3)
%5 = icmp eq i8 %3, 13
br i1 %5, label %21, label %6
6: ; preds = %2
%7 = add i32 %1, -1
%8 = call i32 @llvm.smax.i32(i32 %7, i32 1)
%9 = add nsw i32 %8, -1
br label %10
10: ; preds = %6, %15
%11 = phi i8 [ %3, %6 ], [ %18, %15 ]
%12 = phi i32 [ 0, %6 ], [ %16, %15 ]
%13 = phi i8* [ %0, %6 ], [ %17, %15 ]
store i8 %11, i8* %13
%14 = icmp eq i32 %12, %9
br i1 %14, label %21, label %15
15: ; preds = %10
%16 = add nuw nsw i32 %12, 1
%17 = getelementptr inbounds i8, i8* %13, i64 1
%18 = tail call signext i8 (...) @getch()
%19 = tail call i32 @putch3(i8 signext %18)
%20 = icmp eq i8 %18, 13
br i1 %20, label %21, label %10
21: ; preds = %15, %10, %2
%22 = phi i8* [ %0, %2 ], [ %13, %10 ], [ %17, %15 ]
store i8 0, i8* %22
ret i8* %22
}