#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 {
  int tv_usec;
  int /*<<< orphan*/ tv_sec;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int snprintf(char *, size_t, char *, int);
scalar_t__ strftime(char *, size_t, char *, struct tm *);
const char *timestamp() {
  struct timeval tv;
  static char buf[1024];
  size_t sz = sizeof(buf) - 1;
  ssize_t written = -1;
  if (gettimeofday(&tv, NULL) != 0) {
    perror("gettimeofday");
    exit(EXIT_FAILURE);
  }
  struct tm *time = localtime(&tv.tv_sec);
  if (time) {
    written = (ssize_t)strftime(buf, sz, "%Y-%m-%d %H:%M:%S", time);
    if ((written > 0) && ((size_t)written < sz)) {
      int w = snprintf(buf + written, sz - (size_t)written, ".%03d",
                       (int)(tv.tv_usec / 1000));
      written = (w > 0) ? written + w : -1;
    }
    buf[written] = 0;
  }
  return buf;
}


x86 assembly (clang, -O0):
<timestamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x3ff,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
je     11a8 <timestamp+0x48>
lea    0xe6b(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    -0x8(%rbp),%rdi
add    $0x4,%rdi
call   1030 <localtime@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1269 <timestamp+0x109>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    0x2e8d(%rip),%rdi
lea    0xe33(%rip),%rdx
call   1060 <strftime@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    125a <timestamp+0xfa>
movslq -0x14(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    125a <timestamp+0xfa>
movslq -0x14(%rbp),%rax
lea    0x2e5b(%rip),%rdi
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
sub    %rax,%rsi
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xdf8(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1247 <timestamp+0xe7>
mov    -0x14(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    1254 <timestamp+0xf4>
mov    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    1254 <timestamp+0xf4>
mov    -0x28(%rbp),%eax
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rcx
lea    0x2dfb(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2df0(%rip),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timestamp>:
push   %r14
push   %rbx
push   %rax
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
jne    120a <timestamp+0xaa>
lea    0x4(%rsp),%rdi
call   1030 <localtime@plt>
test   %rax,%rax
je     11fb <timestamp+0x9b>
lea    0x2ed3(%rip),%r14
lea    0xe79(%rip),%rdx
mov    $0x3ff,%esi
mov    %r14,%rdi
mov    %rax,%rcx
call   1060 <strftime@plt>
mov    %rax,%rbx
test   %ebx,%ebx
jle    11f3 <timestamp+0x93>
cmp    $0x3fe,%ebx
ja     11f3 <timestamp+0x93>
movslq %ebx,%rax
lea    (%r14,%rax,1),%rdi
mov    $0x3ff,%esi
sub    %rax,%rsi
movslq (%rsp),%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rax
shr    $0x3f,%rax
sar    $0x26,%rcx
add    %eax,%ecx
lea    0xe3e(%rip),%rdx
call   1040 <snprintf@plt>
lea    (%rax,%rbx,1),%ecx
test   %eax,%eax
mov    $0xffffffff,%ebx
cmovg  %ecx,%ebx
movslq %ebx,%rax
movb   $0x0,(%rax,%r14,1)
lea    0x2e5e(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdef(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32 }
@timestamp.buf = internal global [1024 x i8] zeroinitializer
@.str = [13 x i8] c"gettimeofday\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.2 = [6 x i8] c".%03d\00"
define i8* @timestamp() {
%1 = alloca %struct.timeval
%2 = alloca i64
%3 = alloca i32
%4 = alloca %struct.tm*
%5 = alloca i32
store i64 1023, i64* %2
store i32 -1, i32* %3
%6 = call i64 @gettimeofday(%struct.timeval* %1, i32* null)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 1
%14 = call %struct.tm* @localtime(i32* %13)
store %struct.tm* %14, %struct.tm** %4
%15 = load %struct.tm*, %struct.tm** %4
%16 = icmp ne %struct.tm* %15, null
br i1 %16, label %17, label %54
17: ; preds = %12
%18 = load i64, i64* %2
%19 = load %struct.tm*, %struct.tm** %4
%20 = call i64 @strftime(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @timestamp.buf, i64 0, i64 0), i64 %18, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %19)
%21 = trunc i64 %20 to i32
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %50
24: ; preds = %17
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = load i64, i64* %2
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %50
29: ; preds = %24
%30 = load i32, i32* %3
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @timestamp.buf, i64 0, i64 0), i64 %31
%33 = load i64, i64* %2
%34 = load i32, i32* %3
%35 = sext i32 %34 to i64
%36 = sub i64 %33, %35
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 0
%38 = load i32, i32* %37
%39 = sdiv i32 %38, 1000
%40 = call i32 @snprintf(i8* %32, i64 %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %39)
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %29
%44 = load i32, i32* %3
%45 = load i32, i32* %5
%46 = add nsw i32 %44, %45
br label %48
47: ; preds = %29
br label %48
48: ; preds = %47, %43
%49 = phi i32 [ %46, %43 ], [ -1, %47 ]
store i32 %49, i32* %3
br label %50
50: ; preds = %48, %24, %17
%51 = load i32, i32* %3
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* @timestamp.buf, i64 0, i64 %52
store i8 0, i8* %53
br label %54
54: ; preds = %50, %12
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @timestamp.buf, i64 0, i64 0)
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32 }
@timestamp.buf = internal global [1024 x i8] zeroinitializer
@.str = [13 x i8] c"gettimeofday\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.2 = [6 x i8] c".%03d\00"
define i8* @timestamp() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = call i64 @gettimeofday(%struct.timeval* %1, i32* null)
%4 = icmp eq i64 %3, 0
br i1 %4, label %9, label %5
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%11 = call %struct.tm* @localtime(i32* %10)
%12 = icmp eq %struct.tm* %11, null
br i1 %12, label %35, label %13
13: ; preds = %9
%14 = call i64 @strftime(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @timestamp.buf, i64 0, i64 0), i64 1023, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %11)
%15 = trunc i64 %14 to i32
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %31
17: ; preds = %13
%18 = shl i64 %14, 32
%19 = ashr exact i64 %18, 32
%20 = icmp ult i64 %18, 4393751543808
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* @timestamp.buf, i64 0, i64 %19
%23 = sub nsw i64 1023, %19
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%25 = load i32, i32* %24
%26 = sdiv i32 %25, 1000
%27 = call i32 @snprintf(i8* %22, i64 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = icmp sgt i32 %27, 0
%29 = add nsw i32 %27, %15
%30 = select i1 %28, i32 %29, i32 -1
br label %31
31: ; preds = %21, %17, %13
%32 = phi i32 [ %30, %21 ], [ %15, %17 ], [ %15, %13 ]
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* @timestamp.buf, i64 0, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %31, %9
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @timestamp.buf, i64 0, i64 0)
}