#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ errno;
int recv(int const, char *, int, int /*<<< orphan*/);
int ReadLine(const int sock, char *const buffer, const size_t buflen,
             size_t *const bytesRead) {
  int done = 0, retval = 0;
  char c, lastC = 0;
  size_t bytesSoFar = 0;
  ssize_t readResult;
  do {
    readResult = recv(sock, &c, 1, 0);
    switch (readResult) {
    case -1:
      if (errno != EINTR) {
        retval = -1;
        done = 1;
      }
      break;
    case 0:
      retval = 0;
      done = 1;
      break;
    case 1:
      buffer[bytesSoFar] = c;
      bytesSoFar += readResult;
      if (bytesSoFar >= buflen) {
        done = 1;
        retval = -1;
      }
      if ((c == '\n') && (lastC == '\r'))
        done = 1;
      lastC = c;
      break;
    }
  } while (!done);
  buffer[bytesSoFar] = 0;
  *bytesRead = bytesSoFar;
  return retval;
}


x86 assembly (clang, -O0):
<ReadLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movb   $0x0,-0x2a(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
sub    $0xffffffff,%eax
je     118c <ReadLine+0x7c>
jmp    116b <ReadLine+0x5b>
mov    -0x40(%rbp),%eax
test   %eax,%eax
je     11b7 <ReadLine+0xa7>
jmp    117b <ReadLine+0x6b>
mov    -0x40(%rbp),%eax
sub    $0x1,%eax
je     11ca <ReadLine+0xba>
jmp    1227 <ReadLine+0x117>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%eax
je     11b2 <ReadLine+0xa2>
movl   $0xffffffff,-0x28(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1227 <ReadLine+0x117>
movl   $0x0,-0x28(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1227 <ReadLine+0x117>
mov    -0x29(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movslq -0x3c(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     1200 <ReadLine+0xf0>
movl   $0x1,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    1221 <ReadLine+0x111>
movsbl -0x2a(%rbp),%eax
cmp    $0xd,%eax
jne    1221 <ReadLine+0x111>
movl   $0x1,-0x24(%rbp)
mov    -0x29(%rbp),%al
mov    %al,-0x2a(%rbp)
jmp    122c <ReadLine+0x11c>
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1141 <ReadLine+0x31>
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadLine>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x8(%rsp)
mov    %rdx,0x10(%rsp)
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
lea    0x7(%rsp),%r13
xor    %r12d,%r12d
xor    %r14d,%r14d
jmp    1160 <ReadLine+0x50>
nopl   0x0(%rax)
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
mov    0x2e88(%rip),%rcx
cmp    (%rcx),%eax
jne    11bb <ReadLine+0xab>
mov    %r12d,%eax
xor    %ecx,%ecx
mov    %eax,%r12d
test   %ecx,%ecx
jne    11c4 <ReadLine+0xb4>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1140 <ReadLine+0x30>
cmp    $0x1,%eax
je     1190 <ReadLine+0x80>
test   %eax,%eax
jne    1154 <ReadLine+0x44>
jmp    11c2 <ReadLine+0xb2>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x7(%rsp),%eax
mov    %al,(%r15,%r14,1)
add    $0x1,%r14
xor    %ecx,%ecx
cmp    0x10(%rsp),%r14
setae  %cl
mov    $0xffffffff,%edx
cmovae %edx,%ebx
cmp    $0xa,%al
jne    1159 <ReadLine+0x49>
cmp    $0xd,%r12b
jne    1159 <ReadLine+0x49>
jmp    11c4 <ReadLine+0xb4>
mov    $0xffffffff,%ebx
jmp    11c4 <ReadLine+0xb4>
mov    %eax,%ebx
movb   $0x0,(%r15,%r14,1)
mov    0x8(%rsp),%rax
mov    %r14,(%rax)
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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