#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 int /*<<< orphan*/ c;

/* Variables and functions */
scalar_t__ EINTR;
size_t EOF;
scalar_t__ errno;
size_t read(int, char *, int);
size_t sreadl(int sockfd, void *buf) {
  char *line = buf;
  size_t index = 0;
  char c;
  for (;;) {
    size_t s = read(sockfd, &c, sizeof(c));
    if (s == -1)
      if (errno == EINTR)
        continue;
      else
        return -1;
    if (s == EOF)
      break;
    if (c == '\r') {
      read(sockfd, &c, sizeof(c));
      if (c == '\n')
        break;
    }
    *line++ = c;
    index++;
  }
  *line++ = '\0';
  index++;
  return index;
}


x86 assembly (clang, -O0):
<sreadl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0xffffffffffffffff,-0x38(%rbp)
jne    117b <sreadl+0x6b>
mov    0x2e82(%rip),%rax
mov    (%rax),%rax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%rax
jne    116e <sreadl+0x5e>
jmp    112f <sreadl+0x1f>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1214 <sreadl+0x104>
mov    -0x38(%rbp),%rax
mov    0x2e62(%rip),%rcx
cmp    (%rcx),%rax
jne    1194 <sreadl+0x84>
jmp    11ee <sreadl+0xde>
movsbl -0x29(%rbp),%eax
cmp    $0xd,%eax
jne    11c9 <sreadl+0xb9>
mov    -0xc(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    11c4 <sreadl+0xb4>
jmp    11ee <sreadl+0xde>
jmp    11c9 <sreadl+0xb9>
mov    -0x29(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    112f <sreadl+0x1f>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sreadl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x1,%r15d
lea    0x7(%rsp),%rbx
mov    0x2ea6(%rip),%r12
mov    0x2e97(%rip),%r13
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffffffffffff,%rax
jne    1161 <sreadl+0x51>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1140 <sreadl+0x30>
jmp    119b <sreadl+0x8b>
mov    0x2e80(%rip),%rcx
cmp    (%rcx),%rax
je     11b4 <sreadl+0xa4>
movzbl 0x7(%rsp),%eax
cmp    $0xd,%al
jne    118e <sreadl+0x7e>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
je     11b4 <sreadl+0xa4>
mov    %al,(%r14)
add    $0x1,%r14
add    $0x1,%r15
jmp    1140 <sreadl+0x30>
mov    $0xffffffffffffffff,%r15
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x0,(%r14)
jmp    11a2 <sreadl+0x92>

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

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