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

/* Variables and functions */
int errno;
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ localtime_r(int /*<<< orphan*/ *, struct tm *);
int lrint(double);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int strftime(char *, int, char *, struct tm *);
int kvs_log_time_add(char *time, int time_size) {
  struct timeval tv;
  struct tm result;
  int pos, millisec;
  if (gettimeofday(&tv, NULL) != 0) {
    return errno;
  }
  millisec = lrint(tv.tv_usec / 1000.0);
  if (millisec >= 1000) {
    millisec -= 1000;
    tv.tv_sec++;
  }
  localtime_r(&tv.tv_sec, &result);
  pos = strftime(time, time_size, "%Y-%m-%d %H:%M:%S", &result);
  snprintf(time + pos, time_size - pos, ".%03d", millisec);
  return 0;
}


x86 assembly (clang, -O0):
<kvs_log_time_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
je     1187 <kvs_log_time_add+0x37>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1213 <kvs_log_time_add+0xc3>
movsd  -0x28(%rbp),%xmm0
movsd  0xe6c(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1060 <lrint@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x3e8,-0x38(%rbp)
jl     11c1 <kvs_log_time_add+0x71>
mov    -0x38(%rbp),%eax
sub    $0x3e8,%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
lea    -0x30(%rbp),%rsi
call   1030 <localtime_r@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    0xe28(%rip),%rdx
lea    -0x30(%rbp),%rcx
call   1070 <strftime@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x34(%rbp),%esi
mov    -0x38(%rbp),%ecx
lea    0xe13(%rip),%rdx
call   1040 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvs_log_time_add>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
je     117b <kvs_log_time_add+0x2b>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
jmp    11e5 <kvs_log_time_add+0x95>
movsd  0x8(%rsp),%xmm0
divsd  0xe77(%rip),%xmm0
call   1060 <lrint@plt>
mov    %eax,%r14d
cmp    $0x3e8,%eax
jl     11a4 <kvs_log_time_add+0x54>
add    $0xfffffc18,%r14d
addl   $0x1,0x10(%rsp)
lea    0x10(%rsp),%rdi
mov    %rsp,%r15
mov    %r15,%rsi
call   1030 <localtime_r@plt>
lea    0xe4d(%rip),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15,%rcx
call   1070 <strftime@plt>
cltq
add    %rax,%rbx
sub    %eax,%ebp
lea    0xe44(%rip),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14d,%ecx
call   1040 <snprintf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { double, i32 }
%struct.tm = type { i32 }
@errno = global i32 0
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [6 x i8] c".%03d\00"
define i32 @kvs_log_time_add(i8* %0, i32 %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 %11, label %9
9: ; preds = %2
%10 = load i32, i32* @errno
br label %31
11: ; preds = %2
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%13 = load double, double* %12
%14 = fdiv double %13, 1.000000e+03
%15 = tail call i32 @lrint(double %14)
%16 = icmp sgt i32 %15, 999
br i1 %16, label %17, label %22
17: ; preds = %11
%18 = add nsw i32 %15, -1000
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%20 = load i32, i32* %19
%21 = add nsw i32 %20, 1
store i32 %21, i32* %19
br label %22
22: ; preds = %17, %11
%23 = phi i32 [ %18, %17 ], [ %15, %11 ]
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%25 = call i32 @localtime_r(i32* %24, %struct.tm* %4)
%26 = call i32 @strftime(i8* %0, i32 %1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %4)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %0, i64 %27
%29 = sub nsw i32 %1, %26
%30 = call i32 @snprintf(i8* %28, i32 %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %31
31: ; preds = %22, %9
%32 = phi i32 [ %10, %9 ], [ 0, %22 ]
ret i32 %32
}