#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int read_line(int fd, char *buf, int len) {
  fd_set rfds;
  struct timeval tv;
  char ch;
  int n, copy = 0;
  for (;;) {
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    tv.tv_sec = 2;
    tv.tv_usec = 0;
    if ((n = select(fd + 1, &rfds, NULL, NULL, &tv)) <= 0)
      break;
    if (FD_ISSET(fd, &rfds)) {
      if ((n = read(fd, &ch, 1)) == 1) {
        buf[copy++] = ch;
        if (ch == '\n')
          break;
      }
    }
  }
  return copy;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x34(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <FD_SET@plt>
movl   $0x2,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jg     11b8 <read_line+0x68>
jmp    1224 <read_line+0xd4>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     121f <read_line+0xcf>
mov    -0x4(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x1,%eax
jne    121a <read_line+0xca>
mov    -0x29(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    1215 <read_line+0xc5>
jmp    1224 <read_line+0xd4>
jmp    121a <read_line+0xca>
jmp    121f <read_line+0xcf>
jmp    1169 <read_line+0x19>
mov    -0x34(%rbp),%eax
add    $0x40,%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    %rsi,0x20(%rsp)
mov    %edi,%ebx
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1070 <FD_SET@plt>
movl   $0x2,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x1(%rbx),%r15d
xor    %r12d,%r12d
lea    0x10(%rsp),%r8
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
jle    1244 <read_line+0xf4>
xor    %r12d,%r12d
lea    0xc(%rsp),%rbp
lea    0x10(%rsp),%r13
lea    0xb(%rsp),%r14
jmp    1209 <read_line+0xb9>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1070 <FD_SET@plt>
movl   $0x2,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1060 <select@plt>
test   %eax,%eax
jle    1244 <read_line+0xf4>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11d0 <read_line+0x80>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    11d0 <read_line+0x80>
movzbl 0xb(%rsp),%eax
movslq %r12d,%rcx
add    $0x1,%r12d
mov    0x20(%rsp),%rdx
mov    %al,(%rdx,%rcx,1)
cmp    $0xa,%al
jne    11d0 <read_line+0x80>
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i32 @read_line(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %11
br label %12
12: ; preds = %44, %3
%13 = call i32 @FD_ZERO(i32* %7)
%14 = load i32, i32* %4
%15 = call i32 @FD_SET(i32 %14, i32* %7)
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 2, i32* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %17
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
%20 = call i32 @select(i32 %19, i32* %7, i32* null, i32* null, %struct.timeval* %8)
store i32 %20, i32* %10
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %12
br label %45
23: ; preds = %12
%24 = load i32, i32* %4
%25 = call i64 @FD_ISSET(i32 %24, i32* %7)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %44
27: ; preds = %23
%28 = load i32, i32* %4
%29 = call i32 @read(i32 %28, i8* %9, i32 1)
store i32 %29, i32* %10
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %43
31: ; preds = %27
%32 = load i8, i8* %9
%33 = load i8*, i8** %5
%34 = load i32, i32* %11
%35 = add nsw i32 %34, 1
store i32 %35, i32* %11
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
store i8 %32, i8* %37
%38 = load i8, i8* %9
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 10
br i1 %40, label %41, label %42
41: ; preds = %31
br label %45
42: ; preds = %31
br label %43
43: ; preds = %42, %27
br label %44
44: ; preds = %43, %23
br label %12
45: ; preds = %41, %22
%46 = load i32, i32* %11
ret i32 %46
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @read_line(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i8
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.timeval* %5 to i8*
%9 = call i32 @FD_ZERO(i32* %4)
%10 = call i32 @FD_SET(i32 %0, i32* %4)
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 2, i32* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %12
%13 = add nsw i32 %0, 1
%14 = call i32 @select(i32 %13, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%15 = icmp slt i32 %14, 1
br i1 %15, label %35, label %16
16: ; preds = %3, %29
%17 = phi i32 [ %30, %29 ], [ 0, %3 ]
%18 = call i64 @FD_ISSET(i32 %0, i32* %4)
%19 = icmp eq i64 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %16
%21 = call i32 @read(i32 %0, i8* %6, i32 1)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load i8, i8* %6
%25 = add nsw i32 %17, 1
%26 = sext i32 %17 to i64
%27 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 %24, i8* %27
%28 = icmp eq i8 %24, 10
br i1 %28, label %35, label %29
29: ; preds = %20, %23, %16
%30 = phi i32 [ %25, %23 ], [ %17, %20 ], [ %17, %16 ]
%31 = call i32 @FD_ZERO(i32* %4)
%32 = call i32 @FD_SET(i32 %0, i32* %4)
store i32 2, i32* %11
store i64 0, i64* %12
%33 = call i32 @select(i32 %13, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%34 = icmp slt i32 %33, 1
br i1 %34, label %35, label %16
35: ; preds = %29, %23, %3
%36 = phi i32 [ 0, %3 ], [ %25, %23 ], [ %30, %29 ]
ret i32 %36
}