#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;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
void pv_timeval_add_usec(struct timeval *val, long usec) {
  val->tv_usec += usec;
  while (val->tv_usec < 0) {
    val->tv_sec--;
    val->tv_usec += 1000000;
  }
  while (val->tv_usec >= 1000000) {
    val->tv_sec++;
    val->tv_usec -= 1000000;
  }
}


x86 assembly (clang, -O0):
<pv_timeval_add_usec>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jge    114b <pv_timeval_add_usec+0x4b>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0xf4240,%rcx
mov    %rcx,(%rax)
jmp    111a <pv_timeval_add_usec+0x1a>
jmp    1150 <pv_timeval_add_usec+0x50>
mov    -0x8(%rbp),%rax
cmpq   $0xf4240,(%rax)
jl     1184 <pv_timeval_add_usec+0x84>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
sub    $0xf4240,%rcx
mov    %rcx,(%rax)
jmp    1150 <pv_timeval_add_usec+0x50>
pop    %rbp
ret

x86 assembly (clang, -O2):
<pv_timeval_add_usec>:
add    (%rdi),%rsi
jns    1153 <pv_timeval_add_usec+0x53>
cmp    $0xfffffffffff0bdc1,%rsi
mov    $0xfffffffffff0bdc0,%rax
cmovge %rsi,%rax
xor    %ecx,%ecx
cmp    $0xfffffffffff0bdc0,%rsi
setb   %cl
sub    %rsi,%rax
sub    %rcx,%rax
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mul    %rdx
shr    $0x12,%rdx
add    %rcx,%rdx
imul   $0xf4240,%rdx,%rax
not    %edx
add    %rax,%rsi
add    $0xf4240,%rsi
add    %edx,0x8(%rdi)
mov    %rsi,(%rdi)
cmp    $0xf4240,%rsi
jl     11ac <pv_timeval_add_usec+0xac>
cmp    $0x1e847f,%rsi
mov    $0x1e847f,%eax
cmovl  %rsi,%rax
mov    0x8(%rdi),%ecx
neg    %rax
add    %rsi,%rax
add    $0xf423f,%rax
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mul    %rdx
shr    $0x12,%rdx
lea    (%rcx,%rdx,1),%eax
add    $0x1,%eax
imul   $0xfffffffffff0bdc0,%rdx,%rcx
add    %rsi,%rcx
add    $0xfffffffffff0bdc0,%rcx
mov    %eax,0x8(%rdi)
mov    %rcx,(%rdi)
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
define void @pv_timeval_add_usec(%struct.timeval* nocapture %0, i64 %1) {
%3 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = add i64 %4, %1
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %23
7: ; preds = %2
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%9 = load i32, i32* %8
%10 = call i64 @llvm.smax.i64(i64 %5, i64 -1000000)
%11 = icmp ult i64 %5, -1000000
%12 = zext i1 %11 to i64
%13 = add nsw i64 %5, %12
%14 = sub i64 %10, %13
%15 = udiv i64 %14, 1000000
%16 = add nuw nsw i64 %15, %12
%17 = trunc i64 %16 to i32
%18 = mul i64 %16, 1000000
%19 = add i64 %5, %18
%20 = xor i32 %17, -1
%21 = add i32 %9, %20
%22 = add i64 %19, 1000000
store i32 %21, i32* %8
br label %23
23: ; preds = %7, %2
%24 = phi i64 [ %22, %7 ], [ %5, %2 ]
store i64 %24, i64* %3
%25 = icmp sgt i64 %24, 999999
br i1 %25, label %26, label %39
26: ; preds = %23
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%28 = load i32, i32* %27
%29 = add nuw i64 %24, 999999
%30 = call i64 @llvm.smin.i64(i64 %24, i64 1999999)
%31 = sub nuw i64 %29, %30
%32 = udiv i64 %31, 1000000
%33 = trunc i64 %32 to i32
%34 = add i32 %28, %33
%35 = add i32 %34, 1
%36 = mul nsw i64 %32, -1000000
%37 = add nsw i64 %24, -1000000
%38 = add nsw i64 %36, %37
store i32 %35, i32* %27
store i64 %38, i64* %3
br label %39
39: ; preds = %26, %23
ret void
}