#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 tm_year;
  int tm_mon;
  int tm_mday;
  int tm_hour;
  int tm_min;
  int tm_sec;
};
struct timeval {
  int tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
double get_ts();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int, ...);
char *strdup(char *);
size_t strlen(char *);
char *get_ts_str(int verbose) {
  char buffer[4096] = {0};
  struct tm *tvm = NULL;
  struct timeval tv;
  (void)gettimeofday(&tv, NULL);
  tvm = localtime(&tv.tv_sec);
  if (verbose == 1)
    sprintf(buffer, "%04d/%02d/%02d ", tvm->tm_year + 1900, tvm->tm_mon + 1,
            tvm->tm_mday);
  else if (verbose >= 2)
    sprintf(buffer, "%.6f", get_ts());
  if (verbose <= 1)
    sprintf(&buffer[strlen(buffer)], "%02d:%02d:%02d.%03d", tvm->tm_hour,
            tvm->tm_min, tvm->tm_sec, (int)(tv.tv_usec / 1000));
  return strdup(buffer);
}


x86 assembly (clang, -O0):
<get_ts_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
movq   $0x0,-0x1018(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
lea    -0x1020(%rbp),%rdi
add    $0x4,%rdi
call   1030 <localtime@plt>
mov    %rax,-0x1018(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    120e <get_ts_str+0x9e>
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%rax
mov    (%rax),%edx
add    $0x76c,%edx
mov    -0x1018(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0x1,%ecx
mov    -0x1018(%rbp),%rax
mov    0x8(%rax),%r8d
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    124b <get_ts_str+0xdb>
cmpl   $0x2,-0x4(%rbp)
jl     1246 <get_ts_str+0xd6>
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1028(%rbp)
mov    $0x0,%al
call   1040 <get_ts@plt>
mov    -0x1028(%rbp),%rdi
cvttsd2si %xmm0,%edx
lea    0xdd1(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    124b <get_ts_str+0xdb>
cmpl   $0x1,-0x4(%rbp)
jg     12b5 <get_ts_str+0x145>
lea    -0x1010(%rbp),%rdi
call   1050 <strlen@plt>
lea    -0x1010(%rbp),%rdi
add    %rax,%rdi
mov    -0x1018(%rbp),%rax
mov    0xc(%rax),%eax
mov    %eax,-0x102c(%rbp)
mov    -0x1018(%rbp),%rax
mov    0x10(%rax),%ecx
mov    -0x1018(%rbp),%rax
mov    0x14(%rax),%r8d
mov    -0x1020(%rbp),%eax
mov    $0x3e8,%esi
cltd
idiv   %esi
mov    -0x102c(%rbp),%edx
mov    %eax,%r9d
lea    0xd67(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0x1010(%rbp),%rdi
call   1090 <strdup@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ts_str>:
push   %rbp
push   %rbx
sub    $0x1018,%rsp
mov    %edi,%ebp
lea    0x10(%rsp),%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   1070 <memset@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
mov    %rax,%rbx
cmp    $0x1,%ebp
jne    11d0 <get_ts_str+0x60>
mov    $0x76c,%edx
add    (%rbx),%edx
mov    0x4(%rbx),%ecx
mov    0x8(%rbx),%r8d
add    $0x1,%ecx
lea    0xe3e(%rip),%rsi
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   1080 <sprintf@plt>
jmp    11f2 <get_ts_str+0x82>
jle    11f2 <get_ts_str+0x82>
xor    %eax,%eax
call   1040 <get_ts@plt>
cvttsd2si %xmm0,%edx
lea    0xe2c(%rip),%rsi
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   1080 <sprintf@plt>
jmp    1236 <get_ts_str+0xc6>
lea    0x10(%rsp),%rdi
call   1050 <strlen@plt>
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
mov    0xc(%rbx),%edx
mov    0x10(%rbx),%ecx
mov    0x14(%rbx),%r8d
movslq 0x8(%rsp),%rax
imul   $0x10624dd3,%rax,%r9
mov    %r9,%rax
shr    $0x3f,%rax
sar    $0x26,%r9
add    %eax,%r9d
lea    0xde6(%rip),%rsi
xor    %eax,%eax
call   1080 <sprintf@plt>
lea    0x10(%rsp),%rdi
call   1090 <strdup@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
%struct.timeval = type { i32, i32 }
@.str = [16 x i8] c"%04d/%02d/%02d \00"
@.str.1 = [5 x i8] c"%.6f\00"
@.str.2 = [20 x i8] c"%02d:%02d:%02d.%03d\00"
define i8* @get_ts_str(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca %struct.tm*
%5 = alloca %struct.timeval
store i32 %0, i32* %2
%6 = bitcast [4096 x i8]* %3 to i8*
store %struct.tm* null, %struct.tm** %4
%7 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%9 = call %struct.tm* @localtime(i32* %8)
store %struct.tm* %9, %struct.tm** %4
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %26
12: ; preds = %1
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%14 = load %struct.tm*, %struct.tm** %4
%15 = getelementptr inbounds %struct.tm, %struct.tm* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = add nsw i32 %16, 1900
%18 = load %struct.tm*, %struct.tm** %4
%19 = getelementptr inbounds %struct.tm, %struct.tm* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = add nsw i32 %20, 1
%22 = load %struct.tm*, %struct.tm** %4
%23 = getelementptr inbounds %struct.tm, %struct.tm* %22, i32 0, i32 2
%24 = load i32, i32* %23
%25 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %21, i32 %24)
br label %35
26: ; preds = %1
%27 = load i32, i32* %2
%28 = icmp sge i32 %27, 2
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%31 = call double (...) @get_ts()
%32 = fptosi double %31 to i32
%33 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %29, %26
br label %35
35: ; preds = %34, %12
%36 = load i32, i32* %2
%37 = icmp sle i32 %36, 1
br i1 %37, label %38, label %55
38: ; preds = %35
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%40 = call i64 @strlen(i8* %39)
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %40
%42 = load %struct.tm*, %struct.tm** %4
%43 = getelementptr inbounds %struct.tm, %struct.tm* %42, i32 0, i32 3
%44 = load i32, i32* %43
%45 = load %struct.tm*, %struct.tm** %4
%46 = getelementptr inbounds %struct.tm, %struct.tm* %45, i32 0, i32 4
%47 = load i32, i32* %46
%48 = load %struct.tm*, %struct.tm** %4
%49 = getelementptr inbounds %struct.tm, %struct.tm* %48, i32 0, i32 5
%50 = load i32, i32* %49
%51 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%52 = load i32, i32* %51
%53 = sdiv i32 %52, 1000
%54 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %41, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %44, i32 %47, i32 %50, i32 %53)
br label %55
55: ; preds = %38, %35
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%57 = call i8* @strdup(i8* %56)
ret i8* %57
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [16 x i8] c"%04d/%02d/%02d \00"
@.str.1 = [5 x i8] c"%.6f\00"
@.str.2 = [20 x i8] c"%02d:%02d:%02d.%03d\00"
define noalias i8* @get_ts_str(i32 %0) {
%2 = alloca [4096 x i8]
%3 = alloca %struct.timeval
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.timeval* %3 to i8*
%6 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%8 = call %struct.tm* @localtime(i32* %7)
%9 = icmp eq i32 %0, 1
br i1 %9, label %10, label %20
10: ; preds = %1
%11 = getelementptr inbounds %struct.tm, %struct.tm* %8, i64 0, i32 0
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 1900
%14 = getelementptr inbounds %struct.tm, %struct.tm* %8, i64 0, i32 1
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1
%17 = getelementptr inbounds %struct.tm, %struct.tm* %8, i64 0, i32 2
%18 = load i32, i32* %17
%19 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %16, i32 %18)
br label %26
20: ; preds = %1
%21 = icmp sgt i32 %0, 1
br i1 %21, label %22, label %26
22: ; preds = %20
%23 = call double (...) @get_ts()
%24 = fptosi double %23 to i32
%25 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %39
26: ; preds = %20, %10
%27 = call i64 @strlen(i8* %4)
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %27
%29 = getelementptr inbounds %struct.tm, %struct.tm* %8, i64 0, i32 3
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.tm, %struct.tm* %8, i64 0, i32 4
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.tm, %struct.tm* %8, i64 0, i32 5
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%36 = load i32, i32* %35
%37 = sdiv i32 %36, 1000
%38 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %28, i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %30, i32 %32, i32 %34, i32 %37)
br label %39
39: ; preds = %22, %26
%40 = call i8* @strdup(i8* %4)
ret i8* %40
}