#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, char *, int);
ssize_t Readline(int sockd, void *vptr, size_t maxlen) {
  ssize_t n, rc;
  char c, *buffer;
  buffer = vptr;
  for (n = 1; n < maxlen; n++) {
    if ((rc = read(sockd, &c, 1)) == 1) {
      *buffer++ = c;
      if (c == '\n')
        break;
    } else if (rc == 0) {
      if (n == 1)
        return 0;
      else
        break;
    } else {
      if (errno == EINTR)
        continue;
      return -1;
    }
  }
  *buffer = 0;
  return n;
}


x86 assembly (clang, -O0):
<Readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
movq   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11f5 <Readline+0xe5>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x1,%rax
jne    118b <Readline+0x7b>
mov    -0x31(%rbp),%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
movsbl -0x31(%rbp),%eax
cmp    $0xa,%eax
jne    1186 <Readline+0x76>
jmp    11f5 <Readline+0xe5>
jmp    11df <Readline+0xcf>
cmpq   $0x0,-0x30(%rbp)
jne    11b3 <Readline+0xa3>
cmpq   $0x1,-0x28(%rbp)
jne    11ae <Readline+0x9e>
movq   $0x0,-0x8(%rbp)
jmp    1204 <Readline+0xf4>
jmp    11f5 <Readline+0xe5>
mov    0x2e26(%rip),%rax
mov    (%rax),%rax
mov    0x2e14(%rip),%rcx
cmp    (%rcx),%rax
jne    11d2 <Readline+0xc2>
jmp    11e4 <Readline+0xd4>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1204 <Readline+0xf4>
jmp    11e4 <Readline+0xd4>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1133 <Readline+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):
<Readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    $0x1,%ebx
cmp    $0x2,%rdx
jb     11a2 <Readline+0x92>
mov    %rdx,%r15
mov    %edi,%ebp
mov    $0x1,%ebx
lea    0x7(%rsp),%r12
mov    0x2e99(%rip),%r13
jmp    116c <Readline+0x5c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x7(%rsp),%eax
mov    %al,(%r14)
add    $0x1,%r14
cmpb   $0xa,0x7(%rsp)
je     11a2 <Readline+0x92>
add    $0x1,%rbx
cmp    %rbx,%r15
je     119f <Readline+0x8f>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%rax
je     1150 <Readline+0x40>
test   %rax,%rax
je     11b8 <Readline+0xa8>
mov    0x2e53(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     1163 <Readline+0x53>
mov    $0xffffffffffffffff,%rbx
jmp    11a6 <Readline+0x96>
mov    %r15,%rbx
movb   $0x0,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x1,%rbx
jne    11a2 <Readline+0x92>
xor    %ebx,%ebx
jmp    11a6 <Readline+0x96>

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

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