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

/* Variables and functions */
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ localtime_r(int /*<<< orphan*/ *, struct tm *);
int snprintf(char *, size_t, char *, int);
size_t strftime(char *, size_t, char *, struct tm *);
int logger_backtick_time(struct logger_streamData *logData, char *outStr,
                         size_t sz) {
  (void)logData;
  struct timeval tv;
  int res = gettimeofday(&tv, NULL);
  if (res) {
    return -1;
  }
  struct tm ts;
  localtime_r(&tv.tv_sec, &ts);
  size_t count = strftime(outStr, sz, "%Y-%m-%d %H:%M:%S", &ts);
  if (count == 0 || count > sz) {
    return -2;
  }
  res = snprintf(outStr + count, sz - count, ".%03d", tv.tv_usec / 1000);
  if (res <= 0) {
    return -3;
  }
  return 0;
}


x86 assembly (clang, -O0):
<logger_backtick_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     117a <logger_backtick_time+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <logger_backtick_time+0xd5>
lea    -0x28(%rbp),%rdi
add    $0x4,%rdi
lea    -0x30(%rbp),%rsi
call   1030 <localtime_r@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    0xe66(%rip),%rdx
lea    -0x30(%rbp),%rcx
call   1060 <strftime@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11c0 <logger_backtick_time+0x80>
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    11cc <logger_backtick_time+0x8c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1215 <logger_backtick_time+0xd5>
mov    -0x18(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
sub    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xe22(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jg     120e <logger_backtick_time+0xce>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1215 <logger_backtick_time+0xd5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logger_backtick_time>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    11d9 <logger_backtick_time+0x99>
lea    0x4(%rsp),%rdi
lea    0x8(%rsp),%r15
mov    %r15,%rsi
call   1030 <localtime_r@plt>
lea    0xe83(%rip),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rcx
call   1060 <strftime@plt>
mov    %rax,%rcx
lea    -0x1(%rax),%rdx
mov    $0xfffffffe,%eax
cmp    %rbx,%rdx
jae    11d9 <logger_backtick_time+0x99>
add    %rcx,%r14
sub    %rcx,%rbx
movslq (%rsp),%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rax
shr    $0x3f,%rax
sar    $0x26,%rcx
add    %eax,%ecx
lea    0xe51(%rip),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <snprintf@plt>
xor    %ecx,%ecx
test   %eax,%eax
setg   %cl
lea    (%rcx,%rcx,2),%eax
add    $0xfffffffd,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.logger_streamData = type { i32 }
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32 }
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [6 x i8] c".%03d\00"
define i32 @logger_backtick_time(%struct.logger_streamData* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.logger_streamData*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca %struct.tm
%11 = alloca i64
store %struct.logger_streamData* %0, %struct.logger_streamData** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = load %struct.logger_streamData*, %struct.logger_streamData** %5
%13 = call i32 @gettimeofday(%struct.timeval* %8, i32* null)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %45
17: ; preds = %3
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%19 = call i32 @localtime_r(i32* %18, %struct.tm* %10)
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = call i64 @strftime(i8* %20, i64 %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %10)
store i64 %22, i64* %11
%23 = load i64, i64* %11
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %17
%26 = load i64, i64* %11
%27 = load i64, i64* %7
%28 = icmp ugt i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25, %17
store i32 -2, i32* %4
br label %45
30: ; preds = %25
%31 = load i8*, i8** %6
%32 = load i64, i64* %11
%33 = getelementptr inbounds i8, i8* %31, i64 %32
%34 = load i64, i64* %7
%35 = load i64, i64* %11
%36 = sub i64 %34, %35
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%38 = load i32, i32* %37
%39 = sdiv i32 %38, 1000
%40 = call i32 @snprintf(i8* %33, i64 %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %39)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = icmp sle i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %30
store i32 -3, i32* %4
br label %45
44: ; preds = %30
store i32 0, i32* %4
br label %45
45: ; preds = %44, %43, %29, %16
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.logger_streamData = type { i32 }
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32 }
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [6 x i8] c".%03d\00"
define i32 @logger_backtick_time(%struct.logger_streamData* nocapture readnone %0, i8* %1, i64 %2) {
%4 = alloca %struct.timeval
%5 = alloca %struct.tm
%6 = bitcast %struct.timeval* %4 to i8*
%7 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %27
9: ; preds = %3
%10 = bitcast %struct.tm* %5 to i8*
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%12 = call i32 @localtime_r(i32* %11, %struct.tm* %5)
%13 = call i64 @strftime(i8* %1, i64 %2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %5)
%14 = add i64 %13, -1
%15 = icmp ult i64 %14, %2
br i1 %15, label %16, label %25
16: ; preds = %9
%17 = getelementptr inbounds i8, i8* %1, i64 %13
%18 = sub i64 %2, %13
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%20 = load i32, i32* %19
%21 = sdiv i32 %20, 1000
%22 = call i32 @snprintf(i8* %17, i64 %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = icmp sgt i32 %22, 0
%24 = select i1 %23, i32 0, i32 -3
br label %25
25: ; preds = %16, %9
%26 = phi i32 [ -2, %9 ], [ %24, %16 ]
br label %27
27: ; preds = %3, %25
%28 = phi i32 [ %26, %25 ], [ -1, %3 ]
ret i32 %28
}