#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*/ cdel(int);
int /*<<< orphan*/ cput(int, char);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ isprint(unsigned char);
int read(int, unsigned char *, int);
int /*<<< orphan*/ xdel(int);
int /*<<< orphan*/ xput(int, unsigned char);
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--;
        if (isprint(*bp))
          cdel(fdo);
        else
          xdel(fdo);
      } else {
        cput(fdo, '\x07');
      }
      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;
        if (isprint(c))
          cput(fdo, c);
        else
          xput(fdo, c);
      } else {
        cput(fdo, '\x07');
      }
      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   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     11ba <fd_readline+0x5a>
movl   $0xffffffff,-0x18(%rbp)
jmp    1302 <fd_readline+0x1a2>
movzbl -0x19(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x3,%eax
je     125d <fd_readline+0xfd>
jmp    11cf <fd_readline+0x6f>
mov    -0x34(%rbp),%eax
sub    $0x8,%eax
je     1202 <fd_readline+0xa2>
jmp    11e0 <fd_readline+0x80>
mov    -0x34(%rbp),%eax
sub    $0xd,%eax
je     127b <fd_readline+0x11b>
jmp    11f1 <fd_readline+0x91>
mov    -0x34(%rbp),%eax
sub    $0x7f,%eax
jne    1295 <fd_readline+0x135>
jmp    1202 <fd_readline+0xa2>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    124b <fd_readline+0xeb>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movzbl (%rax),%edi
call   1050 <isprint@plt>
cmp    $0x0,%eax
je     123e <fd_readline+0xde>
mov    -0x8(%rbp),%edi
call   1040 <cdel@plt>
jmp    1246 <fd_readline+0xe6>
mov    -0x8(%rbp),%edi
call   1080 <xdel@plt>
jmp    1258 <fd_readline+0xf8>
mov    -0x8(%rbp),%edi
mov    $0x7,%esi
call   1060 <cput@plt>
jmp    12fd <fd_readline+0x19d>
movl   $0xffffffff,-0x18(%rbp)
mov    0x2d6d(%rip),%rax
mov    (%rax),%ecx
mov    0x2d6c(%rip),%rax
mov    %ecx,(%rax)
jmp    1302 <fd_readline+0x1a2>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x18(%rbp)
jmp    1302 <fd_readline+0x1a2>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    12eb <fd_readline+0x18b>
mov    -0x19(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
movzbl -0x19(%rbp),%edi
call   1050 <isprint@plt>
cmp    $0x0,%eax
je     12da <fd_readline+0x17a>
mov    -0x8(%rbp),%edi
movsbl -0x19(%rbp),%esi
call   1060 <cput@plt>
jmp    12e6 <fd_readline+0x186>
mov    -0x8(%rbp),%edi
movzbl -0x19(%rbp),%esi
call   1070 <xput@plt>
jmp    12f8 <fd_readline+0x198>
mov    -0x8(%rbp),%edi
mov    $0x7,%esi
call   1060 <cput@plt>
jmp    12fd <fd_readline+0x19d>
jmp    1190 <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
sub    $0x18,%rsp
mov    %ecx,%r12d
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %edi,%r13d
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    $0xffffffff,%ecx
test   %eax,%eax
jle    1294 <fd_readline+0x134>
movslq %r12d,%rax
lea    (%rax,%rbp,1),%r12
add    $0xffffffffffffffff,%r12
lea    0xf(%rsp),%rbx
mov    %rbp,%r15
mov    %rbp,0x10(%rsp)
jmp    11d5 <fd_readline+0x75>
mov    %r14d,%edi
mov    $0x7,%esi
call   1060 <cput@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    1283 <fd_readline+0x123>
movzbl 0xf(%rsp),%eax
cmp    $0xc,%al
jg     11f0 <fd_readline+0x90>
cmp    $0x8,%al
je     11f4 <fd_readline+0x94>
cmp    $0x3,%al
jne    1219 <fd_readline+0xb9>
jmp    1271 <fd_readline+0x111>
nopl   0x0(%rax,%rax,1)
cmp    $0x7f,%al
jne    1215 <fd_readline+0xb5>
cmp    %rbp,%r15
jbe    11b0 <fd_readline+0x50>
movzbl -0x1(%r15),%edi
add    $0xffffffffffffffff,%r15
call   1050 <isprint@plt>
mov    %r14d,%edi
test   %eax,%eax
je     124b <fd_readline+0xeb>
call   1040 <cdel@plt>
jmp    11bd <fd_readline+0x5d>
cmp    $0xd,%al
je     128a <fd_readline+0x12a>
cmp    %r12,%r15
jae    11b0 <fd_readline+0x50>
mov    %r12,%rbx
mov    %al,(%r15)
add    $0x1,%r15
movzbl 0xf(%rsp),%r12d
movzbl %r12b,%ebp
mov    %ebp,%edi
call   1050 <isprint@plt>
test   %eax,%eax
je     1255 <fd_readline+0xf5>
movsbl %r12b,%esi
mov    %r14d,%edi
call   1060 <cput@plt>
jmp    125f <fd_readline+0xff>
call   1080 <xdel@plt>
jmp    11bd <fd_readline+0x5d>
mov    %r14d,%edi
mov    %ebp,%esi
call   1070 <xput@plt>
mov    %rbx,%r12
mov    0x10(%rsp),%rbp
lea    0xf(%rsp),%rbx
jmp    11bd <fd_readline+0x5d>
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    0x2d5f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ecx
jmp    1294 <fd_readline+0x134>
movb   $0x0,(%r15)
sub    %ebp,%r15d
mov    %r15d,%ecx
mov    %ecx,%eax
add    $0x18,%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*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%13 = load i8*, i8** %7
store i8* %13, i8** %11
%14 = load i8*, i8** %7
%15 = load i32, i32* %8
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = getelementptr inbounds i8, i8* %17, i64 -1
store i8* %18, i8** %12
br label %19
19: ; preds = %4, %84
%20 = load i32, i32* %5
%21 = call i32 @read(i32 %20, i8* %10, i32 1)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %9
br label %85
25: ; preds = %19
%26 = load i8, i8* %10
%27 = zext i8 %26 to i32
switch i32 %27, label %60 [
i32 8, label %28
i32 127, label %28
i32 3, label %50
i32 13, label %52
]
28: ; preds = %25, %25
%29 = load i8*, i8** %11
%30 = load i8*, i8** %7
%31 = icmp ugt i8* %29, %30
br i1 %31, label %32, label %46
32: ; preds = %28
%33 = load i8*, i8** %11
%34 = getelementptr inbounds i8, i8* %33, i32 -1
store i8* %34, i8** %11
%35 = load i8*, i8** %11
%36 = load i8, i8* %35
%37 = call i32 @isprint(i8 zeroext %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = load i32, i32* %6
%41 = call i32 @cdel(i32 %40)
br label %45
42: ; preds = %32
%43 = load i32, i32* %6
%44 = call i32 @xdel(i32 %43)
br label %45
45: ; preds = %42, %39
br label %49
46: ; preds = %28
%47 = load i32, i32* %6
%48 = call i32 @cput(i32 %47, i8 signext 7)
br label %49
49: ; preds = %46, %45
br label %84
50: ; preds = %25
store i32 -1, i32* %9
%51 = load i32, i32* @EINTR
store i32 %51, i32* @errno
br label %85
52: ; preds = %25
%53 = load i8*, i8** %11
store i8 0, i8* %53
%54 = load i8*, i8** %11
%55 = load i8*, i8** %7
%56 = ptrtoint i8* %54 to i64
%57 = ptrtoint i8* %55 to i64
%58 = sub i64 %56, %57
%59 = trunc i64 %58 to i32
store i32 %59, i32* %9
br label %85
60: ; preds = %25
%61 = load i8*, i8** %11
%62 = load i8*, i8** %12
%63 = icmp ult i8* %61, %62
br i1 %63, label %64, label %80
64: ; preds = %60
%65 = load i8, i8* %10
%66 = load i8*, i8** %11
%67 = getelementptr inbounds i8, i8* %66, i32 1
store i8* %67, i8** %11
store i8 %65, i8* %66
%68 = load i8, i8* %10
%69 = call i32 @isprint(i8 zeroext %68)
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %64
%72 = load i32, i32* %6
%73 = load i8, i8* %10
%74 = call i32 @cput(i32 %72, i8 signext %73)
br label %79
75: ; preds = %64
%76 = load i32, i32* %6
%77 = load i8, i8* %10
%78 = call i32 @xput(i32 %76, i8 zeroext %77)
br label %79
79: ; preds = %75, %71
br label %83
80: ; preds = %60
%81 = load i32, i32* %6
%82 = call i32 @cput(i32 %81, i8 signext 7)
br label %83
83: ; preds = %80, %79
br label %84
84: ; preds = %83, %49
br label %19
85: ; preds = %52, %50, %24
%86 = load i32, i32* %9
ret i32 %86
}

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 = sext i32 %3 to i64
%7 = getelementptr inbounds i8, i8* %2, i64 %6
%8 = getelementptr inbounds i8, i8* %7, i64 -1
%9 = call i32 @read(i32 %0, i8* %5, i32 1)
%10 = icmp slt i32 %9, 1
br i1 %10, label %51, label %11
11: ; preds = %4, %47
%12 = phi i8* [ %48, %47 ], [ %2, %4 ]
%13 = load i8, i8* %5
switch i8 %13, label %34 [
i8 8, label %14
i8 127, label %14
i8 3, label %27
i8 13, label %29
]
14: ; preds = %11, %11
%15 = icmp ugt i8* %12, %2
br i1 %15, label %16, label %25
16: ; preds = %14
%17 = getelementptr inbounds i8, i8* %12, i64 -1
%18 = load i8, i8* %17
%19 = tail call i32 @isprint(i8 zeroext %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = tail call i32 @cdel(i32 %1)
br label %47
23: ; preds = %16
%24 = tail call i32 @xdel(i32 %1)
br label %47
25: ; preds = %14
%26 = tail call i32 @cput(i32 %1, i8 signext 7)
br label %47
27: ; preds = %11
%28 = load i32, i32* @EINTR
store i32 %28, i32* @errno
br label %51
29: ; preds = %11
store i8 0, i8* %12
%30 = ptrtoint i8* %12 to i64
%31 = ptrtoint i8* %2 to i64
%32 = sub i64 %30, %31
%33 = trunc i64 %32 to i32
br label %51
34: ; preds = %11
%35 = icmp ult i8* %12, %8
br i1 %35, label %36, label %45
36: ; preds = %34
%37 = getelementptr inbounds i8, i8* %12, i64 1
store i8 %13, i8* %12
%38 = load i8, i8* %5
%39 = tail call i32 @isprint(i8 zeroext %38)
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %36
%42 = tail call i32 @cput(i32 %1, i8 signext %38)
br label %47
43: ; preds = %36
%44 = tail call i32 @xput(i32 %1, i8 zeroext %38)
br label %47
45: ; preds = %34
%46 = tail call i32 @cput(i32 %1, i8 signext 7)
br label %47
47: ; preds = %45, %43, %41, %25, %23, %21
%48 = phi i8* [ %37, %41 ], [ %37, %43 ], [ %12, %45 ], [ %17, %21 ], [ %17, %23 ], [ %12, %25 ]
%49 = call i32 @read(i32 %0, i8* %5, i32 1)
%50 = icmp slt i32 %49, 1
br i1 %50, label %51, label %11
51: ; preds = %47, %4, %29, %27
%52 = phi i32 [ %33, %29 ], [ -1, %27 ], [ -1, %4 ], [ -1, %47 ]
ret i32 %52
}