#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;
  if (!tv)
    return;
  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)
cmpq   $0x0,-0x8(%rbp)
jne    111c <util_timeval_add+0x1c>
jmp    11a7 <util_timeval_add+0xa7>
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    119c <util_timeval_add+0x9c>
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    11a7 <util_timeval_add+0xa7>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_timeval_add>:
test   %rdi,%rdi
je     1178 <util_timeval_add+0x78>
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     1175 <util_timeval_add+0x75>
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 %struct.timeval*, %struct.timeval** %3
%7 = icmp ne %struct.timeval* %6, null
br i1 %7, label %9, label %8
8: ; preds = %2
br label %38
9: ; preds = %2
%10 = load i64, i64* %4
%11 = sdiv i64 %10, 1000
%12 = load %struct.timeval*, %struct.timeval** %3
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
%14 = load i64, i64* %13
%15 = add nsw i64 %14, %11
store i64 %15, i64* %13
%16 = load %struct.timeval*, %struct.timeval** %3
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
%18 = load i64, i64* %17
%19 = load i64, i64* %4
%20 = srem i64 %19, 1000
%21 = mul nsw i64 %20, 1000
%22 = add nsw i64 %18, %21
store i64 %22, i64* %5
%23 = load i64, i64* %5
%24 = icmp sgt i64 %23, 1000000
br i1 %24, label %25, label %34
25: ; preds = %9
%26 = load i64, i64* %5
%27 = srem i64 %26, 1000000
%28 = load %struct.timeval*, %struct.timeval** %3
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %28, i32 0, i32 0
store i64 %27, i64* %29
%30 = load %struct.timeval*, %struct.timeval** %3
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %30, i32 0, i32 1
%32 = load i64, i64* %31
%33 = add nsw i64 %32, 1
store i64 %33, i64* %31
br label %38
34: ; preds = %9
%35 = load i64, i64* %5
%36 = load %struct.timeval*, %struct.timeval** %3
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %36, i32 0, i32 0
store i64 %35, i64* %37
br label %38
38: ; preds = %8, %34, %25
ret void
}

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