#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_hour;
  int tm_min;
  int tm_sec;
};
struct timeval {
  int tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *localtime_r(int /*<<< orphan*/ *, struct tm *);
int snprintf(char *, size_t, char *, int, int, int, int);
int _snprintf_timestamp(char *buf, size_t len) {
  struct timeval tv;
  struct tm tm;
  if (gettimeofday(&tv, NULL) != 0) {
    return -1;
  }
  if (localtime_r(&tv.tv_sec, &tm) == NULL) {
    return -1;
  }
  return snprintf(buf, len, "<%02d:%02d:%02d.%03d>", tm.tm_hour, tm.tm_min,
                  tm.tm_sec, (int)(tv.tv_usec / 1000));
}


x86 assembly (clang, -O0):
<_snprintf_timestamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
je     1163 <_snprintf_timestamp+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <_snprintf_timestamp+0x8c>
lea    -0x20(%rbp),%rdi
add    $0x4,%rdi
lea    -0x30(%rbp),%rsi
call   1030 <localtime_r@plt>
cmp    $0x0,%rax
jne    118a <_snprintf_timestamp+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <_snprintf_timestamp+0x8c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
mov    -0x28(%rbp),%r9d
mov    -0x20(%rbp),%eax
mov    $0x3e8,%r10d
cltd
idiv   %r10d
lea    0xe4f(%rip),%rdx
mov    %eax,(%rsp)
call   1040 <snprintf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_snprintf_timestamp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11a6 <_snprintf_timestamp+0x76>
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <localtime_r@plt>
test   %rax,%rax
je     11a6 <_snprintf_timestamp+0x76>
mov    0x10(%rsp),%ecx
mov    0x14(%rsp),%r8d
mov    0x18(%rsp),%r9d
movslq 0x8(%rsp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
mov    %eax,(%rsp)
lea    0xe67(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <snprintf@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32, i32, i32 }
@.str = [22 x i8] c"<%02d:%02d:%02d.%03d>\00"
define i32 @_snprintf_timestamp(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.timeval
%7 = alloca %struct.tm
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = call i64 @gettimeofday(%struct.timeval* %6, i32* null)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %29
11: ; preds = %2
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%13 = call i32* @localtime_r(i32* %12, %struct.tm* %7)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 -1, i32* %3
br label %29
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = load i64, i64* %5
%19 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 0
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 1
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 2
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%26 = load i32, i32* %25
%27 = sdiv i32 %26, 1000
%28 = call i32 @snprintf(i8* %17, i64 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %22, i32 %24, i32 %27)
store i32 %28, i32* %3
br label %29
29: ; preds = %16, %15, %10
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32, i32, i32 }
@.str = [22 x i8] c"<%02d:%02d:%02d.%03d>\00"
define i32 @_snprintf_timestamp(i8* %0, i64 %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.tm
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast %struct.tm* %4 to i8*
%7 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %2
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%11 = call i32* @localtime_r(i32* %10, %struct.tm* %4)
%12 = icmp eq i32* %11, null
br i1 %12, label %24, label %13
13: ; preds = %9
%14 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 2
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = sdiv i32 %21, 1000
%23 = call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %17, i32 %19, i32 %22)
br label %24
24: ; preds = %9, %2, %13
%25 = phi i32 [ %23, %13 ], [ -1, %2 ], [ -1, %9 ]
ret i32 %25
}