#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, unsigned char *, int);
int /*<<< orphan*/ write(int, int *, int);
int fd_readline(int fdi, int fdo, char *b, int bsz) {
  int r;
  unsigned char c;
  unsigned char *bp, *bpe;
  bp = b;
  bpe = b + bsz - 1;
  while (1) {
    r = read(fdi, &c, 1);
    if (r <= 0) {
      r--;
      goto out;
    }
    switch (c) {
    case '\b':
      if (bp > (unsigned char *)b) {
        bp--;
        do {
          int cl = c;
          write((fdo), &(cl), 1);
        } while (0);
        do {
          int cl = ' ';
          write((fdo), &(cl), 1);
        } while (0);
        do {
          int cl = c;
          write((fdo), &(cl), 1);
        } while (0);
      } else {
        do {
          int cl = '\x07';
          write((fdo), &(cl), 1);
        } while (0);
      }
      break;
    case '\r':
      *bp = '\0';
      r = bp - (unsigned char *)b;
      goto out;
    default:
      if (bp < bpe) {
        *bp++ = c;
        do {
          int cl = c;
          write((fdo), &(cl), 1);
        } while (0);
      } else {
        do {
          int cl = '\x07';
          write((fdo), &(cl), 1);
        } while (0);
      }
      break;
    }
  }
out:
  return r;
}


x86 assembly (clang, -O0):
<fd_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     117c <fd_readline+0x5c>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    12bf <fd_readline+0x19f>
movzbl -0x19(%rbp),%eax
mov    %eax,-0x4c(%rbp)
sub    $0x8,%eax
je     11a2 <fd_readline+0x82>
jmp    1191 <fd_readline+0x71>
mov    -0x4c(%rbp),%eax
sub    $0xd,%eax
je     123a <fd_readline+0x11a>
jmp    1254 <fd_readline+0x134>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    1213 <fd_readline+0xf3>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
movzbl -0x19(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11d9 <fd_readline+0xb9>
movl   $0x20,-0x38(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11f6 <fd_readline+0xd6>
movzbl -0x19(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x3c(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1235 <fd_readline+0x115>
jmp    1218 <fd_readline+0xf8>
movl   $0x7,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1235 <fd_readline+0x115>
jmp    12ba <fd_readline+0x19a>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x18(%rbp)
jmp    12bf <fd_readline+0x19f>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1293 <fd_readline+0x173>
mov    -0x19(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
movzbl -0x19(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x44(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12b5 <fd_readline+0x195>
jmp    1298 <fd_readline+0x178>
movl   $0x7,-0x48(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12b5 <fd_readline+0x195>
jmp    12ba <fd_readline+0x19a>
jmp    1150 <fd_readline+0x30>
mov    -0x18(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%r15d
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1204 <fd_readline+0xe4>
movslq %r12d,%rax
lea    (%r14,%rax,1),%r13
add    $0xffffffffffffffff,%r13
lea    0x4(%rsp),%r12
mov    %r14,%rbx
jmp    119d <fd_readline+0x7d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movl   $0x7,0x4(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %r15d,%edi
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1204 <fd_readline+0xe4>
movzbl 0x3(%rsp),%eax
cmp    $0x8,%al
je     11c0 <fd_readline+0xa0>
cmp    $0xd,%al
je     120b <fd_readline+0xeb>
cmp    %r13,%rbx
jae    1170 <fd_readline+0x50>
mov    %al,(%rbx)
add    $0x1,%rbx
movzbl 0x3(%rsp),%eax
mov    %eax,0x4(%rsp)
jmp    1178 <fd_readline+0x58>
cmp    %r14,%rbx
jbe    1170 <fd_readline+0x50>
add    $0xffffffffffffffff,%rbx
movl   $0x8,0x4(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movl   $0x20,0x4(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movl   $0x8,0x4(%rsp)
jmp    1178 <fd_readline+0x58>
mov    %eax,%ebx
add    $0xffffffff,%ebx
jmp    1211 <fd_readline+0xf1>
movb   $0x0,(%rbx)
sub    %r14d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fd_readline(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%19 = load i8*, i8** %7
store i8* %19, i8** %11
%20 = load i8*, i8** %7
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = getelementptr inbounds i8, i8* %23, i64 -1
store i8* %24, i8** %12
br label %25
25: ; preds = %4, %93
%26 = load i32, i32* %5
%27 = call i32 @read(i32 %26, i8* %10, i32 1)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp sle i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i32, i32* %9
%32 = add nsw i32 %31, -1
store i32 %32, i32* %9
br label %94
33: ; preds = %25
%34 = load i8, i8* %10
%35 = zext i8 %34 to i32
switch i32 %35, label %73 [
i32 8, label %36
i32 13, label %65
]
36: ; preds = %33
%37 = load i8*, i8** %11
%38 = load i8*, i8** %7
%39 = icmp ugt i8* %37, %38
br i1 %39, label %40, label %59
40: ; preds = %36
%41 = load i8*, i8** %11
%42 = getelementptr inbounds i8, i8* %41, i32 -1
store i8* %42, i8** %11
br label %43
43: ; preds = %40
%44 = load i8, i8* %10
%45 = zext i8 %44 to i32
store i32 %45, i32* %13
%46 = load i32, i32* %6
%47 = call i32 @write(i32 %46, i32* %13, i32 1)
br label %48
48: ; preds = %43
br label %49
49: ; preds = %48
store i32 32, i32* %14
%50 = load i32, i32* %6
%51 = call i32 @write(i32 %50, i32* %14, i32 1)
br label %52
52: ; preds = %49
br label %53
53: ; preds = %52
%54 = load i8, i8* %10
%55 = zext i8 %54 to i32
store i32 %55, i32* %15
%56 = load i32, i32* %6
%57 = call i32 @write(i32 %56, i32* %15, i32 1)
br label %58
58: ; preds = %53
br label %64
59: ; preds = %36
br label %60
60: ; preds = %59
store i32 7, i32* %16
%61 = load i32, i32* %6
%62 = call i32 @write(i32 %61, i32* %16, i32 1)
br label %63
63: ; preds = %60
br label %64
64: ; preds = %63, %58
br label %93
65: ; preds = %33
%66 = load i8*, i8** %11
store i8 0, i8* %66
%67 = load i8*, i8** %11
%68 = load i8*, i8** %7
%69 = ptrtoint i8* %67 to i64
%70 = ptrtoint i8* %68 to i64
%71 = sub i64 %69, %70
%72 = trunc i64 %71 to i32
store i32 %72, i32* %9
br label %94
73: ; preds = %33
%74 = load i8*, i8** %11
%75 = load i8*, i8** %12
%76 = icmp ult i8* %74, %75
br i1 %76, label %77, label %87
77: ; preds = %73
%78 = load i8, i8* %10
%79 = load i8*, i8** %11
%80 = getelementptr inbounds i8, i8* %79, i32 1
store i8* %80, i8** %11
store i8 %78, i8* %79
br label %81
81: ; preds = %77
%82 = load i8, i8* %10
%83 = zext i8 %82 to i32
store i32 %83, i32* %17
%84 = load i32, i32* %6
%85 = call i32 @write(i32 %84, i32* %17, i32 1)
br label %86
86: ; preds = %81
br label %92
87: ; preds = %73
br label %88
88: ; preds = %87
store i32 7, i32* %18
%89 = load i32, i32* %6
%90 = call i32 @write(i32 %89, i32* %18, i32 1)
br label %91
91: ; preds = %88
br label %92
92: ; preds = %91, %86
br label %93
93: ; preds = %92, %64
br label %25
94: ; preds = %65, %30
%95 = load i32, i32* %9
ret i32 %95
}

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