#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 */
typedef size_t ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, int);
ssize_t readline(int sockfd, void *vptr, size_t maxlen) {
  ssize_t n, chars_read;
  char c, *buffer;
  int cr = 0;
  buffer = vptr;
  for (n = 1; n < maxlen; n++) {
    if ((chars_read = read(sockfd, &c, 1)) == 1) {
      *buffer++ = c;
      if (c == '\r') {
        cr = 1;
        continue;
      }
      if (cr == 1 && c == '\n') {
        if (n == 2) {
          return n;
        } else {
          break;
        }
      }
      if (c == '\n') {
        break;
      }
      if (cr == 1) {
        cr = 0;
      }
    } else if (chars_read == 0) {
      if (n == 1) {
        return 0;
      } else {
        break;
      }
    } else {
      if (errno == EINTR) {
        continue;
      }
      perror("readline");
      exit(EXIT_FAILURE);
    }
  }
  *buffer = 0;
  return n;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
movq   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1287 <readline+0x157>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x1,%rax
jne    1210 <readline+0xe0>
mov    -0x31(%rbp),%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
movsbl -0x31(%rbp),%eax
cmp    $0xd,%eax
jne    11b4 <readline+0x84>
movl   $0x1,-0x44(%rbp)
jmp    1276 <readline+0x146>
cmpl   $0x1,-0x44(%rbp)
jne    11e8 <readline+0xb8>
movsbl -0x31(%rbp),%eax
cmp    $0xa,%eax
jne    11e8 <readline+0xb8>
cmpq   $0x2,-0x28(%rbp)
jne    11e3 <readline+0xb3>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1296 <readline+0x166>
jmp    1287 <readline+0x157>
movsbl -0x31(%rbp),%eax
cmp    $0xa,%eax
jne    11fa <readline+0xca>
jmp    1287 <readline+0x157>
cmpl   $0x1,-0x44(%rbp)
jne    120b <readline+0xdb>
movl   $0x0,-0x44(%rbp)
jmp    1271 <readline+0x141>
cmpq   $0x0,-0x30(%rbp)
jne    1238 <readline+0x108>
cmpq   $0x1,-0x28(%rbp)
jne    1233 <readline+0x103>
movq   $0x0,-0x8(%rbp)
jmp    1296 <readline+0x166>
jmp    1287 <readline+0x157>
mov    0x2da1(%rip),%rax
mov    (%rax),%rax
mov    0x2d87(%rip),%rcx
cmp    (%rcx),%rax
jne    1257 <readline+0x127>
jmp    1276 <readline+0x146>
lea    0xda2(%rip),%rdi
call   1040 <perror@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    1276 <readline+0x146>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    115a <readline+0x2a>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    $0x1,%ebp
cmp    $0x2,%rdx
jb     120b <readline+0xdb>
mov    %rdx,%r14
mov    %edi,%r12d
mov    $0x1,%ebp
lea    0x7(%rsp),%r15
xor    %r13d,%r13d
jmp    117f <readline+0x4f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x1,%r13d
add    $0x1,%rbp
cmp    %rbp,%r14
je     11f1 <readline+0xc1>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%rax
je     11c0 <readline+0x90>
test   %rax,%rax
je     11f6 <readline+0xc6>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
je     1176 <readline+0x46>
jmp    1220 <readline+0xf0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x7(%rsp),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
movzbl 0x7(%rsp),%eax
cmp    $0xd,%al
je     1170 <readline+0x40>
cmp    $0x1,%r13d
jne    11de <readline+0xae>
cmp    $0xa,%al
je     1200 <readline+0xd0>
cmp    $0xa,%al
je     120b <readline+0xdb>
cmp    $0x1,%r13d
mov    $0x0,%eax
cmove  %eax,%r13d
jmp    1176 <readline+0x46>
mov    %r14,%rbp
jmp    120b <readline+0xdb>
cmp    $0x1,%rbp
jne    120b <readline+0xdb>
xor    %eax,%eax
jmp    1211 <readline+0xe1>
mov    $0x2,%eax
cmp    $0x2,%rbp
je     1211 <readline+0xe1>
movb   $0x0,(%rbx)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd9(%rip),%rdi
call   1040 <perror@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [9 x i8] c"readline\00"
@EXIT_FAILURE = global i32 0
define i64 @readline(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8
%11 = alloca i8*
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %12
%13 = load i8*, i8** %6
store i8* %13, i8** %11
store i64 1, i64* %8
br label %14
14: ; preds = %71, %3
%15 = load i64, i64* %8
%16 = load i64, i64* %7
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %74
18: ; preds = %14
%19 = load i32, i32* %5
%20 = call i64 @read(i32 %19, i8* %10, i32 1)
store i64 %20, i64* %9
%21 = icmp eq i64 %20, 1
br i1 %21, label %22, label %53
22: ; preds = %18
%23 = load i8, i8* %10
%24 = load i8*, i8** %11
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %11
store i8 %23, i8* %24
%26 = load i8, i8* %10
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 13
br i1 %28, label %29, label %30
29: ; preds = %22
store i32 1, i32* %12
br label %71
30: ; preds = %22
%31 = load i32, i32* %12
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %43
33: ; preds = %30
%34 = load i8, i8* %10
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 10
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = load i64, i64* %8
%39 = icmp eq i64 %38, 2
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = load i64, i64* %8
store i64 %41, i64* %4
br label %77
42: ; preds = %37
br label %74
43: ; preds = %33, %30
%44 = load i8, i8* %10
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 10
br i1 %46, label %47, label %48
47: ; preds = %43
br label %74
48: ; preds = %43
%49 = load i32, i32* %12
%50 = icmp eq i32 %49, 1
br i1 %50, label %51, label %52
51: ; preds = %48
store i32 0, i32* %12
br label %52
52: ; preds = %51, %48
br label %70
53: ; preds = %18
%54 = load i64, i64* %9
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %53
%57 = load i64, i64* %8
%58 = icmp eq i64 %57, 1
br i1 %58, label %59, label %60
59: ; preds = %56
store i64 0, i64* %4
br label %77
60: ; preds = %56
br label %74
61: ; preds = %53
%62 = load i64, i64* @errno
%63 = load i64, i64* @EINTR
%64 = icmp eq i64 %62, %63
br i1 %64, label %65, label %66
65: ; preds = %61
br label %71
66: ; preds = %61
%67 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%68 = load i32, i32* @EXIT_FAILURE
%69 = call i32 @exit(i32 %68)
unreachable
70: ; preds = %52
br label %71
71: ; preds = %70, %65, %29
%72 = load i64, i64* %8
%73 = add i64 %72, 1
store i64 %73, i64* %8
br label %14
74: ; preds = %60, %47, %42, %14
%75 = load i8*, i8** %11
store i8 0, i8* %75
%76 = load i64, i64* %8
store i64 %76, i64* %4
br label %77
77: ; preds = %74, %59, %40
%78 = load i64, i64* %4
ret i64 %78
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [9 x i8] c"readline\00"
@EXIT_FAILURE = global i32 0
define i64 @readline(i32 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = alloca i8
%5 = icmp ugt i64 %2, 1
br i1 %5, label %6, label %40
6: ; preds = %3, %35
%7 = phi i32 [ %37, %35 ], [ 0, %3 ]
%8 = phi i8* [ %36, %35 ], [ %1, %3 ]
%9 = phi i64 [ %38, %35 ], [ 1, %3 ]
%10 = call i64 @read(i32 %0, i8* %4, i32 1)
switch i64 %10, label %27 [
i64 1, label %11
i64 0, label %25
]
11: ; preds = %6
%12 = load i8, i8* %4
%13 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %12, i8* %8
%14 = load i8, i8* %4
%15 = icmp eq i8 %14, 13
br i1 %15, label %35, label %16
16: ; preds = %11
%17 = icmp eq i32 %7, 1
%18 = icmp eq i8 %14, 10
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = icmp eq i64 %9, 2
br i1 %21, label %43, label %40
22: ; preds = %16
br i1 %18, label %40, label %23
23: ; preds = %22
%24 = select i1 %17, i32 0, i32 %7
br label %35
25: ; preds = %6
%26 = icmp eq i64 %9, 1
br i1 %26, label %43, label %40
27: ; preds = %6
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %11, %27, %23
%36 = phi i8* [ %13, %23 ], [ %8, %27 ], [ %13, %11 ]
%37 = phi i32 [ %24, %23 ], [ %7, %27 ], [ 1, %11 ]
%38 = add nuw i64 %9, 1
%39 = icmp eq i64 %38, %2
br i1 %39, label %40, label %6
40: ; preds = %35, %22, %3, %25, %20
%41 = phi i64 [ %9, %20 ], [ %9, %25 ], [ 1, %3 ], [ %2, %35 ], [ %9, %22 ]
%42 = phi i8* [ %13, %20 ], [ %8, %25 ], [ %1, %3 ], [ %36, %35 ], [ %13, %22 ]
store i8 0, i8* %42
br label %43
43: ; preds = %25, %20, %40
%44 = phi i64 [ %41, %40 ], [ 2, %20 ], [ 0, %25 ]
ret i64 %44
}