#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 {
  unsigned int tv_sec;
  unsigned int tv_usec;
};
typedef int /*<<< orphan*/ now;

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct timeval *, int, int);
int timeval_sub(struct timeval *, struct timeval *);
int timeout_chk(struct timeval *start, unsigned int timeout) {
  int ret;
  struct timeval now;
  struct timeval max_diff;
  max_diff.tv_sec = timeout / 1000;
  max_diff.tv_usec = (timeout % 1000) * 1000;
  memset(&now, 0xff, sizeof(now));
  gettimeofday(&now, NULL);
  ret = timeval_sub(&now, start);
  if (ret < 0) {
    return (-1);
  }
  ret = (now.tv_sec < max_diff.tv_sec) ||
        ((now.tv_sec == max_diff.tv_sec) && (now.tv_usec < max_diff.tv_usec));
  return (ret);
}


x86 assembly (clang, -O0):
<timeout_chk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x24(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xff,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdi
call   1030 <timeval_sub@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11a9 <timeout_chk+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <timeout_chk+0xbe>
mov    -0x20(%rbp),%ecx
mov    $0x1,%al
cmp    -0x28(%rbp),%ecx
mov    %al,-0x29(%rbp)
jb     11dd <timeout_chk+0xad>
mov    -0x20(%rbp),%ecx
xor    %eax,%eax
cmp    -0x28(%rbp),%ecx
mov    %al,-0x2a(%rbp)
jne    11d7 <timeout_chk+0xa7>
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
setb   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeout_chk>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0xff,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <timeval_sub@plt>
test   %eax,%eax
js     11a5 <timeout_chk+0x75>
mov    %ebx,%eax
imul   $0x10624dd3,%rax,%rdx
shr    $0x26,%rdx
imul   $0x3e8,%edx,%eax
sub    %eax,%ebx
imul   $0x3e8,%ebx,%ecx
mov    $0x1,%eax
cmp    %edx,0x8(%rsp)
jb     11aa <timeout_chk+0x7a>
sete   %al
cmp    %ecx,0xc(%rsp)
setb   %cl
and    %al,%cl
movzbl %cl,%eax
jmp    11aa <timeout_chk+0x7a>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @timeout_chk(%struct.timeval* %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = udiv i32 %1, 1000
%6 = urem i32 %1, 1000
%7 = mul nuw nsw i32 %6, 1000
%8 = call i32 @memset(%struct.timeval* %3, i32 255, i32 8)
%9 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%10 = call i32 @timeval_sub(%struct.timeval* %3, %struct.timeval* %0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %23, label %12
12: ; preds = %2
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = icmp ult i32 %14, %5
br i1 %15, label %23, label %16
16: ; preds = %12
%17 = icmp eq i32 %14, %5
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%19 = load i32, i32* %18
%20 = icmp ult i32 %19, %7
%21 = select i1 %17, i1 %20, i1 false
%22 = zext i1 %21 to i32
br label %23
23: ; preds = %12, %16, %2
%24 = phi i32 [ -1, %2 ], [ 1, %12 ], [ %22, %16 ]
ret i32 %24
}