#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 read(int /*<<< orphan*/, char *, int);
int strlen(char *);
int /*<<< orphan*/ write(int, int /*<<< orphan*/, int);
int readline(char *buf, int maxl) {
  char *p;
  p = buf;
  while (read(0, p, 1) == 1 && *p != '\n' && *p != '\r' && p < buf + maxl) {
    write(1, p++, 1);
    if (*(p - 1) == 0x8)
      p -= 2;
  }
  *p = '\0';
  return strlen(buf);
}


x86 assembly (clang, -O0):
<readline>:
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),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
mov    %al,-0x19(%rbp)
jne    11a9 <readline+0x79>
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x19(%rbp)
je     11a9 <readline+0x79>
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0xd,%ecx
mov    %al,-0x19(%rbp)
je     11a9 <readline+0x79>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0xc(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11b9 <readline+0x89>
jmp    11f8 <readline+0xc8>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    %eax,%esi
mov    $0x1,%edx
mov    %edx,%edi
call   1030 <write@plt>
mov    -0x18(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x8,%eax
jne    11f3 <readline+0xc3>
mov    -0x18(%rbp),%rax
add    $0xfffffffffffffffe,%rax
mov    %rax,-0x18(%rbp)
jmp    1147 <readline+0x17>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %r14,%rbx
cmp    $0x1,%eax
jne    11a5 <readline+0x75>
movslq %ebp,%rbp
add    %r14,%rbp
mov    %r14,%rbx
nopl   0x0(%rax)
movzbl (%rbx),%eax
cmp    $0xa,%al
je     11a5 <readline+0x75>
cmp    $0xd,%al
je     11a5 <readline+0x75>
cmp    %rbp,%rbx
jae    11a5 <readline+0x75>
mov    $0x1,%edi
mov    %ebx,%esi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %eax,%eax
cmpb   $0x8,(%rbx)
setne  %al
lea    (%rbx,%rax,2),%rbx
add    $0xffffffffffffffff,%rbx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     1160 <readline+0x30>
movb   $0x0,(%rbx)
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <strlen@plt>

LLVM IR (-O0):
define i32 @readline(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
store i8* %6, i8** %5
br label %7
7: ; preds = %43, %2
%8 = load i8*, i8** %5
%9 = call i32 @read(i32 0, i8* %8, i32 1)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %28
11: ; preds = %7
%12 = load i8*, i8** %5
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 10
br i1 %15, label %16, label %28
16: ; preds = %11
%17 = load i8*, i8** %5
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 13
br i1 %20, label %21, label %28
21: ; preds = %16
%22 = load i8*, i8** %5
%23 = load i8*, i8** %3
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = icmp ult i8* %22, %26
br label %28
28: ; preds = %21, %16, %11, %7
%29 = phi i1 [ false, %16 ], [ false, %11 ], [ false, %7 ], [ %27, %21 ]
br i1 %29, label %30, label %44
30: ; preds = %28
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %5
%33 = ptrtoint i8* %31 to i32
%34 = call i32 @write(i32 1, i32 %33, i32 1)
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i64 -1
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 8
br i1 %39, label %40, label %43
40: ; preds = %30
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i64 -2
store i8* %42, i8** %5
br label %43
43: ; preds = %40, %30
br label %7
44: ; preds = %28
%45 = load i8*, i8** %5
store i8 0, i8* %45
%46 = load i8*, i8** %3
%47 = call i32 @strlen(i8* %46)
ret i32 %47
}

LLVM IR (-O2):
define i32 @readline(i8* %0, i32 %1) {
%3 = sext i32 %1 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = tail call i32 @read(i32 0, i8* %0, i32 1)
%6 = icmp eq i32 %5, 1
br i1 %6, label %7, label %22
7: ; preds = %2, %12
%8 = phi i8* [ %19, %12 ], [ %0, %2 ]
%9 = load i8, i8* %8
switch i8 %9, label %10 [
i8 10, label %22
i8 13, label %22
]
10: ; preds = %7
%11 = icmp ult i8* %8, %4
br i1 %11, label %12, label %22
12: ; preds = %10
%13 = ptrtoint i8* %8 to i64
%14 = trunc i64 %13 to i32
%15 = tail call i32 @write(i32 1, i32 %14, i32 1)
%16 = load i8, i8* %8
%17 = icmp eq i8 %16, 8
%18 = select i1 %17, i64 -1, i64 1
%19 = getelementptr inbounds i8, i8* %8, i64 %18
%20 = tail call i32 @read(i32 0, i8* %19, i32 1)
%21 = icmp eq i32 %20, 1
br i1 %21, label %7, label %22
22: ; preds = %10, %12, %7, %7, %2
%23 = phi i8* [ %0, %2 ], [ %8, %7 ], [ %8, %7 ], [ %19, %12 ], [ %8, %10 ]
store i8 0, i8* %23
%24 = tail call i32 @strlen(i8* %0)
ret i32 %24
}