#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*/ va_list;
typedef int /*<<< orphan*/ time_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ localtime(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strftime(char *, int, char *, int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ vprintf(char const *, int /*<<< orphan*/);
void log_format(const char *tag, const char *message, va_list args) {
  time_t curr_time;
  char *date = malloc(20 * sizeof(char));
  time(&curr_time);
  strftime(date, 20, "%Y-%m-%d %H:%M:%S", localtime(&curr_time));
  printf("%s [%s] ", date, tag);
  vprintf(message, args);
  if (message[strlen(message) - 1] != '\n')
    printf("\n");
  free(date);
}


x86 assembly (clang, -O0):
<log_format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x14,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <time@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <localtime@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%ecx
mov    $0x14,%esi
lea    0xe33(%rip),%rdx
call   10a0 <strftime@plt>
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <vprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     122c <log_format+0xac>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_format>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r12
mov    $0x14,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <time@plt>
mov    %rbp,%rdi
call   1050 <localtime@plt>
lea    0xe32(%rip),%rdx
mov    %r14,%rdi
mov    $0x14,%esi
mov    %eax,%ecx
call   10b0 <strftime@plt>
lea    0xe2e(%rip),%rdi
mov    %r14,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
call   10a0 <vprintf@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
je     1217 <log_format+0x87>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [9 x i8] c"%s [%s] \00"
@.str.2 = [2 x i8] c"\0A\00"
define void @log_format(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = call i8* @malloc(i32 20)
store i8* %9, i8** %8
%10 = call i32 @time(i32* %7)
%11 = load i8*, i8** %8
%12 = call i32 @localtime(i32* %7)
%13 = call i32 @strftime(i8* %11, i32 20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i8*, i8** %8
%15 = load i8*, i8** %4
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %15)
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = call i32 @vprintf(i8* %17, i32 %18)
%20 = load i8*, i8** %5
%21 = load i8*, i8** %5
%22 = call i32 @strlen(i8* %21)
%23 = sub nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %20, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 10
br i1 %28, label %29, label %31
29: ; preds = %3
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %3
%32 = load i8*, i8** %8
%33 = call i32 @free(i8* %32)
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [9 x i8] c"%s [%s] \00"
define void @log_format(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call dereferenceable_or_null(20) i8* @malloc(i32 20)
%7 = call i32 @time(i32* %4)
%8 = call i32 @localtime(i32* %4)
%9 = call i32 @strftime(i8* %6, i32 20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %0)
%11 = call i32 @vprintf(i8* %1, i32 %2)
%12 = call i32 @strlen(i8* %1)
%13 = add nsw i32 %12, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
br i1 %17, label %20, label %18
18: ; preds = %3
%19 = call i32 @putchar(i32 10)
br label %20
20: ; preds = %18, %3
%21 = call i32 @free(i8* %6)
ret void
}