#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_sec;
  scalar_t__ tv_usec;
};

/* Variables and functions */
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int _fd_get_poll_timeout(const struct timeval *when) {
  struct timeval now;
  int msecs;
  if (when == NULL) {
    return (-1);
  }
  if ((when->tv_sec == 0) && (when->tv_usec == 0)) {
    return (0);
  }
  if (gettimeofday(&now, NULL) < 0) {
    return (0);
  }
  msecs = ((when->tv_sec - now.tv_sec) * 1000) +
          ((when->tv_usec - now.tv_usec + 999) / 1000);
  return ((msecs < 0) ? 0 : msecs);
}


x86 assembly (clang, -O0):
<_fd_get_poll_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1133 <_fd_get_poll_timeout+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <_fd_get_poll_timeout+0xce>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    115c <_fd_get_poll_timeout+0x4c>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    115c <_fd_get_poll_timeout+0x4c>
movl   $0x0,-0x4(%rbp)
jmp    11de <_fd_get_poll_timeout+0xce>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
jge    117f <_fd_get_poll_timeout+0x6f>
movl   $0x0,-0x4(%rbp)
jmp    11de <_fd_get_poll_timeout+0xce>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
sub    -0x20(%rbp),%rax
imul   $0x3e8,%rax,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
sub    -0x18(%rbp),%rax
add    $0x3e7,%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d2 <_fd_get_poll_timeout+0xc2>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    11d8 <_fd_get_poll_timeout+0xc8>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fd_get_poll_timeout>:
push   %r14
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     1181 <_fd_get_poll_timeout+0x71>
mov    %rdi,%r14
cmpq   $0x0,(%rdi)
jne    112c <_fd_get_poll_timeout+0x1c>
cmpq   $0x0,0x8(%r14)
je     1188 <_fd_get_poll_timeout+0x78>
xor    %ebx,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
js     118a <_fd_get_poll_timeout+0x7a>
mov    (%r14),%ecx
sub    0x8(%rsp),%ecx
mov    0x8(%r14),%rax
sub    0x10(%rsp),%rax
add    $0x3e7,%rax
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
imul   %rdx
mov    %rdx,%rbx
imul   $0x3e8,%ecx,%eax
mov    %rdx,%rcx
shr    $0x3f,%rcx
shr    $0x7,%rbx
add    %ecx,%ebx
xor    %ecx,%ecx
add    %eax,%ebx
cmovs  %ecx,%ebx
jmp    118a <_fd_get_poll_timeout+0x7a>
mov    $0xffffffff,%ebx
jmp    118a <_fd_get_poll_timeout+0x7a>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

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