#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;
struct tm {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *calloc(int, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char const *, size_t);
int /*<<< orphan*/ vfprintf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int _log_vfprintf(FILE *out, time_t *epoch, const char *level, const char *fmt,
                  va_list args) {
  struct tm *time;
  size_t size;
  char *buffer;
  if (out != NULL) {
    buffer = calloc(1024 + 1, sizeof(char));
    if (buffer == NULL)
      return -1;
    time = localtime(epoch);
    strftime(buffer, 1024, "%Y-%m-%d %H:%M:%S ", time);
    size = 1024 - strlen(buffer);
    if (level != NULL) {
      strncat(buffer, level, size);
      size = 1024 - strlen(buffer);
    }
    strncat(buffer, ": ", size);
    size = 1024 - strlen(buffer);
    strncat(buffer, fmt, size);
    size = 1024 - strlen(buffer);
    strncat(buffer, "\n", size);
    vfprintf(out, buffer, args);
    fflush(out);
    free(buffer);
  }
  return 0;
}


x86 assembly (clang, -O0):
<_log_vfprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12ce <_log_vfprintf+0x14e>
mov    $0x401,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11d1 <_log_vfprintf+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d5 <_log_vfprintf+0x155>
mov    -0x18(%rbp),%rdi
call   1040 <localtime@plt>
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x400,%esi
lea    0xe0e(%rip),%rdx
call   1090 <strftime@plt>
mov    -0x48(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    $0x400,%eax
sub    %ecx,%eax
cltq
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1243 <_log_vfprintf+0xc3>
mov    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1060 <strncat@plt>
mov    -0x48(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    $0x400,%eax
sub    %ecx,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rdx
lea    0xdc1(%rip),%rsi
call   1060 <strncat@plt>
mov    -0x48(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    $0x400,%eax
sub    %ecx,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1060 <strncat@plt>
mov    -0x48(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    $0x400,%eax
sub    %ecx,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rdx
lea    0xd6f(%rip),%rsi
call   1060 <strncat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   10a0 <vfprintf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_log_vfprintf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r15d,%r15d
test   %rdi,%rdi
je     128c <_log_vfprintf+0x10c>
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r13
mov    %r8d,0x4(%rsp)
mov    $0x401,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     1286 <_log_vfprintf+0x106>
mov    %rax,%rbp
mov    %r14,%rdi
call   1040 <localtime@plt>
lea    0xe2e(%rip),%rdx
mov    $0x400,%r14d
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %rax,%rcx
call   1090 <strftime@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    $0x400,%ecx
sub    %eax,%ecx
movslq %ecx,%rdx
test   %rbx,%rbx
je     121c <_log_vfprintf+0x9c>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <strncat@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    $0x400,%ecx
sub    %eax,%ecx
movslq %ecx,%rdx
lea    0xdf0(%rip),%rsi
mov    %rbp,%rdi
call   1060 <strncat@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    $0x400,%ecx
sub    %eax,%ecx
movslq %ecx,%rdx
mov    %rbp,%rdi
mov    %r12,%rsi
call   1060 <strncat@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
sub    %eax,%r14d
movslq %r14d,%rdx
lea    0xdb9(%rip),%rsi
mov    %rbp,%rdi
call   1060 <strncat@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    0x4(%rsp),%edx
call   10a0 <vfprintf@plt>
mov    %r13,%rdi
call   1080 <fflush@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    128c <_log_vfprintf+0x10c>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@.str = [19 x i8] c"%Y-%m-%d %H:%M:%S \00"
@.str.1 = [3 x i8] c": \00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @_log_vfprintf(i32* %0, i32* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca %struct.tm*
%13 = alloca i64
%14 = alloca i8*
store i32* %0, i32** %7
store i32* %1, i32** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %70
17: ; preds = %5
%18 = call i8* @calloc(i32 1025, i32 1)
store i8* %18, i8** %14
%19 = load i8*, i8** %14
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -1, i32* %6
br label %71
22: ; preds = %17
%23 = load i32*, i32** %8
%24 = call %struct.tm* @localtime(i32* %23)
store %struct.tm* %24, %struct.tm** %12
%25 = load i8*, i8** %14
%26 = load %struct.tm*, %struct.tm** %12
%27 = call i32 @strftime(i8* %25, i32 1024, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), %struct.tm* %26)
%28 = load i8*, i8** %14
%29 = call i32 @strlen(i8* %28)
%30 = sub nsw i32 1024, %29
%31 = sext i32 %30 to i64
store i64 %31, i64* %13
%32 = load i8*, i8** %9
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %43
34: ; preds = %22
%35 = load i8*, i8** %14
%36 = load i8*, i8** %9
%37 = load i64, i64* %13
%38 = call i32 @strncat(i8* %35, i8* %36, i64 %37)
%39 = load i8*, i8** %14
%40 = call i32 @strlen(i8* %39)
%41 = sub nsw i32 1024, %40
%42 = sext i32 %41 to i64
store i64 %42, i64* %13
br label %43
43: ; preds = %34, %22
%44 = load i8*, i8** %14
%45 = load i64, i64* %13
%46 = call i32 @strncat(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64 %45)
%47 = load i8*, i8** %14
%48 = call i32 @strlen(i8* %47)
%49 = sub nsw i32 1024, %48
%50 = sext i32 %49 to i64
store i64 %50, i64* %13
%51 = load i8*, i8** %14
%52 = load i8*, i8** %10
%53 = load i64, i64* %13
%54 = call i32 @strncat(i8* %51, i8* %52, i64 %53)
%55 = load i8*, i8** %14
%56 = call i32 @strlen(i8* %55)
%57 = sub nsw i32 1024, %56
%58 = sext i32 %57 to i64
store i64 %58, i64* %13
%59 = load i8*, i8** %14
%60 = load i64, i64* %13
%61 = call i32 @strncat(i8* %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i64 %60)
%62 = load i32*, i32** %7
%63 = load i8*, i8** %14
%64 = load i32, i32* %11
%65 = call i32 @vfprintf(i32* %62, i8* %63, i32 %64)
%66 = load i32*, i32** %7
%67 = call i32 @fflush(i32* %66)
%68 = load i8*, i8** %14
%69 = call i32 @free(i8* %68)
br label %70
70: ; preds = %43, %5
store i32 0, i32* %6
br label %71
71: ; preds = %70, %21
%72 = load i32, i32* %6
ret i32 %72
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@.str = [19 x i8] c"%Y-%m-%d %H:%M:%S \00"
@.str.1 = [3 x i8] c": \00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @_log_vfprintf(i32* %0, i32* %1, i8* %2, i8* %3, i32 %4) {
%6 = icmp eq i32* %0, null
br i1 %6, label %36, label %7
7: ; preds = %5
%8 = tail call dereferenceable_or_null(1025) i8* @calloc(i32 1025, i32 1)
%9 = icmp eq i8* %8, null
br i1 %9, label %36, label %10
10: ; preds = %7
%11 = tail call %struct.tm* @localtime(i32* %1)
%12 = tail call i32 @strftime(i8* %8, i32 1024, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), %struct.tm* %11)
%13 = tail call i32 @strlen(i8* %8)
%14 = sub nsw i32 1024, %13
%15 = sext i32 %14 to i64
%16 = icmp eq i8* %2, null
br i1 %16, label %22, label %17
17: ; preds = %10
%18 = tail call i32 @strncat(i8* %8, i8* %2, i64 %15)
%19 = tail call i32 @strlen(i8* %8)
%20 = sub nsw i32 1024, %19
%21 = sext i32 %20 to i64
br label %22
22: ; preds = %17, %10
%23 = phi i64 [ %21, %17 ], [ %15, %10 ]
%24 = tail call i32 @strncat(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64 %23)
%25 = tail call i32 @strlen(i8* %8)
%26 = sub nsw i32 1024, %25
%27 = sext i32 %26 to i64
%28 = tail call i32 @strncat(i8* %8, i8* %3, i64 %27)
%29 = tail call i32 @strlen(i8* %8)
%30 = sub nsw i32 1024, %29
%31 = sext i32 %30 to i64
%32 = tail call i32 @strncat(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i64 %31)
%33 = tail call i32 @vfprintf(i32* %0, i8* %8, i32 %4)
%34 = tail call i32 @fflush(i32* %0)
%35 = tail call i32 @free(i8* %8)
br label %36
36: ; preds = %5, %22, %7
%37 = phi i32 [ -1, %7 ], [ 0, %22 ], [ 0, %5 ]
ret i32 %37
}