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

/* Variables and functions */
long long LONG_MAX;
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
void getTimeoutRelative(struct timespec *ts, long long millis,
                        long long nanos) {
  struct timeval tv;
  long long seconds;
  gettimeofday(&tv, NULL);
  seconds = tv.tv_sec + millis / 1000 + nanos / 1000000000;
  nanos %= 1000000000;
  nanos += (tv.tv_usec + ((millis % 1000) * 1000)) * 1000;
  seconds += nanos / 1000000000;
  nanos %= 1000000000;
  ts->tv_sec = seconds > LONG_MAX ? LONG_MAX : seconds;
  ts->tv_nsec = nanos;
}


x86 assembly (clang, -O0):
<getTimeoutRelative>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    -0x38(%rbp),%rax
imul   $0x3e8,%rdx,%rcx
add    %rcx,%rax
imul   $0x3e8,%rax,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x18(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%rax
jle    1205 <getTimeoutRelative+0xf5>
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    120d <getTimeoutRelative+0xfd>
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x8(%rax)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getTimeoutRelative>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movabs $0x112e0be826d694b3,%r8
0b 2e 11
mov    %r15,%rax
imul   %r8
mov    %rdx,%rsi
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1a,%rsi
add    %rax,%rsi
imul   $0x3b9aca00,%rsi,%rax
sub    %rax,%r15
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mov    %rbx,%rax
imul   %rcx
mov    %rdx,%rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rcx
add    %rax,%rcx
imul   $0x3e8,%rcx,%rax
sub    %rax,%rbx
imul   $0x3e8,%rbx,%rax
add    0x8(%rsp),%rax
imul   $0x3e8,%rax,%rdi
add    %rsi,%rcx
add    (%rsp),%rcx
add    %r15,%rdi
mov    %rdi,%rax
imul   %r8
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1a,%rdx
add    %rax,%rdx
add    %rdx,%rcx
imul   $0x3b9aca00,%rdx,%rax
sub    %rax,%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
cmp    %rax,%rcx
cmovg  %rax,%rcx
mov    %rcx,(%r14)
mov    %rdi,0x8(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
%struct.timeval = type { i64, i64 }
@LONG_MAX = global i64 0
define void @getTimeoutRelative(%struct.timespec* nocapture writeonly %0, i64 %1, i64 %2) {
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %4 to i8*
%6 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%8 = load i64, i64* %7
%9 = sdiv i64 %1, 1000
%10 = sdiv i64 %2, 1000000000
%11 = srem i64 %2, 1000000000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%13 = load i64, i64* %12
%14 = srem i64 %1, 1000
%15 = mul nsw i64 %14, 1000
%16 = add nsw i64 %13, %15
%17 = mul nsw i64 %16, 1000
%18 = add nsw i64 %17, %11
%19 = sdiv i64 %18, 1000000000
%20 = add nsw i64 %10, %9
%21 = add i64 %20, %8
%22 = add i64 %21, %19
%23 = srem i64 %18, 1000000000
%24 = load i64, i64* @LONG_MAX
%25 = icmp sgt i64 %22, %24
%26 = select i1 %25, i64 %24, i64 %22
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
store i64 %26, i64* %27
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
store i64 %23, i64* %28
ret void
}