#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 int /*<<< orphan*/ time_t;
struct tm {
  int dummy;
};
struct timeval {
  int tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, void *);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
void get_local_time(struct tm *tm) {
  static struct timeval global_tv;
  static struct tm global_tm;
  static int first = 1;
  struct timeval current_tv;
  struct tm current_tm;
  time_t t;
  gettimeofday(&current_tv, ((void *)0));
  time(&t);
  current_tm = *localtime(&t);
  if (first) {
    first = 0;
    global_tv = current_tv;
    global_tm = current_tm;
  }
  if (current_tv.tv_sec - global_tv.tv_sec < 3) {
    *tm = global_tm;
  } else {
    *tm = current_tm;
    global_tm = current_tm;
    global_tv = current_tv;
  }
}


x86 assembly (clang, -O0):
<get_local_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
lea    -0x1c(%rbp),%rdi
call   1050 <time@plt>
lea    -0x1c(%rbp),%rdi
call   1030 <localtime@plt>
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,0x2ed1(%rip)
je     1189 <get_local_time+0x59>
movl   $0x0,0x2ec1(%rip)
mov    -0x10(%rbp),%eax
mov    %eax,0x2ec0(%rip)
mov    -0x18(%rbp),%eax
mov    %eax,0x2ebb(%rip)
mov    -0x10(%rbp),%eax
sub    0x2eae(%rip),%eax
cmp    $0x3,%eax
jge    11ac <get_local_time+0x7c>
mov    -0x8(%rbp),%rax
mov    0x2e9f(%rip),%ecx
mov    %ecx,(%rax)
jmp    11c7 <get_local_time+0x97>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%eax
mov    %eax,0x2e86(%rip)
mov    -0x10(%rbp),%eax
mov    %eax,0x2e79(%rip)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_local_time>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <time@plt>
mov    %rbx,%rdi
call   1030 <localtime@plt>
mov    (%rax),%eax
cmpb   $0x1,0x2ee5(%rip)
jne    116c <get_local_time+0x3c>
mov    (%rsp),%ecx
mov    0x2ed2(%rip),%edx
jmp    1184 <get_local_time+0x54>
movb   $0x1,0x2ed1(%rip)
mov    (%rsp),%edx
mov    %edx,0x2ec0(%rip)
mov    %eax,0x2ebe(%rip)
mov    %edx,%ecx
mov    %ecx,%esi
sub    %edx,%esi
cmp    $0x2,%esi
jg     1198 <get_local_time+0x68>
mov    0x2ead(%rip),%eax
mov    %eax,(%r14)
jmp    11a7 <get_local_time+0x77>
mov    %eax,(%r14)
mov    %eax,0x2e9f(%rip)
mov    %ecx,0x2e95(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
%struct.tm = type { i32 }
@get_local_time.global_tv = internal global %struct.timeval zeroinitializer
@get_local_time.global_tm = internal global %struct.tm zeroinitializer
@get_local_time.first = internal global i32 1
define void @get_local_time(%struct.tm* %0) {
%2 = alloca %struct.tm*
%3 = alloca %struct.timeval
%4 = alloca %struct.tm
%5 = alloca i32
store %struct.tm* %0, %struct.tm** %2
%6 = call i32 @gettimeofday(%struct.timeval* %3, i8* null)
%7 = call i32 @time(i32* %5)
%8 = call %struct.tm* @localtime(i32* %5)
%9 = bitcast %struct.tm* %4 to i8*
%10 = bitcast %struct.tm* %8 to i8*
%11 = load i32, i32* @get_local_time.first
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
store i32 0, i32* @get_local_time.first
%14 = bitcast %struct.timeval* %3 to i8*
%15 = bitcast %struct.tm* %4 to i8*
br label %16
16: ; preds = %13, %1
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @get_local_time.global_tv, i32 0, i32 0)
%20 = sub nsw i32 %18, %19
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load %struct.tm*, %struct.tm** %2
%24 = bitcast %struct.tm* %23 to i8*
br label %31
25: ; preds = %16
%26 = load %struct.tm*, %struct.tm** %2
%27 = bitcast %struct.tm* %26 to i8*
%28 = bitcast %struct.tm* %4 to i8*
%29 = bitcast %struct.tm* %4 to i8*
%30 = bitcast %struct.timeval* %3 to i8*
br label %31
31: ; preds = %25, %22
ret void
}

LLVM IR (-O2):
%struct.tm = type { i32 }
%struct.timeval = type { i32 }
@get_local_time.global_tv.0 = internal unnamed_addr global i32 0
@get_local_time.global_tm.0 = internal unnamed_addr global i32 0
@get_local_time.first = internal unnamed_addr global i1 false
define void @get_local_time(%struct.tm* nocapture writeonly %0) {
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = call i32 @gettimeofday(%struct.timeval* %2, i8* null)
%7 = call i32 @time(i32* %3)
%8 = call %struct.tm* @localtime(i32* %3)
%9 = getelementptr inbounds %struct.tm, %struct.tm* %8, i64 0, i32 0
%10 = load i32, i32* %9
%11 = load i1, i1* @get_local_time.first
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @get_local_time.global_tv.0
br label %19
16: ; preds = %1
store i1 true, i1* @get_local_time.first
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%18 = load i32, i32* %17
store i32 %18, i32* @get_local_time.global_tv.0
store i32 %10, i32* @get_local_time.global_tm.0
br label %19
19: ; preds = %12, %16
%20 = phi i32 [ %15, %12 ], [ %18, %16 ]
%21 = phi i32 [ %14, %12 ], [ %18, %16 ]
%22 = sub nsw i32 %21, %20
%23 = icmp slt i32 %22, 3
%24 = getelementptr %struct.tm, %struct.tm* %0, i64 0, i32 0
br i1 %23, label %25, label %27
25: ; preds = %19
%26 = load i32, i32* @get_local_time.global_tm.0
store i32 %26, i32* %24
br label %28
27: ; preds = %19
store i32 %10, i32* %24
store i32 %10, i32* @get_local_time.global_tm.0
store i32 %21, i32* @get_local_time.global_tv.0
br label %28
28: ; preds = %27, %25
ret void
}