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

/* Variables and functions */
void timeval_add_ts(struct timeval *a, const struct timespec *b) {
  long fivehundred = b->tv_nsec < 0 ? -500 : 500;
  a->tv_usec += (b->tv_nsec + fivehundred) / 1000;
  if (a->tv_usec < 0) {
    --a->tv_sec;
    a->tv_usec += 1000000;
  }
  a->tv_sec += b->tv_sec + a->tv_usec / 1000000;
  a->tv_usec %= 1000000;
}


x86 assembly (clang, -O0):
<timeval_add_ts>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    $0x1f4,%eax
mov    $0xfffffe0c,%ecx
cmp    $0x0,%rdx
cmovl  %ecx,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
add    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jge    117b <timeval_add_ts+0x7b>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0xf4240,%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    $0xf4240,%esi
cqto
idiv   %rsi
add    %rax,%rcx
mov    -0x8(%rbp),%rax
add    0x8(%rax),%rcx
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    (%rax),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    -0x20(%rbp),%rax
mov    %rdx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_add_ts>:
mov    (%rsi),%rcx
lea    0x1f4(%rcx),%rax
lea    -0x1f4(%rcx),%rdx
test   %rcx,%rcx
cmovs  %rdx,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
mov    (%rdi),%rcx
lea    (%rdx,%rcx,1),%rax
mov    %rax,%r8
sar    $0x3f,%r8
add    0x8(%rdi),%r8
test   %rax,%rax
lea    0xf4240(%rcx,%rdx,1),%rcx
cmovns %rax,%rcx
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mov    %rcx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x12,%rdx
add    %rax,%rdx
imul   $0xf4240,%rdx,%rax
add    0x8(%rsi),%rdx
add    %r8,%rdx
mov    %rdx,0x8(%rdi)
sub    %rax,%rcx
mov    %rcx,(%rdi)
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
%struct.timespec = type { i64, i64 }
define void @timeval_add_ts(%struct.timeval* %0, %struct.timespec* %1) {
%3 = alloca %struct.timeval*
%4 = alloca %struct.timespec*
%5 = alloca i64
store %struct.timeval* %0, %struct.timeval** %3
store %struct.timespec* %1, %struct.timespec** %4
%6 = load %struct.timespec*, %struct.timespec** %4
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%8 = load i64, i64* %7
%9 = icmp slt i64 %8, 0
%10 = zext i1 %9 to i64
%11 = select i1 %9, i32 -500, i32 500
%12 = sext i32 %11 to i64
store i64 %12, i64* %5
%13 = load %struct.timespec*, %struct.timespec** %4
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 0
%15 = load i64, i64* %14
%16 = load i64, i64* %5
%17 = add nsw i64 %15, %16
%18 = sdiv i64 %17, 1000
%19 = load %struct.timeval*, %struct.timeval** %3
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %19, i32 0, i32 0
%21 = load i64, i64* %20
%22 = add nsw i64 %21, %18
store i64 %22, i64* %20
%23 = load %struct.timeval*, %struct.timeval** %3
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %2
%28 = load %struct.timeval*, %struct.timeval** %3
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %28, i32 0, i32 1
%30 = load i64, i64* %29
%31 = add nsw i64 %30, -1
store i64 %31, i64* %29
%32 = load %struct.timeval*, %struct.timeval** %3
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %32, i32 0, i32 0
%34 = load i64, i64* %33
%35 = add nsw i64 %34, 1000000
store i64 %35, i64* %33
br label %36
36: ; preds = %27, %2
%37 = load %struct.timespec*, %struct.timespec** %4
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %37, i32 0, i32 1
%39 = load i64, i64* %38
%40 = load %struct.timeval*, %struct.timeval** %3
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = sdiv i64 %42, 1000000
%44 = add nsw i64 %39, %43
%45 = load %struct.timeval*, %struct.timeval** %3
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %45, i32 0, i32 1
%47 = load i64, i64* %46
%48 = add nsw i64 %47, %44
store i64 %48, i64* %46
%49 = load %struct.timeval*, %struct.timeval** %3
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %49, i32 0, i32 0
%51 = load i64, i64* %50
%52 = srem i64 %51, 1000000
store i64 %52, i64* %50
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
%struct.timespec = type { i64, i64 }
define void @timeval_add_ts(%struct.timeval* nocapture %0, %struct.timespec* nocapture %1) {
%3 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%4 = load i64, i64* %3
%5 = icmp slt i64 %4, 0
%6 = select i1 %5, i64 -500, i64 500
%7 = add nsw i64 %6, %4
%8 = sdiv i64 %7, 1000
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%10 = load i64, i64* %9
%11 = add nsw i64 %8, %10
%12 = icmp slt i64 %11, 0
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%14 = load i64, i64* %13
%15 = add nsw i64 %11, 1000000
%16 = ashr i64 %11, 63
%17 = add nsw i64 %14, %16
%18 = select i1 %12, i64 %15, i64 %11
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%20 = load i64, i64* %19
%21 = sdiv i64 %18, 1000000
%22 = add nsw i64 %21, %20
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%24 = add nsw i64 %22, %17
store i64 %24, i64* %23
%25 = srem i64 %18, 1000000
store i64 %25, i64* %9
ret void
}