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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
int /*<<< orphan*/ localtime_r(int /*<<< orphan*/ *, struct tm *const);
int /*<<< orphan*/ tcache_get(struct timeval *, struct tm *const);
int /*<<< orphan*/ tcache_set(struct timeval *, struct tm *const);
void time_callback(struct tm *const tm, unsigned *const msec) {
  struct timeval tv;
  gettimeofday(&tv, 0);
  if (!tcache_get(&tv, tm)) {
    localtime_r(&tv.tv_sec, tm);
    tcache_set(&tv, tm);
  }
  *msec = (unsigned)tv.tv_usec / 1000;
}


x86 assembly (clang, -O0):
<time_callback>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    -0x8(%rbp),%rsi
lea    -0x20(%rbp),%rdi
call   1030 <tcache_get@plt>
cmp    $0x0,%eax
jne    118f <time_callback+0x4f>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x8(%rbp),%rsi
call   1040 <localtime_r@plt>
mov    -0x8(%rbp),%rsi
lea    -0x20(%rbp),%rdi
call   1060 <tcache_set@plt>
mov    -0x20(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_callback>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <tcache_get@plt>
test   %eax,%eax
jne    1183 <time_callback+0x43>
lea    0x8(%rsp),%rdi
mov    %rbx,%rsi
call   1040 <localtime_r@plt>
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1060 <tcache_set@plt>
mov    (%rsp),%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %eax,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
%struct.timeval = type { i64, i32 }
define void @time_callback(%struct.tm* %0, i32* %1) {
%3 = alloca %struct.tm*
%4 = alloca i32*
%5 = alloca %struct.timeval
store %struct.tm* %0, %struct.tm** %3
store i32* %1, i32** %4
%6 = call i32 @gettimeofday(%struct.timeval* %5, i32 0)
%7 = load %struct.tm*, %struct.tm** %3
%8 = call i32 @tcache_get(%struct.timeval* %5, %struct.tm* %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%12 = load %struct.tm*, %struct.tm** %3
%13 = call i32 @localtime_r(i32* %11, %struct.tm* %12)
%14 = load %struct.tm*, %struct.tm** %3
%15 = call i32 @tcache_set(%struct.timeval* %5, %struct.tm* %14)
br label %16
16: ; preds = %10, %2
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%18 = load i64, i64* %17
%19 = trunc i64 %18 to i32
%20 = udiv i32 %19, 1000
%21 = load i32*, i32** %4
store i32 %20, i32* %21
ret void
}

LLVM IR (-O2):
%struct.tm = type { i32 }
%struct.timeval = type { i64, i32 }
define void @time_callback(%struct.tm* %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %3, i32 0)
%6 = call i32 @tcache_get(%struct.timeval* %3, %struct.tm* %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%10 = call i32 @localtime_r(i32* %9, %struct.tm* %0)
%11 = call i32 @tcache_set(%struct.timeval* %3, %struct.tm* %0)
br label %12
12: ; preds = %8, %2
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%14 = load i64, i64* %13
%15 = trunc i64 %14 to i32
%16 = udiv i32 %15, 1000
store i32 %16, i32* %1
ret void
}