#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 */
struct pollfd {
  int /*<<< orphan*/ fd;
  int /*<<< orphan*/ events;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int errno;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int poll(struct pollfd *, int, int);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
char *strerror(int);
int rxline(struct pollfd *fds, char *buf) {
  int len;
  fds->events = POLLIN;
  memset(buf, 0, 64);
  for (len = 0; len <= 64 - 2;) {
    int rc = poll(fds, 1, 2000);
    if (rc < 0) {
      snprintf(buf, 64, "poll error %d: %s", errno, strerror(errno));
      return -1;
    }
    if (rc == 0) {
      snprintf(buf, 64, "poll timeout");
      return -1;
    }
    ssize_t got = read(fds->fd, buf + len, 1);
    if (got < 0) {
      snprintf(buf, 64, "read error %d: %s", errno, strerror(errno));
      return -1;
    }
    if (rc == 0) {
      snprintf(buf, 64, "read nothing");
      return -1;
    }
    len += got;
    if (buf[len - 1] == '\n' || (len > 1 && buf[0] == '\003'))
      break;
  }
  return len;
}


x86 assembly (clang, -O0):
<rxline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3e,-0x1c(%rbp)
jg     1317 <rxline+0x1c7>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
mov    $0x7d0,%edx
call   1060 <poll@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11fc <rxline+0xac>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%ecx
mov    %rax,%r8
mov    $0x40,%esi
lea    0xe17(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131d <rxline+0x1cd>
cmpl   $0x0,-0x20(%rbp)
jne    1229 <rxline+0xd9>
mov    -0x18(%rbp),%rdi
mov    $0x40,%esi
lea    0xdfc(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131d <rxline+0x1cd>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    129e <rxline+0x14e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%ecx
mov    %rax,%r8
mov    $0x40,%esi
lea    0xd94(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131d <rxline+0x1cd>
cmpl   $0x0,-0x20(%rbp)
jne    12cb <rxline+0x17b>
mov    -0x18(%rbp),%rdi
mov    $0x40,%esi
lea    0xd79(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131d <rxline+0x1cd>
mov    -0x28(%rbp),%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     130d <rxline+0x1bd>
cmpl   $0x1,-0x1c(%rbp)
jle    1312 <rxline+0x1c2>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3,%eax
jne    1312 <rxline+0x1c2>
jmp    1317 <rxline+0x1c7>
jmp    1187 <rxline+0x37>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rxline>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rdi)
xor    %ebp,%ebp
mov    %rsi,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x7d0,%edx
call   1060 <poll@plt>
test   %eax,%eax
js     11d0 <rxline+0x80>
test   %eax,%eax
je     11e9 <rxline+0x99>
mov    (%rbx),%edi
movslq %ebp,%rsi
add    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
js     1201 <rxline+0xb1>
add    %eax,%ebp
movslq %ebp,%rax
cmpb   $0xa,-0x1(%rax,%r14,1)
je     1231 <rxline+0xe1>
cmp    $0x2,%ebp
jl     1180 <rxline+0x30>
cmpb   $0x3,(%r14)
je     1231 <rxline+0xe1>
cmp    $0x3e,%ebp
jbe    1180 <rxline+0x30>
jmp    1231 <rxline+0xe1>
mov    0x2e01(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xe19(%rip),%rdx
jmp    1218 <rxline+0xc8>
lea    0xe22(%rip),%rdx
mov    %r14,%rdi
mov    $0x40,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    122c <rxline+0xdc>
mov    0x2dd0(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xe07(%rip),%rdx
mov    %r14,%rdi
mov    $0x40,%esi
mov    %ebx,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@.str = [18 x i8] c"poll error %d: %s\00"
@errno = global i32 0
@.str.1 = [13 x i8] c"poll timeout\00"
@.str.2 = [18 x i8] c"read error %d: %s\00"
@.str.3 = [13 x i8] c"read nothing\00"
define i32 @rxline(%struct.pollfd* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.pollfd*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store %struct.pollfd* %0, %struct.pollfd** %4
store i8* %1, i8** %5
%9 = load i32, i32* @POLLIN
%10 = load %struct.pollfd*, %struct.pollfd** %4
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 1
store i32 %9, i32* %11
%12 = load i8*, i8** %5
%13 = call i32 @memset(i8* %12, i32 0, i32 64)
store i32 0, i32* %6
br label %14
14: ; preds = %81, %2
%15 = load i32, i32* %6
%16 = icmp sle i32 %15, 62
br i1 %16, label %17, label %82
17: ; preds = %14
%18 = load %struct.pollfd*, %struct.pollfd** %4
%19 = call i32 @poll(%struct.pollfd* %18, i32 1, i32 2000)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i8*, i8** %5
%24 = load i32, i32* @errno
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %23, i32 64, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %24, i8* %26)
store i32 -1, i32* %3
br label %84
28: ; preds = %17
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i8*, i8** %5
%33 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %32, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %84
34: ; preds = %28
%35 = load %struct.pollfd*, %struct.pollfd** %4
%36 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i8*, i8** %5
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = call i64 @read(i32 %37, i8* %41, i32 1)
store i64 %42, i64* %8
%43 = load i64, i64* %8
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %34
%46 = load i8*, i8** %5
%47 = load i32, i32* @errno
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %46, i32 64, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %47, i8* %49)
store i32 -1, i32* %3
br label %84
51: ; preds = %34
%52 = load i32, i32* %7
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i8*, i8** %5
%56 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %55, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %84
57: ; preds = %51
%58 = load i64, i64* %8
%59 = load i32, i32* %6
%60 = sext i32 %59 to i64
%61 = add nsw i64 %60, %58
%62 = trunc i64 %61 to i32
store i32 %62, i32* %6
%63 = load i8*, i8** %5
%64 = load i32, i32* %6
%65 = sub nsw i32 %64, 1
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %63, i64 %66
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp eq i32 %69, 10
br i1 %70, label %80, label %71
71: ; preds = %57
%72 = load i32, i32* %6
%73 = icmp sgt i32 %72, 1
br i1 %73, label %74, label %81
74: ; preds = %71
%75 = load i8*, i8** %5
%76 = getelementptr inbounds i8, i8* %75, i64 0
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
%79 = icmp eq i32 %78, 3
br i1 %79, label %80, label %81
80: ; preds = %74, %57
br label %82
81: ; preds = %74, %71
br label %14
82: ; preds = %80, %14
%83 = load i32, i32* %6
store i32 %83, i32* %3
br label %84
84: ; preds = %82, %54, %45, %31, %22
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@.str = [18 x i8] c"poll error %d: %s\00"
@errno = global i32 0
@.str.1 = [13 x i8] c"poll timeout\00"
@.str.2 = [18 x i8] c"read error %d: %s\00"
define i32 @rxline(%struct.pollfd* %0, i8* %1) {
%3 = load i32, i32* @POLLIN
%4 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %0, i64 0, i32 1
store i32 %3, i32* %4
%5 = tail call i32 @memset(i8* %1, i32 0, i32 64)
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %0, i64 0, i32 0
%7 = tail call i32 @poll(%struct.pollfd* %0, i32 1, i32 2000)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %44, %2
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 64, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %10, i8* %11)
br label %47
13: ; preds = %2, %44
%14 = phi i32 [ %45, %44 ], [ %7, %2 ]
%15 = phi i32 [ %31, %44 ], [ 0, %2 ]
%16 = icmp eq i32 %14, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %47
19: ; preds = %13
%20 = load i32, i32* %6
%21 = sext i32 %15 to i64
%22 = getelementptr inbounds i8, i8* %1, i64 %21
%23 = tail call i64 @read(i32 %20, i8* %22, i32 1)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 64, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %26, i8* %27)
br label %47
29: ; preds = %19
%30 = trunc i64 %23 to i32
%31 = add i32 %15, %30
%32 = add nsw i32 %31, -1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %1, i64 %33
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 10
br i1 %36, label %47, label %37
37: ; preds = %29
%38 = icmp sgt i32 %31, 1
br i1 %38, label %39, label %44
39: ; preds = %37
%40 = load i8, i8* %1
%41 = icmp ne i8 %40, 3
%42 = icmp ult i32 %31, 63
%43 = select i1 %41, i1 %42, i1 false
br i1 %43, label %44, label %47
44: ; preds = %39, %37
%45 = tail call i32 @poll(%struct.pollfd* %0, i32 1, i32 2000)
%46 = icmp slt i32 %45, 0
br i1 %46, label %9, label %13
47: ; preds = %29, %39, %25, %17, %9
%48 = phi i32 [ -1, %9 ], [ -1, %17 ], [ -1, %25 ], [ %31, %39 ], [ %31, %29 ]
ret i32 %48
}