#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 tm_year;
  int tm_mon;
  int tm_mday;
  int tm_hour;
  int tm_min;
  int tm_sec;
};
struct timeval {
  scalar_t__ tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
struct tm *localtime(int /*<<< orphan*/ *);
scalar_t__ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ *tzname;
int timestamp(char *buf, int len) {
  struct timeval tv;
  struct tm *ltime;
  int rv = 0;
  gettimeofday(&tv, NULL);
  ltime = localtime(&tv.tv_sec);
  rv += snprintf(buf, len, "%04d-%02d-%02dT%02d:%02d:%02d.%03d%s",
                 (ltime)->tm_year + 1900, (ltime)->tm_mon + 1, (ltime)->tm_mday,
                 (ltime)->tm_hour, (ltime)->tm_min, (ltime)->tm_sec,
                 (int)(&tv)->tv_usec / 1000, tzname[0]);
  rv += snprintf(buf + rv, len - rv, ": ");
  return rv;
}


x86 assembly (clang, -O0):
<timestamp>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x60,%rsp
mov    %rdi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
movl   $0x0,-0x3c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
call   1030 <localtime@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%ecx
add    $0x76c,%ecx
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%r8d
add    $0x1,%r8d
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%r9d
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%r14d
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%ebx
mov    -0x38(%rbp),%rax
mov    0x14(%rax),%r11d
mov    -0x30(%rbp),%rax
mov    $0x3e8,%r10d
cltd
idiv   %r10d
mov    %eax,%r10d
mov    0x2e23(%rip),%rax
mov    (%rax),%rax
mov    (%rax),%eax
lea    0xe37(%rip),%rdx
mov    %r14d,(%rsp)
mov    %ebx,0x8(%rsp)
mov    %r11d,0x10(%rsp)
mov    %r10d,0x18(%rsp)
mov    %eax,0x20(%rsp)
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %rax,%rcx
movslq -0x3c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x3c(%rbp),%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%esi
sub    -0x3c(%rbp),%esi
lea    0xe1a(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %rax,%rcx
movslq -0x3c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

x86 assembly (clang, -O2):
<timestamp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
mov    %rdi,%r12
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0x8(%rsp),%rdi
call   1030 <localtime@plt>
mov    $0x76c,%ecx
add    (%rax),%ecx
mov    0x4(%rax),%r8d
mov    0x8(%rax),%r9d
add    $0x1,%r8d
mov    0xc(%rax),%r10d
mov    0x10(%rax),%r11d
mov    0x14(%rax),%r14d
movslq (%rsp),%rax
imul   $0x10624dd3,%rax,%rbp
mov    %rbp,%rax
shr    $0x3f,%rax
sar    $0x26,%rbp
add    %eax,%ebp
mov    0x2e4c(%rip),%rax
mov    (%rax),%rax
mov    (%rax),%ebx
sub    $0x8,%rsp
lea    0xe5c(%rip),%rdx
mov    %r12,%rdi
mov    %r15d,%esi
xor    %eax,%eax
push   %rbx
push   %rbp
push   %r14
push   %r11
push   %r10
call   1040 <snprintf@plt>
add    $0x30,%rsp
movslq %eax,%rbx
add    %rbx,%r12
sub    %ebx,%r15d
lea    0xe58(%rip),%rdx
mov    %r12,%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
add    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [37 x i8] c"%04d-%02d-%02dT%02d:%02d:%02d.%03d%s\00"
@tzname = global i32* null
@.str.1 = [3 x i8] c": \00"
define i32 @timestamp(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca %struct.tm*
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %7
%8 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%10 = call %struct.tm* @localtime(i32* %9)
store %struct.tm* %10, %struct.tm** %6
%11 = load i8*, i8** %3
%12 = load i32, i32* %4
%13 = load %struct.tm*, %struct.tm** %6
%14 = getelementptr inbounds %struct.tm, %struct.tm* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1900
%17 = load %struct.tm*, %struct.tm** %6
%18 = getelementptr inbounds %struct.tm, %struct.tm* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = add nsw i32 %19, 1
%21 = load %struct.tm*, %struct.tm** %6
%22 = getelementptr inbounds %struct.tm, %struct.tm* %21, i32 0, i32 2
%23 = load i32, i32* %22
%24 = load %struct.tm*, %struct.tm** %6
%25 = getelementptr inbounds %struct.tm, %struct.tm* %24, i32 0, i32 3
%26 = load i32, i32* %25
%27 = load %struct.tm*, %struct.tm** %6
%28 = getelementptr inbounds %struct.tm, %struct.tm* %27, i32 0, i32 4
%29 = load i32, i32* %28
%30 = load %struct.tm*, %struct.tm** %6
%31 = getelementptr inbounds %struct.tm, %struct.tm* %30, i32 0, i32 5
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%34 = load i64, i64* %33
%35 = trunc i64 %34 to i32
%36 = sdiv i32 %35, 1000
%37 = load i32*, i32** @tzname
%38 = getelementptr inbounds i32, i32* %37, i64 0
%39 = load i32, i32* %38
%40 = call i64 (i8*, i32, i8*, ...) @snprintf(i8* %11, i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %20, i32 %23, i32 %26, i32 %29, i32 %32, i32 %36, i32 %39)
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = add nsw i64 %42, %40
%44 = trunc i64 %43 to i32
store i32 %44, i32* %7
%45 = load i8*, i8** %3
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i32, i32* %4
%50 = load i32, i32* %7
%51 = sub nsw i32 %49, %50
%52 = call i64 (i8*, i32, i8*, ...) @snprintf(i8* %48, i32 %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = add nsw i64 %54, %52
%56 = trunc i64 %55 to i32
store i32 %56, i32* %7
%57 = load i32, i32* %7
ret i32 %57
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [37 x i8] c"%04d-%02d-%02dT%02d:%02d:%02d.%03d%s\00"
@tzname = global i32* null
@.str.1 = [3 x i8] c": \00"
define i32 @timestamp(i8* %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%7 = call %struct.tm* @localtime(i32* %6)
%8 = getelementptr inbounds %struct.tm, %struct.tm* %7, i64 0, i32 0
%9 = load i32, i32* %8
%10 = add nsw i32 %9, 1900
%11 = getelementptr inbounds %struct.tm, %struct.tm* %7, i64 0, i32 1
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 1
%14 = getelementptr inbounds %struct.tm, %struct.tm* %7, i64 0, i32 2
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.tm, %struct.tm* %7, i64 0, i32 3
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.tm, %struct.tm* %7, i64 0, i32 4
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.tm, %struct.tm* %7, i64 0, i32 5
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%23 = load i64, i64* %22
%24 = trunc i64 %23 to i32
%25 = sdiv i32 %24, 1000
%26 = load i32*, i32** @tzname
%27 = load i32, i32* %26
%28 = call i64 (i8*, i32, i8*, ...) @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %13, i32 %15, i32 %17, i32 %19, i32 %21, i32 %25, i32 %27)
%29 = trunc i64 %28 to i32
%30 = shl i64 %28, 32
%31 = ashr exact i64 %30, 32
%32 = getelementptr inbounds i8, i8* %0, i64 %31
%33 = sub nsw i32 %1, %29
%34 = call i64 (i8*, i32, i8*, ...) @snprintf(i8* %32, i32 %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%35 = add i64 %34, %28
%36 = trunc i64 %35 to i32
ret i32 %36
}