#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*/ *);
int /*<<< orphan*/ localtime_r(int /*<<< orphan*/ *, struct tm *);
int snprintf(char *, size_t const, char *, int, int, int, int, int, int, int);
int log_tstamp(char *tstamp, size_t const len) {
  int ret = 0;
  struct tm date;
  struct timeval time;
  gettimeofday(&time, NULL);
  localtime_r(&time.tv_sec, &date);
  ret =
      snprintf(tstamp, len, "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
               date.tm_year + 1900, date.tm_mon + 1, date.tm_mday, date.tm_hour,
               date.tm_min, date.tm_sec, (int)time.tv_usec / 1000);
  return ret;
}


x86 assembly (clang, -O0):
<log_tstamp>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x60,%rsp
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
lea    -0x50(%rbp),%rdi
add    $0x8,%rdi
lea    -0x40(%rbp),%rsi
call   1030 <localtime_r@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%ecx
add    $0x76c,%ecx
mov    -0x3c(%rbp),%r8d
add    $0x1,%r8d
mov    -0x38(%rbp),%r9d
mov    -0x34(%rbp),%ebx
mov    -0x30(%rbp),%r11d
mov    -0x2c(%rbp),%r10d
mov    -0x50(%rbp),%rax
mov    $0x3e8,%r14d
cltd
idiv   %r14d
lea    0xe5b(%rip),%rdx
mov    %ebx,(%rsp)
mov    %r11d,0x8(%rsp)
mov    %r10d,0x10(%rsp)
mov    %eax,0x18(%rsp)
call   1040 <snprintf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_tstamp>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x20(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0x28(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1030 <localtime_r@plt>
mov    $0x76c,%ecx
add    0x8(%rsp),%ecx
mov    0xc(%rsp),%r8d
mov    0x10(%rsp),%r9d
add    $0x1,%r8d
mov    0x14(%rsp),%r10d
mov    0x18(%rsp),%r11d
mov    0x1c(%rsp),%eax
movslq 0x20(%rsp),%rdx
imul   $0x10624dd3,%rdx,%rbx
mov    %rbx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rbx
add    %edx,%ebx
lea    0xe61(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
push   %rbx
push   %rax
push   %r11
push   %r10
call   1040 <snprintf@plt>
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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