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

/* Variables and functions */
void timeval_add_msecs(struct timeval *tv, unsigned int msecs) {
  tv->tv_sec += msecs / 1000;
  tv->tv_usec += (msecs % 1000) * 1000;
  if (tv->tv_usec >= 1000000) {
    tv->tv_sec++;
    tv->tv_usec -= 1000000;
  }
}


x86 assembly (clang, -O0):
<timeval_add_msecs>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%eax
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
add    0x8(%rax),%rcx
mov    %rcx,0x8(%rax)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
imul   $0x3e8,%edx,%ecx
mov    -0x8(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0xf4240,(%rax)
jb     116f <timeval_add_msecs+0x6f>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x1,%rcx
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sub    $0xf4240,%ecx
mov    %ecx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_add_msecs>:
mov    %esi,%eax
imul   $0x10624dd3,%rax,%rcx
shr    $0x26,%rcx
mov    0x8(%rdi),%rax
add    %rcx,%rax
mov    %rax,0x8(%rdi)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%esi
imul   $0x3e8,%esi,%ecx
add    (%rdi),%ecx
mov    %ecx,(%rdi)
cmp    $0xf4240,%ecx
jb     1142 <timeval_add_msecs+0x42>
add    $0x1,%rax
mov    %rax,0x8(%rdi)
add    $0xfff0bdc0,%ecx
mov    %ecx,(%rdi)
ret

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

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