#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 tm {
  int dummy;
};

/* Variables and functions */
struct tm *localtime(scalar_t__ *);
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
scalar_t__ time(int /*<<< orphan*/ *);
const char *timestamp(char *dst, size_t dst_len) {
  time_t now = time(NULL);
  static struct {
    time_t when;
    char string[32];
  } timestamp_cache = {.when = 0, .string = {'\0'}};
  if (now != timestamp_cache.when) {
    struct tm *tmp = localtime(&now);
    strftime(timestamp_cache.string, sizeof(timestamp_cache.string), "%F %T ",
             tmp);
    timestamp_cache.when = now;
  }
  if (dst != NULL)
    strncpy(dst, timestamp_cache.string, dst_len);
  return timestamp_cache.string;
}


x86 assembly (clang, -O0):
<timestamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    0x2ee0(%rip),%rax
je     11a6 <timestamp+0x66>
lea    -0x18(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
lea    0x2ec2(%rip),%rdi
add    $0x8,%rdi
mov    $0x20,%esi
lea    0xe6a(%rip),%rdx
call   1060 <strftime@plt>
mov    -0x18(%rbp),%rax
mov    %rax,0x2ea2(%rip)
cmpq   $0x0,-0x8(%rbp)
je     11c9 <timestamp+0x89>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0x2e88(%rip),%rsi
add    $0x8,%rsi
call   1040 <strncpy@plt>
lea    0x2e78(%rip),%rax
add    $0x8,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timestamp>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
call   1050 <time@plt>
mov    %rax,(%rsp)
cmp    0x2eec(%rip),%rax
je     118c <timestamp+0x4c>
mov    %rsp,%rdi
call   1030 <localtime@plt>
lea    0x2ee3(%rip),%rdi
lea    0xe8c(%rip),%rdx
mov    $0x20,%esi
mov    %rax,%rcx
call   1060 <strftime@plt>
mov    (%rsp),%rax
mov    %rax,0x2ebc(%rip)
test   %rbx,%rbx
je     11a3 <timestamp+0x63>
lea    0x2eb8(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <strncpy@plt>
lea    0x2ea6(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.anon = type { i64, [32 x i8] }
%struct.tm = type { i32 }
@timestamp.timestamp_cache = internal global %struct.anon zeroinitializer
@.str = [7 x i8] c"%F %T \00"
define i8* @timestamp(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca %struct.tm*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = call i64 @time(i32* null)
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = load i64, i64* getelementptr inbounds (%struct.anon, %struct.anon* @timestamp.timestamp_cache, i32 0, i32 0)
%10 = icmp ne i64 %8, %9
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = call %struct.tm* @localtime(i64* %5)
store %struct.tm* %12, %struct.tm** %6
%13 = load %struct.tm*, %struct.tm** %6
%14 = call i32 @strftime(i8* getelementptr inbounds (%struct.anon, %struct.anon* @timestamp.timestamp_cache, i32 0, i32 1, i64 0), i32 32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %struct.tm* %13)
%15 = load i64, i64* %5
store i64 %15, i64* getelementptr inbounds (%struct.anon, %struct.anon* @timestamp.timestamp_cache, i32 0, i32 0)
br label %16
16: ; preds = %11, %2
%17 = load i8*, i8** %3
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i8*, i8** %3
%21 = load i64, i64* %4
%22 = call i32 @strncpy(i8* %20, i8* getelementptr inbounds (%struct.anon, %struct.anon* @timestamp.timestamp_cache, i32 0, i32 1, i64 0), i64 %21)
br label %23
23: ; preds = %19, %16
ret i8* getelementptr inbounds (%struct.anon, %struct.anon* @timestamp.timestamp_cache, i32 0, i32 1, i64 0)
}

LLVM IR (-O2):
%struct.anon = type { i64, [32 x i8] }
%struct.tm = type { i32 }
@timestamp.timestamp_cache = internal global %struct.anon zeroinitializer
@.str = [7 x i8] c"%F %T \00"
define i8* @timestamp(i8* %0, i64 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = tail call i64 @time(i32* null)
store i64 %5, i64* %3
%6 = load i64, i64* getelementptr inbounds (%struct.anon, %struct.anon* @timestamp.timestamp_cache, i64 0, i32 0)
%7 = icmp eq i64 %5, %6
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = call %struct.tm* @localtime(i64* %3)
%10 = call i32 @strftime(i8* getelementptr inbounds (%struct.anon, %struct.anon* @timestamp.timestamp_cache, i64 0, i32 1, i64 0), i32 32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %struct.tm* %9)
%11 = load i64, i64* %3
store i64 %11, i64* getelementptr inbounds (%struct.anon, %struct.anon* @timestamp.timestamp_cache, i64 0, i32 0)
br label %12
12: ; preds = %8, %2
%13 = icmp eq i8* %0, null
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = call i32 @strncpy(i8* %0, i8* getelementptr inbounds (%struct.anon, %struct.anon* @timestamp.timestamp_cache, i64 0, i32 1, i64 0), i64 %1)
br label %16
16: ; preds = %14, %12
ret i8* getelementptr inbounds (%struct.anon, %struct.anon* @timestamp.timestamp_cache, i64 0, i32 1, i64 0)
}