#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 dgetc();
int /*<<< orphan*/ dputc(int);
int dreadline(char *buffer, int len) {
  char c;
  char *p = buffer;
  while (p < buffer + len - 1) {
    c = dgetc();
    if (c == 13 || c == 10)
      break;
    else if (c == 8 && p > buffer)
      p--;
    else {
      *p++ = c;
    }
    dputc(c);
  }
  dputc(13);
  dputc(10);
  *p = 0;
  return p - buffer;
}


x86 assembly (clang, -O0):
<dreadline>:
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    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0xc(%rbp),%rdx
add    %rdx,%rcx
add    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jae    11cf <dreadline+0xaf>
mov    $0x0,%al
call   1030 <dgetc@plt>
mov    %al,-0xd(%rbp)
movsbl -0xd(%rbp),%eax
cmp    $0xd,%eax
je     1177 <dreadline+0x57>
movsbl -0xd(%rbp),%eax
cmp    $0xa,%eax
jne    117c <dreadline+0x5c>
jmp    11cf <dreadline+0xaf>
movsbl -0xd(%rbp),%eax
cmp    $0x8,%eax
jne    11a8 <dreadline+0x88>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    11a8 <dreadline+0x88>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    11bc <dreadline+0x9c>
mov    -0xd(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    11c1 <dreadline+0xa1>
movsbl -0xd(%rbp),%edi
call   1040 <dputc@plt>
jmp    1137 <dreadline+0x17>
mov    $0xd,%edi
call   1040 <dputc@plt>
mov    $0xa,%edi
call   1040 <dputc@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dreadline>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
movslq %esi,%rax
lea    (%rdi,%rax,1),%r15
add    $0xffffffffffffffff,%r15
mov    %rdi,%rbx
cmp    %rdi,%r15
jbe    117b <dreadline+0x5b>
mov    %r14,%rbx
jmp    1153 <dreadline+0x33>
mov    %al,(%rbx)
add    $0x1,%rbx
movsbl %al,%edi
call   1040 <dputc@plt>
cmp    %r15,%rbx
jae    117b <dreadline+0x5b>
xor    %eax,%eax
call   1030 <dgetc@plt>
cmp    $0x8,%al
je     1170 <dreadline+0x50>
cmp    $0xa,%al
je     117b <dreadline+0x5b>
cmp    $0xd,%al
jne    1140 <dreadline+0x20>
jmp    117b <dreadline+0x5b>
nopl   0x0(%rax,%rax,1)
cmp    %r14,%rbx
jbe    1140 <dreadline+0x20>
add    $0xffffffffffffffff,%rbx
jmp    1146 <dreadline+0x26>
mov    $0xd,%edi
call   1040 <dputc@plt>
mov    $0xa,%edi
call   1040 <dputc@plt>
movb   $0x0,(%rbx)
sub    %r14d,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @dreadline(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
store i8* %7, i8** %6
br label %8
8: ; preds = %42, %2
%9 = load i8*, i8** %6
%10 = load i8*, i8** %3
%11 = load i32, i32* %4
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = getelementptr inbounds i8, i8* %13, i64 -1
%15 = icmp ult i8* %9, %14
br i1 %15, label %16, label %46
16: ; preds = %8
%17 = call signext i8 (...) @dgetc()
store i8 %17, i8* %5
%18 = load i8, i8* %5
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 13
br i1 %20, label %25, label %21
21: ; preds = %16
%22 = load i8, i8* %5
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 10
br i1 %24, label %25, label %26
25: ; preds = %21, %16
br label %46
26: ; preds = %21
%27 = load i8, i8* %5
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 8
br i1 %29, label %30, label %37
30: ; preds = %26
%31 = load i8*, i8** %6
%32 = load i8*, i8** %3
%33 = icmp ugt i8* %31, %32
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i8*, i8** %6
%36 = getelementptr inbounds i8, i8* %35, i32 -1
store i8* %36, i8** %6
br label %41
37: ; preds = %30, %26
%38 = load i8, i8* %5
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %6
store i8 %38, i8* %39
br label %41
41: ; preds = %37, %34
br label %42
42: ; preds = %41
%43 = load i8, i8* %5
%44 = sext i8 %43 to i32
%45 = call i32 @dputc(i32 %44)
br label %8
46: ; preds = %25, %8
%47 = call i32 @dputc(i32 13)
%48 = call i32 @dputc(i32 10)
%49 = load i8*, i8** %6
store i8 0, i8* %49
%50 = load i8*, i8** %6
%51 = load i8*, i8** %3
%52 = ptrtoint i8* %50 to i64
%53 = ptrtoint i8* %51 to i64
%54 = sub i64 %52, %53
%55 = trunc i64 %54 to i32
ret i32 %55
}

LLVM IR (-O2):
define i32 @dreadline(i8* %0, i32 %1) {
%3 = sext i32 %1 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = getelementptr inbounds i8, i8* %4, i64 -1
%6 = icmp ugt i8* %5, %0
br i1 %6, label %7, label %21
7: ; preds = %2, %17
%8 = phi i8* [ %18, %17 ], [ %0, %2 ]
%9 = tail call signext i8 (...) @dgetc()
%10 = sext i8 %9 to i32
switch i8 %9, label %15 [
i8 13, label %21
i8 10, label %21
i8 8, label %11
]
11: ; preds = %7
%12 = icmp ugt i8* %8, %0
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = getelementptr inbounds i8, i8* %8, i64 -1
br label %17
15: ; preds = %7, %11
%16 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %9, i8* %8
br label %17
17: ; preds = %13, %15
%18 = phi i8* [ %14, %13 ], [ %16, %15 ]
%19 = tail call i32 @dputc(i32 %10)
%20 = icmp ult i8* %18, %5
br i1 %20, label %7, label %21
21: ; preds = %17, %7, %7, %2
%22 = phi i8* [ %0, %2 ], [ %8, %7 ], [ %8, %7 ], [ %18, %17 ]
%23 = tail call i32 @dputc(i32 13)
%24 = tail call i32 @dputc(i32 10)
store i8 0, i8* %22
%25 = ptrtoint i8* %22 to i64
%26 = ptrtoint i8* %0 to i64
%27 = sub i64 %25, %26
%28 = trunc i64 %27 to i32
ret i32 %28
}