#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 */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int _readn(register int fd, register char *ptr, register int nbytes,
           void (*timeout_function)(), int doselect) {
  int nleft, nread;
  int r;
  fd_set set;
  struct timeval ltspfs_timeout;
  struct timeval *timeout_ptr = &ltspfs_timeout;
  FD_ZERO(&set);
  FD_SET(fd, &set);
  if (!doselect)
    timeout_ptr = NULL;
  nleft = nbytes;
  while (nleft > 0) {
    ltspfs_timeout.tv_sec = 30;
    ltspfs_timeout.tv_usec = 0;
    r = select(FD_SETSIZE, &set, NULL, NULL, timeout_ptr);
    if (r < 0)
      return r;
    else if (r == 0)
      timeout_function();
    else {
      nread = read(fd, ptr, nleft);
      if (nread < 0)
        return (nread);
      else if (nread == 0)
        break;
      nleft -= nread;
      ptr += nread;
    }
  }
  return (nbytes - nleft);
}


x86 assembly (clang, -O0):
<_readn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
lea    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x34(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1060 <FD_SET@plt>
cmpl   $0x0,-0x24(%rbp)
jne    1189 <_readn+0x49>
movq   $0x0,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    1252 <_readn+0x112>
movl   $0x1e,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%r8
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11dd <_readn+0x9d>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125b <_readn+0x11b>
cmpl   $0x0,-0x30(%rbp)
jne    11f1 <_readn+0xb1>
mov    $0x0,%al
call   *-0x20(%rbp)
jmp    1248 <_readn+0x108>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1218 <_readn+0xd8>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125b <_readn+0x11b>
cmpl   $0x0,-0x2c(%rbp)
jne    1227 <_readn+0xe7>
jmp    1252 <_readn+0x112>
jmp    122c <_readn+0xec>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    124d <_readn+0x10d>
jmp    118f <_readn+0x4f>
mov    -0x14(%rbp),%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_readn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%ebp
mov    %rcx,0x10(%rsp)
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%r12d
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
xor    %eax,%eax
test   %ebp,%ebp
lea    0x18(%rsp),%r13
cmove  %rax,%r13
mov    %r15d,%ebp
test   %r15d,%r15d
jle    11f7 <_readn+0xb7>
lea    0xc(%rsp),%r14
mov    %r15d,%ebp
jmp    11aa <_readn+0x6a>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   *0x10(%rsp)
test   %ebp,%ebp
jle    11f7 <_readn+0xb7>
movl   $0x1e,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1050 <select@plt>
test   %eax,%eax
js     11fc <_readn+0xbc>
je     11a0 <_readn+0x60>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
test   %eax,%eax
js     11fc <_readn+0xbc>
je     11f7 <_readn+0xb7>
sub    %eax,%ebp
mov    %eax,%eax
add    %rax,%rbx
test   %ebp,%ebp
jg     11aa <_readn+0x6a>
sub    %ebp,%r15d
jmp    11ff <_readn+0xbf>
mov    %eax,%r15d
mov    %r15d,%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 }
@FD_SETSIZE = global i32 0
define i32 @_readn(i32 %0, i8* %1, i32 %2, void (...)* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca void (...)*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.timeval
%17 = alloca %struct.timeval*
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store void (...)* %3, void (...)** %10
store i32 %4, i32* %11
store %struct.timeval* %16, %struct.timeval** %17
%18 = call i32 @FD_ZERO(i32* %15)
%19 = load i32, i32* %7
%20 = call i32 @FD_SET(i32 %19, i32* %15)
%21 = load i32, i32* %11
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %5
store %struct.timeval* null, %struct.timeval** %17
br label %24
24: ; preds = %23, %5
%25 = load i32, i32* %9
store i32 %25, i32* %12
br label %26
26: ; preds = %67, %24
%27 = load i32, i32* %12
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %68
29: ; preds = %26
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
store i32 30, i32* %30
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
store i64 0, i64* %31
%32 = load i32, i32* @FD_SETSIZE
%33 = load %struct.timeval*, %struct.timeval** %17
%34 = call i32 @select(i32 %32, i32* %15, i32* null, i32* null, %struct.timeval* %33)
store i32 %34, i32* %14
%35 = load i32, i32* %14
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = load i32, i32* %14
store i32 %38, i32* %6
br label %72
39: ; preds = %29
%40 = load i32, i32* %14
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = load void (...)*, void (...)** %10
br label %66
44: ; preds = %39
%45 = load i32, i32* %7
%46 = load i8*, i8** %8
%47 = load i32, i32* %12
%48 = call i32 @read(i32 %45, i8* %46, i32 %47)
store i32 %48, i32* %13
%49 = load i32, i32* %13
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %44
%52 = load i32, i32* %13
store i32 %52, i32* %6
br label %72
53: ; preds = %44
%54 = load i32, i32* %13
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %53
br label %68
57: ; preds = %53
br label %58
58: ; preds = %57
%59 = load i32, i32* %13
%60 = load i32, i32* %12
%61 = sub nsw i32 %60, %59
store i32 %61, i32* %12
%62 = load i32, i32* %13
%63 = load i8*, i8** %8
%64 = sext i32 %62 to i64
%65 = getelementptr inbounds i8, i8* %63, i64 %64
store i8* %65, i8** %8
br label %66
66: ; preds = %58, %42
br label %67
67: ; preds = %66
br label %26
68: ; preds = %56, %26
%69 = load i32, i32* %9
%70 = load i32, i32* %12
%71 = sub nsw i32 %69, %70
store i32 %71, i32* %6
br label %72
72: ; preds = %68, %51, %37
%73 = load i32, i32* %6
ret i32 %73
}

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