#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;
char read(int, char *, int);
char recv(int, char *, int, int /*<<< orphan*/);
int read_line(int fd, char *buffer, int size) {
  char next = '\0';
  char err;
  int i = 0;
  while ((i < (size - 1)) && (next != '\n')) {
    err = read(fd, &next, 1);
    if (err <= 0)
      break;
    if (next == '\r') {
      err = recv(fd, &next, 1, MSG_PEEK);
      if (err > 0 && next == '\n') {
        read(fd, &next, 1);
      } else {
        next = '\n';
      }
    }
    buffer[i] = next;
    i++;
  }
  buffer[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movb   $0x0,-0x15(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x1d(%rbp)
jge    1160 <read_line+0x40>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1170 <read_line+0x50>
jmp    1215 <read_line+0xf5>
mov    -0x4(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %al,-0x16(%rbp)
movsbl -0x16(%rbp),%eax
cmp    $0x0,%eax
jg     1196 <read_line+0x76>
jmp    1215 <read_line+0xf5>
movsbl -0x15(%rbp),%eax
cmp    $0xd,%eax
jne    11f9 <read_line+0xd9>
mov    -0x4(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <recv@plt>
mov    %al,-0x16(%rbp)
movsbl -0x16(%rbp),%eax
cmp    $0x0,%eax
jle    11f0 <read_line+0xd0>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    11f0 <read_line+0xd0>
mov    -0x4(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
jmp    11f4 <read_line+0xd4>
movb   $0xa,-0x15(%rbp)
jmp    11f9 <read_line+0xd9>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113d <read_line+0x1d>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
movb   $0x0,0x7(%rsp)
xor    %ebx,%ebx
cmp    $0x2,%edx
jl     11ca <read_line+0xaa>
mov    %edx,%r15d
mov    %edi,%ebp
add    $0xffffffff,%r15d
xor    %ebx,%ebx
lea    0x7(%rsp),%r12
mov    0x2e93(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %al,%al
jle    11ca <read_line+0xaa>
movzbl 0x7(%rsp),%eax
cmp    $0xd,%al
jne    11ae <read_line+0x8e>
mov    0x0(%r13),%ecx
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <recv@plt>
test   %al,%al
jle    11c1 <read_line+0xa1>
cmpb   $0xa,0x7(%rsp)
jne    11c1 <read_line+0xa1>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %r15,%rbx
jae    11ca <read_line+0xaa>
cmp    $0xa,%al
jne    1160 <read_line+0x40>
jmp    11ca <read_line+0xaa>
movb   $0xa,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
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
define i32 @read_line(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i8 0, i8* %7
store i32 0, i32* %9
br label %10
10: ; preds = %48, %3
%11 = load i32, i32* %9
%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* %7
%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 signext i8 @read(i32 %22, i8* %7, i32 1)
store i8 %23, i8* %8
%24 = load i8, i8* %8
%25 = sext i8 %24 to i32
%26 = icmp sle i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
br label %56
28: ; preds = %21
%29 = load i8, i8* %7
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 13
br i1 %31, label %32, label %48
32: ; preds = %28
%33 = load i32, i32* %4
%34 = load i32, i32* @MSG_PEEK
%35 = call signext i8 @recv(i32 %33, i8* %7, i32 1, i32 %34)
store i8 %35, i8* %8
%36 = load i8, i8* %8
%37 = sext i8 %36 to i32
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %32
%40 = load i8, i8* %7
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 10
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* %4
%45 = call signext i8 @read(i32 %44, i8* %7, i32 1)
br label %47
46: ; preds = %39, %32
store i8 10, i8* %7
br label %47
47: ; preds = %46, %43
br label %48
48: ; preds = %47, %28
%49 = load i8, i8* %7
%50 = load i8*, i8** %5
%51 = load i32, i32* %9
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 %49, i8* %53
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
store i32 %55, i32* %9
br label %10
56: ; preds = %27, %19
%57 = load i8*, i8** %5
%58 = load i32, i32* %9
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
store i8 0, i8* %60
%61 = load i32, i32* %9
ret i32 %61
}

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