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

/* Variables and functions */
void add_ms_to_timeval(struct timeval *a, unsigned long interval_ms,
                       struct timeval *result) {
  result->tv_sec = a->tv_sec + (interval_ms / 1000);
  result->tv_usec = a->tv_usec + ((interval_ms % 1000) * 1000);
  if (result->tv_usec > 1000000) {
    result->tv_usec -= 1000000;
    result->tv_sec++;
  }
}


x86 assembly (clang, -O0):
<add_ms_to_timeval>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x10(%rbp),%rax
mov    $0x3e8,%esi
xor    %edx,%edx
div    %rsi
add    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
movslq (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    -0x20(%rbp),%rax
imul   $0x3e8,%rdx,%rcx
add    %rcx,%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0xf4240,(%rax)
jle    118e <add_ms_to_timeval+0x8e>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
sub    $0xf4240,%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x1,%rcx
mov    %rcx,0x8(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_ms_to_timeval>:
mov    %rdx,%rcx
mov    %rsi,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
mul    %rdx
shr    $0x4,%rdx
mov    0x8(%rdi),%rax
add    %rdx,%rax
mov    %rax,0x8(%rcx)
imul   $0x3e8,%edx,%edx
sub    %edx,%esi
imul   $0x3e8,%esi,%edx
add    (%rdi),%edx
mov    %edx,(%rcx)
cmp    $0xf4241,%edx
jl     1150 <add_ms_to_timeval+0x50>
add    $0xfff0bdc0,%edx
mov    %edx,(%rcx)
add    $0x1,%rax
mov    %rax,0x8(%rcx)
ret

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

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