#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;
typedef int /*<<< orphan*/ c;

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


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114e <read_line+0x3e>
mov    0x2e9b(%rip),%rax
mov    (%rax),%rcx
mov    0x2ea1(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <read_line+0x138>
cmpq   $0x0,-0x18(%rbp)
jne    1165 <read_line+0x55>
movl   $0x0,-0x4(%rbp)
jmp    1248 <read_line+0x138>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    123a <read_line+0x12a>
mov    -0x8(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    11e4 <read_line+0xd4>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x25(%rbp),%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
movzbl -0x25(%rbp),%eax
cmp    $0xa,%eax
jne    11df <read_line+0xcf>
jmp    123a <read_line+0x12a>
jmp    1235 <read_line+0x125>
cmpl   $0x0,-0x24(%rbp)
jne    120a <read_line+0xfa>
cmpq   $0x0,-0x20(%rbp)
jne    1205 <read_line+0xf5>
movl   $0x0,-0x4(%rbp)
jmp    1248 <read_line+0x138>
jmp    123a <read_line+0x12a>
mov    0x2dcf(%rip),%rax
mov    (%rax),%rax
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%rax
jne    1229 <read_line+0x119>
jmp    1181 <read_line+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <read_line+0x138>
jmp    1181 <read_line+0x71>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rsi,%rsi
je     119d <read_line+0x8d>
test   %rdx,%rdx
je     11c4 <read_line+0xb4>
mov    %rsi,%r14
mov    %edi,%ebp
add    $0xffffffffffffffff,%rdx
xor    %r12d,%r12d
lea    0xf(%rsp),%rbx
mov    0x2e9c(%rip),%r13
mov    0x2e8d(%rip),%r15
mov    %rdx,0x10(%rsp)
cmp    %rdx,%r12
je     11d9 <read_line+0xc9>
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
je     11b9 <read_line+0xa9>
cmp    $0x1,%eax
je     1183 <read_line+0x73>
mov    0x0(%r13),%rax
cmp    (%r15),%rax
je     1160 <read_line+0x50>
jmp    11b1 <read_line+0xa1>
add    $0x1,%r12
mov    0xf(%rsp),%al
mov    %al,(%r14)
add    $0x1,%r14
cmp    $0xa,%al
mov    0x10(%rsp),%rdx
jne    1150 <read_line+0x40>
jmp    11be <read_line+0xae>
mov    0x2e2c(%rip),%rax
mov    (%rax),%rax
mov    0x2e32(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%r12d
jmp    11c7 <read_line+0xb7>
test   %r12,%r12
je     11c4 <read_line+0xb4>
movb   $0x0,(%r14)
jmp    11c7 <read_line+0xb7>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rdx,%r12
jmp    11be <read_line+0xae>

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

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