#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 term_t {
  int dummy;
};

/* Variables and functions */
scalar_t__ term_read_dev(struct term_t *, unsigned char *, int);
int term_read_line(struct term_t *info, char *buf, int max) {
  int count = 0;
  unsigned char tmp = 0;
  do {
    if (term_read_dev(info, &tmp, 1) < 0) {
      goto err;
    }
    buf[count] = tmp;
    if (tmp == '\n' || tmp == '\r') {
      break;
    }
    count += 1;
  } while (count != max);
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<term_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <term_read_dev@plt>
cmp    $0x0,%rax
jge    114f <term_read_line+0x3f>
jmp    119d <term_read_line+0x8d>
mov    -0x21(%rbp),%dl
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movzbl -0x21(%rbp),%eax
cmp    $0xa,%eax
je     1177 <term_read_line+0x67>
movzbl -0x21(%rbp),%eax
cmp    $0xd,%eax
jne    117c <term_read_line+0x6c>
jmp    1191 <term_read_line+0x81>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    112e <term_read_line+0x1e>
movl   $0x0,-0x4(%rbp)
jmp    11a4 <term_read_line+0x94>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<term_read_line>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r13
movb   $0x0,0xf(%rsp)
mov    %edx,%r12d
xor    %ebx,%ebx
lea    0xf(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <term_read_dev@plt>
test   %rax,%rax
js     1175 <term_read_line+0x65>
movzbl 0xf(%rsp),%ecx
mov    %cl,(%r14,%rbx,1)
xor    %eax,%eax
cmp    $0xa,%cl
je     117a <term_read_line+0x6a>
cmp    $0xd,%cl
je     117a <term_read_line+0x6a>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1140 <term_read_line+0x30>
jmp    117a <term_read_line+0x6a>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.term_t = type { i32 }
define i32 @term_read_line(%struct.term_t* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.term_t*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
store %struct.term_t* %0, %struct.term_t** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i8 0, i8* %9
br label %10
10: ; preds = %32, %3
%11 = load %struct.term_t*, %struct.term_t** %5
%12 = call i64 @term_read_dev(%struct.term_t* %11, i8* %9, i32 1)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
br label %37
15: ; preds = %10
%16 = load i8, i8* %9
%17 = load i8*, i8** %6
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
store i8 %16, i8* %20
%21 = load i8, i8* %9
%22 = zext i8 %21 to i32
%23 = icmp eq i32 %22, 10
br i1 %23, label %28, label %24
24: ; preds = %15
%25 = load i8, i8* %9
%26 = zext i8 %25 to i32
%27 = icmp eq i32 %26, 13
br i1 %27, label %28, label %29
28: ; preds = %24, %15
br label %36
29: ; preds = %24
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 1
store i32 %31, i32* %8
br label %32
32: ; preds = %29
%33 = load i32, i32* %8
%34 = load i32, i32* %7
%35 = icmp ne i32 %33, %34
br i1 %35, label %10, label %36
36: ; preds = %32, %28
store i32 0, i32* %4
br label %38
37: ; preds = %14
store i32 -1, i32* %4
br label %38
38: ; preds = %37, %36
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
%struct.term_t = type { i32 }
define i32 @term_read_line(%struct.term_t* %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i8
store i8 0, i8* %4
%5 = zext i32 %2 to i64
br label %6
6: ; preds = %13, %3
%7 = phi i64 [ %14, %13 ], [ 0, %3 ]
%8 = call i64 @term_read_dev(%struct.term_t* %0, i8* %4, i32 1)
%9 = icmp slt i64 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %6
%11 = load i8, i8* %4
%12 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 %11, i8* %12
switch i8 %11, label %13 [
i8 13, label %16
i8 10, label %16
]
13: ; preds = %10
%14 = add nuw nsw i64 %7, 1
%15 = icmp eq i64 %14, %5
br i1 %15, label %16, label %6
16: ; preds = %6, %13, %10, %10
%17 = phi i32 [ 0, %10 ], [ 0, %10 ], [ 0, %13 ], [ -1, %6 ]
ret i32 %17
}