#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_usec;
  int /*<<< orphan*/ tv_sec;
};
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ process_cancel_request(int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
void wait_for_data_from(pid_t pid, FILE *f, int fd) {
  const struct timeval ts_init = {.tv_sec = 0, .tv_usec = 1000};
  struct timeval ts;
  int select_result;
  fd_set read_ready;
  FD_ZERO(&read_ready);
  fd = (f != NULL) ? fileno(f) : fd;
  do {
    process_cancel_request(pid);
    ts = ts_init;
    FD_SET(fd, &read_ready);
    select_result = select(fd + 1, &read_ready, NULL, NULL, &ts);
  } while (select_result == 0 || (select_result == -1 && errno == EINTR));
}


x86 assembly (clang, -O0):
<wait_for_data_from>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0xe97(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <FD_ZERO@plt>
cmpq   $0x0,-0x10(%rbp)
je     1192 <wait_for_data_from+0x42>
mov    -0x10(%rbp),%rdi
call   1030 <fileno@plt>
mov    %eax,-0x34(%rbp)
jmp    1198 <wait_for_data_from+0x48>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
call   1070 <process_cancel_request@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x1,%al
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x35(%rbp)
je     1215 <wait_for_data_from+0xc5>
xor    %eax,%eax
cmpl   $0xffffffff,-0x2c(%rbp)
mov    %al,-0x36(%rbp)
jne    120f <wait_for_data_from+0xbf>
mov    0x2de4(%rip),%rax
mov    (%rax),%rax
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x36(%rbp)
mov    -0x36(%rbp),%al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    119e <wait_for_data_from+0x4e>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_data_from>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0xc(%rsp),%rdi
call   1040 <FD_ZERO@plt>
test   %rbx,%rbx
je     1181 <wait_for_data_from+0x31>
mov    %rbx,%rdi
call   1030 <fileno@plt>
mov    %eax,%r15d
lea    0x1(%r15),%ebp
lea    0xc(%rsp),%rbx
lea    0x10(%rsp),%r12
mov    0x2e42(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%edi
call   1070 <process_cancel_request@plt>
movq   $0x3e8,0x10(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1050 <select@plt>
test   %eax,%eax
je     11a0 <wait_for_data_from+0x50>
cmp    $0xffffffff,%eax
jne    11e6 <wait_for_data_from+0x96>
mov    0x0(%r13),%rax
mov    0x2dff(%rip),%rcx
cmp    %rax,(%rcx)
je     11a0 <wait_for_data_from+0x50>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@__const.wait_for_data_from.ts_init = %struct.timeval { i32 1000, i32 0 }
@errno = global i64 0
@EINTR = global i64 0
define void @wait_for_data_from(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%11 = bitcast %struct.timeval* %7 to i8*
%12 = call i32 @FD_ZERO(i32* %10)
%13 = load i32*, i32** %5
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32*, i32** %5
%17 = call i32 @fileno(i32* %16)
br label %20
18: ; preds = %3
%19 = load i32, i32* %6
br label %20
20: ; preds = %18, %15
%21 = phi i32 [ %17, %15 ], [ %19, %18 ]
store i32 %21, i32* %6
br label %22
22: ; preds = %44, %20
%23 = load i32, i32* %4
%24 = call i32 @process_cancel_request(i32 %23)
%25 = bitcast %struct.timeval* %8 to i8*
%26 = bitcast %struct.timeval* %7 to i8*
%27 = load i32, i32* %6
%28 = call i32 @FD_SET(i32 %27, i32* %10)
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
%31 = call i32 @select(i32 %30, i32* %10, i32* null, i32* null, %struct.timeval* %8)
store i32 %31, i32* %9
br label %32
32: ; preds = %22
%33 = load i32, i32* %9
%34 = icmp eq i32 %33, 0
br i1 %34, label %44, label %35
35: ; preds = %32
%36 = load i32, i32* %9
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i64, i64* @errno
%40 = load i64, i64* @EINTR
%41 = icmp eq i64 %39, %40
br label %42
42: ; preds = %38, %35
%43 = phi i1 [ false, %35 ], [ %41, %38 ]
br label %44
44: ; preds = %42, %32
%45 = phi i1 [ true, %32 ], [ %43, %42 ]
br i1 %45, label %22, label %46
46: ; preds = %44
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @wait_for_data_from(i32 %0, i32* %1, i32 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.timeval*
%6 = alloca i32
%7 = bitcast i64* %4 to i8*
%8 = bitcast i32* %6 to i8*
%9 = call i32 @FD_ZERO(i32* %6)
%10 = icmp eq i32* %1, null
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = call i32 @fileno(i32* %1)
br label %13
13: ; preds = %3, %11
%14 = phi i32 [ %12, %11 ], [ %2, %3 ]
%15 = add nsw i32 %14, 1
br label %16
16: ; preds = %27, %13
%17 = call i32 @process_cancel_request(i32 %0)
store i64 1000, i64* %4
%18 = call i32 @FD_SET(i32 %14, i32* %6)
%19 = call i32 @select(i32 %15, i32* %6, i32* null, i32* null, %struct.timeval* %5)
%20 = icmp eq i32 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %16
%22 = icmp eq i32 %19, -1
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
%26 = select i1 %22, i1 %25, i1 false
br i1 %26, label %27, label %28
27: ; preds = %21, %16
br label %16
28: ; preds = %21
ret void
}