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

/* Variables and functions */
void tkh_timeval_normalize(struct timeval *tv) {
  if (tv->tv_usec < 0) {
    tv->tv_sec -= 1;
    tv->tv_usec += 1000000;
  }
  if (tv->tv_usec < 0 || tv->tv_usec >= 1000000) {
    int div = (tv->tv_usec - 999999) / 1000000;
    tv->tv_sec += div;
    tv->tv_usec -= 1000000 * div;
  }
}


x86 assembly (clang, -O0):
<tkh_timeval_normalize>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1130 <tkh_timeval_normalize+0x30>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%ecx
sub    $0x1,%ecx
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
add    $0xf4240,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jl     114d <tkh_timeval_normalize+0x4d>
mov    -0x8(%rbp),%rax
cmpl   $0xf4240,(%rax)
jl     1181 <tkh_timeval_normalize+0x81>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
sub    $0xf423f,%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
add    0x4(%rax),%ecx
mov    %ecx,0x4(%rax)
imul   $0xf4240,-0xc(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sub    %edx,%ecx
mov    %ecx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<tkh_timeval_normalize>:
mov    (%rdi),%eax
test   %eax,%eax
js     110e <tkh_timeval_normalize+0xe>
cmp    $0xf4240,%eax
jae    1120 <tkh_timeval_normalize+0x20>
ret
addl   $0xffffffff,0x4(%rdi)
add    $0xf4240,%eax
mov    %eax,(%rdi)
cmp    $0xf4240,%eax
jb     110d <tkh_timeval_normalize+0xd>
lea    -0xf423f(%rax),%ecx
movslq %ecx,%rcx
imul   $0x431bde83,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
add    %ecx,0x4(%rdi)
imul   $0xfff0bdc0,%ecx,%ecx
add    %eax,%ecx
mov    %ecx,(%rdi)
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define void @tkh_timeval_normalize(%struct.timeval* %0) {
%2 = alloca %struct.timeval*
%3 = alloca i32
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 slt i32 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %1
%9 = load %struct.timeval*, %struct.timeval** %2
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%11 = load i32, i32* %10
%12 = sub nsw i32 %11, 1
store i32 %12, i32* %10
%13 = load %struct.timeval*, %struct.timeval** %2
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1000000
store i32 %16, i32* %14
br label %17
17: ; preds = %8, %1
%18 = load %struct.timeval*, %struct.timeval** %2
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = icmp slt i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = load %struct.timeval*, %struct.timeval** %2
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = icmp sge i32 %25, 1000000
br i1 %26, label %27, label %44
27: ; preds = %22, %17
%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 = sub nsw i32 %30, 999999
%32 = sdiv i32 %31, 1000000
store i32 %32, i32* %3
%33 = load i32, i32* %3
%34 = load %struct.timeval*, %struct.timeval** %2
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = add nsw i32 %36, %33
store i32 %37, i32* %35
%38 = load i32, i32* %3
%39 = mul nsw i32 1000000, %38
%40 = load %struct.timeval*, %struct.timeval** %2
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = sub nsw i32 %42, %39
store i32 %43, i32* %41
br label %44
44: ; preds = %27, %22
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define void @tkh_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 slt i32 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%7 = load i32, i32* %6
%8 = add nsw i32 %7, -1
store i32 %8, i32* %6
%9 = add nsw i32 %3, 1000000
store i32 %9, i32* %2
br label %10
10: ; preds = %5, %1
%11 = phi i32 [ %9, %5 ], [ %3, %1 ]
%12 = icmp ugt i32 %11, 999999
br i1 %12, label %13, label %21
13: ; preds = %10
%14 = add nsw i32 %11, -999999
%15 = sdiv i32 %14, 1000000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%17 = load i32, i32* %16
%18 = add nsw i32 %17, %15
store i32 %18, i32* %16
%19 = mul nsw i32 %15, -1000000
%20 = add i32 %19, %11
store i32 %20, i32* %2
br label %21
21: ; preds = %10, %13
ret void
}