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

/* Variables and functions */
int abs(int);
int /*<<< orphan*/ add_time(struct timespec *, struct timespec *,
                            struct timespec *);
int /*<<< orphan*/ adj_freq(int);
int /*<<< orphan*/ get_time(struct timespec *);
int /*<<< orphan*/ set_time(struct timespec *);
int /*<<< orphan*/ sub_time(struct timespec *, struct timespec *,
                            struct timespec *);
void update_clock(struct timespec *offset) {
  int adj = 0;
  static int ap = 2;
  static int ai = 10;
  static int observed_drift = 0;
  if (offset->tv_sec || abs(offset->tv_nsec) > 100000000) {
    struct timespec timeTmpA;
    struct timespec timeTmpB;
    struct timespec timeTmpC;
    struct timespec timeTmpD;
    struct timespec timeTmpE;
    struct timespec timeTmpF;
    get_time(&timeTmpA);
    get_time(&timeTmpB);
    sub_time(&timeTmpC, &timeTmpB, &timeTmpA);
    get_time(&timeTmpD);
    sub_time(&timeTmpE, &timeTmpD, offset);
    add_time(&timeTmpF, &timeTmpE, &timeTmpC);
    set_time(&timeTmpF);
  } else {
    observed_drift += offset->tv_nsec / ai;
    if (observed_drift > 512000) {
      observed_drift = 512000;
    } else if (observed_drift < -512000) {
      observed_drift = -512000;
    }
    adj = offset->tv_nsec / ap + observed_drift;
    adj_freq(-adj);
  }
}


x86 assembly (clang, -O0):
<update_clock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    1198 <update_clock+0x38>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1030 <abs@plt>
cmp    $0x5f5e100,%eax
jle    11f4 <update_clock+0x94>
lea    -0x20(%rbp),%rdi
call   1050 <get_time@plt>
lea    -0x30(%rbp),%rdi
call   1050 <get_time@plt>
lea    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <sub_time@plt>
lea    -0x50(%rbp),%rdi
call   1050 <get_time@plt>
mov    -0x8(%rbp),%rdx
lea    -0x60(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1080 <sub_time@plt>
lea    -0x70(%rbp),%rdi
lea    -0x60(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1040 <add_time@plt>
lea    -0x70(%rbp),%rdi
call   1060 <set_time@plt>
jmp    126b <update_clock+0x10b>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
cltd
idivl  0x2e53(%rip)
add    0x2e55(%rip),%eax
mov    %eax,0x2e4f(%rip)
cmpl   $0x7d000,0x2e45(%rip)
jle    122c <update_clock+0xcc>
movl   $0x7d000,0x2e35(%rip)
jmp    124b <update_clock+0xeb>
cmpl   $0xfff83000,0x2e26(%rip)
30 f8 ff
jge    1246 <update_clock+0xe6>
movl   $0xfff83000,0x2e16(%rip)
30 f8 ff
jmp    124b <update_clock+0xeb>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
cltd
idivl  0x2df8(%rip)
add    0x2dfe(%rip),%eax
mov    %eax,-0xc(%rbp)
xor    %edi,%edi
sub    -0xc(%rbp),%edi
call   1070 <adj_freq@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_clock>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%rbx
cmpq   $0x0,0x8(%rdi)
jne    1176 <update_clock+0x26>
mov    (%rbx),%eax
mov    %eax,%ecx
neg    %ecx
cmovs  %eax,%ecx
cmp    $0x5f5e101,%ecx
jb     11ea <update_clock+0x9a>
lea    0x58(%rsp),%r14
mov    %r14,%rdi
call   1040 <get_time@plt>
lea    0x48(%rsp),%r15
mov    %r15,%rdi
call   1040 <get_time@plt>
lea    0x38(%rsp),%r12
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <sub_time@plt>
lea    0x28(%rsp),%r14
mov    %r14,%rdi
call   1040 <get_time@plt>
lea    0x18(%rsp),%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <sub_time@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <add_time@plt>
mov    %rbx,%rdi
call   1050 <set_time@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
movslq %eax,%rcx
imul   $0x66666667,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    0x2e45(%rip),%ecx
mov    %ecx,0x2e3f(%rip)
mov    $0x7d000,%edx
cmp    $0x7d000,%ecx
jg     1227 <update_clock+0xd7>
mov    $0xfff83000,%edx
cmp    $0xfff82fff,%ecx
jg     122f <update_clock+0xdf>
mov    %edx,0x2e1f(%rip)
mov    %edx,%ecx
mov    %eax,%edi
shr    $0x1f,%edi
add    %eax,%edi
sar    %edi
add    %ecx,%edi
neg    %edi
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1060 <adj_freq@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@update_clock.ap = internal global i32 2
@update_clock.ai = internal global i32 10
@update_clock.observed_drift = internal global i32 0
define void @update_clock(%struct.timespec* %0) {
%2 = alloca %struct.timespec*
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
%8 = alloca %struct.timespec
%9 = alloca %struct.timespec
store %struct.timespec* %0, %struct.timespec** %2
store i32 0, i32* %3
%10 = load %struct.timespec*, %struct.timespec** %2
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 1
%12 = load i64, i64* %11
%13 = icmp ne i64 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %1
%15 = load %struct.timespec*, %struct.timespec** %2
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @abs(i32 %17)
%19 = icmp sgt i32 %18, 100000000
br i1 %19, label %20, label %29
20: ; preds = %14, %1
%21 = call i32 @get_time(%struct.timespec* %4)
%22 = call i32 @get_time(%struct.timespec* %5)
%23 = call i32 @sub_time(%struct.timespec* %6, %struct.timespec* %5, %struct.timespec* %4)
%24 = call i32 @get_time(%struct.timespec* %7)
%25 = load %struct.timespec*, %struct.timespec** %2
%26 = call i32 @sub_time(%struct.timespec* %8, %struct.timespec* %7, %struct.timespec* %25)
%27 = call i32 @add_time(%struct.timespec* %9, %struct.timespec* %8, %struct.timespec* %6)
%28 = call i32 @set_time(%struct.timespec* %9)
br label %56
29: ; preds = %14
%30 = load %struct.timespec*, %struct.timespec** %2
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i32, i32* @update_clock.ai
%34 = sdiv i32 %32, %33
%35 = load i32, i32* @update_clock.observed_drift
%36 = add nsw i32 %35, %34
store i32 %36, i32* @update_clock.observed_drift
%37 = load i32, i32* @update_clock.observed_drift
%38 = icmp sgt i32 %37, 512000
br i1 %38, label %39, label %40
39: ; preds = %29
store i32 512000, i32* @update_clock.observed_drift
br label %45
40: ; preds = %29
%41 = load i32, i32* @update_clock.observed_drift
%42 = icmp slt i32 %41, -512000
br i1 %42, label %43, label %44
43: ; preds = %40
store i32 -512000, i32* @update_clock.observed_drift
br label %44
44: ; preds = %43, %40
br label %45
45: ; preds = %44, %39
%46 = load %struct.timespec*, %struct.timespec** %2
%47 = getelementptr inbounds %struct.timespec, %struct.timespec* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = load i32, i32* @update_clock.ap
%50 = sdiv i32 %48, %49
%51 = load i32, i32* @update_clock.observed_drift
%52 = add nsw i32 %50, %51
store i32 %52, i32* %3
%53 = load i32, i32* %3
%54 = sub nsw i32 0, %53
%55 = call i32 @adj_freq(i32 %54)
br label %56
56: ; preds = %45, %20
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@update_clock.observed_drift = internal unnamed_addr global i32 0
define void @update_clock(%struct.timespec* %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%9 = load i64, i64* %8
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i32 @llvm.abs.i32(i32 %13, i1 true)
%15 = icmp ugt i32 %14, 100000000
br i1 %15, label %16, label %30
16: ; preds = %11, %1
%17 = bitcast %struct.timespec* %2 to i8*
%18 = bitcast %struct.timespec* %3 to i8*
%19 = bitcast %struct.timespec* %4 to i8*
%20 = bitcast %struct.timespec* %5 to i8*
%21 = bitcast %struct.timespec* %6 to i8*
%22 = bitcast %struct.timespec* %7 to i8*
%23 = call i32 @get_time(%struct.timespec* %2)
%24 = call i32 @get_time(%struct.timespec* %3)
%25 = call i32 @sub_time(%struct.timespec* %4, %struct.timespec* %3, %struct.timespec* %2)
%26 = call i32 @get_time(%struct.timespec* %5)
%27 = call i32 @sub_time(%struct.timespec* %6, %struct.timespec* %5, %struct.timespec* %0)
%28 = call i32 @add_time(%struct.timespec* %7, %struct.timespec* %6, %struct.timespec* %4)
%29 = call i32 @set_time(%struct.timespec* %7)
br label %44
30: ; preds = %11
%31 = sdiv i32 %13, 10
%32 = load i32, i32* @update_clock.observed_drift
%33 = add nsw i32 %32, %31
store i32 %33, i32* @update_clock.observed_drift
%34 = icmp sgt i32 %33, 512000
br i1 %34, label %37, label %35
35: ; preds = %30
%36 = icmp slt i32 %33, -512000
br i1 %36, label %37, label %39
37: ; preds = %35, %30
%38 = phi i32 [ 512000, %30 ], [ -512000, %35 ]
store i32 %38, i32* @update_clock.observed_drift
br label %39
39: ; preds = %37, %35
%40 = phi i32 [ %33, %35 ], [ %38, %37 ]
%41 = sdiv i32 %13, -2
%42 = sub nsw i32 %41, %40
%43 = tail call i32 @adj_freq(i32 %42)
br label %44
44: ; preds = %39, %16
ret void
}