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

/* Variables and functions */
int /*<<< orphan*/ get_time_now(struct timeval *);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, long);
size_t strftime(char *, int, char *, struct tm *);
char *usecs_time_str(struct timeval *tv, char *buf) {
  struct timeval now;
  struct tm *tm_info;
  if (!tv) {
    tv = &now;
    get_time_now(tv);
  }
  time_t t_secs = tv->tv_sec;
  tm_info = localtime(&t_secs);
  size_t l = strftime(buf, 32, "%Y-%m-%d %H:%M:%S", tm_info);
  snprintf(buf + l, 32 - l, ".%06ld", (long)tv->tv_usec);
  return buf;
}


x86 assembly (clang, -O0):
<usecs_time_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    116c <usecs_time_str+0x2c>
lea    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <get_time_now@plt>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x20,%esi
lea    0xe69(%rip),%rdx
call   1060 <strftime@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    $0x20,%eax
sub    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
lea    0xe51(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usecs_time_str>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
jne    1160 <usecs_time_str+0x20>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <get_time_now@plt>
mov    0x8(%rbx),%eax
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
lea    0xe88(%rip),%rdx
mov    $0x20,%ebp
mov    %r14,%rdi
mov    $0x20,%esi
mov    %rax,%rcx
call   1060 <strftime@plt>
lea    (%r14,%rax,1),%rdi
sub    %eax,%ebp
mov    (%rbx),%rcx
lea    0xe75(%rip),%rdx
mov    %ebp,%esi
call   1040 <snprintf@plt>
mov    %r14,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32 }
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [7 x i8] c".%06ld\00"
define i8* @usecs_time_str(%struct.timeval* %0, i8* %1) {
%3 = alloca %struct.timeval*
%4 = alloca i8*
%5 = alloca %struct.timeval
%6 = alloca %struct.tm*
%7 = alloca i32
%8 = alloca i64
store %struct.timeval* %0, %struct.timeval** %3
store i8* %1, i8** %4
%9 = load %struct.timeval*, %struct.timeval** %3
%10 = icmp ne %struct.timeval* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
store %struct.timeval* %5, %struct.timeval** %3
%12 = load %struct.timeval*, %struct.timeval** %3
%13 = call i32 @get_time_now(%struct.timeval* %12)
br label %14
14: ; preds = %11, %2
%15 = load %struct.timeval*, %struct.timeval** %3
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
%17 = load i32, i32* %16
store i32 %17, i32* %7
%18 = call %struct.tm* @localtime(i32* %7)
store %struct.tm* %18, %struct.tm** %6
%19 = load i8*, i8** %4
%20 = load %struct.tm*, %struct.tm** %6
%21 = call i64 @strftime(i8* %19, i32 32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %20)
store i64 %21, i64* %8
%22 = load i8*, i8** %4
%23 = load i64, i64* %8
%24 = getelementptr inbounds i8, i8* %22, i64 %23
%25 = load i64, i64* %8
%26 = sub i64 32, %25
%27 = trunc i64 %26 to i32
%28 = load %struct.timeval*, %struct.timeval** %3
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %28, i32 0, i32 0
%30 = load i64, i64* %29
%31 = call i32 @snprintf(i8* %24, i32 %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %30)
%32 = load i8*, i8** %4
ret i8* %32
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
%struct.tm = type { i32 }
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [7 x i8] c".%06ld\00"
define i8* @usecs_time_str(%struct.timeval* %0, i8* returned %1) {
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = bitcast %struct.timeval* %3 to i8*
%6 = icmp eq %struct.timeval* %0, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = call i32 @get_time_now(%struct.timeval* %3)
br label %9
9: ; preds = %7, %2
%10 = phi %struct.timeval* [ %0, %2 ], [ %3, %7 ]
%11 = bitcast i32* %4 to i8*
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i64 0, i32 1
%13 = load i32, i32* %12
store i32 %13, i32* %4
%14 = call %struct.tm* @localtime(i32* %4)
%15 = call i64 @strftime(i8* %1, i32 32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %14)
%16 = getelementptr inbounds i8, i8* %1, i64 %15
%17 = trunc i64 %15 to i32
%18 = sub i32 32, %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i64 0, i32 0
%20 = load i64, i64* %19
%21 = call i32 @snprintf(i8* %16, i32 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %20)
ret i8* %1
}