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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
size_t read(int, unsigned char *, int);
ssize_t fd_read_line(int fd, void *buf, size_t maxlen) {
  ssize_t n, rc;
  unsigned char c, *p;
  n = 0;
  p = buf;
  while (n < maxlen - 1) {
    if ((rc = read(fd, &c, 1)) == 1) {
      n++;
      *p++ = c;
      if (c == '\n')
        break;
    } else if (rc == 0) {
      if (n == 0)
        return (0);
      else
        break;
    } else {
      if (errno == EINTR)
        continue;
      return (-1);
    }
  }
  *p = '\0';
  return (n);
}


x86 assembly (clang, -O0):
<fd_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    11f7 <fd_read_line+0xe7>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x1,%rax
jne    119e <fd_read_line+0x8e>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x31(%rbp),%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
movzbl -0x31(%rbp),%eax
cmp    $0xa,%eax
jne    1199 <fd_read_line+0x89>
jmp    11f7 <fd_read_line+0xe7>
jmp    11f2 <fd_read_line+0xe2>
cmpq   $0x0,-0x30(%rbp)
jne    11c6 <fd_read_line+0xb6>
cmpq   $0x0,-0x28(%rbp)
jne    11c1 <fd_read_line+0xb1>
movq   $0x0,-0x8(%rbp)
jmp    1206 <fd_read_line+0xf6>
jmp    11f7 <fd_read_line+0xe7>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%rax
jne    11e5 <fd_read_line+0xd5>
jmp    1133 <fd_read_line+0x23>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1206 <fd_read_line+0xf6>
jmp    1133 <fd_read_line+0x23>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,0x10(%rsp)
mov    %edi,%ebp
add    $0xffffffffffffffff,%r15
xor    %r12d,%r12d
lea    0xf(%rsp),%rbx
mov    0x2ea5(%rip),%r13
mov    0x2e96(%rip),%r14
cmp    %r15,%r12
je     11cb <fd_read_line+0xbb>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
je     119e <fd_read_line+0x8e>
cmp    $0x1,%rax
je     1180 <fd_read_line+0x70>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     1150 <fd_read_line+0x40>
jmp    11ad <fd_read_line+0x9d>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r12
mov    0xf(%rsp),%al
mov    0x10(%rsp),%rcx
mov    %al,(%rcx)
add    $0x1,%rcx
mov    %rcx,0x10(%rsp)
cmp    $0xa,%al
jne    1142 <fd_read_line+0x32>
jmp    11a3 <fd_read_line+0x93>
test   %r12,%r12
je     11b6 <fd_read_line+0xa6>
mov    0x10(%rsp),%rax
movb   $0x0,(%rax)
jmp    11b9 <fd_read_line+0xa9>
mov    $0xffffffffffffffff,%r12
jmp    11b9 <fd_read_line+0xa9>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%r12
jmp    11a3 <fd_read_line+0x93>

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i64 @fd_read_line(i32 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = alloca i8
%5 = add i64 %2, -1
br label %6
6: ; preds = %12, %3
%7 = phi i64 [ %13, %12 ], [ 0, %3 ]
%8 = phi i8* [ %15, %12 ], [ %1, %3 ]
%9 = icmp eq i64 %7, %5
br i1 %9, label %23, label %10
10: ; preds = %6, %19
%11 = call i64 @read(i32 %0, i8* %4, i32 1)
switch i64 %11, label %19 [
i64 1, label %12
i64 0, label %17
]
12: ; preds = %10
%13 = add i64 %7, 1
%14 = load i8, i8* %4
%15 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %14, i8* %8
%16 = icmp eq i8 %14, 10
br i1 %16, label %23, label %6
17: ; preds = %10
%18 = icmp eq i64 %7, 0
br i1 %18, label %26, label %23
19: ; preds = %10
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %10, label %26
23: ; preds = %6, %12, %17
%24 = phi i64 [ %7, %17 ], [ %5, %6 ], [ %13, %12 ]
%25 = phi i8* [ %8, %17 ], [ %8, %6 ], [ %15, %12 ]
store i8 0, i8* %25
br label %26
26: ; preds = %19, %17, %23
%27 = phi i64 [ %24, %23 ], [ 0, %17 ], [ -1, %19 ]
ret i64 %27
}