#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 fd;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
int poll(struct pollfd *, int, int);
int recv(int, char *, int, int /*<<< orphan*/);
int read_line(char *out, int fd, int max) {
  int bytes, status;
  char ch;
  struct pollfd pfd;
  pfd.fd = fd;
  pfd.events = POLLIN;
  bytes = 0;
  status = 1;
  while (status > 0) {
    status = poll(&pfd, 1, 10000);
    if (status == 0)
      return -2;
    else if (status == -1 && errno == EINTR)
      continue;
    else if (status == -1)
      err(1, "read_line poll");
    status = recv(fd, &ch, 1, 0);
    if (status == 0)
      return -1;
    else if (status == -1)
      err(1, "read_line recv");
    if (bytes >= max)
      return -1;
    if (ch == '\n') {
      out[bytes] = '\0';
      return bytes;
    } else {
      out[bytes++] = ch;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    128b <read_line+0x15b>
lea    -0x30(%rbp),%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1050 <poll@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1198 <read_line+0x68>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1292 <read_line+0x162>
cmpl   $0xffffffff,-0x20(%rbp)
jne    11c1 <read_line+0x91>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
jne    11c1 <read_line+0x91>
jmp    1162 <read_line+0x32>
cmpl   $0xffffffff,-0x20(%rbp)
jne    11dc <read_line+0xac>
mov    $0x1,%edi
lea    0xe29(%rip),%rsi
call   1040 <err@plt>
jmp    11e1 <read_line+0xb1>
jmp    11e6 <read_line+0xb6>
mov    -0x14(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1212 <read_line+0xe2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <read_line+0x162>
cmpl   $0xffffffff,-0x20(%rbp)
jne    122d <read_line+0xfd>
mov    $0x1,%edi
lea    0xde7(%rip),%rsi
call   1040 <err@plt>
jmp    1232 <read_line+0x102>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     124a <read_line+0x11a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <read_line+0x162>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
jne    126e <read_line+0x13e>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1292 <read_line+0x162>
mov    -0x21(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1162 <read_line+0x32>
movl   $0x0,-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    $0x28,%rsp
mov    %rdi,0x10(%rsp)
mov    %esi,0xc(%rsp)
mov    %esi,0x18(%rsp)
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
xor    %r14d,%r14d
test   %edx,%edx
cmovle %r14d,%edx
mov    %rdx,0x20(%rsp)
mov    %eax,0x1c(%rsp)
mov    $0x1,%r12d
lea    0x18(%rsp),%rbp
mov    0x2e60(%rip),%r15
mov    0x2e51(%rip),%rbx
nop
test   %r12d,%r12d
jle    1228 <read_line+0xf8>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1050 <poll@plt>
cmp    $0xffffffff,%eax
jne    11c1 <read_line+0x91>
mov    (%r15),%rax
mov    $0xffffffff,%r12d
cmp    (%rbx),%rax
je     1180 <read_line+0x50>
mov    $0x1,%edi
lea    0xe46(%rip),%rsi
call   1040 <err@plt>
jmp    11cb <read_line+0x9b>
mov    $0xfffffffe,%r13d
test   %eax,%eax
je     122b <read_line+0xfb>
mov    0xc(%rsp),%edi
lea    0xb(%rsp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r12d
mov    $0xffffffff,%r13d
cmp    $0xffffffff,%eax
je     11f5 <read_line+0xc5>
test   %r12d,%r12d
jne    1206 <read_line+0xd6>
jmp    122b <read_line+0xfb>
mov    $0x1,%edi
lea    0xe0e(%rip),%rsi
call   1040 <err@plt>
cmp    0x20(%rsp),%r14
je     122b <read_line+0xfb>
movzbl 0xb(%rsp),%eax
cmp    $0xa,%al
je     123d <read_line+0x10d>
mov    0x10(%rsp),%rcx
mov    %al,(%rcx,%r14,1)
add    $0x1,%r14
jmp    1180 <read_line+0x50>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14d,%eax
mov    0x10(%rsp),%rcx
movb   $0x0,(%rcx,%rax,1)
mov    %r14d,%r13d
jmp    122b <read_line+0xfb>

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

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [15 x i8] c"read_line poll\00"
@.str.1 = [15 x i8] c"read_line recv\00"
define i32 @read_line(i8* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = alloca i8
%5 = alloca %struct.pollfd
%6 = bitcast %struct.pollfd* %5 to i8*
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %1, i32* %7
%8 = load i32, i32* @POLLIN
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
store i32 %8, i32* %9
%10 = call i32 @llvm.smax.i32(i32 %2, i32 0)
%11 = zext i32 %10 to i64
br label %12
12: ; preds = %39, %3
%13 = phi i64 [ %40, %39 ], [ 0, %3 ]
%14 = phi i32 [ %27, %39 ], [ 1, %3 ]
br label %15
15: ; preds = %12, %20
%16 = phi i32 [ -1, %20 ], [ %14, %12 ]
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %42
18: ; preds = %15
%19 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 10000)
switch i32 %19, label %26 [
i32 0, label %42
i32 -1, label %20
]
20: ; preds = %18
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %15, label %24
24: ; preds = %20
%25 = call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %18, %24
%27 = call i32 @recv(i32 %1, i8* %4, i32 1, i32 0)
switch i32 %27, label %30 [
i32 0, label %42
i32 -1, label %28
]
28: ; preds = %26
%29 = call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %26, %28
%31 = icmp eq i64 %13, %11
br i1 %31, label %42, label %32
32: ; preds = %30
%33 = load i8, i8* %4
%34 = icmp eq i8 %33, 10
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = trunc i64 %13 to i32
%37 = and i64 %13, 4294967295
%38 = getelementptr inbounds i8, i8* %0, i64 %37
store i8 0, i8* %38
br label %42
39: ; preds = %32
%40 = add nuw nsw i64 %13, 1
%41 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 %33, i8* %41
br label %12
42: ; preds = %30, %26, %18, %15, %35
%43 = phi i32 [ %36, %35 ], [ -2, %18 ], [ 0, %15 ], [ -1, %26 ], [ -1, %30 ]
ret i32 %43
}