#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 */

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EINVAL;
scalar_t__ errno;
scalar_t__ readtimed(int, char *, int, double);
int readlinetimed(int fd, char *buf, int nbytes, double seconds) {
  int numread = 0;
  int returnval;
  while (numread < nbytes - 1) {
    returnval = (int)readtimed(fd, buf + numread, 1, seconds);
    if ((returnval == -1) && (errno == EINTR))
      continue;
    if ((returnval == 0) && (numread == 0))
      return 0;
    if (returnval == 0)
      break;
    if (returnval == -1)
      return -1;
    numread++;
    if (buf[numread - 1] == '\n') {
      buf[numread] = '\0';
      return numread;
    }
  }
  errno = EINVAL;
  return -1;
}


x86 assembly (clang, -O0):
<readlinetimed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    120c <readlinetimed+0xfc>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
movsd  -0x20(%rbp),%xmm0
mov    $0x1,%edx
call   1030 <readtimed@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1188 <readlinetimed+0x78>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
jne    1188 <readlinetimed+0x78>
jmp    112e <readlinetimed+0x1e>
cmpl   $0x0,-0x28(%rbp)
jne    11a8 <readlinetimed+0x98>
cmpl   $0x0,-0x24(%rbp)
jne    11a8 <readlinetimed+0x98>
movl   $0x0,-0x4(%rbp)
jmp    1227 <readlinetimed+0x117>
cmpl   $0x0,-0x28(%rbp)
jne    11b7 <readlinetimed+0xa7>
jmp    120c <readlinetimed+0xfc>
cmpl   $0xffffffff,-0x28(%rbp)
jne    11cd <readlinetimed+0xbd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <readlinetimed+0x117>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1207 <readlinetimed+0xf7>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1227 <readlinetimed+0x117>
jmp    112e <readlinetimed+0x1e>
mov    0x2dbd(%rip),%rax
mov    (%rax),%rcx
mov    0x2dc3(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlinetimed>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movsd  %xmm0,0x10(%rsp)
mov    %edx,%r15d
mov    %edi,%ebp
add    $0xffffffff,%r15d
xor    %r12d,%r12d
test   %r15d,%r15d
cmovle %r12d,%r15d
mov    0x2ea2(%rip),%r13
mov    0x2e93(%rip),%r14
mov    %rsi,0x8(%rsp)
cmp    %r15,%r12
je     11b4 <readlinetimed+0xa4>
lea    (%rsi,%r12,1),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
movsd  0x10(%rsp),%xmm0
call   1030 <readtimed@plt>
cmp    $0xffffffff,%eax
jne    1190 <readlinetimed+0x80>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     1160 <readlinetimed+0x50>
jmp    11c2 <readlinetimed+0xb2>
cs nopw 0x0(%rax,%rax,1)
nop
test   %eax,%eax
jne    1199 <readlinetimed+0x89>
test   %r12,%r12
je     11da <readlinetimed+0xca>
test   %eax,%eax
mov    0x8(%rsp),%rsi
je     11b4 <readlinetimed+0xa4>
add    $0x1,%r12
cmpb   $0xa,(%rbx)
jne    114a <readlinetimed+0x3a>
mov    %r12d,%eax
movb   $0x0,(%rsi,%rax,1)
jmp    11c8 <readlinetimed+0xb8>
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x0(%r13)
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r12d,%r12d
jmp    11c8 <readlinetimed+0xb8>

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EINVAL = global i64 0
define i32 @readlinetimed(i32 %0, i8* %1, i32 %2, double %3) {
%5 = add i32 %2, -1
%6 = call i32 @llvm.smax.i32(i32 %5, i32 0)
%7 = zext i32 %6 to i64
br label %8
8: ; preds = %26, %4
%9 = phi i64 [ %27, %26 ], [ 0, %4 ]
%10 = getelementptr inbounds i8, i8* %1, i64 %9
%11 = icmp eq i64 %9, %7
br i1 %11, label %34, label %12
12: ; preds = %8, %16
%13 = tail call i64 @readtimed(i32 %0, i8* %10, i32 1, double %3)
%14 = trunc i64 %13 to i32
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %12, label %36
20: ; preds = %12
%21 = trunc i64 %13 to i32
%22 = icmp eq i32 %21, 0
%23 = icmp eq i64 %9, 0
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %36, label %25
25: ; preds = %20
br i1 %22, label %34, label %26
26: ; preds = %25
%27 = add nuw nsw i64 %9, 1
%28 = load i8, i8* %10
%29 = icmp eq i8 %28, 10
br i1 %29, label %30, label %8
30: ; preds = %26
%31 = trunc i64 %27 to i32
%32 = and i64 %27, 4294967295
%33 = getelementptr inbounds i8, i8* %1, i64 %32
store i8 0, i8* %33
br label %36
34: ; preds = %25, %8
%35 = load i64, i64* @EINVAL
store i64 %35, i64* @errno
br label %36
36: ; preds = %20, %16, %34, %30
%37 = phi i32 [ -1, %34 ], [ %31, %30 ], [ -1, %16 ], [ 0, %20 ]
ret i32 %37
}