#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,
           int timeout_secs, 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 = timeout_secs;
    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    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9d,-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    118c <_readn+0x4c>
movq   $0x0,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    1254 <_readn+0x114>
mov    -0x18(%rbp),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2e2f(%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    11df <_readn+0x9f>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125d <_readn+0x11d>
cmpl   $0x0,-0x30(%rbp)
jne    11f3 <_readn+0xb3>
mov    $0x0,%al
call   *-0x20(%rbp)
jmp    124a <_readn+0x10a>
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    121a <_readn+0xda>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125d <_readn+0x11d>
cmpl   $0x0,-0x2c(%rbp)
jne    1229 <_readn+0xe9>
jmp    1254 <_readn+0x114>
jmp    122e <_readn+0xee>
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    124f <_readn+0x10f>
jmp    1192 <_readn+0x52>
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    %r9d,%r15d
mov    %r8,0x10(%rsp)
mov    %ecx,%r13d
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r12d
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
xor    %eax,%eax
test   %r15d,%r15d
lea    0x18(%rsp),%rbp
cmove  %rax,%rbp
mov    %r14d,%r15d
test   %r14d,%r14d
jle    11fa <_readn+0xba>
mov    %r14d,%r15d
jmp    11ab <_readn+0x6b>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   *0x10(%rsp)
test   %r15d,%r15d
jle    11fa <_readn+0xba>
mov    %r13d,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1050 <select@plt>
test   %eax,%eax
js     11ff <_readn+0xbf>
je     11a0 <_readn+0x60>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <read@plt>
test   %eax,%eax
js     11ff <_readn+0xbf>
je     11fa <_readn+0xba>
sub    %eax,%r15d
mov    %eax,%eax
add    %rax,%rbx
test   %r15d,%r15d
jg     11ab <_readn+0x6b>
sub    %r15d,%r14d
jmp    1202 <_readn+0xc2>
mov    %eax,%r14d
mov    %r14d,%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, i32 %3, void (...)* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca void (...)*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca %struct.timeval
%19 = alloca %struct.timeval*
store i32 %0, i32* %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store void (...)* %4, void (...)** %12
store i32 %5, i32* %13
store %struct.timeval* %18, %struct.timeval** %19
%20 = call i32 @FD_ZERO(i32* %17)
%21 = load i32, i32* %8
%22 = call i32 @FD_SET(i32 %21, i32* %17)
%23 = load i32, i32* %13
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %6
store %struct.timeval* null, %struct.timeval** %19
br label %26
26: ; preds = %25, %6
%27 = load i32, i32* %10
store i32 %27, i32* %14
br label %28
28: ; preds = %70, %26
%29 = load i32, i32* %14
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %71
31: ; preds = %28
%32 = load i32, i32* %11
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 0
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 1
store i64 0, i64* %34
%35 = load i32, i32* @FD_SETSIZE
%36 = load %struct.timeval*, %struct.timeval** %19
%37 = call i32 @select(i32 %35, i32* %17, i32* null, i32* null, %struct.timeval* %36)
store i32 %37, i32* %16
%38 = load i32, i32* %16
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = load i32, i32* %16
store i32 %41, i32* %7
br label %75
42: ; preds = %31
%43 = load i32, i32* %16
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = load void (...)*, void (...)** %12
br label %69
47: ; preds = %42
%48 = load i32, i32* %8
%49 = load i8*, i8** %9
%50 = load i32, i32* %14
%51 = call i32 @read(i32 %48, i8* %49, i32 %50)
store i32 %51, i32* %15
%52 = load i32, i32* %15
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %47
%55 = load i32, i32* %15
store i32 %55, i32* %7
br label %75
56: ; preds = %47
%57 = load i32, i32* %15
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %56
br label %71
60: ; preds = %56
br label %61
61: ; preds = %60
%62 = load i32, i32* %15
%63 = load i32, i32* %14
%64 = sub nsw i32 %63, %62
store i32 %64, i32* %14
%65 = load i32, i32* %15
%66 = load i8*, i8** %9
%67 = sext i32 %65 to i64
%68 = getelementptr inbounds i8, i8* %66, i64 %67
store i8* %68, i8** %9
br label %69
69: ; preds = %61, %45
br label %70
70: ; preds = %69
br label %28
71: ; preds = %59, %28
%72 = load i32, i32* %10
%73 = load i32, i32* %14
%74 = sub nsw i32 %72, %73
store i32 %74, i32* %7
br label %75
75: ; preds = %71, %54, %40
%76 = load i32, i32* %7
ret i32 %76
}

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