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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ localtime_r(int /*<<< orphan*/ *, struct tm *);
int snprintf(char *, size_t, char *, int, int, int, int);
int time_GetUTCTime_s(char *buf, size_t buf_len) {
  if (buf == NULL || buf_len <= 0) {
    return 0;
  }
  int copy_len = 0;
  struct timeval tmval;
  struct tm tm_now = {0};
  time_t t_now = 0;
  gettimeofday(&tmval, NULL);
  t_now = tmval.tv_sec;
  localtime_r(&t_now, &tm_now);
  copy_len =
      snprintf(buf, buf_len - 1, "[%02d:%02d:%02d.%03ld]", tm_now.tm_hour,
               tm_now.tm_min, tm_now.tm_sec, tmval.tv_usec / 1000);
  return copy_len;
}


x86 assembly (clang, -O0):
<time_GetUTCTime_s>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1166 <time_GetUTCTime_s+0x26>
cmpq   $0x0,-0x18(%rbp)
ja     1172 <time_GetUTCTime_s+0x32>
movl   $0x0,-0x4(%rbp)
jmp    11ec <time_GetUTCTime_s+0xac>
movl   $0x0,-0x1c(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
movl   $0x0,-0x3c(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x3c(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1030 <localtime_r@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
sub    $0x1,%rsi
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%r8d
mov    -0x30(%rbp),%r9d
mov    -0x28(%rbp),%eax
mov    $0x3e8,%r10d
cltd
idiv   %r10d
lea    0xe25(%rip),%rdx
mov    %eax,(%rsp)
call   1040 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_GetUTCTime_s>:
push   %r14
push   %rbx
sub    $0x38,%rsp
xor    %eax,%eax
test   %rdi,%rdi
je     11b4 <time_GetUTCTime_s+0x84>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11b4 <time_GetUTCTime_s+0x84>
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    0x1c(%rsp),%eax
mov    %eax,0x14(%rsp)
lea    0x14(%rsp),%rdi
lea    0x20(%rsp),%rsi
call   1030 <localtime_r@plt>
add    $0xffffffffffffffff,%rbx
mov    0x20(%rsp),%ecx
mov    0x24(%rsp),%r8d
mov    0x28(%rsp),%r9d
movslq 0x18(%rsp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
mov    %eax,(%rsp)
lea    0xe57(%rip),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <snprintf@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32, i32, i32, i32 }
@.str = [23 x i8] c"[%02d:%02d:%02d.%03ld]\00"
define i32 @time_GetUTCTime_s(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca %struct.tm
%9 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i64, i64* %5
%14 = icmp ule i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12, %2
store i32 0, i32* %3
br label %36
16: ; preds = %12
store i32 0, i32* %6
%17 = bitcast %struct.tm* %8 to i8*
store i32 0, i32* %9
%18 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%20 = load i32, i32* %19
store i32 %20, i32* %9
%21 = call i32 @localtime_r(i32* %9, %struct.tm* %8)
%22 = load i8*, i8** %4
%23 = load i64, i64* %5
%24 = sub i64 %23, 1
%25 = getelementptr inbounds %struct.tm, %struct.tm* %8, i32 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.tm, %struct.tm* %8, i32 0, i32 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.tm, %struct.tm* %8, i32 0, i32 2
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%32 = load i32, i32* %31
%33 = sdiv i32 %32, 1000
%34 = call i32 @snprintf(i8* %22, i64 %24, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %26, i32 %28, i32 %30, i32 %33)
store i32 %34, i32* %6
%35 = load i32, i32* %6
store i32 %35, i32* %3
br label %36
36: ; preds = %16, %15
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.tm = type { i32, i32, i32, i32 }
@.str = [23 x i8] c"[%02d:%02d:%02d.%03ld]\00"
define i32 @time_GetUTCTime_s(i8* %0, i64 %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.tm
%5 = alloca i32
%6 = icmp eq i8* %0, null
%7 = icmp eq i64 %1, 0
%8 = or i1 %6, %7
br i1 %8, label %28, label %9
9: ; preds = %2
%10 = bitcast %struct.timeval* %3 to i8*
%11 = bitcast %struct.tm* %4 to i8*
%12 = bitcast i32* %5 to i8*
%13 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%15 = load i32, i32* %14
store i32 %15, i32* %5
%16 = call i32 @localtime_r(i32* %5, %struct.tm* %4)
%17 = add i64 %1, -1
%18 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 0
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 1
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 2
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = sdiv i32 %25, 1000
%27 = call i32 @snprintf(i8* %0, i64 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %21, i32 %23, i32 %26)
br label %28
28: ; preds = %2, %9
%29 = phi i32 [ %27, %9 ], [ 0, %2 ]
ret i32 %29
}