#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 readnbytes(int, char *, int);
int get_line(int fd, char *buf, int size) {
  int i = 0;
  char c = '\0';
  int n;
  while ((i < size - 1) && (c != '\n')) {
    n = readnbytes(fd, &c, 1);
    if (n > 0) {
      if (c == '\r') {
        n = readnbytes(fd, &c, 1);
        if ((n > 0) && (c == '\n')) {
          continue;
        } 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    11e9 <get_line+0xd9>
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <readnbytes@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11e0 <get_line+0xd0>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
jne    11c4 <get_line+0xb4>
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <readnbytes@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11bb <get_line+0xab>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    11bb <get_line+0xab>
jmp    112d <get_line+0x1d>
movb   $0xa,-0x19(%rbp)
jmp    11c4 <get_line+0xb4>
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    11e4 <get_line+0xd4>
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   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
movb   $0x0,0xf(%rsp)
xor    %ebp,%ebp
cmp    $0x2,%edx
jl     11a0 <get_line+0x90>
mov    %edx,%r12d
mov    %edi,%ebx
add    $0xffffffff,%r12d
xor    %ebp,%ebp
lea    0xf(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <readnbytes@plt>
test   %eax,%eax
jle    1190 <get_line+0x80>
movzbl 0xf(%rsp),%eax
cmp    $0xd,%al
jne    117d <get_line+0x6d>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <readnbytes@plt>
test   %eax,%eax
jle    1176 <get_line+0x66>
cmpb   $0xa,0xf(%rsp)
je     11a0 <get_line+0x90>
movb   $0xa,0xf(%rsp)
mov    $0xa,%al
movslq %ebp,%rbp
mov    %al,(%r14,%rbp,1)
add    $0x1,%ebp
cmp    %r12d,%ebp
jl     119c <get_line+0x8c>
jmp    11a0 <get_line+0x90>
xchg   %ax,%ax
movb   $0xa,0xf(%rsp)
mov    $0xa,%al
cmp    %r12d,%ebp
jge    11a0 <get_line+0x90>
cmp    $0xa,%al
jne    1140 <get_line+0x30>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
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 = %51, %39, %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 %52
21: ; preds = %19
%22 = load i32, i32* %4
%23 = call i32 @readnbytes(i32 %22, i8* %8, i32 1)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %50
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 %42
30: ; preds = %26
%31 = load i32, i32* %4
%32 = call i32 @readnbytes(i32 %31, i8* %8, i32 1)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = load i8, i8* %8
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %40
39: ; preds = %35
br label %10
40: ; preds = %35, %30
store i8 10, i8* %8
br label %41
41: ; preds = %40
br label %42
42: ; preds = %41, %26
%43 = load i8, i8* %8
%44 = load i8*, i8** %5
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 %43, i8* %47
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %51
50: ; preds = %21
store i8 10, i8* %8
br label %51
51: ; preds = %50, %42
br label %10
52: ; preds = %19
%53 = load i8*, i8** %5
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 0, i8* %56
%57 = load i32, i32* %7
ret i32 %57
}

LLVM IR (-O2):
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 %33
7: ; preds = %3, %27
%8 = phi i32 [ %29, %27 ], [ 0, %3 ]
%9 = call i32 @readnbytes(i32 %0, i8* %4, i32 1)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %26
11: ; preds = %7
%12 = load i8, i8* %4
%13 = icmp eq i8 %12, 13
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = call i32 @readnbytes(i32 %0, i8* %4, i32 1)
%16 = icmp sgt i32 %15, 0
%17 = load i8, i8* %4
%18 = icmp eq i8 %17, 10
%19 = select i1 %16, i1 %18, i1 false
br i1 %19, label %33, label %20
20: ; preds = %14
store i8 10, i8* %4
br label %21
21: ; preds = %11, %20
%22 = phi i8 [ 10, %20 ], [ %12, %11 ]
%23 = sext i32 %8 to i64
%24 = getelementptr inbounds i8, i8* %1, i64 %23
store i8 %22, i8* %24
%25 = add nsw i32 %8, 1
br label %27
26: ; preds = %7
store i8 10, i8* %4
br label %27
27: ; preds = %26, %21
%28 = phi i8 [ %22, %21 ], [ 10, %26 ]
%29 = phi i32 [ %25, %21 ], [ %8, %26 ]
%30 = icmp slt i32 %29, %5
%31 = icmp ne i8 %28, 10
%32 = select i1 %30, i1 %31, i1 false
br i1 %32, label %7, label %33
33: ; preds = %14, %27, %3
%34 = phi i32 [ 0, %3 ], [ %8, %14 ], [ %29, %27 ]
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %1, i64 %35
store i8 0, i8* %36
ret i32 %34
}