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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
struct timespec reltime_to_abstime(unsigned long milliseconds) {
  struct timespec result;
  clock_gettime(CLOCK_REALTIME, &result);
  result.tv_sec += milliseconds / 1000;
  long nanoseconds = (milliseconds % 1000) * 1000000l;
  if (result.tv_nsec >= 1000000000l - nanoseconds) {
    result.tv_sec += 1;
    result.tv_nsec += nanoseconds - 1000000000l;
  } else {
    result.tv_nsec += nanoseconds;
  }
  return result;
}


x86 assembly (clang, -O0):
<reltime_to_abstime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x18(%rbp)
mov    0x2ebd(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
imul   $0xf4240,%rdx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3b9aca00,%ecx
sub    -0x20(%rbp),%rcx
cmp    %rcx,%rax
jl     1196 <reltime_to_abstime+0x86>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
sub    $0x3b9aca00,%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a2 <reltime_to_abstime+0x92>
mov    -0x20(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rdx
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reltime_to_abstime>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ec1(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
mov    %rbx,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
mov    %rdx,%rax
shr    $0x4,%rax
imul   $0x3e8,%rax,%rcx
add    (%rsp),%rax
sub    %rcx,%rbx
imul   $0xf4240,%rbx,%rdx
mov    0x8(%rsp),%rcx
mov    $0x3b9aca00,%esi
sub    %rdx,%rsi
cmp    %rsi,%rcx
jl     117a <reltime_to_abstime+0x6a>
add    $0x1,%rax
mov    %rax,(%rsp)
add    $0xffffffffc4653600,%rdx
add    %rcx,%rdx
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_REALTIME = global i32 0
define { i64, i64 } @reltime_to_abstime(i64 %0) {
%2 = alloca %struct.timespec
%3 = alloca i64
%4 = alloca i64
store i64 %0, i64* %3
%5 = load i32, i32* @CLOCK_REALTIME
%6 = call i32 @clock_gettime(i32 %5, %struct.timespec* %2)
%7 = load i64, i64* %3
%8 = udiv i64 %7, 1000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%10 = load i64, i64* %9
%11 = add i64 %10, %8
store i64 %11, i64* %9
%12 = load i64, i64* %3
%13 = urem i64 %12, 1000
%14 = mul i64 %13, 1000000
store i64 %14, i64* %4
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%16 = load i64, i64* %15
%17 = load i64, i64* %4
%18 = sub nsw i64 1000000000, %17
%19 = icmp sge i64 %16, %18
br i1 %19, label %20, label %29
20: ; preds = %1
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%22 = load i64, i64* %21
%23 = add i64 %22, 1
store i64 %23, i64* %21
%24 = load i64, i64* %4
%25 = sub nsw i64 %24, 1000000000
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%27 = load i64, i64* %26
%28 = add nsw i64 %27, %25
store i64 %28, i64* %26
br label %34
29: ; preds = %1
%30 = load i64, i64* %4
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%32 = load i64, i64* %31
%33 = add nsw i64 %32, %30
store i64 %33, i64* %31
br label %34
34: ; preds = %29, %20
%35 = bitcast %struct.timespec* %2 to { i64, i64 }*
%36 = load { i64, i64 }, { i64, i64 }* %35
ret { i64, i64 } %36
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_REALTIME = global i32 0
define { i64, i64 } @reltime_to_abstime(i64 %0) {
%2 = alloca %struct.timespec
%3 = load i32, i32* @CLOCK_REALTIME
%4 = call i32 @clock_gettime(i32 %3, %struct.timespec* %2)
%5 = udiv i64 %0, 1000
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%7 = load i64, i64* %6
%8 = add i64 %7, %5
%9 = urem i64 %0, 1000
%10 = mul nuw nsw i64 %9, 1000000
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%12 = load i64, i64* %11
%13 = sub nuw nsw i64 1000000000, %10
%14 = icmp slt i64 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %1
%16 = add i64 %8, 1
store i64 %16, i64* %6
%17 = add nuw nsw i64 %10, -1000000000
br label %18
18: ; preds = %1, %15
%19 = phi i64 [ %16, %15 ], [ %8, %1 ]
%20 = phi i64 [ %17, %15 ], [ %10, %1 ]
%21 = add nsw i64 %20, %12
%22 = insertvalue { i64, i64 } poison, i64 %19, 0
%23 = insertvalue { i64, i64 } %22, i64 %21, 1
ret { i64, i64 } %23
}