#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 */
struct nc {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ nc_dump(char *, unsigned char *, size_t);
scalar_t__ nc_read(struct nc *, unsigned char *, int);
int nc_read_line(struct nc *nc, unsigned char *buf, size_t maxlen) {
  size_t r = 0;
  nc_dump("Reading line", NULL, maxlen);
  while (r < maxlen) {
    if (nc_read(nc, buf + r, 1) <= 0) {
      return r;
    }
    if (buf[r] == '\012') {
      buf[r] = '\0';
      if (r > 0 && buf[r - 1] == '\015') {
        r--;
        buf[r] = '\0';
      }
      nc_dump("Read line", buf, r);
      return r;
    }
    r++;
  }
  nc_dump("Read line", buf, r);
  return r;
}


x86 assembly (clang, -O0):
<nc_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdx
lea    0xeb9(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nc_dump@plt>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1218 <nc_read_line+0xf8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1040 <nc_read@plt>
cmp    $0x0,%rax
jg     118a <nc_read_line+0x6a>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1233 <nc_read_line+0x113>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1207 <nc_read_line+0xe7>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x28(%rbp)
jbe    11e7 <nc_read_line+0xc7>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    11e7 <nc_read_line+0xc7>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe17(%rip),%rdi
call   1030 <nc_dump@plt>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1233 <nc_read_line+0x113>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1150 <nc_read_line+0x30>
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xde6(%rip),%rdi
call   1030 <nc_dump@plt>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nc_read_line>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xec7(%rip),%rdi
xor    %esi,%esi
call   1030 <nc_dump@plt>
test   %r15,%r15
je     1177 <nc_read_line+0x57>
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
lea    (%r14,%r13,1),%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <nc_read@plt>
test   %rax,%rax
jle    11be <nc_read_line+0x9e>
cmpb   $0xa,(%rbx)
je     118b <nc_read_line+0x6b>
add    $0x1,%r13
cmp    %r13,%r15
jne    1150 <nc_read_line+0x30>
lea    0xe8f(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <nc_dump@plt>
jmp    11c1 <nc_read_line+0xa1>
movb   $0x0,(%r14,%r13,1)
test   %r13,%r13
je     11a9 <nc_read_line+0x89>
cmpb   $0xd,-0x1(%r14,%r13,1)
jne    11ac <nc_read_line+0x8c>
movb   $0x0,-0x1(%r14,%r13,1)
add    $0xffffffffffffffff,%r13
jmp    11ac <nc_read_line+0x8c>
xor    %r13d,%r13d
lea    0xe5a(%rip),%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1030 <nc_dump@plt>
mov    %r13,%r15
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.nc = type { i32 }
@.str = [13 x i8] c"Reading line\00"
@.str.1 = [10 x i8] c"Read line\00"
define i32 @nc_read_line(%struct.nc* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.nc*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store %struct.nc* %0, %struct.nc** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
%9 = load i64, i64* %7
%10 = call i32 @nc_dump(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* null, i64 %9)
br label %11
11: ; preds = %58, %3
%12 = load i64, i64* %8
%13 = load i64, i64* %7
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %61
15: ; preds = %11
%16 = load %struct.nc*, %struct.nc** %5
%17 = load i8*, i8** %6
%18 = load i64, i64* %8
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = call i64 @nc_read(%struct.nc* %16, i8* %19, i32 1)
%21 = icmp sle i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i64, i64* %8
%24 = trunc i64 %23 to i32
store i32 %24, i32* %4
br label %67
25: ; preds = %15
%26 = load i8*, i8** %6
%27 = load i64, i64* %8
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = icmp eq i32 %30, 10
br i1 %31, label %32, label %58
32: ; preds = %25
%33 = load i8*, i8** %6
%34 = load i64, i64* %8
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = load i64, i64* %8
%37 = icmp ugt i64 %36, 0
br i1 %37, label %38, label %52
38: ; preds = %32
%39 = load i8*, i8** %6
%40 = load i64, i64* %8
%41 = sub i64 %40, 1
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = icmp eq i32 %44, 13
br i1 %45, label %46, label %52
46: ; preds = %38
%47 = load i64, i64* %8
%48 = add i64 %47, -1
store i64 %48, i64* %8
%49 = load i8*, i8** %6
%50 = load i64, i64* %8
%51 = getelementptr inbounds i8, i8* %49, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %46, %38, %32
%53 = load i8*, i8** %6
%54 = load i64, i64* %8
%55 = call i32 @nc_dump(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %53, i64 %54)
%56 = load i64, i64* %8
%57 = trunc i64 %56 to i32
store i32 %57, i32* %4
br label %67
58: ; preds = %25
%59 = load i64, i64* %8
%60 = add i64 %59, 1
store i64 %60, i64* %8
br label %11
61: ; preds = %11
%62 = load i8*, i8** %6
%63 = load i64, i64* %8
%64 = call i32 @nc_dump(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %62, i64 %63)
%65 = load i64, i64* %8
%66 = trunc i64 %65 to i32
store i32 %66, i32* %4
br label %67
67: ; preds = %61, %52, %22
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
%struct.nc = type { i32 }
@.str = [13 x i8] c"Reading line\00"
@.str.1 = [10 x i8] c"Read line\00"
define i32 @nc_read_line(%struct.nc* %0, i8* %1, i64 %2) {
%4 = tail call i32 @nc_dump(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* null, i64 %2)
%5 = icmp eq i64 %2, 0
br i1 %5, label %28, label %6
6: ; preds = %3, %25
%7 = phi i64 [ %26, %25 ], [ 0, %3 ]
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = tail call i64 @nc_read(%struct.nc* %0, i8* %8, i32 1)
%10 = icmp slt i64 %9, 1
br i1 %10, label %30, label %11
11: ; preds = %6
%12 = load i8, i8* %8
%13 = icmp eq i8 %12, 10
br i1 %13, label %14, label %25
14: ; preds = %11
store i8 0, i8* %8
%15 = icmp eq i64 %7, 0
br i1 %15, label %22, label %16
16: ; preds = %14
%17 = add i64 %7, -1
%18 = getelementptr inbounds i8, i8* %1, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 13
br i1 %20, label %21, label %22
21: ; preds = %16
store i8 0, i8* %18
br label %22
22: ; preds = %21, %16, %14
%23 = phi i64 [ %17, %21 ], [ %7, %16 ], [ 0, %14 ]
%24 = tail call i32 @nc_dump(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %1, i64 %23)
br label %30
25: ; preds = %11
%26 = add nuw i64 %7, 1
%27 = icmp eq i64 %26, %2
br i1 %27, label %28, label %6
28: ; preds = %25, %3
%29 = tail call i32 @nc_dump(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %1, i64 %2)
br label %30
30: ; preds = %6, %28, %22
%31 = phi i64 [ %23, %22 ], [ %2, %28 ], [ %7, %6 ]
%32 = trunc i64 %31 to i32
ret i32 %32
}