#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 timeval_add_msec(struct timeval *d, const struct timeval *s, int msecs) {
  int usecs;
  d->tv_sec = s->tv_sec + msecs / 1000;
  usecs = s->tv_usec + (msecs % 1000) * 1000;
  if (usecs < 1000000) {
    d->tv_usec = usecs;
  } else {
    d->tv_usec = usecs - 1000000;
    d->tv_sec++;
  }
}


x86 assembly (clang, -O0):
<timeval_add_msec>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x14(%rbp),%eax
mov    $0x3e8,%esi
cltd
idiv   %esi
cltq
add    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    -0x1c(%rbp),%eax
imul   $0x3e8,%edx,%ecx
add    %ecx,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0xf4240,-0x18(%rbp)
jge    116c <timeval_add_msec+0x6c>
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    118b <timeval_add_msec+0x8b>
mov    -0x18(%rbp),%ecx
sub    $0xf4240,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x1,%rcx
mov    %rcx,0x8(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_add_msec>:
movslq %edx,%rcx
imul   $0x10624dd3,%rcx,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
movslq %eax,%rdx
mov    0x8(%rsi),%rax
add    %rdx,%rax
mov    %rax,0x8(%rdi)
imul   $0x3e8,%edx,%edx
sub    %edx,%ecx
imul   $0x3e8,%ecx,%ecx
add    (%rsi),%ecx
cmp    $0xf4240,%ecx
jl     114b <timeval_add_msec+0x4b>
add    $0xfff0bdc0,%ecx
add    $0x1,%rax
mov    %rax,0x8(%rdi)
mov    %ecx,(%rdi)
ret

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

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