#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 ssize_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/, struct timeval *);
scalar_t__ strlen(char *);
const char *fgfsread(int sock, int timeout) {
  static char buf[256];
  char *p;
  fd_set ready;
  struct timeval tv;
  ssize_t len;
  FD_ZERO(&ready);
  FD_SET(sock, &ready);
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  if (!select(32, &ready, 0, 0, &tv))
    return NULL;
  len = read(sock, buf, 256 - 1);
  if (len < 0) {
    perror("fgfsread");
    exit(EXIT_FAILURE);
  }
  if (len == 0)
    return NULL;
  for (p = &buf[len - 1]; p >= buf; p--)
    if (*p != '\015' && *p != '\012')
      break;
  *++p = '\0';
  return strlen(buf) ? buf : NULL;
}


x86 assembly (clang, -O0):
<fgfsread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x20,%edi
lea    -0x1c(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x30(%rbp),%r8
mov    %ecx,%edx
call   1060 <select@plt>
cmp    $0x0,%eax
jne    11cd <fgfsread+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    12b9 <fgfsread+0x149>
mov    -0xc(%rbp),%edi
lea    0x2e99(%rip),%rsi
mov    $0xff,%edx
call   1040 <read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1208 <fgfsread+0x98>
lea    0xe0b(%rip),%rdi
call   1070 <perror@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x34(%rbp)
jne    121f <fgfsread+0xaf>
movq   $0x0,-0x8(%rbp)
jmp    12b9 <fgfsread+0x149>
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
movslq %eax,%rcx
lea    0x2e41(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
lea    0x2e33(%rip),%rax
cmp    %rax,-0x18(%rbp)
jb     1282 <fgfsread+0x112>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
je     126c <fgfsread+0xfc>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     126c <fgfsread+0xfc>
jmp    1282 <fgfsread+0x112>
jmp    1271 <fgfsread+0x101>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1236 <fgfsread+0xc6>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,0x1(%rax)
lea    0x2dd4(%rip),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdx
xor    %eax,%eax
lea    0x2dc3(%rip),%rcx
cmp    $0x0,%rdx
cmovne %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgfsread>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
mov    %edi,%r14d
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %ebx,0x10(%rsp)
movq   $0x0,0x18(%rsp)
xor    %ebx,%ebx
lea    0x10(%rsp),%r8
mov    $0x20,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
je     120b <fgfsread+0xab>
lea    0x2eab(%rip),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1217 <fgfsread+0xb7>
je     1209 <fgfsread+0xa9>
add    $0xffffffff,%eax
add    %rbx,%rax
jmp    11e9 <fgfsread+0x89>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
jb     11f6 <fgfsread+0x96>
movzbl (%rax),%ecx
cmp    $0xd,%cl
je     11e0 <fgfsread+0x80>
cmp    $0xa,%cl
je     11e0 <fgfsread+0x80>
movb   $0x0,0x1(%rax)
xor    %eax,%eax
cmpb   $0x0,0x2e5d(%rip)
cmove  %rax,%rbx
jmp    120b <fgfsread+0xab>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde2(%rip),%rdi
call   1060 <perror@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@fgfsread.buf = internal global [256 x i8] zeroinitializer
@.str = [9 x i8] c"fgfsread\00"
@EXIT_FAILURE = global i32 0
define i8* @fgfsread(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = call i32 @FD_ZERO(i32* %7)
%11 = load i32, i32* %4
%12 = call i32 @FD_SET(i32 %11, i32* %7)
%13 = load i32, i32* %5
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %15
%16 = call i32 @select(i32 32, i32* %7, i32 0, i32 0, %struct.timeval* %8)
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %2
store i8* null, i8** %3
br label %62
19: ; preds = %2
%20 = load i32, i32* %4
%21 = call i32 @read(i32 %20, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @fgfsread.buf, i64 0, i64 0), i32 255)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %19
%29 = load i32, i32* %9
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
store i8* null, i8** %3
br label %62
32: ; preds = %28
%33 = load i32, i32* %9
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [256 x i8], [256 x i8]* @fgfsread.buf, i64 0, i64 %35
store i8* %36, i8** %6
br label %37
37: ; preds = %52, %32
%38 = load i8*, i8** %6
%39 = icmp uge i8* %38, getelementptr inbounds ([256 x i8], [256 x i8]* @fgfsread.buf, i64 0, i64 0)
br i1 %39, label %40, label %55
40: ; preds = %37
%41 = load i8*, i8** %6
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 13
br i1 %44, label %45, label %51
45: ; preds = %40
%46 = load i8*, i8** %6
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 10
br i1 %49, label %50, label %51
50: ; preds = %45
br label %55
51: ; preds = %45, %40
br label %52
52: ; preds = %51
%53 = load i8*, i8** %6
%54 = getelementptr inbounds i8, i8* %53, i32 -1
store i8* %54, i8** %6
br label %37
55: ; preds = %50, %37
%56 = load i8*, i8** %6
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %6
store i8 0, i8* %57
%58 = call i64 @strlen(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @fgfsread.buf, i64 0, i64 0))
%59 = icmp ne i64 %58, 0
%60 = zext i1 %59 to i64
%61 = select i1 %59, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @fgfsread.buf, i64 0, i64 0), i8* null
store i8* %61, i8** %3
br label %62
62: ; preds = %55, %31, %18
%63 = load i8*, i8** %3
ret i8* %63
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@fgfsread.buf = internal global [256 x i8] zeroinitializer
@.str = [9 x i8] c"fgfsread\00"
@EXIT_FAILURE = global i32 0
define i8* @fgfsread(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = call i32 @FD_ZERO(i32* %3)
%8 = call i32 @FD_SET(i32 %0, i32* %3)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %1, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %10
%11 = call i32 @select(i32 32, i32* %3, i32 0, i32 0, %struct.timeval* %4)
%12 = icmp eq i32 %11, 0
br i1 %12, label %38, label %13
13: ; preds = %2
%14 = call i32 @read(i32 %0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @fgfsread.buf, i64 0, i64 0), i32 255)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %13
%21 = icmp eq i32 %14, 0
br i1 %21, label %38, label %22
22: ; preds = %20
%23 = add nsw i32 %14, -1
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds [256 x i8], [256 x i8]* @fgfsread.buf, i64 0, i64 %24
br label %26
26: ; preds = %22, %29
%27 = phi i8* [ %30, %29 ], [ %25, %22 ]
%28 = load i8, i8* %27
switch i8 %28, label %32 [
i8 13, label %29
i8 10, label %29
]
29: ; preds = %26, %26
%30 = getelementptr inbounds i8, i8* %27, i64 -1
%31 = icmp ult i8* %30, getelementptr inbounds ([256 x i8], [256 x i8]* @fgfsread.buf, i64 0, i64 0)
br i1 %31, label %32, label %26
32: ; preds = %29, %26
%33 = phi i8* [ %30, %29 ], [ %27, %26 ]
%34 = getelementptr inbounds i8, i8* %33, i64 1
store i8 0, i8* %34
%35 = load i8, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @fgfsread.buf, i64 0, i64 0)
%36 = icmp eq i8 %35, 0
%37 = select i1 %36, i8* null, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @fgfsread.buf, i64 0, i64 0)
br label %38
38: ; preds = %20, %2, %32
%39 = phi i8* [ %37, %32 ], [ null, %2 ], [ null, %20 ]
ret i8* %39
}