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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
scalar_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 < (ssize_t)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
jge    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,%r12
add    $0xffffffffffffffff,%r12
xor    %r15d,%r15d
test   %r12,%r12
cmovle %r15,%r12
mov    %rsi,0x10(%rsp)
mov    %edi,%ebp
lea    0xf(%rsp),%rbx
mov    0x2e9e(%rip),%r13
mov    0x2e8f(%rip),%r14
cmp    %r12,%r15
je     11cb <fd_read_line+0xbb>
xchg   %ax,%ax
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,%r15
mov    0xf(%rsp),%al
mov    0x10(%rsp),%rcx
mov    %al,(%rcx)
add    $0x1,%rcx
mov    %rcx,0x10(%rsp)
cmp    $0xa,%al
jne    1149 <fd_read_line+0x39>
jmp    11a3 <fd_read_line+0x93>
test   %r15,%r15
je     11b6 <fd_read_line+0xa6>
mov    0x10(%rsp),%rax
movb   $0x0,(%rax)
jmp    11b9 <fd_read_line+0xa9>
mov    $0xffffffffffffffff,%r15
jmp    11b9 <fd_read_line+0xa9>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%r15
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 nsw i64 %15, 1
%17 = icmp slt 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 nsw 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
%6 = call i64 @llvm.smax.i64(i64 %5, i64 0)
br label %7
7: ; preds = %13, %3
%8 = phi i64 [ %14, %13 ], [ 0, %3 ]
%9 = phi i8* [ %16, %13 ], [ %1, %3 ]
%10 = icmp eq i64 %8, %6
br i1 %10, label %24, label %11
11: ; preds = %7, %20
%12 = call i64 @read(i32 %0, i8* %4, i32 1)
switch i64 %12, label %20 [
i64 1, label %13
i64 0, label %18
]
13: ; preds = %11
%14 = add nuw i64 %8, 1
%15 = load i8, i8* %4
%16 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %15, i8* %9
%17 = icmp eq i8 %15, 10
br i1 %17, label %24, label %7
18: ; preds = %11
%19 = icmp eq i64 %8, 0
br i1 %19, label %27, label %24
20: ; preds = %11
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %11, label %27
24: ; preds = %7, %13, %18
%25 = phi i64 [ %8, %18 ], [ %6, %7 ], [ %14, %13 ]
%26 = phi i8* [ %9, %18 ], [ %9, %7 ], [ %16, %13 ]
store i8 0, i8* %26
br label %27
27: ; preds = %20, %18, %24
%28 = phi i64 [ %25, %24 ], [ 0, %18 ], [ -1, %20 ]
ret i64 %28
}