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

/* Variables and functions */
void timespec_normalize(struct timespec *ts) {
  while (ts->tv_nsec >= (1000000000L)) {
    ts->tv_sec++;
    ts->tv_nsec -= (1000000000L);
  }
  while (ts->tv_nsec < 0) {
    ts->tv_sec--;
    ts->tv_nsec += (1000000000L);
  }
}


x86 assembly (clang, -O0):
<timespec_normalize>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x3b9aca00,(%rax)
jl     113c <timespec_normalize+0x3c>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
sub    $0x3b9aca00,%rcx
mov    %rcx,(%rax)
jmp    1108 <timespec_normalize+0x8>
jmp    1141 <timespec_normalize+0x41>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jge    1172 <timespec_normalize+0x72>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x3b9aca00,%rcx
mov    %rcx,(%rax)
jmp    1141 <timespec_normalize+0x41>
pop    %rbp
ret

x86 assembly (clang, -O2):
<timespec_normalize>:
mov    (%rdi),%rcx
cmp    $0x3b9aca00,%rcx
jge    1112 <timespec_normalize+0x12>
test   %rcx,%rcx
js     1168 <timespec_normalize+0x68>
ret
mov    0x8(%rdi),%esi
cmp    $0x773593ff,%rcx
mov    $0x773593ff,%eax
cmovl  %rcx,%rax
neg    %rax
add    %rcx,%rax
add    $0x3b9ac9ff,%rax
shr    $0x9,%rax
movabs $0x44b82fa09b5a53,%rdx
mul    %rdx
shr    $0xb,%rdx
lea    (%rsi,%rdx,1),%eax
add    $0x1,%eax
imul   $0xffffffffc4653600,%rdx,%rdx
add    %rdx,%rcx
add    $0xffffffffc4653600,%rcx
mov    %eax,0x8(%rdi)
mov    %rcx,(%rdi)
test   %rcx,%rcx
jns    1111 <timespec_normalize+0x11>
cmp    $0xffffffffc4653601,%rcx
mov    $0xffffffffc4653600,%rax
cmovge %rcx,%rax
xor    %esi,%esi
sub    %rcx,%rax
setne  %sil
sub    %rsi,%rax
shr    $0x9,%rax
movabs $0x44b82fa09b5a53,%rdx
mul    %rdx
shr    $0xb,%rdx
add    %rsi,%rdx
imul   $0x3b9aca00,%rdx,%rax
not    %edx
add    %rcx,%rax
add    $0x3b9aca00,%rax
add    %edx,0x8(%rdi)
mov    %rax,(%rdi)
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
define void @timespec_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 i64, i64* %5
%7 = icmp sge i64 %6, 1000000000
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 1
%11 = load i32, i32* %10
%12 = add nsw i32 %11, 1
store i32 %12, i32* %10
%13 = load %struct.timespec*, %struct.timespec** %2
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 0
%15 = load i64, i64* %14
%16 = sub nsw i64 %15, 1000000000
store i64 %16, i64* %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 i64, i64* %20
%22 = icmp slt i64 %21, 0
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 1
%26 = load i32, i32* %25
%27 = add nsw i32 %26, -1
store i32 %27, i32* %25
%28 = load %struct.timespec*, %struct.timespec** %2
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %28, i32 0, i32 0
%30 = load i64, i64* %29
%31 = add nsw i64 %30, 1000000000
store i64 %31, i64* %29
br label %18
32: ; preds = %18
ret void
}

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