#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*/ printf(char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int get_line(int sock, char *buf, int size) {
  int i = 0;
  char c = '\0';
  int n;
  while ((i < size - 1) && (c != '\n')) {
    n = recv(sock, &c, 1, 0);
    if (n > 0) {
      if (c == '\r') {
        n = recv(sock, &c, 1, MSG_PEEK);
        if ((n > 0) && (c == '\n'))
          recv(sock, &c, 1, 0);
        else
          c = '\n';
      }
      buf[i] = c;
      i++;
    } else
      c = '\n';
  }
  buf[i] = '\0';
  printf("Get:  %s \n", buf);
  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    1160 <get_line+0x40>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1170 <get_line+0x50>
jmp    1217 <get_line+0xf7>
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    120e <get_line+0xee>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
jne    11f2 <get_line+0xd2>
mov    -0x4(%rbp),%edi
mov    0x2e41(%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    11e9 <get_line+0xc9>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    11e9 <get_line+0xc9>
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
jmp    11ed <get_line+0xcd>
movb   $0xa,-0x19(%rbp)
jmp    11f2 <get_line+0xd2>
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    1212 <get_line+0xf2>
movb   $0xa,-0x19(%rbp)
jmp    113d <get_line+0x1d>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rsi
lea    0xdd2(%rip),%rdi
call   1040 <printf@plt>
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     11d9 <get_line+0xb9>
mov    %edx,%r12d
mov    %edi,%ebx
add    $0xffffffff,%r12d
xor    %ebp,%ebp
lea    0x7(%rsp),%r15
mov    0x2e93(%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    11c5 <get_line+0xa5>
movzbl 0x7(%rsp),%eax
cmp    $0xd,%al
jne    11b2 <get_line+0x92>
mov    0x0(%r13),%ecx
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <recv@plt>
test   %eax,%eax
jle    11cc <get_line+0xac>
cmpb   $0xa,0x7(%rsp)
jne    11cc <get_line+0xac>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmp    %r12d,%ebp
jge    11d9 <get_line+0xb9>
cmp    $0xa,%al
jne    1160 <get_line+0x40>
jmp    11d9 <get_line+0xb9>
movb   $0xa,0x7(%rsp)
jmp    11d9 <get_line+0xb9>
movb   $0xa,0x7(%rsp)
movb   $0xa,(%r14,%rbp,1)
add    $0x1,%ebp
movslq %ebp,%rbx
movb   $0x0,(%r14,%rbx,1)
lea    0xe18(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_PEEK = global i32 0
@.str = [11 x i8] c"Get: %s \0A\00"
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 = %54, %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 %55
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 %53
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 %45
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 %43
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 %43
40: ; preds = %36
%41 = load i32, i32* %4
%42 = call i32 @recv(i32 %41, i8* %8, i32 1, i32 0)
br label %44
43: ; preds = %36, %30
store i8 10, i8* %8
br label %44
44: ; preds = %43, %40
br label %45
45: ; preds = %44, %26
%46 = load i8, i8* %8
%47 = load i8*, i8** %5
%48 = load i32, i32* %7
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 %46, i8* %50
%51 = load i32, i32* %7
%52 = add nsw i32 %51, 1
store i32 %52, i32* %7
br label %54
53: ; preds = %21
store i8 10, i8* %8
br label %54
54: ; preds = %53, %45
br label %10
55: ; preds = %19
%56 = load i8*, i8** %5
%57 = load i32, i32* %7
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 0, i8* %59
%60 = load i8*, i8** %5
%61 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %60)
%62 = load i32, i32* %7
ret i32 %62
}

LLVM IR (-O2):
@MSG_PEEK = global i32 0
@.str = [11 x i8] c"Get: %s \0A\00"
define i32 @get_line(i32 %0, i8* %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 %28
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 %24
21: ; preds = %14
%22 = call i32 @recv(i32 %0, i8* %4, i32 1, i32 0)
%23 = load i8, i8* %4
br label %29
24: ; preds = %14
store i8 10, i8* %4
%25 = zext i32 %8 to i64
%26 = getelementptr inbounds i8, i8* %1, i64 %25
store i8 10, i8* %26
%27 = add nuw nsw i32 %8, 1
br label %37
28: ; preds = %7
store i8 10, i8* %4
br label %37
29: ; preds = %11, %21
%30 = phi i8 [ %23, %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, %28, %24, %3
%38 = phi i32 [ 0, %3 ], [ %8, %28 ], [ %27, %24 ], [ %33, %29 ]
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %1, i64 %39
store i8 0, i8* %40
%41 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %1)
ret i32 %38
}