#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 */
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ MSG_PEEK;
int recv(int, char *, int, int /*<<< orphan*/);
int32_t get_line(int32_t sock, char *buf, int32_t size) {
  int32_t i = 0;
  char c = '\0';
  int32_t 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';
  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    1207 <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    11fe <get_line+0xee>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
jne    11e2 <get_line+0xd2>
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    11d9 <get_line+0xc9>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    11d9 <get_line+0xc9>
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
jmp    11dd <get_line+0xcd>
movb   $0xa,-0x19(%rbp)
jmp    11e2 <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    1202 <get_line+0xf2>
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     11bd <get_line+0xad>
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    11bd <get_line+0xad>
movzbl 0x7(%rsp),%eax
cmp    $0xd,%al
jne    11a2 <get_line+0x92>
mov    0x0(%r13),%ecx
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <recv@plt>
test   %eax,%eax
jle    11b5 <get_line+0xa5>
cmpb   $0xa,0x7(%rsp)
jne    11b5 <get_line+0xa5>
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    11bd <get_line+0xad>
cmp    $0xa,%al
jne    1150 <get_line+0x40>
jmp    11bd <get_line+0xad>
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 = %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 i32, i32* %7
ret i32 %60
}

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 %36
7: ; preds = %3, %28
%8 = phi i32 [ %32, %28 ], [ 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 %36
11: ; preds = %7
%12 = load i8, i8* %4
%13 = icmp eq i8 %12, 13
br i1 %13, label %14, label %28
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 %28
24: ; preds = %14
%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 %36
28: ; preds = %11, %21
%29 = phi i8 [ %23, %21 ], [ %12, %11 ]
%30 = zext i32 %8 to i64
%31 = getelementptr inbounds i8, i8* %1, i64 %30
store i8 %29, i8* %31
%32 = add nuw nsw i32 %8, 1
%33 = icmp slt i32 %32, %5
%34 = icmp ne i8 %29, 10
%35 = select i1 %33, i1 %34, i1 false
br i1 %35, label %7, label %36
36: ; preds = %28, %7, %24, %3
%37 = phi i32 [ 0, %3 ], [ %27, %24 ], [ %8, %7 ], [ %32, %28 ]
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %1, i64 %38
store i8 0, i8* %39
ret i32 %37
}