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


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x21(%rbp)
jge    1150 <get_line+0x40>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1160 <get_line+0x50>
jmp    1211 <get_line+0x101>
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    1208 <get_line+0xf8>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
jne    11ec <get_line+0xdc>
mov    -0x4(%rbp),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%ecx
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11e3 <get_line+0xd3>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    11e3 <get_line+0xd3>
mov    -0x4(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%ecx
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
jmp    11e7 <get_line+0xd7>
movb   $0xa,-0x19(%rbp)
jmp    11ec <get_line+0xdc>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    120c <get_line+0xfc>
movb   $0xa,-0x19(%rbp)
jmp    112d <get_line+0x1d>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
movb   $0x0,0x7(%rsp)
xor    %ebp,%ebp
cmp    $0x2,%edx
jl     11bf <get_line+0xaf>
mov    %edx,%r12d
mov    %edi,%ebx
add    $0xffffffff,%r12d
xor    %ebp,%ebp
lea    0x7(%rsp),%r15
mov    0x2ea3(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    11bf <get_line+0xaf>
movzbl 0x7(%rsp),%eax
cmp    $0xd,%al
jne    11a4 <get_line+0x94>
mov    0x0(%r13),%ecx
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <recv@plt>
test   %eax,%eax
jle    11b7 <get_line+0xa7>
cmpb   $0xa,0x7(%rsp)
jne    11b7 <get_line+0xa7>
mov    0x0(%r13),%ecx
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <recv@plt>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmp    %r12d,%ebp
jge    11bf <get_line+0xaf>
cmp    $0xa,%al
jne    1150 <get_line+0x40>
jmp    11bf <get_line+0xaf>
movb   $0xa,(%r14,%rbp,1)
add    $0x1,%ebp
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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