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

/* Variables and functions */
void timeval_normalize(struct timeval *tv) {
  while (tv->tv_usec < 0) {
    tv->tv_usec += 1000000;
    tv->tv_sec--;
  }
  while (tv->tv_usec >= 1000000) {
    tv->tv_usec -= 1000000;
    tv->tv_sec++;
  }
}


x86 assembly (clang, -O0):
<timeval_normalize>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1135 <timeval_normalize+0x35>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
add    $0xf4240,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,0x4(%rax)
jmp    1108 <timeval_normalize+0x8>
jmp    113a <timeval_normalize+0x3a>
mov    -0x8(%rbp),%rax
cmpl   $0xf4240,(%rax)
jl     116a <timeval_normalize+0x6a>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sub    $0xf4240,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0x4(%rax)
jmp    113a <timeval_normalize+0x3a>
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_normalize>:
mov    (%rdi),%eax
test   %eax,%eax
js     110e <timeval_normalize+0xe>
cmp    $0xf4240,%eax
jge    114c <timeval_normalize+0x4c>
ret
cmp    $0xfff0bdc1,%eax
mov    $0xfff0bdc0,%ecx
cmovge %eax,%ecx
xor    %edx,%edx
sub    %eax,%ecx
setne  %dl
sub    %edx,%ecx
imul   $0x431bde83,%rcx,%rcx
shr    $0x32,%rcx
add    %edx,%ecx
imul   $0xf4240,%ecx,%edx
add    %edx,%eax
add    $0xf4240,%eax
not    %ecx
mov    %eax,(%rdi)
add    %ecx,0x4(%rdi)
cmp    $0xf4240,%eax
jl     110d <timeval_normalize+0xd>
mov    0x4(%rdi),%ecx
cmp    $0x1e847f,%eax
mov    $0x1e847f,%edx
cmovl  %eax,%edx
neg    %edx
add    %eax,%edx
add    $0xf423f,%edx
imul   $0x431bde83,%rdx,%rdx
shr    $0x32,%rdx
imul   $0xfff0bdc0,%edx,%esi
add    %esi,%eax
add    $0xfff0bdc0,%eax
add    %edx,%ecx
add    $0x1,%ecx
mov    %eax,(%rdi)
mov    %ecx,0x4(%rdi)
ret

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

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