#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 */
typedef int /*<<< orphan*/ tmp;
typedef int /*<<< orphan*/ time_t;
struct tm {
  int dummy;
};
struct timeval {
  int tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
scalar_t__ localtime_r(int /*<<< orphan*/ *, struct tm *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, ...);
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
void timeval_dump(char *buf, int buf_size, const struct timeval *tv_now) {
  char tmp[100];
  struct tm tm_now;
  if (localtime_r((time_t *)&tv_now->tv_sec, &tm_now)) {
    strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", &tm_now);
  } else {
    snprintf(tmp, sizeof(tmp), "%s", "<timestamp?>");
  }
  snprintf(buf, buf_size, "%s.%03ld", tmp, tv_now->tv_usec / 1000);
}


x86 assembly (clang, -O0):
<timeval_dump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x4,%rdi
lea    -0x88(%rbp),%rsi
call   1030 <localtime_r@plt>
cmp    $0x0,%rax
je     1185 <timeval_dump+0x55>
lea    -0x80(%rbp),%rdi
mov    $0x64,%esi
lea    0xe8c(%rip),%rdx
lea    -0x88(%rbp),%rcx
call   1050 <strftime@plt>
jmp    11a3 <timeval_dump+0x73>
lea    -0x80(%rbp),%rdi
mov    $0x64,%esi
lea    0xe7d(%rip),%rdx
lea    0xe79(%rip),%rcx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
lea    -0x80(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    $0x3e8,%r8d
cltd
idiv   %r8d
mov    %eax,%r8d
lea    0xe5a(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_dump>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0x4(%rdx),%rdi
lea    0x8(%rsp),%rsi
call   1030 <localtime_r@plt>
test   %rax,%rax
je     1174 <timeval_dump+0x44>
lea    0xea2(%rip),%rdx
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rcx
mov    $0x64,%esi
call   1050 <strftime@plt>
jmp    1193 <timeval_dump+0x63>
lea    0xe97(%rip),%rdx
lea    0xe93(%rip),%rcx
lea    0x10(%rsp),%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
movslq 0x0(%rbp),%rax
imul   $0x10624dd3,%rax,%r8
mov    %r8,%rax
shr    $0x3f,%rax
sar    $0x26,%r8
add    %eax,%r8d
lea    0xe6f(%rip),%rdx
lea    0x10(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32 }
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [13 x i8] c"<timestamp?>\00"
@.str.3 = [9 x i8] c"%s.%03ld\00"
define void @timeval_dump(i8* %0, i32 %1, %struct.timeval* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.timeval*
%7 = alloca [100 x i8]
%8 = alloca %struct.tm
store i8* %0, i8** %4
store i32 %1, i32* %5
store %struct.timeval* %2, %struct.timeval** %6
%9 = load %struct.timeval*, %struct.timeval** %6
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%11 = call i64 @localtime_r(i32* %10, %struct.tm* %8)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%15 = call i32 @strftime(i8* %14, i32 100, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %8)
br label %19
16: ; preds = %3
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%18 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %17, i32 100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %16, %13
%20 = load i8*, i8** %4
%21 = load i32, i32* %5
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%23 = load %struct.timeval*, %struct.timeval** %6
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = sdiv i32 %25, 1000
%27 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %20, i32 %21, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %22, i32 %26)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32 }
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [13 x i8] c"<timestamp?>\00"
@.str.3 = [9 x i8] c"%s.%03ld\00"
define void @timeval_dump(i8* %0, i32 %1, %struct.timeval* %2) {
%4 = alloca [100 x i8]
%5 = alloca %struct.tm
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%7 = bitcast %struct.tm* %5 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%9 = call i64 @localtime_r(i32* %8, %struct.tm* %5)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = call i32 @strftime(i8* %6, i32 100, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %5)
br label %15
13: ; preds = %3
%14 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %11
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = sdiv i32 %17, 1000
%19 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %6, i32 %18)
ret void
}