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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ LOG_ERR;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int TimerThread_GetTime(struct timespec *next, long long int interval) {
  int ret = clock_gettime((CLOCK_REALTIME), (next));
  if (ret < 0) {
    syslog(LOG_ERR, "Failed to get the OS time.");
    return ret;
  }
  next->tv_nsec += interval;
  next->tv_sec += next->tv_nsec / 1000000000;
  next->tv_nsec %= 1000000000;
  return ret;
}


x86 assembly (clang, -O0):
<TimerThread_GetTime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    116f <TimerThread_GetTime+0x4f>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
lea    0xea1(%rip),%rsi
call   1040 <syslog@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c1 <TimerThread_GetTime+0xa1>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq 0x8(%rax),%rcx
add    %rdx,%rcx
mov    %ecx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    (%rax),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    -0x28(%rbp),%rax
mov    %rdx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TimerThread_GetTime>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    0x2eaf(%rip),%rax
mov    (%rax),%edi
mov    %rbp,%rsi
call   1030 <clock_gettime@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1177 <TimerThread_GetTime+0x57>
add    0x0(%rbp),%rbx
movabs $0x112e0be826d694b3,%rcx
0b 2e 11
mov    %rbx,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1a,%rdx
add    %rax,%rdx
add    %edx,0x8(%rbp)
imul   $0x3b9aca00,%rdx,%rax
sub    %rax,%rbx
mov    %rbx,0x0(%rbp)
jmp    118c <TimerThread_GetTime+0x6c>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
call   1040 <syslog@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@CLOCK_REALTIME = global i32 0
@LOG_ERR = global i32 0
@.str = [27 x i8] c"Failed to get the OS time.\00"
define i32 @TimerThread_GetTime(%struct.timespec* %0, i64 %1) {
%3 = alloca i32
%4 = alloca %struct.timespec*
%5 = alloca i64
%6 = alloca i32
store %struct.timespec* %0, %struct.timespec** %4
store i64 %1, i64* %5
%7 = load i32, i32* @CLOCK_REALTIME
%8 = load %struct.timespec*, %struct.timespec** %4
%9 = call i32 @clock_gettime(i32 %7, %struct.timespec* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @LOG_ERR
%14 = call i32 @syslog(i32 %13, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* %6
store i32 %15, i32* %3
br label %37
16: ; preds = %2
%17 = load i64, i64* %5
%18 = load %struct.timespec*, %struct.timespec** %4
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = add nsw i64 %20, %17
store i64 %21, i64* %19
%22 = load %struct.timespec*, %struct.timespec** %4
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %22, i32 0, i32 0
%24 = load i64, i64* %23
%25 = sdiv i64 %24, 1000000000
%26 = load %struct.timespec*, %struct.timespec** %4
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = sext i32 %28 to i64
%30 = add nsw i64 %29, %25
%31 = trunc i64 %30 to i32
store i32 %31, i32* %27
%32 = load %struct.timespec*, %struct.timespec** %4
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %32, i32 0, i32 0
%34 = load i64, i64* %33
%35 = srem i64 %34, 1000000000
store i64 %35, i64* %33
%36 = load i32, i32* %6
store i32 %36, i32* %3
br label %37
37: ; preds = %16, %12
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@CLOCK_REALTIME = global i32 0
@LOG_ERR = global i32 0
@.str = [27 x i8] c"Failed to get the OS time.\00"
define i32 @TimerThread_GetTime(%struct.timespec* %0, i64 %1) {
%3 = load i32, i32* @CLOCK_REALTIME
%4 = tail call i32 @clock_gettime(i32 %3, %struct.timespec* %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @LOG_ERR
%8 = tail call i32 @syslog(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %19
9: ; preds = %2
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%11 = load i64, i64* %10
%12 = add nsw i64 %11, %1
%13 = sdiv i64 %12, 1000000000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%15 = load i32, i32* %14
%16 = trunc i64 %13 to i32
%17 = add i32 %15, %16
store i32 %17, i32* %14
%18 = srem i64 %12, 1000000000
store i64 %18, i64* %10
br label %19
19: ; preds = %9, %6
ret i32 %4
}