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

/* Variables and functions */
scalar_t__ sock_readbyte(int, char *);
ssize_t sock_readline(int sock, char *line_buf, size_t len) {
  ssize_t n;
  size_t n_read = 0;
  char byte = '\0';
  while (n_read++ <= len) {
    n = sock_readbyte(sock, &byte);
    if (n <= 0) {
      *line_buf = '\0';
      if (n == 0)
        return n_read - 1;
      return n;
    }
    *line_buf++ = byte;
    if (byte == '\n' && *(line_buf - 2) == '\r')
      break;
  }
  *line_buf = '\0';
  return n_read;
}


x86 assembly (clang, -O0):
<sock_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
movb   $0x0,-0x31(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
cmp    -0x20(%rbp),%rax
ja     11cf <sock_readline+0xbf>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
call   1030 <sock_readbyte@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jg     1193 <sock_readline+0x83>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
cmpq   $0x0,-0x28(%rbp)
jne    1186 <sock_readline+0x76>
mov    -0x30(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11de <sock_readline+0xce>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11de <sock_readline+0xce>
mov    -0x31(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
movsbl -0x31(%rbp),%eax
cmp    $0xa,%eax
jne    11ca <sock_readline+0xba>
mov    -0x18(%rbp),%rax
movsbl -0x2(%rax),%eax
cmp    $0xd,%eax
jne    11ca <sock_readline+0xba>
jmp    11cf <sock_readline+0xbf>
jmp    112f <sock_readline+0x1f>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_readline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%ebp
movb   $0x0,0xf(%rsp)
lea    0xf(%rsp),%rsi
call   1030 <sock_readbyte@plt>
test   %rax,%rax
jle    1175 <sock_readline+0x65>
xor    %ecx,%ecx
lea    0xf(%rsp),%r12
nop
movzbl 0xf(%rsp),%eax
mov    %al,(%r14,%rcx,1)
cmp    $0xa,%al
jne    1155 <sock_readline+0x45>
cmpb   $0xd,-0x1(%r14,%rcx,1)
je     118a <sock_readline+0x7a>
lea    0x1(%rcx),%rbx
cmp    %r15,%rbx
ja     1184 <sock_readline+0x74>
mov    %ebp,%edi
mov    %r12,%rsi
call   1030 <sock_readbyte@plt>
mov    %rbx,%rcx
test   %rax,%rax
jg     1140 <sock_readline+0x30>
add    %rbx,%r14
jmp    1177 <sock_readline+0x67>
xor    %ebx,%ebx
movb   $0x0,(%r14)
test   %rax,%rax
cmovne %rax,%rbx
jmp    1194 <sock_readline+0x84>
lea    0x2(%rcx),%rbx
jmp    118e <sock_readline+0x7e>
lea    0x1(%rcx),%rbx
movb   $0x0,0x1(%r14,%rcx,1)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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