#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 const *, struct tm *);
char *usecs_time_str(char *buf, char const *format, struct timeval *tv) {
  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);
  if (!format || !*format)
    format = "%Y-%m-%d %H:%M:%S";
  size_t l = strftime(buf, 32, format, 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)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1170 <usecs_time_str+0x30>
lea    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <get_time_now@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x34(%rbp)
lea    -0x34(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119f <usecs_time_str+0x5f>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11aa <usecs_time_str+0x6a>
lea    0xe5a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x20,%esi
call   1060 <strftime@plt>
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
add    -0x40(%rbp),%rdi
mov    $0x20,%eax
sub    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
lea    0xe2d(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usecs_time_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
test   %rdx,%rdx
jne    1165 <usecs_time_str+0x25>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
call   1050 <get_time_now@plt>
mov    0x8(%r15),%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdi
call   1030 <localtime@plt>
test   %rbx,%rbx
je     1181 <usecs_time_str+0x41>
cmpb   $0x0,(%rbx)
jne    1188 <usecs_time_str+0x48>
lea    0xe78(%rip),%rbx
mov    $0x20,%ebp
mov    %r14,%rdi
mov    $0x20,%esi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1060 <strftime@plt>
lea    (%r14,%rax,1),%rdi
sub    %eax,%ebp
mov    (%r15),%rcx
lea    0xe62(%rip),%rdx
mov    %ebp,%esi
call   1040 <snprintf@plt>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
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(i8* %0, i8* %1, %struct.timeval* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.timeval*
%7 = alloca %struct.timeval
%8 = alloca %struct.tm*
%9 = alloca i32
%10 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store %struct.timeval* %2, %struct.timeval** %6
%11 = load %struct.timeval*, %struct.timeval** %6
%12 = icmp ne %struct.timeval* %11, null
br i1 %12, label %16, label %13
13: ; preds = %3
store %struct.timeval* %7, %struct.timeval** %6
%14 = load %struct.timeval*, %struct.timeval** %6
%15 = call i32 @get_time_now(%struct.timeval* %14)
br label %16
16: ; preds = %13, %3
%17 = load %struct.timeval*, %struct.timeval** %6
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 1
%19 = load i32, i32* %18
store i32 %19, i32* %9
%20 = call %struct.tm* @localtime(i32* %9)
store %struct.tm* %20, %struct.tm** %8
%21 = load i8*, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %16
%24 = load i8*, i8** %5
%25 = load i8, i8* %24
%26 = icmp ne i8 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %23, %16
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %5
br label %28
28: ; preds = %27, %23
%29 = load i8*, i8** %4
%30 = load i8*, i8** %5
%31 = load %struct.tm*, %struct.tm** %8
%32 = call i64 @strftime(i8* %29, i32 32, i8* %30, %struct.tm* %31)
store i64 %32, i64* %10
%33 = load i8*, i8** %4
%34 = load i64, i64* %10
%35 = getelementptr inbounds i8, i8* %33, i64 %34
%36 = load i64, i64* %10
%37 = sub i64 32, %36
%38 = trunc i64 %37 to i32
%39 = load %struct.timeval*, %struct.timeval** %6
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %39, i32 0, i32 0
%41 = load i64, i64* %40
%42 = call i32 @snprintf(i8* %35, i32 %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %41)
%43 = load i8*, i8** %4
ret i8* %43
}

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(i8* returned %0, i8* %1, %struct.timeval* %2) {
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = bitcast %struct.timeval* %4 to i8*
%7 = icmp eq %struct.timeval* %2, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = call i32 @get_time_now(%struct.timeval* %4)
br label %10
10: ; preds = %8, %3
%11 = phi %struct.timeval* [ %2, %3 ], [ %4, %8 ]
%12 = bitcast i32* %5 to i8*
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i64 0, i32 1
%14 = load i32, i32* %13
store i32 %14, i32* %5
%15 = call %struct.tm* @localtime(i32* %5)
%16 = icmp eq i8* %1, null
br i1 %16, label %20, label %17
17: ; preds = %10
%18 = load i8, i8* %1
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17, %10
br label %21
21: ; preds = %20, %17
%22 = phi i8* [ %1, %17 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %20 ]
%23 = call i64 @strftime(i8* %0, i32 32, i8* %22, %struct.tm* %15)
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = trunc i64 %23 to i32
%26 = sub i32 32, %25
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i64 0, i32 0
%28 = load i64, i64* %27
%29 = call i32 @snprintf(i8* %24, i32 %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %28)
ret i8* %0
}