#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__ time_t;

/* Variables and functions */
int /*<<< orphan*/ ETIMEDOUT;
int daemon_read(int, char *, int, scalar_t__);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ time(scalar_t__ *);
int daemon_readline(int fd, char *ptr, int maxlen, int to) {
  time_t s, e;
  int n, rc;
  char c;
  time(&s);
  e = s + to;
  n = 0;
  while (1) {
    time(&s);
    if (to > 0 && s >= e) {
      errno = ETIMEDOUT;
      return -1;
    }
    if ((rc = daemon_read(fd, &c, 1, to > 0 ? e - s : 0)) == 1) {
      if (n + 1 >= maxlen) {
        return -1;
      }
      n++;
      *ptr++ = c;
      if (c == '\n') {
        *ptr = 0;
        return n;
      }
    } else {
      return -1;
    }
  }
}


x86 assembly (clang, -O0):
<daemon_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <time@plt>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <time@plt>
cmpl   $0x0,-0x18(%rbp)
jle    1193 <daemon_readline+0x73>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jl     1193 <daemon_readline+0x73>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <daemon_readline+0x12d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11b4 <daemon_readline+0x94>
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11bf <daemon_readline+0x9f>
xor    %eax,%eax
mov    %rax,-0x40(%rbp)
jmp    11bf <daemon_readline+0x9f>
mov    -0x38(%rbp),%edi
mov    -0x40(%rbp),%rcx
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1040 <daemon_read@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x1,%eax
jne    123c <daemon_readline+0x11c>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
cmp    -0x14(%rbp),%eax
jl     11fb <daemon_readline+0xdb>
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <daemon_readline+0x12d>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x31(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movsbl -0x31(%rbp),%eax
cmp    $0xa,%eax
jne    1237 <daemon_readline+0x117>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124d <daemon_readline+0x12d>
jmp    1248 <daemon_readline+0x128>
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <daemon_readline+0x12d>
jmp    1154 <daemon_readline+0x34>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebp
mov    %edx,%ebx
mov    %rsi,0x10(%rsp)
mov    %edi,%r15d
lea    0x8(%rsp),%rdi
call   1030 <time@plt>
movslq %ebp,%rax
mov    0x8(%rsp),%r12
add    %rax,%r12
test   %eax,%eax
setle  %r13b
cmp    $0x2,%ebx
mov    $0x1,%eax
cmovl  %eax,%ebx
add    $0xffffffffffffffff,%rbx
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
lea    0x8(%rsp),%rdi
call   1030 <time@plt>
mov    %r12,%rcx
sub    0x8(%rsp),%rcx
setg   %al
or     %r13b,%al
je     11dd <daemon_readline+0xbd>
test   %ebp,%ebp
mov    $0x0,%eax
cmovle %rax,%rcx
mov    %r15d,%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1040 <daemon_read@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x1,%ecx
jne    11f4 <daemon_readline+0xd4>
cmp    %r14d,%ebx
je     11f4 <daemon_readline+0xd4>
movzbl 0x7(%rsp),%eax
mov    0x10(%rsp),%rcx
mov    %al,(%rcx,%r14,1)
add    $0x1,%r14
cmp    $0xa,%al
jne    1170 <daemon_readline+0x50>
mov    0x10(%rsp),%rax
movb   $0x0,(%rax,%r14,1)
mov    %r14d,%eax
jmp    11f4 <daemon_readline+0xd4>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    0x2df3(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@ETIMEDOUT = global i32 0
@errno = global i32 0
define i32 @daemon_readline(i32 %0, i8* nocapture writeonly %1, i32 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i8
%7 = bitcast i64* %5 to i8*
%8 = call i32 @time(i64* %5)
%9 = load i64, i64* %5
%10 = sext i32 %3 to i64
%11 = add nsw i64 %9, %10
%12 = icmp sgt i32 %3, 0
%13 = xor i1 %12, true
%14 = call i32 @llvm.smax.i32(i32 %2, i32 1)
%15 = add nsw i32 %14, -1
br label %16
16: ; preds = %33, %4
%17 = phi i8* [ %1, %4 ], [ %35, %33 ]
%18 = phi i32 [ 0, %4 ], [ %31, %33 ]
%19 = call i32 @time(i64* %5)
%20 = load i64, i64* %5
%21 = icmp sgt i64 %11, %20
%22 = select i1 %13, i1 true, i1 %21
br i1 %22, label %25, label %23
23: ; preds = %16
%24 = load i32, i32* @ETIMEDOUT
store i32 %24, i32* @errno
br label %38
25: ; preds = %16
%26 = sub nsw i64 %11, %20
%27 = select i1 %12, i64 %26, i64 0
%28 = call i32 @daemon_read(i32 %0, i8* %6, i32 1, i64 %27)
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %38
30: ; preds = %25
%31 = add nuw nsw i32 %18, 1
%32 = icmp eq i32 %18, %15
br i1 %32, label %38, label %33
33: ; preds = %30
%34 = load i8, i8* %6
%35 = getelementptr inbounds i8, i8* %17, i64 1
store i8 %34, i8* %17
%36 = icmp eq i8 %34, 10
br i1 %36, label %37, label %16
37: ; preds = %33
store i8 0, i8* %35
br label %38
38: ; preds = %25, %30, %37, %23
%39 = phi i32 [ -1, %23 ], [ %31, %37 ], [ -1, %30 ], [ -1, %25 ]
ret i32 %39
}