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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
char pipeHelperReadChar(int fd, int timeout) {
  char d;
  struct timeval time = {timeout, 0};
  fd_set read_fds;
  FD_ZERO(&read_fds);
  FD_SET(fd, &read_fds);
  if (select(fd + 1, &read_fds, NULL, NULL, &time) <= 0)
    return 0;
  if (read(fd, &d, 1) != 1)
    return 0;
  return d;
}


x86 assembly (clang, -O0):
<pipeHelperReadChar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x0,%rax
jg     119d <pipeHelperReadChar+0x5d>
movb   $0x0,-0x1(%rbp)
jmp    11c6 <pipeHelperReadChar+0x86>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     11c0 <pipeHelperReadChar+0x80>
movb   $0x0,-0x1(%rbp)
jmp    11c6 <pipeHelperReadChar+0x86>
mov    -0xd(%rbp),%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipeHelperReadChar>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    %esi,0x10(%rsp)
movl   $0x0,0x14(%rsp)
lea    0x1c(%rsp),%r15
mov    %r15,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbx),%edi
xor    %r14d,%r14d
lea    0x10(%rsp),%r8
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %rax,%rax
jle    11a7 <pipeHelperReadChar+0x67>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
movzbl 0xf(%rsp),%eax
cmove  %eax,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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