#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 */
typedef scalar_t__ time_t;
struct timeval {
  scalar_t__ tv_sec;
};

/* Variables and functions */
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int gettime(struct timeval *tv) {
  int rc;
  static time_t offset = 0, previous = 0;
  rc = gettimeofday(tv, NULL);
  if (rc < 0)
    return rc;
  tv->tv_sec += offset;
  if (previous > tv->tv_sec) {
    offset += previous - tv->tv_sec;
    tv->tv_sec = previous;
  }
  previous = tv->tv_sec;
  return rc;
}


x86 assembly (clang, -O0):
<gettime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1141 <gettime+0x31>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a4 <gettime+0x94>
mov    0x2ee8(%rip),%rcx
mov    -0x10(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    0x2edf(%rip),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
jle    1190 <gettime+0x80>
mov    0x2ecb(%rip),%rax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%rax
add    0x2eb5(%rip),%rax
mov    %rax,0x2eae(%rip)
mov    0x2eaf(%rip),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,0x2e9a(%rip)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gettime>:
push   %rbx
mov    %rdi,%rbx
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %eax,%eax
js     1155 <gettime+0x45>
mov    (%rbx),%rcx
mov    0x2f07(%rip),%rdx
add    %rcx,%rdx
mov    %rdx,(%rbx)
mov    0x2f02(%rip),%rsi
cmp    %rdx,%rsi
jle    114e <gettime+0x3e>
mov    %rsi,%rdx
sub    %rcx,%rdx
mov    %rdx,0x2ee8(%rip)
mov    %rsi,(%rbx)
mov    %rsi,%rdx
mov    %rdx,0x2ee3(%rip)
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i64 }
@gettime.offset = internal global i64 0
@gettime.previous = internal global i64 0
define i32 @gettime(%struct.timeval* %0) {
%2 = alloca i32
%3 = alloca %struct.timeval*
%4 = alloca i32
store %struct.timeval* %0, %struct.timeval** %3
%5 = load %struct.timeval*, %struct.timeval** %3
%6 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = load i32, i32* %4
store i32 %10, i32* %2
br label %38
11: ; preds = %1
%12 = load i64, i64* @gettime.offset
%13 = load %struct.timeval*, %struct.timeval** %3
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%15 = load i64, i64* %14
%16 = add nsw i64 %15, %12
store i64 %16, i64* %14
%17 = load i64, i64* @gettime.previous
%18 = load %struct.timeval*, %struct.timeval** %3
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = icmp sgt i64 %17, %20
br i1 %21, label %22, label %33
22: ; preds = %11
%23 = load i64, i64* @gettime.previous
%24 = load %struct.timeval*, %struct.timeval** %3
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = sub nsw i64 %23, %26
%28 = load i64, i64* @gettime.offset
%29 = add nsw i64 %28, %27
store i64 %29, i64* @gettime.offset
%30 = load i64, i64* @gettime.previous
%31 = load %struct.timeval*, %struct.timeval** %3
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %31, i32 0, i32 0
store i64 %30, i64* %32
br label %33
33: ; preds = %22, %11
%34 = load %struct.timeval*, %struct.timeval** %3
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %34, i32 0, i32 0
%36 = load i64, i64* %35
store i64 %36, i64* @gettime.previous
%37 = load i32, i32* %4
store i32 %37, i32* %2
br label %38
38: ; preds = %33, %9
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.timeval = type { i64 }
@gettime.offset = internal unnamed_addr global i64 0
@gettime.previous = internal unnamed_addr global i64 0
define i32 @gettime(%struct.timeval* nocapture %0) {
%2 = tail call i32 @gettimeofday(%struct.timeval* %0, i32* null)
%3 = icmp slt i32 %2, 0
br i1 %3, label %15, label %4
4: ; preds = %1
%5 = load i64, i64* @gettime.offset
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%7 = load i64, i64* %6
%8 = add nsw i64 %7, %5
store i64 %8, i64* %6
%9 = load i64, i64* @gettime.previous
%10 = icmp sgt i64 %9, %8
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = sub i64 %9, %7
store i64 %12, i64* @gettime.offset
store i64 %9, i64* %6
br label %13
13: ; preds = %11, %4
%14 = phi i64 [ %9, %11 ], [ %8, %4 ]
store i64 %14, i64* @gettime.previous
br label %15
15: ; preds = %1, %13
ret i32 %2
}