#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 */
scalar_t__ EINTR;
scalar_t__ errno;
size_t read(int, char *, int);
size_t socket_readline(int sockd, void *vptr, size_t maxlen) {
  size_t n, rc;
  char c, *buffer;
  buffer = vptr;
  for (n = 1; n < maxlen; n++) {
    if ((rc = read(sockd, &c, 1)) == 1) {
      if (c == '\n' || c == '\r')
        break;
      *buffer++ = c;
    } else if (rc == 0) {
      if (n == 1)
        return 0;
      else
        break;
    } else {
      if (errno == EINTR)
        continue;
      return -1;
    }
  }
  *buffer = 0;
  return n;
}


x86 assembly (clang, -O0):
<socket_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
movq   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1202 <socket_readline+0xf2>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x1,%rax
jne    1198 <socket_readline+0x88>
movsbl -0x31(%rbp),%eax
cmp    $0xa,%eax
je     117a <socket_readline+0x6a>
movsbl -0x31(%rbp),%eax
cmp    $0xd,%eax
jne    117f <socket_readline+0x6f>
jmp    1202 <socket_readline+0xf2>
mov    -0x31(%rbp),%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
jmp    11ec <socket_readline+0xdc>
cmpq   $0x0,-0x30(%rbp)
jne    11c0 <socket_readline+0xb0>
cmpq   $0x1,-0x28(%rbp)
jne    11bb <socket_readline+0xab>
movq   $0x0,-0x8(%rbp)
jmp    1211 <socket_readline+0x101>
jmp    1202 <socket_readline+0xf2>
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
mov    0x2e07(%rip),%rcx
cmp    (%rcx),%rax
jne    11df <socket_readline+0xcf>
jmp    11f1 <socket_readline+0xe1>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1211 <socket_readline+0x101>
jmp    11f1 <socket_readline+0xe1>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1133 <socket_readline+0x23>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    $0x1,%ebx
cmp    $0x2,%rdx
jb     11b5 <socket_readline+0xa5>
mov    %rdx,%r15
mov    %edi,%ebp
mov    $0x1,%ebx
lea    0x7(%rsp),%r12
mov    0x2e95(%rip),%r13
jmp    1159 <socket_readline+0x49>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rbx
cmp    %rbx,%r15
je     11a6 <socket_readline+0x96>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%rax
je     1190 <socket_readline+0x80>
test   %rax,%rax
je     11ab <socket_readline+0x9b>
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     1150 <socket_readline+0x40>
jmp    11cb <socket_readline+0xbb>
cs nopw 0x0(%rax,%rax,1)
nop
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
je     11b5 <socket_readline+0xa5>
cmp    $0xd,%al
je     11b5 <socket_readline+0xa5>
mov    %al,(%r14)
add    $0x1,%r14
jmp    1150 <socket_readline+0x40>
mov    %r15,%rbx
jmp    11b5 <socket_readline+0xa5>
cmp    $0x1,%rbx
jne    11b5 <socket_readline+0xa5>
xor    %ebx,%ebx
jmp    11b9 <socket_readline+0xa9>
movb   $0x0,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffffffffffff,%rbx
jmp    11b9 <socket_readline+0xa9>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i64 @socket_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*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = load i8*, i8** %6
store i8* %12, i8** %11
store i64 1, i64* %8
br label %13
13: ; preds = %49, %3
%14 = load i64, i64* %8
%15 = load i64, i64* %7
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %52
17: ; preds = %13
%18 = load i32, i32* %5
%19 = call i64 @read(i32 %18, i8* %10, i32 1)
store i64 %19, i64* %9
%20 = icmp eq i64 %19, 1
br i1 %20, label %21, label %34
21: ; preds = %17
%22 = load i8, i8* %10
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 10
br i1 %24, label %29, label %25
25: ; preds = %21
%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 = %25, %21
br label %52
30: ; preds = %25
%31 = load i8, i8* %10
%32 = load i8*, i8** %11
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %11
store i8 %31, i8* %32
br label %48
34: ; preds = %17
%35 = load i64, i64* %9
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = load i64, i64* %8
%39 = icmp eq i64 %38, 1
br i1 %39, label %40, label %41
40: ; preds = %37
store i64 0, i64* %4
br label %55
41: ; preds = %37
br label %52
42: ; preds = %34
%43 = load i64, i64* @errno
%44 = load i64, i64* @EINTR
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %42
br label %49
47: ; preds = %42
store i64 -1, i64* %4
br label %55
48: ; preds = %30
br label %49
49: ; preds = %48, %46
%50 = load i64, i64* %8
%51 = add i64 %50, 1
store i64 %51, i64* %8
br label %13
52: ; preds = %41, %29, %13
%53 = load i8*, i8** %11
store i8 0, i8* %53
%54 = load i64, i64* %8
store i64 %54, i64* %4
br label %55
55: ; preds = %52, %47, %40
%56 = load i64, i64* %4
ret i64 %56
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i64 @socket_readline(i32 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = alloca i8
%5 = icmp ugt i64 %2, 1
br i1 %5, label %6, label %24
6: ; preds = %3, %20
%7 = phi i8* [ %21, %20 ], [ %1, %3 ]
%8 = phi i64 [ %22, %20 ], [ 1, %3 ]
%9 = call i64 @read(i32 %0, i8* %4, i32 1)
switch i64 %9, label %16 [
i64 1, label %10
i64 0, label %14
]
10: ; preds = %6
%11 = load i8, i8* %4
switch i8 %11, label %12 [
i8 13, label %24
i8 10, label %24
]
12: ; preds = %10
%13 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %11, i8* %7
br label %20
14: ; preds = %6
%15 = icmp eq i64 %8, 1
br i1 %15, label %27, label %24
16: ; preds = %6
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %27
20: ; preds = %16, %12
%21 = phi i8* [ %13, %12 ], [ %7, %16 ]
%22 = add nuw i64 %8, 1
%23 = icmp eq i64 %22, %2
br i1 %23, label %24, label %6
24: ; preds = %20, %10, %10, %3, %14
%25 = phi i64 [ %8, %14 ], [ 1, %3 ], [ %2, %20 ], [ %8, %10 ], [ %8, %10 ]
%26 = phi i8* [ %7, %14 ], [ %1, %3 ], [ %21, %20 ], [ %7, %10 ], [ %7, %10 ]
store i8 0, i8* %26
br label %27
27: ; preds = %16, %14, %24
%28 = phi i64 [ %25, %24 ], [ 0, %14 ], [ -1, %16 ]
ret i64 %28
}