#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;
  int tv_usec;
};
struct timespec {
  unsigned int tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
void __buf_getabstimeout(struct timespec *ts, unsigned int timeout_ms) {
  struct timeval now;
  gettimeofday(&now, NULL);
  ts->tv_sec = now.tv_sec;
  ts->tv_nsec = now.tv_usec * 1000;
  ts->tv_sec += timeout_ms / 1000;
  ts->tv_nsec += timeout_ms % 1000 * 1000000;
  if (ts->tv_nsec > 999999999) {
    ts->tv_sec++;
    ts->tv_nsec = ts->tv_nsec % 1000000000;
  }
}


x86 assembly (clang, -O0):
<__buf_getabstimeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
imul   $0x3e8,-0x14(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
imul   $0xf4240,%edx,%ecx
mov    -0x8(%rbp),%rax
add    0x4(%rax),%ecx
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0x3b9ac9ff,0x4(%rax)
jle    11a7 <__buf_getabstimeout+0x97>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    -0x8(%rbp),%rax
mov    %edx,0x4(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__buf_getabstimeout>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
imul   $0x3e8,0x4(%rsp),%edx
mov    %ebp,%eax
imul   $0x10624dd3,%rax,%rcx
shr    $0x26,%rcx
imul   $0x3e8,%ecx,%eax
add    (%rsp),%ecx
mov    %ecx,(%rbx)
sub    %eax,%ebp
imul   $0xf4240,%ebp,%eax
add    %edx,%eax
mov    %eax,0x4(%rbx)
cmp    $0x3b9aca00,%eax
jl     1176 <__buf_getabstimeout+0x66>
add    $0x1,%ecx
mov    %ecx,(%rbx)
mov    %eax,%ecx
shr    $0x9,%ecx
imul   $0x44b83,%rcx,%rcx
shr    $0x27,%rcx
imul   $0x3b9aca00,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,0x4(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
define void @__buf_getabstimeout(%struct.timespec* %0, i32 %1) {
%3 = alloca %struct.timespec*
%4 = alloca i32
%5 = alloca %struct.timeval
store %struct.timespec* %0, %struct.timespec** %3
store i32 %1, i32* %4
%6 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%8 = load i32, i32* %7
%9 = load %struct.timespec*, %struct.timespec** %3
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
store i32 %8, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%12 = load i32, i32* %11
%13 = mul nsw i32 %12, 1000
%14 = load %struct.timespec*, %struct.timespec** %3
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %14, i32 0, i32 1
store i32 %13, i32* %15
%16 = load i32, i32* %4
%17 = udiv i32 %16, 1000
%18 = load %struct.timespec*, %struct.timespec** %3
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = add i32 %20, %17
store i32 %21, i32* %19
%22 = load i32, i32* %4
%23 = urem i32 %22, 1000
%24 = mul i32 %23, 1000000
%25 = load %struct.timespec*, %struct.timespec** %3
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = add i32 %27, %24
store i32 %28, i32* %26
%29 = load %struct.timespec*, %struct.timespec** %3
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %29, i32 0, i32 1
%31 = load i32, i32* %30
%32 = icmp sgt i32 %31, 999999999
br i1 %32, label %33, label %44
33: ; preds = %2
%34 = load %struct.timespec*, %struct.timespec** %3
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = add i32 %36, 1
store i32 %37, i32* %35
%38 = load %struct.timespec*, %struct.timespec** %3
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = srem i32 %40, 1000000000
%42 = load %struct.timespec*, %struct.timespec** %3
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %42, i32 0, i32 1
store i32 %41, i32* %43
br label %44
44: ; preds = %33, %2
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
define void @__buf_getabstimeout(%struct.timespec* nocapture %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%10 = load i32, i32* %9
%11 = mul nsw i32 %10, 1000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%13 = udiv i32 %1, 1000
%14 = add i32 %7, %13
store i32 %14, i32* %8
%15 = urem i32 %1, 1000
%16 = mul nuw nsw i32 %15, 1000000
%17 = add i32 %11, %16
store i32 %17, i32* %12
%18 = icmp sgt i32 %17, 999999999
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = add i32 %14, 1
store i32 %20, i32* %8
%21 = urem i32 %17, 1000000000
store i32 %21, i32* %12
br label %22
22: ; preds = %19, %2
ret void
}