#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_usec;
  scalar_t__ tv_sec;
};
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 fdgetline(int fd, char *buffer, int maxlen) {
  fd_set rfd;
  struct timeval tv;
  int nr = 0;
  int rc = 0;
  char inch[2];
  int rv __attribute__((unused));
  for (;;) {
    tv.tv_sec = 0;
    tv.tv_usec = 1000;
    FD_ZERO(&rfd);
    FD_SET(fd, &rfd);
    rc = select(fd + 1, &rfd, NULL, NULL, &tv);
    if (rc <= 0) {
      return nr;
    }
    if (FD_ISSET(fd, &rfd)) {
      rv = read(fd, inch, 1);
      if (inch[0] == '\n') {
        return nr;
      }
      if (inch[0] != '\r') {
        buffer[nr++] = inch[0];
        buffer[nr] = 0;
      }
      if (nr == maxlen) {
        return maxlen;
      }
    }
  }
}


x86 assembly (clang, -O0):
<fdgetline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x3e8,-0x28(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x8(%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)
cmpl   $0x0,-0x30(%rbp)
jg     11c6 <fdgetline+0x76>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125a <fdgetline+0x10a>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1255 <fdgetline+0x105>
mov    -0x8(%rbp),%edi
lea    -0x32(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x38(%rbp)
movsbl -0x32(%rbp),%eax
cmp    $0xa,%eax
jne    1208 <fdgetline+0xb8>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125a <fdgetline+0x10a>
movsbl -0x32(%rbp),%eax
cmp    $0xd,%eax
je     1239 <fdgetline+0xe9>
mov    -0x32(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1250 <fdgetline+0x100>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125a <fdgetline+0x10a>
jmp    1255 <fdgetline+0x105>
jmp    1170 <fdgetline+0x20>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdgetline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebx
movq   $0x0,0x10(%rsp)
movl   $0x3e8,0x8(%rsp)
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1070 <FD_SET@plt>
lea    0x1(%rbx),%r12d
xor    %r13d,%r13d
lea    0x8(%rsp),%r8
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
jle    1243 <fdgetline+0xf3>
xor    %r13d,%r13d
lea    0x4(%rsp),%rbp
jmp    1211 <fdgetline+0xc1>
nopl   0x0(%rax,%rax,1)
movslq %r13d,%rcx
lea    0x1(%rcx),%r13
mov    %al,(%r14,%rcx,1)
movb   $0x0,0x1(%r14,%rcx,1)
cmp    %r15d,%r13d
je     1240 <fdgetline+0xf0>
movq   $0x0,0x10(%rsp)
movl   $0x3e8,0x8(%rsp)
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1070 <FD_SET@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1060 <select@plt>
test   %eax,%eax
jle    1243 <fdgetline+0xf3>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11d6 <fdgetline+0x86>
mov    %ebx,%edi
lea    0x2(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
movzbl 0x2(%rsp),%eax
cmp    $0xd,%al
je     11d1 <fdgetline+0x81>
cmp    $0xa,%al
jne    11c0 <fdgetline+0x70>
jmp    1243 <fdgetline+0xf3>
mov    %r15d,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i32 @fdgetline(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32
%12 = alloca [2 x i8]
%13 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %10
store i32 0, i32* %11
br label %14
14: ; preds = %65, %3
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %15
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 1000, i32* %16
%17 = call i32 @FD_ZERO(i32* %8)
%18 = load i32, i32* %5
%19 = call i32 @FD_SET(i32 %18, i32* %8)
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
%22 = call i32 @select(i32 %21, i32* %8, i32* null, i32* null, %struct.timeval* %9)
store i32 %22, i32* %11
%23 = load i32, i32* %11
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = load i32, i32* %10
store i32 %26, i32* %4
br label %66
27: ; preds = %14
%28 = load i32, i32* %5
%29 = call i64 @FD_ISSET(i32 %28, i32* %8)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %65
31: ; preds = %27
%32 = load i32, i32* %5
%33 = getelementptr inbounds [2 x i8], [2 x i8]* %12, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 1)
store i32 %34, i32* %13
%35 = getelementptr inbounds [2 x i8], [2 x i8]* %12, i64 0, i64 0
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %41
39: ; preds = %31
%40 = load i32, i32* %10
store i32 %40, i32* %4
br label %66
41: ; preds = %31
%42 = getelementptr inbounds [2 x i8], [2 x i8]* %12, i64 0, i64 0
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 13
br i1 %45, label %46, label %58
46: ; preds = %41
%47 = getelementptr inbounds [2 x i8], [2 x i8]* %12, i64 0, i64 0
%48 = load i8, i8* %47
%49 = load i8*, i8** %6
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds i8, i8* %49, i64 %52
store i8 %48, i8* %53
%54 = load i8*, i8** %6
%55 = load i32, i32* %10
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
store i8 0, i8* %57
br label %58
58: ; preds = %46, %41
%59 = load i32, i32* %10
%60 = load i32, i32* %7
%61 = icmp eq i32 %59, %60
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = load i32, i32* %7
store i32 %63, i32* %4
br label %66
64: ; preds = %58
br label %65
65: ; preds = %64, %27
br label %14
66: ; preds = %62, %39, %25
%67 = load i32, i32* %4
ret i32 %67
}

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