#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 {
  long tv_sec;
  long tv_usec;
};

/* Variables and functions */
long tmr_mstimeout(struct timeval *);
struct timeval *tmr_task_timeout(struct timeval *now) {
  long msecs = 0;
  static struct timeval timeout;
  msecs = tmr_mstimeout(now);
  if (msecs == -1) {
    return NULL;
  }
  timeout.tv_sec = msecs / 1000L;
  timeout.tv_usec = (msecs % 1000L) * 1000L;
  return &timeout;
}


x86 assembly (clang, -O0):
<tmr_task_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <tmr_mstimeout@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    1149 <tmr_task_timeout+0x39>
movq   $0x0,-0x8(%rbp)
jmp    1185 <tmr_task_timeout+0x75>
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,0x2ed2(%rip)
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
imul   $0x3e8,%rdx,%rax
mov    %rax,0x2ebe(%rip)
lea    0x2eaf(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tmr_task_timeout>:
push   %rax
call   1030 <tmr_mstimeout@plt>
cmp    $0xffffffffffffffff,%rax
je     1162 <tmr_task_timeout+0x52>
mov    %rax,%rcx
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
mov    %rdx,0x2eef(%rip)
lea    0x2ee8(%rip),%rax
imul   $0x3e8,%rdx,%rdx
sub    %rdx,%rcx
imul   $0x3e8,%rcx,%rcx
mov    %rcx,0x2ed8(%rip)
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@tmr_task_timeout.timeout = internal global %struct.timeval zeroinitializer
define %struct.timeval* @tmr_task_timeout(%struct.timeval* %0) {
%2 = alloca %struct.timeval*
%3 = alloca %struct.timeval*
%4 = alloca i64
store %struct.timeval* %0, %struct.timeval** %3
store i64 0, i64* %4
%5 = load %struct.timeval*, %struct.timeval** %3
%6 = call i64 @tmr_mstimeout(%struct.timeval* %5)
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp eq i64 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %1
store %struct.timeval* null, %struct.timeval** %2
br label %16
10: ; preds = %1
%11 = load i64, i64* %4
%12 = sdiv i64 %11, 1000
store i64 %12, i64* getelementptr inbounds (%struct.timeval, %struct.timeval* @tmr_task_timeout.timeout, i32 0, i32 0)
%13 = load i64, i64* %4
%14 = srem i64 %13, 1000
%15 = mul nsw i64 %14, 1000
store i64 %15, i64* getelementptr inbounds (%struct.timeval, %struct.timeval* @tmr_task_timeout.timeout, i32 0, i32 1)
store %struct.timeval* @tmr_task_timeout.timeout, %struct.timeval** %2
br label %16
16: ; preds = %10, %9
%17 = load %struct.timeval*, %struct.timeval** %2
ret %struct.timeval* %17
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@tmr_task_timeout.timeout = internal global %struct.timeval zeroinitializer
define %struct.timeval* @tmr_task_timeout(%struct.timeval* %0) {
%2 = tail call i64 @tmr_mstimeout(%struct.timeval* %0)
%3 = icmp eq i64 %2, -1
br i1 %3, label %8, label %4
4: ; preds = %1
%5 = sdiv i64 %2, 1000
store i64 %5, i64* getelementptr inbounds (%struct.timeval, %struct.timeval* @tmr_task_timeout.timeout, i64 0, i32 0)
%6 = srem i64 %2, 1000
%7 = mul nsw i64 %6, 1000
store i64 %7, i64* getelementptr inbounds (%struct.timeval, %struct.timeval* @tmr_task_timeout.timeout, i64 0, i32 1)
br label %8
8: ; preds = %1, %4
%9 = phi %struct.timeval* [ @tmr_task_timeout.timeout, %4 ], [ null, %1 ]
ret %struct.timeval* %9
}