#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 */
typedef int uint64_t;
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
void timeval_add_usec(struct timeval *tv, uint64_t v) {
  tv->tv_sec += v / 1000000;
  tv->tv_usec += v % 1000000;
  if (tv->tv_usec > 1000000) {
    tv->tv_usec -= 1000000;
    tv->tv_sec += 1;
  }
}


x86 assembly (clang, -O0):
<timeval_add_usec>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    -0x8(%rbp),%rax
add    0x4(%rax),%edx
mov    %edx,0x4(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0xf4240,0x4(%rax)
jle    1161 <timeval_add_usec+0x61>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%ecx
sub    $0xf4240,%ecx
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_add_usec>:
movslq %esi,%rcx
imul   $0x431bde83,%rcx,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rax
add    %edx,%eax
imul   $0xf4240,%eax,%edx
add    (%rdi),%eax
mov    %eax,(%rdi)
sub    %edx,%ecx
add    0x4(%rdi),%ecx
mov    %ecx,0x4(%rdi)
cmp    $0xf4241,%ecx
jl     113f <timeval_add_usec+0x3f>
add    $0xfff0bdc0,%ecx
mov    %ecx,0x4(%rdi)
add    $0x1,%eax
mov    %eax,(%rdi)
ret

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

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