#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;
  int tv_usec;
};
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 = ((tv.tv_sec - start_time.tv_sec) * 1000 +
                    (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    $0x40,%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    -0x28(%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    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x30(%rbp),%eax
sub    -0x28(%rbp),%eax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x34(%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,-0x35(%rbp)
jne    11d3 <poll_retry+0xb3>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    0x2e1b(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x35(%rbp)
jne    11d3 <poll_retry+0xb3>
cmpl   $0x0,-0x18(%rbp)
setg   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    114c <poll_retry+0x2c>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poll_retry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,%r13
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x10(%rsp),%ebp
mov    0x14(%rsp),%r15d
mov    %r12d,%edx
nop
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    11b7 <poll_retry+0x97>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%rax
jne    11b7 <poll_retry+0x97>
mov    0x8(%rsp),%eax
sub    %ebp,%eax
mov    0xc(%rsp),%ecx
sub    %r15d,%ecx
imul   $0xfffffc18,%eax,%edx
movslq %ecx,%rax
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    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32 }
%struct.timeval = type { i32, i32 }
@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 = %46, %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 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%23 = load i32, i32* %22
%24 = sub nsw i32 %21, %23
%25 = mul nsw i32 %24, 1000
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%29 = load i32, i32* %28
%30 = sub nsw i32 %27, %29
%31 = sdiv i32 %30, 1000
%32 = add nsw i32 %25, %31
store i32 %32, i32* %9
%33 = load i32, i32* %6
%34 = load i32, i32* %9
%35 = sub nsw i32 %33, %34
store i32 %35, i32* %8
br label %36
36: ; preds = %14
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINTR
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* %8
%45 = icmp sgt i32 %44, 0
br label %46
46: ; preds = %43, %39, %36
%47 = phi i1 [ false, %39 ], [ false, %36 ], [ %45, %43 ]
br i1 %47, label %14, label %48
48: ; preds = %46
%49 = load i32, i32* %7
ret i32 %49
}

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