#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;
  long tv_sec;
};

/* Variables and functions */
void gfarm_timeval_normalize(struct timeval *t) {
  long n;
  if (t->tv_usec >= 1000000) {
    n = t->tv_usec / 1000000;
    t->tv_usec -= n * 1000000;
    t->tv_sec += n;
  } else if (t->tv_usec < 0) {
    n = -t->tv_usec / 1000000 + 1;
    t->tv_usec += n * 1000000;
    t->tv_sec -= n;
  }
}


x86 assembly (clang, -O0):
<gfarm_timeval_normalize>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0xf4240,(%rax)
jl     1155 <gfarm_timeval_normalize+0x55>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x10(%rbp)
imul   $0xf4240,-0x10(%rbp),%rdx
mov    -0x8(%rbp),%rax
movslq (%rax),%rcx
sub    %rdx,%rcx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rax
add    0x8(%rax),%rcx
mov    %rcx,0x8(%rax)
jmp    11a7 <gfarm_timeval_normalize+0xa7>
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jge    11a2 <gfarm_timeval_normalize+0xa2>
mov    -0x8(%rbp),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
cltq
mov    %rax,-0x10(%rbp)
imul   $0xf4240,-0x10(%rbp),%rdx
mov    -0x8(%rbp),%rax
movslq (%rax),%rcx
add    %rdx,%rcx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
sub    %rdx,%rcx
mov    %rcx,0x8(%rax)
jmp    11a7 <gfarm_timeval_normalize+0xa7>
pop    %rbp
ret

x86 assembly (clang, -O2):
<gfarm_timeval_normalize>:
mov    (%rdi),%eax
cmp    $0xf4240,%eax
jl     1125 <gfarm_timeval_normalize+0x25>
mov    %eax,%ecx
imul   $0x431bde83,%rcx,%rcx
shr    $0x32,%rcx
imul   $0xfff0bdc0,%ecx,%edx
add    %eax,%edx
mov    %edx,(%rdi)
add    %rcx,0x8(%rdi)
ret
test   %eax,%eax
js     112a <gfarm_timeval_normalize+0x2a>
ret
mov    %eax,%ecx
neg    %ecx
imul   $0x431bde83,%rcx,%rcx
shr    $0x32,%rcx
add    $0x1,%ecx
imul   $0xf4240,%ecx,%edx
add    %eax,%edx
mov    %edx,(%rdi)
sub    %rcx,0x8(%rdi)
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define void @gfarm_timeval_normalize(%struct.timeval* %0) {
%2 = alloca %struct.timeval*
%3 = alloca i64
store %struct.timeval* %0, %struct.timeval** %2
%4 = load %struct.timeval*, %struct.timeval** %2
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%6 = load i32, i32* %5
%7 = icmp sge i32 %6, 1000000
br i1 %7, label %8, label %27
8: ; preds = %1
%9 = load %struct.timeval*, %struct.timeval** %2
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = sdiv i32 %11, 1000000
%13 = sext i32 %12 to i64
store i64 %13, i64* %3
%14 = load i64, i64* %3
%15 = mul nsw i64 %14, 1000000
%16 = load %struct.timeval*, %struct.timeval** %2
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = sext i32 %18 to i64
%20 = sub nsw i64 %19, %15
%21 = trunc i64 %20 to i32
store i32 %21, i32* %17
%22 = load i64, i64* %3
%23 = load %struct.timeval*, %struct.timeval** %2
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %23, i32 0, i32 1
%25 = load i64, i64* %24
%26 = add nsw i64 %25, %22
store i64 %26, i64* %24
br label %54
27: ; preds = %1
%28 = load %struct.timeval*, %struct.timeval** %2
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %53
32: ; preds = %27
%33 = load %struct.timeval*, %struct.timeval** %2
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = sub nsw i32 0, %35
%37 = sdiv i32 %36, 1000000
%38 = add nsw i32 %37, 1
%39 = sext i32 %38 to i64
store i64 %39, i64* %3
%40 = load i64, i64* %3
%41 = mul nsw i64 %40, 1000000
%42 = load %struct.timeval*, %struct.timeval** %2
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = sext i32 %44 to i64
%46 = add nsw i64 %45, %41
%47 = trunc i64 %46 to i32
store i32 %47, i32* %43
%48 = load i64, i64* %3
%49 = load %struct.timeval*, %struct.timeval** %2
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %49, i32 0, i32 1
%51 = load i64, i64* %50
%52 = sub nsw i64 %51, %48
store i64 %52, i64* %50
br label %53
53: ; preds = %32, %27
br label %54
54: ; preds = %53, %8
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define void @gfarm_timeval_normalize(%struct.timeval* nocapture %0) {
%2 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%3 = load i32, i32* %2
%4 = icmp sgt i32 %3, 999999
br i1 %4, label %5, label %13
5: ; preds = %1
%6 = udiv i32 %3, 1000000
%7 = zext i32 %6 to i64
%8 = mul nsw i32 %6, -1000000
%9 = add nsw i32 %8, %3
store i32 %9, i32* %2
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%11 = load i64, i64* %10
%12 = add nsw i64 %11, %7
store i64 %12, i64* %10
br label %25
13: ; preds = %1
%14 = icmp slt i32 %3, 0
br i1 %14, label %15, label %25
15: ; preds = %13
%16 = sub i32 0, %3
%17 = udiv i32 %16, 1000000
%18 = add nuw nsw i32 %17, 1
%19 = zext i32 %18 to i64
%20 = mul nuw i32 %18, 1000000
%21 = add i32 %20, %3
store i32 %21, i32* %2
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%23 = load i64, i64* %22
%24 = sub nsw i64 %23, %19
store i64 %24, i64* %22
br label %25
25: ; preds = %13, %15, %5
ret void
}