#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 */
int /*<<< orphan*/ MSG_PEEK;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
size_t strlen(char *);
size_t get_line(int sockfd, char *buf, size_t len) {
  memset(buf, 0, len);
  char c = '\0';
  for (int i = 0; i < len; i++) {
    if (recv(sockfd, &c, 1, 0) > 0) {
      if (c != '\n' && c != '\r') {
        buf[i] = c;
      } else if (c == '\r' && recv(sockfd, &c, 1, MSG_PEEK) > 0 && c == '\n') {
        recv(sockfd, &c, 1, 0);
        break;
      } else {
        break;
      }
    } else {
      break;
    }
  }
  return strlen(buf);
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
movb   $0x0,-0x19(%rbp)
movl   $0x0,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    122d <get_line+0xfd>
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jle    1215 <get_line+0xe5>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
je     11b5 <get_line+0x85>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
je     11b5 <get_line+0x85>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1210 <get_line+0xe0>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
jne    120b <get_line+0xdb>
mov    -0x4(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%ecx
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <recv@plt>
cmp    $0x0,%rax
jle    120b <get_line+0xdb>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    120b <get_line+0xdb>
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
jmp    122d <get_line+0xfd>
jmp    122d <get_line+0xfd>
jmp    121a <get_line+0xea>
jmp    122d <get_line+0xfd>
jmp    121f <get_line+0xef>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    115d <get_line+0x2d>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbp
mov    %rsi,%r12
mov    %edi,%r14d
mov    %rsi,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
movb   $0x0,0xf(%rsp)
test   %rbp,%rbp
je     11ce <get_line+0x9e>
xor    %ebx,%ebx
lea    0xf(%rsp),%r15
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jle    11ce <get_line+0x9e>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
je     11ce <get_line+0x9e>
cmp    $0xd,%al
je     1193 <get_line+0x63>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1160 <get_line+0x30>
jmp    11ce <get_line+0x9e>
mov    0x2e4e(%rip),%rax
mov    (%rax),%ecx
lea    0xf(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1030 <recv@plt>
test   %rax,%rax
jle    11ce <get_line+0x9e>
cmpb   $0xa,0xf(%rsp)
jne    11ce <get_line+0x9e>
lea    0xf(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@MSG_PEEK = global i32 0
define i64 @get_line(i32 %0, i8* %1, i64 %2) {
%4 = alloca i8
%5 = tail call i32 @memset(i8* %1, i32 0, i64 %2)
store i8 0, i8* %4
%6 = icmp eq i64 %2, 0
br i1 %6, label %26, label %7
7: ; preds = %3, %13
%8 = phi i64 [ %15, %13 ], [ 0, %3 ]
%9 = call i64 @recv(i32 %0, i8* %4, i32 1, i32 0)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %26
11: ; preds = %7
%12 = load i8, i8* %4
switch i8 %12, label %13 [
i8 13, label %17
i8 10, label %26
]
13: ; preds = %11
%14 = getelementptr inbounds i8, i8* %1, i64 %8
store i8 %12, i8* %14
%15 = add nuw i64 %8, 1
%16 = icmp eq i64 %15, %2
br i1 %16, label %26, label %7
17: ; preds = %11
%18 = load i32, i32* @MSG_PEEK
%19 = call i64 @recv(i32 %0, i8* %4, i32 1, i32 %18)
%20 = icmp sgt i64 %19, 0
%21 = load i8, i8* %4
%22 = icmp eq i8 %21, 10
%23 = select i1 %20, i1 %22, i1 false
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i64 @recv(i32 %0, i8* %4, i32 1, i32 0)
br label %26
26: ; preds = %13, %11, %7, %3, %17, %24
%27 = call i64 @strlen(i8* dereferenceable(1) %1)
ret i64 %27
}