#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 /*<<< orphan*/ memcpy(char *, char *, size_t);
size_t read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
size_t __tbexec_read(int fd, int timeout_in_sec, char *out, size_t outsz) {
  char buffer[4096];
  fd_set r_set;
  size_t offset;
  struct timeval timeout;
  offset = 0;
  timeout.tv_sec = timeout_in_sec;
  timeout.tv_usec = 0;
  FD_ZERO(&r_set);
  FD_SET(fd, &r_set);
  while (1) {
    int rc = select(fd + 1, &r_set, NULL, NULL, &timeout);
    if (rc == -1) {
      return (offset = -1);
    }
    if (timeout.tv_sec <= 0) {
      return (-2);
    }
    if (FD_ISSET(fd, &r_set)) {
      size_t rc = read(fd, buffer, 4096);
      if (rc == 0) {
        break;
      } else if (rc == (size_t)-1) {
        return (-1);
      } else {
        if (outsz > rc) {
          memcpy(out + offset, buffer, rc);
          outsz -= rc;
          offset += rc;
        } else {
          return (-3);
        }
      }
    }
  }
  return (offset);
}


x86 assembly (clang, -O0):
<__tbexec_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x1030(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x1040(%rbp)
movq   $0x0,-0x1038(%rbp)
lea    -0x1024(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x1024(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
lea    -0x1024(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x1040(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x1044(%rbp)
cmpl   $0xffffffff,-0x1044(%rbp)
jne    11fe <__tbexec_read+0x9e>
movq   $0xffffffffffffffff,-0x1030(%rbp)
ff ff ff ff
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1301 <__tbexec_read+0x1a1>
cmpl   $0x0,-0x1040(%rbp)
jg     1218 <__tbexec_read+0xb8>
movq   $0xfffffffffffffffe,-0x8(%rbp)
ff
jmp    1301 <__tbexec_read+0x1a1>
mov    -0xc(%rbp),%edi
lea    -0x1024(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12f1 <__tbexec_read+0x191>
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,-0x1050(%rbp)
cmpq   $0x0,-0x1050(%rbp)
jne    125f <__tbexec_read+0xff>
jmp    12f6 <__tbexec_read+0x196>
cmpq   $0xffffffffffffffff,-0x1050(%rbp)
ff
jne    127a <__tbexec_read+0x11a>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1301 <__tbexec_read+0x1a1>
mov    -0x20(%rbp),%rax
cmp    -0x1050(%rbp),%rax
jbe    12d5 <__tbexec_read+0x175>
mov    -0x18(%rbp),%rdi
add    -0x1030(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x1050(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x1050(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x1050(%rbp),%rax
add    -0x1030(%rbp),%rax
mov    %rax,-0x1030(%rbp)
jmp    12e2 <__tbexec_read+0x182>
movq   $0xfffffffffffffffd,-0x8(%rbp)
ff
jmp    1301 <__tbexec_read+0x1a1>
jmp    12e7 <__tbexec_read+0x187>
jmp    12ec <__tbexec_read+0x18c>
jmp    12f1 <__tbexec_read+0x191>
jmp    11b3 <__tbexec_read+0x53>
mov    -0x1030(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__tbexec_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    %rcx,%rbx
mov    %rdx,0x18(%rsp)
mov    %edi,%r15d
mov    %esi,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1060 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r14,%rsi
call   1080 <FD_SET@plt>
lea    0x1(%r15),%ebp
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
mov    $0xffffffffffffffff,%r14
cmp    $0xffffffff,%eax
je     126d <__tbexec_read+0x10d>
xor    %r12d,%r12d
lea    0x4(%rsp),%r13
jmp    11f8 <__tbexec_read+0x98>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1070 <select@plt>
cmp    $0xffffffff,%eax
je     1258 <__tbexec_read+0xf8>
cmpl   $0x0,0x8(%rsp)
jle    124f <__tbexec_read+0xef>
mov    %r15d,%edi
mov    %r13,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11e0 <__tbexec_read+0x80>
mov    %r15d,%edi
lea    0x20(%rsp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,%r14
cmp    $0xffffffffffffffff,%rax
je     126d <__tbexec_read+0x10d>
test   %r14,%r14
je     1261 <__tbexec_read+0x101>
sub    %r14,%rbx
jbe    1266 <__tbexec_read+0x106>
mov    0x18(%rsp),%rax
lea    (%rax,%r12,1),%rdi
lea    0x20(%rsp),%rsi
mov    %r14,%rdx
call   1050 <memcpy@plt>
add    %r14,%r12
jmp    11e0 <__tbexec_read+0x80>
mov    $0xfffffffffffffffe,%r14
jmp    126d <__tbexec_read+0x10d>
mov    $0xffffffffffffffff,%r14
jmp    126d <__tbexec_read+0x10d>
mov    %r12,%r14
jmp    126d <__tbexec_read+0x10d>
mov    $0xfffffffffffffffd,%r14
mov    %r14,%rax
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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