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

/* Variables and functions */
void normalize(struct timespec *t) {
  while (t->tv_nsec < 0) {
    t->tv_nsec += 1000000000;
    t->tv_sec -= 1;
  }
  while (t->tv_nsec >= 1000000000) {
    t->tv_nsec -= 1000000000;
    t->tv_sec += 1;
  }
}


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

x86 assembly (clang, -O2):
<normalize>:
mov    (%rdi),%eax
test   %eax,%eax
js     110e <normalize+0xe>
cmp    $0x3b9aca00,%eax
jge    114f <normalize+0x4f>
ret
cmp    $0xc4653601,%eax
mov    $0xc4653600,%ecx
cmovge %eax,%ecx
xor    %edx,%edx
sub    %eax,%ecx
setne  %dl
sub    %edx,%ecx
shr    $0x9,%ecx
imul   $0x44b83,%rcx,%rcx
shr    $0x27,%rcx
add    %edx,%ecx
imul   $0x3b9aca00,%ecx,%edx
add    %edx,%eax
add    $0x3b9aca00,%eax
not    %ecx
mov    %eax,(%rdi)
add    %ecx,0x4(%rdi)
cmp    $0x3b9aca00,%eax
jl     110d <normalize+0xd>
mov    0x4(%rdi),%ecx
cmp    $0x773593ff,%eax
mov    $0x773593ff,%edx
cmovl  %eax,%edx
neg    %edx
add    %eax,%edx
add    $0x3b9ac9ff,%edx
shr    $0x9,%edx
imul   $0x44b83,%rdx,%rdx
shr    $0x27,%rdx
imul   $0xc4653600,%edx,%esi
add    %esi,%eax
add    $0xc4653600,%eax
add    %edx,%ecx
add    $0x1,%ecx
mov    %eax,(%rdi)
mov    %ecx,0x4(%rdi)
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define void @normalize(%struct.timespec* %0) {
%2 = alloca %struct.timespec*
store %struct.timespec* %0, %struct.timespec** %2
br label %3
3: ; preds = %8, %1
%4 = load %struct.timespec*, %struct.timespec** %2
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %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.timespec*, %struct.timespec** %2
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = add nsw i32 %11, 1000000000
store i32 %12, i32* %10
%13 = load %struct.timespec*, %struct.timespec** %2
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 1
%15 = load i32, i32* %14
%16 = sub 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.timespec*, %struct.timespec** %2
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = icmp sge i32 %21, 1000000000
br i1 %22, label %23, label %32
23: ; preds = %18
%24 = load %struct.timespec*, %struct.timespec** %2
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = sub nsw i32 %26, 1000000000
store i32 %27, i32* %25
%28 = load %struct.timespec*, %struct.timespec** %2
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %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.timespec = type { i32, i32 }
define void @normalize(%struct.timespec* nocapture %0) {
%2 = getelementptr inbounds %struct.timespec, %struct.timespec* %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.timespec, %struct.timespec* %0, i64 0, i32 1
%7 = load i32, i32* %6
%8 = call i32 @llvm.smax.i32(i32 %3, i32 -1000000000)
%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, 1000000000
%14 = add nuw nsw i32 %13, %11
%15 = mul i32 %14, 1000000000
%16 = add i32 %3, %15
%17 = add i32 %16, 1000000000
%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, 999999999
br i1 %22, label %23, label %35
23: ; preds = %20
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%25 = load i32, i32* %24
%26 = add nuw i32 %21, 999999999
%27 = call i32 @llvm.smin.i32(i32 %21, i32 1999999999)
%28 = sub nuw i32 %26, %27
%29 = udiv i32 %28, 1000000000
%30 = mul nsw i32 %29, -1000000000
%31 = add i32 %25, %29
%32 = add nsw i32 %21, -1000000000
%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
}