#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 {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
struct pollfd {
  int dummy;
};
typedef int /*<<< orphan*/ nfds_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int poll(struct pollfd *, int /*<<< orphan*/, int);
int poll_retry(struct pollfd *fds, nfds_t nfsd, int timeout) {
  int ret;
  int time_remain = timeout;
  int time_elapsed = 0;
  struct timeval start_time;
  struct timeval tv;
  gettimeofday(&start_time, NULL);
  do {
    ret = poll(fds, nfsd, time_remain);
    gettimeofday(&tv, NULL);
    time_elapsed = ((int)(tv.tv_sec - start_time.tv_sec) * 1000 +
                    (int)(tv.tv_usec - start_time.tv_usec) / 1000);
    time_remain = timeout - time_elapsed;
  } while (ret == -1 && errno == EINTR && time_remain > 0);
  return ret;
}


x86 assembly (clang, -O0):
<poll_retry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <poll@plt>
mov    %eax,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x38(%rbp),%rax
sub    -0x28(%rbp),%rax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x14(%rbp)
mov    %al,-0x45(%rbp)
jne    11d7 <poll_retry+0xb7>
mov    0x2e2b(%rip),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    0x2e17(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x45(%rbp)
jne    11d7 <poll_retry+0xb7>
cmpl   $0x0,-0x18(%rbp)
setg   %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
test   $0x1,%al
jne    114c <poll_retry+0x2c>
mov    -0x14(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poll_retry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,%r13
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x18(%rsp),%rbp
mov    0x20(%rsp),%r15
mov    %r12d,%edx
mov    %r13,%rdi
mov    %r14d,%esi
call   1040 <poll@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmp    $0xffffffff,%ebx
jne    11b6 <poll_retry+0x96>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%rax
jne    11b6 <poll_retry+0x96>
mov    0x8(%rsp),%eax
mov    0x10(%rsp),%ecx
sub    %r15d,%ecx
imul   $0xfffffc18,%ecx,%edx
sub    %ebp,%eax
cltq
imul   $0xffffffffef9db22d,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
add    %r12d,%edx
add    %eax,%edx
test   %edx,%edx
jg     1150 <poll_retry+0x30>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32 }
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @poll_retry(%struct.pollfd* %0, i32 %1, i32 %2) {
%4 = alloca %struct.pollfd*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = alloca %struct.timeval
store %struct.pollfd* %0, %struct.pollfd** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%12 = load i32, i32* %6
store i32 %12, i32* %8
store i32 0, i32* %9
%13 = call i32 @gettimeofday(%struct.timeval* %10, i32* null)
br label %14
14: ; preds = %48, %3
%15 = load %struct.pollfd*, %struct.pollfd** %4
%16 = load i32, i32* %5
%17 = load i32, i32* %8
%18 = call i32 @poll(%struct.pollfd* %15, i32 %16, i32 %17)
store i32 %18, i32* %7
%19 = call i32 @gettimeofday(%struct.timeval* %11, i32* null)
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%21 = load i64, i64* %20
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%23 = load i64, i64* %22
%24 = sub nsw i64 %21, %23
%25 = trunc i64 %24 to i32
%26 = mul nsw i32 %25, 1000
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
%28 = load i64, i64* %27
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%30 = load i64, i64* %29
%31 = sub nsw i64 %28, %30
%32 = trunc i64 %31 to i32
%33 = sdiv i32 %32, 1000
%34 = add nsw i32 %26, %33
store i32 %34, i32* %9
%35 = load i32, i32* %6
%36 = load i32, i32* %9
%37 = sub nsw i32 %35, %36
store i32 %37, i32* %8
br label %38
38: ; preds = %14
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load i64, i64* @errno
%43 = load i64, i64* @EINTR
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i32, i32* %8
%47 = icmp sgt i32 %46, 0
br label %48
48: ; preds = %45, %41, %38
%49 = phi i1 [ false, %41 ], [ false, %38 ], [ %47, %45 ]
br i1 %49, label %14, label %50
50: ; preds = %48
%51 = load i32, i32* %7
ret i32 %51
}

LLVM IR (-O2):
%struct.pollfd = type { i32 }
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @poll_retry(%struct.pollfd* %0, i32 %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = bitcast %struct.timeval* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%11 = load i64, i64* %10
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%14 = load i64, i64* %13
br label %15
15: ; preds = %30, %3
%16 = phi i32 [ %2, %3 ], [ %28, %30 ]
%17 = tail call i32 @poll(%struct.pollfd* %0, i32 %1, i32 %16)
%18 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%19 = load i64, i64* %9
%20 = sub nsw i64 %19, %11
%21 = trunc i64 %20 to i32
%22 = mul i32 %21, -1000
%23 = load i64, i64* %12
%24 = sub nsw i64 %23, %14
%25 = trunc i64 %24 to i32
%26 = sdiv i32 %25, -1000
%27 = add i32 %22, %2
%28 = add i32 %27, %26
%29 = icmp eq i32 %17, -1
br i1 %29, label %30, label %36
30: ; preds = %15
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp eq i64 %31, %32
%34 = icmp sgt i32 %28, 0
%35 = select i1 %33, i1 %34, i1 false
br i1 %35, label %15, label %36
36: ; preds = %15, %30
ret i32 %17
}