#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 read(int, void *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int read_chunk(int fd, void *buf, int ml) {
  fd_set rfd;
  struct timeval tv;
  tv.tv_sec = 1;
  tv.tv_usec = 0;
  int rv __attribute__((unused));
  FD_ZERO(&rfd);
  FD_SET(fd, &rfd);
  rv = select(9999, &rfd, NULL, NULL, &tv);
  if (FD_ISSET(fd, &rfd)) {
    return read(fd, buf, ml);
  }
  return -1;
}


x86 assembly (clang, -O0):
<read_chunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    $0x270f,%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11cf <read_chunk+0x7f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x4(%rbp)
jmp    11d6 <read_chunk+0x86>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_chunk>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
lea    0x8(%rsp),%r8
mov    $0x270f,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11be <read_chunk+0x6e>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <read@plt>
jmp    11c3 <read_chunk+0x73>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i32 @read_chunk(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
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 1, i32* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %12
%13 = call i32 @FD_ZERO(i32* %8)
%14 = load i32, i32* %5
%15 = call i32 @FD_SET(i32 %14, i32* %8)
%16 = call i32 @select(i32 9999, i32* %8, i32* null, i32* null, %struct.timeval* %9)
store i32 %16, i32* %10
%17 = load i32, i32* %5
%18 = call i64 @FD_ISSET(i32 %17, i32* %8)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %3
%21 = load i32, i32* %5
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = call i32 @read(i32 %21, i8* %22, i32 %23)
store i32 %24, i32* %4
br label %26
25: ; preds = %3
store i32 -1, i32* %4
br label %26
26: ; preds = %25, %20
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @read_chunk(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 1, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %9
%10 = call i32 @FD_ZERO(i32* %4)
%11 = call i32 @FD_SET(i32 %0, i32* %4)
%12 = call i32 @select(i32 9999, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%13 = call i64 @FD_ISSET(i32 %0, i32* %4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %3
%16 = call i32 @read(i32 %0, i8* %1, i32 %2)
br label %17
17: ; preds = %3, %15
%18 = phi i32 [ %16, %15 ], [ -1, %3 ]
ret i32 %18
}