#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 timer_tvafter(struct timeval *tv, int msec) {
  tv->tv_sec += msec / 1000;
  tv->tv_usec += msec % 1000;
  while (tv->tv_usec > ((1) * 1000 * 1000)) {
    tv->tv_usec -= ((1) * 1000 * 1000);
    tv->tv_sec++;
  }
}


x86 assembly (clang, -O0):
<timer_tvafter>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
movslq %eax,%rcx
mov    -0x8(%rbp),%rax
add    0x8(%rax),%rcx
mov    %rcx,0x8(%rax)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    -0x8(%rbp),%rax
add    (%rax),%edx
mov    %edx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0xf4240,(%rax)
jle    116b <timer_tvafter+0x6b>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sub    $0xf4240,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x1,%rcx
mov    %rcx,0x8(%rax)
jmp    1138 <timer_tvafter+0x38>
pop    %rbp
ret

x86 assembly (clang, -O2):
<timer_tvafter>:
movslq %esi,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
movslq %ecx,%rdx
mov    0x8(%rdi),%rcx
add    %rdx,%rcx
mov    %rcx,0x8(%rdi)
imul   $0x3e8,%edx,%edx
sub    %edx,%eax
add    (%rdi),%eax
mov    %eax,(%rdi)
cmp    $0xf4241,%eax
jl     1181 <timer_tvafter+0x81>
cmp    $0x1e8480,%eax
mov    $0x1e8480,%edx
cmovl  %eax,%edx
neg    %edx
lea    (%rax,%rdx,1),%r9d
add    $0xf423f,%r9d
imul   $0x431bde83,%r9,%rsi
shr    $0x32,%rsi
imul   $0xf4240,%esi,%r8d
mov    %r9d,%edx
sub    %r8d,%edx
sub    %r9d,%edx
add    %edx,%eax
add    $0xfff0bdc0,%eax
add    %rsi,%rcx
add    $0x1,%rcx
mov    %eax,(%rdi)
mov    %rcx,0x8(%rdi)
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define void @timer_tvafter(%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 = sdiv i32 %5, 1000
%7 = sext 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 = srem i32 %12, 1000
%14 = load %struct.timeval*, %struct.timeval** %3
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = add nsw i32 %16, %13
store i32 %17, i32* %15
br label %18
18: ; preds = %23, %2
%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 sgt i32 %21, 1000000
br i1 %22, label %23, label %32
23: ; preds = %18
%24 = load %struct.timeval*, %struct.timeval** %3
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = sub nsw i32 %26, 1000000
store i32 %27, i32* %25
%28 = load %struct.timeval*, %struct.timeval** %3
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %28, i32 0, i32 1
%30 = load i64, i64* %29
%31 = add nsw i64 %30, 1
store i64 %31, i64* %29
br label %18
32: ; preds = %18
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define void @timer_tvafter(%struct.timeval* nocapture %0, i32 %1) {
%3 = sdiv i32 %1, 1000
%4 = sext 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 = srem i32 %1, 1000
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%10 = load i32, i32* %9
%11 = add i32 %10, %8
store i32 %11, i32* %9
%12 = icmp sgt i32 %11, 1000000
br i1 %12, label %13, label %25
13: ; preds = %2
%14 = add nuw i32 %11, 999999
%15 = call i32 @llvm.smin.i32(i32 %11, i32 2000000)
%16 = sub nuw i32 %14, %15
%17 = urem i32 %16, 1000000
%18 = sub nsw i32 %17, %16
%19 = udiv i32 %16, 1000000
%20 = zext i32 %19 to i64
%21 = add i64 %7, %20
%22 = add nsw i32 %11, -1000000
%23 = add nsw i32 %18, %22
%24 = add i64 %21, 1
store i32 %23, i32* %9
store i64 %24, i64* %5
br label %25
25: ; preds = %13, %2
ret void
}