#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 */
char EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int read_line(int sock, char *buffer) {
  size_t length = 0;
  while (1) {
    char data;
    int result = recv(sock, &data, 1, 0);
    if ((result <= 0) || (data == EOF)) {
      perror("Connection closed");
      exit(1);
    }
    buffer[length] = data;
    length++;
    if (length >= 2 && buffer[length - 2] == '\r' &&
        buffer[length - 1] == '\n') {
      buffer[length - 2] = '\0';
      return length;
    }
  }
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    117d <read_line+0x4d>
movsbl -0x19(%rbp),%eax
mov    0x2e76(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1193 <read_line+0x63>
lea    0xe7c(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x2,-0x18(%rbp)
jb     1204 <read_line+0xd4>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x2,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    1204 <read_line+0xd4>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1204 <read_line+0xd4>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x2,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
jmp    1147 <read_line+0x17>

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    11be <read_line+0x8e>
mov    $0x1,%ebp
mov    0x2e85(%rip),%r12
lea    0xf(%rsp),%r15
jmp    118a <read_line+0x5a>
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
add    $0x1,%rbp
test   %eax,%eax
jle    11be <read_line+0x8e>
movzbl 0xf(%rsp),%eax
cmp    (%r12),%al
je     11be <read_line+0x8e>
mov    %al,-0x1(%rbx,%rbp,1)
cmp    $0x2,%rbp
jb     1170 <read_line+0x40>
cmpb   $0xd,-0x2(%rbx,%rbp,1)
jne    1170 <read_line+0x40>
cmp    $0xa,%al
jne    1170 <read_line+0x40>
movb   $0x0,-0x2(%rbx,%rbp,1)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3b(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@EOF = global i8 0
@.str = [18 x i8] c"Connection closed\00"
define i32 @read_line(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i64 0, i64* %5
br label %8
8: ; preds = %2, %54
%9 = load i32, i32* %3
%10 = call i32 @recv(i32 %9, i8* %6, i32 1, i32 0)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp sle i32 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %8
%14 = load i8, i8* %6
%15 = sext i8 %14 to i32
%16 = load i8, i8* @EOF
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %15, %17
br i1 %18, label %19, label %22
19: ; preds = %13, %8
%20 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = load i8, i8* %6
%24 = load i8*, i8** %4
%25 = load i64, i64* %5
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 %23, i8* %26
%27 = load i64, i64* %5
%28 = add i64 %27, 1
store i64 %28, i64* %5
%29 = load i64, i64* %5
%30 = icmp uge i64 %29, 2
br i1 %30, label %31, label %54
31: ; preds = %22
%32 = load i8*, i8** %4
%33 = load i64, i64* %5
%34 = sub i64 %33, 2
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 13
br i1 %38, label %39, label %54
39: ; preds = %31
%40 = load i8*, i8** %4
%41 = load i64, i64* %5
%42 = sub i64 %41, 1
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 10
br i1 %46, label %47, label %54
47: ; preds = %39
%48 = load i8*, i8** %4
%49 = load i64, i64* %5
%50 = sub i64 %49, 2
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
%52 = load i64, i64* %5
%53 = trunc i64 %52 to i32
ret i32 %53
54: ; preds = %39, %31, %22
br label %8
}

LLVM IR (-O2):
@EOF = global i8 0
@.str = [18 x i8] c"Connection closed\00"
define i32 @read_line(i32 %0, i8* nocapture %1) {
%3 = alloca i8
%4 = call i32 @recv(i32 %0, i8* %3, i32 1, i32 0)
%5 = icmp slt i32 %4, 1
br i1 %5, label %11, label %6
6: ; preds = %2, %25
%7 = phi i64 [ %16, %25 ], [ 0, %2 ]
%8 = load i8, i8* %3
%9 = load i8, i8* @EOF
%10 = icmp eq i8 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %25, %6, %2
%12 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %6
%15 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 %8, i8* %15
%16 = add i64 %7, 1
%17 = icmp ugt i64 %16, 1
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = add i64 %7, -1
%20 = getelementptr inbounds i8, i8* %1, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 13
%23 = icmp eq i8 %8, 10
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %28, label %25
25: ; preds = %14, %18
%26 = call i32 @recv(i32 %0, i8* %3, i32 1, i32 0)
%27 = icmp slt i32 %26, 1
br i1 %27, label %11, label %6
28: ; preds = %18
%29 = getelementptr inbounds i8, i8* %1, i64 %19
store i8 0, i8* %29
%30 = trunc i64 %16 to i32
ret i32 %30
}