#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;
};

/* Variables and functions */
void util_timeval_add(struct timeval *tv, long interval) {
  long usec;
  tv->tv_sec += (interval / 1000);
  usec = tv->tv_usec + ((interval % 1000) * 1000);
  if (usec > 1000000) {
    tv->tv_usec = usec % 1000000;
    tv->tv_sec++;
  } else
    tv->tv_usec = usec;
}


x86 assembly (clang, -O0):
<util_timeval_add>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
add    0x8(%rax),%rcx
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    -0x20(%rbp),%rax
imul   $0x3e8,%rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0xf4240,-0x18(%rbp)
jle    118c <util_timeval_add+0x8c>
mov    -0x18(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    -0x8(%rbp),%rax
mov    %rdx,(%rax)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x1,%rcx
mov    %rcx,0x8(%rax)
jmp    1197 <util_timeval_add+0x97>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_timeval_add>:
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mov    %rsi,%rax
imul   %rcx
mov    %rdx,%rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rcx
add    %rax,%rcx
imul   $0x3e8,%rcx,%rax
add    0x8(%rdi),%rcx
mov    %rcx,0x8(%rdi)
sub    %rax,%rsi
imul   $0x3e8,%rsi,%rsi
add    (%rdi),%rsi
cmp    $0xf4241,%rsi
jl     1170 <util_timeval_add+0x70>
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mov    %rsi,%rax
mul    %rdx
shr    $0x12,%rdx
imul   $0xf4240,%rdx,%rax
sub    %rax,%rsi
mov    %rsi,(%rdi)
add    $0x1,%rcx
mov    %rcx,0x8(%rdi)
ret
mov    %rsi,(%rdi)
ret

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

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