#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int read(int, char *, int);
int nebula_read_line(int fd, char *line, ssize_t len) {
  int chars_read = 0;
  int rv = 0;
  memset(line, 0, len);
  while (1) {
    if (chars_read >= len - 1)
      return -1;
    rv = read(fd, &line[chars_read], 1);
    if (rv == 0)
      return chars_read;
    if (rv < 0)
      return -1;
    if (line[chars_read] == '\n') {
      line[chars_read] = '\0';
      return chars_read;
    }
    chars_read++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<nebula_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     116b <nebula_read_line+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <nebula_read_line+0xcb>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    119b <nebula_read_line+0x7b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11eb <nebula_read_line+0xcb>
cmpl   $0x0,-0x1c(%rbp)
jge    11b1 <nebula_read_line+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <nebula_read_line+0xcb>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11dd <nebula_read_line+0xbd>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11eb <nebula_read_line+0xcb>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    114e <nebula_read_line+0x2e>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nebula_read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r13d
mov    %rsi,%r12
mov    %edi,%r15d
mov    %rsi,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    $0xffffffff,%r14d
cmp    $0x2,%r13d
jl     1182 <nebula_read_line+0x62>
add    $0xffffffff,%r13d
xor    %ebp,%ebp
lea    (%r12,%rbp,1),%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     117f <nebula_read_line+0x5f>
js     1182 <nebula_read_line+0x62>
cmpb   $0xa,(%rbx)
je     117a <nebula_read_line+0x5a>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1150 <nebula_read_line+0x30>
jmp    1182 <nebula_read_line+0x62>
movb   $0x0,(%r12,%rbp,1)
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @nebula_read_line(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%10 = load i8*, i8** %6
%11 = load i32, i32* %7
%12 = call i32 @memset(i8* %10, i32 0, i32 %11)
br label %13
13: ; preds = %3, %48
%14 = load i32, i32* %8
%15 = load i32, i32* %7
%16 = sub nsw i32 %15, 1
%17 = icmp sge i32 %14, %16
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 -1, i32* %4
br label %51
19: ; preds = %13
%20 = load i32, i32* %5
%21 = load i8*, i8** %6
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = call i32 @read(i32 %20, i8* %24, i32 1)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %19
%29 = load i32, i32* %8
store i32 %29, i32* %4
br label %51
30: ; preds = %19
%31 = load i32, i32* %9
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 -1, i32* %4
br label %51
34: ; preds = %30
%35 = load i8*, i8** %6
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 10
br i1 %41, label %42, label %48
42: ; preds = %34
%43 = load i8*, i8** %6
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 0, i8* %46
%47 = load i32, i32* %8
store i32 %47, i32* %4
br label %51
48: ; preds = %34
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 1
store i32 %50, i32* %8
br label %13
51: ; preds = %42, %33, %28, %18
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
define i32 @nebula_read_line(i32 %0, i8* %1, i32 %2) {
%4 = tail call i32 @memset(i8* %1, i32 0, i32 %2)
%5 = icmp sgt i32 %2, 1
br i1 %5, label %6, label %26
6: ; preds = %3
%7 = add nsw i32 %2, -1
%8 = zext i32 %7 to i64
br label %9
9: ; preds = %6, %21
%10 = phi i64 [ 0, %6 ], [ %22, %21 ]
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = tail call i32 @read(i32 %0, i8* %11, i32 1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %24, label %14
14: ; preds = %9
%15 = icmp slt i32 %12, 0
br i1 %15, label %26, label %16
16: ; preds = %14
%17 = load i8, i8* %11
%18 = icmp eq i8 %17, 10
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = trunc i64 %10 to i32
store i8 0, i8* %11
br label %26
21: ; preds = %16
%22 = add nuw nsw i64 %10, 1
%23 = icmp eq i64 %22, %8
br i1 %23, label %26, label %9
24: ; preds = %9
%25 = trunc i64 %10 to i32
br label %26
26: ; preds = %14, %21, %24, %3, %19
%27 = phi i32 [ %20, %19 ], [ -1, %3 ], [ %25, %24 ], [ -1, %21 ], [ -1, %14 ]
ret i32 %27
}