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


x86 assembly (clang, -O0):
<fd_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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     117a <fd_readline+0x5a>
movl   $0xffffffff,-0x18(%rbp)
jmp    12ed <fd_readline+0x1cd>
movzbl -0x19(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x3,%eax
je     124e <fd_readline+0x12e>
jmp    118f <fd_readline+0x6f>
mov    -0x3c(%rbp),%eax
sub    $0x8,%eax
je     11c2 <fd_readline+0xa2>
jmp    11a0 <fd_readline+0x80>
mov    -0x3c(%rbp),%eax
sub    $0xd,%eax
je     126c <fd_readline+0x14c>
jmp    11b1 <fd_readline+0x91>
mov    -0x3c(%rbp),%eax
sub    $0x7f,%eax
jne    1286 <fd_readline+0x166>
jmp    11c2 <fd_readline+0xa2>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    122a <fd_readline+0x10a>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
movb   $0x8,-0x31(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11f6 <fd_readline+0xd6>
movb   $0x20,-0x32(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x32(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1210 <fd_readline+0xf0>
movb   $0x8,-0x33(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x33(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1249 <fd_readline+0x129>
jmp    122f <fd_readline+0x10f>
movb   $0x7,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1249 <fd_readline+0x129>
jmp    12e8 <fd_readline+0x1c8>
movl   $0xffffffff,-0x18(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%ecx
mov    0x2d7b(%rip),%rax
mov    %ecx,(%rax)
jmp    12ed <fd_readline+0x1cd>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x18(%rbp)
jmp    12ed <fd_readline+0x1cd>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    12c4 <fd_readline+0x1a4>
mov    -0x19(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x19(%rbp),%al
mov    %al,-0x35(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x35(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12e3 <fd_readline+0x1c3>
jmp    12c9 <fd_readline+0x1a9>
movb   $0x7,-0x36(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x36(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12e3 <fd_readline+0x1c3>
jmp    12e8 <fd_readline+0x1c8>
jmp    1150 <fd_readline+0x30>
mov    -0x18(%rbp),%eax
add    $0x40,%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,%r14d
mov    %rdx,%r12
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x1(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    $0xffffffff,%ecx
test   %eax,%eax
jle    1250 <fd_readline+0x130>
movslq %r14d,%rax
lea    (%r12,%rax,1),%r14
add    $0xffffffffffffffff,%r14
lea    0x1(%rsp),%r13
mov    %r12,%r15
jmp    119d <fd_readline+0x7d>
cs nopw 0x0(%rax,%rax,1)
movb   $0x7,0x4(%rsp)
mov    %ebp,%edi
lea    0x4(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    123f <fd_readline+0x11f>
movzbl 0x1(%rsp),%eax
cmp    $0xc,%al
jg     11b0 <fd_readline+0x90>
cmp    $0x8,%al
je     11b4 <fd_readline+0x94>
cmp    $0x3,%al
jne    11fb <fd_readline+0xdb>
jmp    122d <fd_readline+0x10d>
cmp    $0x7f,%al
jne    11f7 <fd_readline+0xd7>
cmp    %r12,%r15
jbe    1170 <fd_readline+0x50>
add    $0xffffffffffffffff,%r15
movb   $0x8,0x7(%rsp)
mov    %ebp,%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movb   $0x20,0x6(%rsp)
mov    %ebp,%edi
lea    0x6(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movb   $0x8,0x5(%rsp)
mov    %ebp,%edi
lea    0x5(%rsp),%rsi
jmp    117c <fd_readline+0x5c>
cmp    $0xd,%al
je     1246 <fd_readline+0x126>
cmp    %r14,%r15
jae    121c <fd_readline+0xfc>
mov    %al,(%r15)
add    $0x1,%r15
movzbl 0x1(%rsp),%eax
mov    %al,0x3(%rsp)
mov    %ebp,%edi
lea    0x3(%rsp),%rsi
jmp    117c <fd_readline+0x5c>
movb   $0x7,0x2(%rsp)
mov    %ebp,%edi
lea    0x2(%rsp),%rsi
jmp    117c <fd_readline+0x5c>
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    0x2da3(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ecx
jmp    1250 <fd_readline+0x130>
movb   $0x0,(%r15)
sub    %r12d,%r15d
mov    %r15d,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINTR = global i32 0
@errno = global i32 0
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 i8
%14 = alloca i8
%15 = alloca i8
%16 = alloca i8
%17 = alloca i8
%18 = alloca i8
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, %88
%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 %31
30: ; preds = %25
store i32 -1, i32* %9
br label %89
31: ; preds = %25
%32 = load i8, i8* %10
%33 = zext i8 %32 to i32
switch i32 %33, label %69 [
i32 8, label %34
i32 127, label %34
i32 3, label %59
i32 13, label %61
]
34: ; preds = %31, %31
%35 = load i8*, i8** %11
%36 = load i8*, i8** %7
%37 = icmp ugt i8* %35, %36
br i1 %37, label %38, label %53
38: ; preds = %34
%39 = load i8*, i8** %11
%40 = getelementptr inbounds i8, i8* %39, i32 -1
store i8* %40, i8** %11
br label %41
41: ; preds = %38
store i8 8, i8* %13
%42 = load i32, i32* %6
%43 = call i32 @write(i32 %42, i8* %13, i32 1)
br label %44
44: ; preds = %41
br label %45
45: ; preds = %44
store i8 32, i8* %14
%46 = load i32, i32* %6
%47 = call i32 @write(i32 %46, i8* %14, i32 1)
br label %48
48: ; preds = %45
br label %49
49: ; preds = %48
store i8 8, i8* %15
%50 = load i32, i32* %6
%51 = call i32 @write(i32 %50, i8* %15, i32 1)
br label %52
52: ; preds = %49
br label %58
53: ; preds = %34
br label %54
54: ; preds = %53
store i8 7, i8* %16
%55 = load i32, i32* %6
%56 = call i32 @write(i32 %55, i8* %16, i32 1)
br label %57
57: ; preds = %54
br label %58
58: ; preds = %57, %52
br label %88
59: ; preds = %31
store i32 -1, i32* %9
%60 = load i32, i32* @EINTR
store i32 %60, i32* @errno
br label %89
61: ; preds = %31
%62 = load i8*, i8** %11
store i8 0, i8* %62
%63 = load i8*, i8** %11
%64 = load i8*, i8** %7
%65 = ptrtoint i8* %63 to i64
%66 = ptrtoint i8* %64 to i64
%67 = sub i64 %65, %66
%68 = trunc i64 %67 to i32
store i32 %68, i32* %9
br label %89
69: ; preds = %31
%70 = load i8*, i8** %11
%71 = load i8*, i8** %12
%72 = icmp ult i8* %70, %71
br i1 %72, label %73, label %82
73: ; preds = %69
%74 = load i8, i8* %10
%75 = load i8*, i8** %11
%76 = getelementptr inbounds i8, i8* %75, i32 1
store i8* %76, i8** %11
store i8 %74, i8* %75
br label %77
77: ; preds = %73
%78 = load i8, i8* %10
store i8 %78, i8* %17
%79 = load i32, i32* %6
%80 = call i32 @write(i32 %79, i8* %17, i32 1)
br label %81
81: ; preds = %77
br label %87
82: ; preds = %69
br label %83
83: ; preds = %82
store i8 7, i8* %18
%84 = load i32, i32* %6
%85 = call i32 @write(i32 %84, i8* %18, i32 1)
br label %86
86: ; preds = %83
br label %87
87: ; preds = %86, %81
br label %88
88: ; preds = %87, %58
br label %25
89: ; preds = %61, %59, %30
%90 = load i32, i32* %9
ret i32 %90
}

LLVM IR (-O2):
@EINTR = global i32 0
@errno = global i32 0
define i32 @fd_readline(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
%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 %48, label %17
17: ; preds = %4, %44
%18 = phi i8* [ %45, %44 ], [ %2, %4 ]
%19 = load i8, i8* %5
switch i8 %19, label %36 [
i8 8, label %20
i8 127, label %20
i8 3, label %29
i8 13, label %31
]
20: ; preds = %17, %17
%21 = icmp ugt i8* %18, %2
br i1 %21, label %22, label %27
22: ; preds = %20
%23 = getelementptr inbounds i8, i8* %18, i64 -1
store i8 8, i8* %6
%24 = call i32 @write(i32 %1, i8* %6, i32 1)
store i8 32, i8* %7
%25 = call i32 @write(i32 %1, i8* %7, i32 1)
store i8 8, i8* %8
%26 = call i32 @write(i32 %1, i8* %8, i32 1)
br label %44
27: ; preds = %20
store i8 7, i8* %9
%28 = call i32 @write(i32 %1, i8* %9, i32 1)
br label %44
29: ; preds = %17
%30 = load i32, i32* @EINTR
store i32 %30, i32* @errno
br label %48
31: ; preds = %17
store i8 0, i8* %18
%32 = ptrtoint i8* %18 to i64
%33 = ptrtoint i8* %2 to i64
%34 = sub i64 %32, %33
%35 = trunc i64 %34 to i32
br label %48
36: ; preds = %17
%37 = icmp ult i8* %18, %14
br i1 %37, label %38, label %42
38: ; preds = %36
%39 = getelementptr inbounds i8, i8* %18, i64 1
store i8 %19, i8* %18
%40 = load i8, i8* %5
store i8 %40, i8* %10
%41 = call i32 @write(i32 %1, i8* %10, i32 1)
br label %44
42: ; preds = %36
store i8 7, i8* %11
%43 = call i32 @write(i32 %1, i8* %11, i32 1)
br label %44
44: ; preds = %38, %42, %22, %27
%45 = phi i8* [ %39, %38 ], [ %18, %42 ], [ %23, %22 ], [ %18, %27 ]
%46 = call i32 @read(i32 %0, i8* %5, i32 1)
%47 = icmp slt i32 %46, 1
br i1 %47, label %48, label %17
48: ; preds = %44, %4, %31, %29
%49 = phi i32 [ %35, %31 ], [ -1, %29 ], [ -1, %4 ], [ -1, %44 ]
ret i32 %49
}