#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 */
scalar_t__ EAGAIN;
int /*<<< orphan*/ MSG_PEEK;
scalar_t__ errno;
int /*<<< orphan*/ printf(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 (-1 == n && errno == EAGAIN) {
      printf("never be here?\n");
      continue;
    } else 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';
  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    1255 <get_line+0x135>
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
mov    $0xffffffff,%eax
cmp    -0x20(%rbp),%eax
jne    11bf <get_line+0x9f>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <get_line+0x9f>
lea    0xe4b(%rip),%rdi
call   1040 <printf@plt>
jmp    113d <get_line+0x1d>
cmpl   $0x0,-0x20(%rbp)
jle    1247 <get_line+0x127>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
jne    122b <get_line+0x10b>
mov    -0x4(%rbp),%edi
mov    0x2e08(%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    1222 <get_line+0x102>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    1222 <get_line+0x102>
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
jmp    1226 <get_line+0x106>
movb   $0xa,-0x19(%rbp)
jmp    122b <get_line+0x10b>
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    124b <get_line+0x12b>
movb   $0xa,-0x19(%rbp)
jmp    1250 <get_line+0x130>
jmp    113d <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
sub    $0x28,%rsp
movb   $0x0,0xf(%rsp)
xor    %r13d,%r13d
cmp    $0x2,%edx
jl     1223 <get_line+0x103>
mov    %edi,%ebx
add    $0xffffffff,%edx
xor    %r13d,%r13d
lea    0xf(%rsp),%rbp
mov    0x2e85(%rip),%r14
mov    0x2e6e(%rip),%r15
lea    0xe9f(%rip),%r12
mov    %rsi,0x10(%rsp)
mov    %edx,0x1c(%rsp)
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    11a3 <get_line+0x83>
mov    (%r14),%rax
cmp    (%r15),%rax
jne    121e <get_line+0xfe>
mov    %r12,%rdi
call   1040 <puts@plt>
cmpb   $0xa,0xf(%rsp)
jne    1170 <get_line+0x50>
jmp    121e <get_line+0xfe>
test   %eax,%eax
mov    0x10(%rsp),%rsi
mov    0x1c(%rsp),%edx
jle    1223 <get_line+0x103>
movzbl 0xf(%rsp),%eax
mov    %r13d,%ecx
cmp    $0xd,%al
jne    1208 <get_line+0xe8>
mov    %rcx,0x20(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <recv@plt>
test   %eax,%eax
jle    1239 <get_line+0x119>
cmpb   $0xa,0xf(%rsp)
jne    1239 <get_line+0x119>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
movzbl 0xf(%rsp),%eax
mov    0x10(%rsp),%rsi
mov    0x1c(%rsp),%edx
mov    0x20(%rsp),%rcx
mov    %al,(%rsi,%rcx,1)
add    $0x1,%r13d
cmp    %edx,%r13d
jge    1223 <get_line+0x103>
cmp    $0xa,%al
jne    1170 <get_line+0x50>
jmp    1223 <get_line+0x103>
mov    0x10(%rsp),%rsi
movslq %r13d,%rax
movb   $0x0,(%rsi,%rax,1)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rsp),%rsi
mov    0x20(%rsp),%rax
movb   $0xa,(%rsi,%rax,1)
add    $0x1,%r13d
jmp    1223 <get_line+0x103>

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [16 x i8] c"never be here?\0A\00"
@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 = %64, %30, %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 %65
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 eq i32 -1, %24
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i64, i64* @errno
%28 = load i64, i64* @EAGAIN
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %10
32: ; preds = %26, %21
%33 = load i32, i32* %9
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %62
35: ; preds = %32
%36 = load i8, i8* %8
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 13
br i1 %38, label %39, label %54
39: ; preds = %35
%40 = load i32, i32* %4
%41 = load i32, i32* @MSG_PEEK
%42 = call i32 @recv(i32 %40, i8* %8, i32 1, i32 %41)
store i32 %42, i32* %9
%43 = load i32, i32* %9
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %39
%46 = load i8, i8* %8
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 10
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i32, i32* %4
%51 = call i32 @recv(i32 %50, i8* %8, i32 1, i32 0)
br label %53
52: ; preds = %45, %39
store i8 10, i8* %8
br label %53
53: ; preds = %52, %49
br label %54
54: ; preds = %53, %35
%55 = load i8, i8* %8
%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 %55, i8* %59
%60 = load i32, i32* %7
%61 = add nsw i32 %60, 1
store i32 %61, i32* %7
br label %63
62: ; preds = %32
store i8 10, i8* %8
br label %63
63: ; preds = %62, %54
br label %64
64: ; preds = %63
br label %10
65: ; preds = %19
%66 = load i8*, i8** %5
%67 = load i32, i32* %7
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
store i8 0, i8* %69
%70 = load i32, i32* %7
ret i32 %70
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@MSG_PEEK = global i32 0
@str = [15 x i8] c"never be here?\00"
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 %47
7: ; preds = %3, %39
%8 = phi i32 [ %43, %39 ], [ 0, %3 ]
br label %9
9: ; preds = %7, %16
%10 = call i32 @recv(i32 %0, i8* %4, i32 1, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EAGAIN
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %47
16: ; preds = %12
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%18 = load i8, i8* %4
%19 = icmp eq i8 %18, 10
br i1 %19, label %47, label %9
20: ; preds = %9
%21 = icmp sgt i32 %10, 0
br i1 %21, label %22, label %47
22: ; preds = %20
%23 = load i8, i8* %4
%24 = icmp eq i8 %23, 13
br i1 %24, label %25, label %39
25: ; preds = %22
%26 = load i32, i32* @MSG_PEEK
%27 = call i32 @recv(i32 %0, i8* %4, i32 1, i32 %26)
%28 = icmp sgt i32 %27, 0
%29 = load i8, i8* %4
%30 = icmp eq i8 %29, 10
%31 = select i1 %28, i1 %30, i1 false
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = call i32 @recv(i32 %0, i8* %4, i32 1, i32 0)
%34 = load i8, i8* %4
br label %39
35: ; preds = %25
%36 = zext i32 %8 to i64
%37 = getelementptr inbounds i8, i8* %1, i64 %36
store i8 10, i8* %37
%38 = add nuw nsw i32 %8, 1
br label %47
39: ; preds = %22, %32
%40 = phi i8 [ %34, %32 ], [ %23, %22 ]
%41 = zext i32 %8 to i64
%42 = getelementptr inbounds i8, i8* %1, i64 %41
store i8 %40, i8* %42
%43 = add nuw nsw i32 %8, 1
%44 = icmp slt i32 %43, %5
%45 = icmp ne i8 %40, 10
%46 = select i1 %44, i1 %45, i1 false
br i1 %46, label %7, label %47
47: ; preds = %39, %20, %16, %12, %35, %3
%48 = phi i32 [ 0, %3 ], [ %38, %35 ], [ %8, %12 ], [ %8, %16 ], [ %8, %20 ], [ %43, %39 ]
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %1, i64 %49
store i8 0, i8* %50
ret i32 %48
}